diff --git a/examples/examples/chainhead_subscription.rs b/examples/examples/chainhead_subscription.rs index fd15c649fe..6c22638735 100644 --- a/examples/examples/chainhead_subscription.rs +++ b/examples/examples/chainhead_subscription.rs @@ -64,20 +64,9 @@ async fn main() -> Result<(), Box> { .call("AccountNonceApi_account_nonce".into(), Some(&call_params)) .await?; println!("[hash={:?}] call={:?}", block.hash(), call); - } - // Subscribe to the `chainHead_follow` method. - let mut follow_sub = api.rpc().subscribe_chainhead_follow(false).await?; - - // Handle all subscriptions from the `chainHead_follow`. - while let Some(event) = follow_sub.next().await { - let event = event?; - - println!( - "sub_id: {:?} event: {:?}", - follow_sub.subscription_id(), - event - ); + // Unpin the block as last step. + block.unpin().await?; } Ok(()) diff --git a/subxt/src/blocks/block_types.rs b/subxt/src/blocks/block_types.rs index dbfe055f7a..8dd38f9f68 100644 --- a/subxt/src/blocks/block_types.rs +++ b/subxt/src/blocks/block_types.rs @@ -162,6 +162,22 @@ where "Failed to execute the runtime API call".into(), )) } + + /// Unpin this block. + /// + /// # Note + /// + /// Call this method when you are no longer interested in making queries + /// against this block. + /// + /// Failing to call this method will eventually terminate the subscription. + pub async fn unpin(self) -> Result<(), Error> { + self.client + .rpc() + .chainhead_unpin(self.subscription_id, self.hash) + .await?; + Ok(()) + } } /// A representation of a block. diff --git a/subxt/src/rpc/rpc.rs b/subxt/src/rpc/rpc.rs index 19dbacc782..f673352b66 100644 --- a/subxt/src/rpc/rpc.rs +++ b/subxt/src/rpc/rpc.rs @@ -698,6 +698,22 @@ impl Rpc { Ok(header) } + /// Unpin a block reported by the `chainHead_follow` subscription. + pub async fn chainhead_unpin( + &self, + subscription_id: String, + hash: T::Hash, + ) -> Result<(), Error> { + self.client + .request( + "chainHead_unstable_unpin", + rpc_params![subscription_id, hash], + ) + .await?; + + Ok(()) + } + /// Parse `chaiHead_header` response and return the block's header. /// /// # Note