diff --git a/subxt/src/blocks/block_types.rs b/subxt/src/blocks/block_types.rs index 00d04ef856..ea3d315e66 100644 --- a/subxt/src/blocks/block_types.rs +++ b/subxt/src/blocks/block_types.rs @@ -12,9 +12,16 @@ use crate::{ Error, }, events, - rpc::ChainBlockResponse, + rpc::{ + subscription_events::{ + ChainHeadEvent, + ChainHeadResult, + }, + ChainBlockResponse, + }, Config, }; +use codec::Decode; use derivative::Derivative; use futures::lock::Mutex as AsyncMutex; use sp_runtime::traits::{ @@ -45,7 +52,46 @@ where client, } } + + /// Return the block hash. + pub fn hash(&self) -> T::Hash { + self.hash.clone() + } } + +impl FollowBlock +where + T: Config, + C: OnlineClientT, +{ + pub async fn body(&self) -> Result>, Error> { + let mut sub = self + .client + .rpc() + .subscribe_chainhead_body(self.hash, self.subscription_id.clone()) + .await?; + + if let Some(event) = sub.next().await { + let event = event?; + + println!("Got event: {:?}", event); + + return match event { + ChainHeadEvent::Done(ChainHeadResult { result }) => { + let bytes = hex::decode(result.trim_start_matches("0x")) + .map_err(|err| Error::Other(err.to_string()))?; + + let extrinsics: Vec> = Decode::decode(&mut &bytes[..])?; + Ok(extrinsics) + } + _ => Err(Error::Other("Failed to fetch the block body".into())), + } + } + + Err(Error::Other("Failed to fetch the block body".into())) + } +} + /// A representation of a block. pub struct Block { header: T::Header, diff --git a/subxt/src/blocks/blocks_client.rs b/subxt/src/blocks/blocks_client.rs index 91848762e3..b760967cc3 100644 --- a/subxt/src/blocks/blocks_client.rs +++ b/subxt/src/blocks/blocks_client.rs @@ -172,11 +172,14 @@ where .subscribe_chainhead_follow(runtime_updates) .await?; - let subscription_id = match sub.subscription_id() { + let _subscription_id = match sub.subscription_id() { Some(id) => id.clone(), None => return Err(Error::Other("Subscription without ID".into())), }; + // TODO: Jsonrpsee needs update. + let subscription_id = "A".to_string(); + // Flatten the finalized events and map them into a `FollowBlock`. Ok(sub .flat_map(move |event| {