diff --git a/examples/examples/tx_basic_light_client.rs b/examples/examples/tx_basic_light_client.rs index f4f8754782..8ab77d9056 100644 --- a/examples/examples/tx_basic_light_client.rs +++ b/examples/examples/tx_basic_light_client.rs @@ -98,23 +98,23 @@ async fn main() -> Result<(), Box> { let mut sub = api .rpc() .chainhead_unstable_body(sub_id.clone(), hash) - .await? - .take(5); + .await?; - while let Some(event) = sub.next().await { + if let Some(event) = sub.next().await { let event = event?; - println!(" chainHead_body event: {event:?}"); } // Fetch the block's header. - // let header = api - // .rpc() - // .chainhead_unstable_header(sub_id.clone(), hash) - // .await?; - // let header = header.expect("RPC must have this header in memory; qed"); - - // println!(" chainHead_header: {header}"); + let header = api + .rpc() + .chainhead_unstable_header(sub_id.clone(), hash) + .await?; + if let Some(header) = header { + println!(" chainHead_header: {header}"); + } else { + println!(" chainHead_header: Header not in memory for {hash}"); + } // Make a storage query. let account_id: AccountId32 = AccountKeyring::Alice.to_account_id().into(); @@ -124,12 +124,10 @@ async fn main() -> Result<(), Box> { let mut sub = api .rpc() .chainhead_unstable_storage(sub_id.clone(), hash, &addr_bytes, None) - .await? - .take(5); + .await?; - while let Some(event) = sub.next().await { + if let Some(event) = sub.next().await { let event = event?; - println!(" chainHead_storage event: {event:?}"); } } diff --git a/subxt/src/rpc/types.rs b/subxt/src/rpc/types.rs index bba52a7bc5..49cc455bff 100644 --- a/subxt/src/rpc/types.rs +++ b/subxt/src/rpc/types.rs @@ -507,15 +507,46 @@ pub enum FollowEvent { /// The result of a chain head method. #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[serde(rename_all = "camelCase")] +#[cfg(feature = "experimental-light-client")] +#[serde(try_from = "LightClientChainHeadResult")] pub struct ChainHeadResult { /// Result of the method. #[cfg(not(feature = "experimental-light-client"))] pub result: T, /// Result of the method. + /// + /// # Note + /// + /// `chainHead_body` returns a vector of values, while + /// `chainHead_storage` returns just one plain element. #[cfg(feature = "experimental-light-client")] pub value: Vec, } +/// The result of a chain head method. +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "camelCase")] +#[cfg(feature = "experimental-light-client")] +pub struct LightClientChainHeadResult { + /// Result of the method. + /// + /// # Note + /// + /// `chainHead_body` returns a vector of values, while + /// `chainHead_storage` returns just one plain element. + pub value: T, +} + +impl TryFrom> for ChainHeadResult { + type Error = &'static str; + + fn try_from(light: LightClientChainHeadResult) -> Result { + Ok(Self { + value: vec![light.value], + }) + } +} + /// The event generated by the body / call / storage methods. #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[serde(rename_all = "camelCase")]