From 35eb2a4b7766525c96e0e593b2e5bc335c532c37 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Mon, 9 Jan 2023 18:31:54 +0000 Subject: [PATCH] blocks: Add method to execute runtime API calls Signed-off-by: Alexandru Vasile --- subxt/src/blocks/block_types.rs | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/subxt/src/blocks/block_types.rs b/subxt/src/blocks/block_types.rs index 14087c217f..ecd9e8d085 100644 --- a/subxt/src/blocks/block_types.rs +++ b/subxt/src/blocks/block_types.rs @@ -184,6 +184,15 @@ where Ok(Some(storage)) } + /// Execute a runtime API call at this block. + pub async fn call( + &self, + function: String, + call_parameters: Option<&[u8]>, + ) -> Result, ChainHeadError> { + self.fetch_call(function, call_parameters).await + } + /// Wrapper to fetch the block's body from the `chainHead_body` subscription. async fn fetch_body( &self, @@ -261,6 +270,36 @@ where "Failed to fetch the block storage".into(), )) } + + /// Execute a runtime API call at this block. + async fn fetch_call( + &self, + function: String, + call_parameters: Option<&[u8]>, + ) -> Result, ChainHeadError> { + let call_parameters = call_parameters.unwrap_or(Default::default()); + + let mut sub = self + .client + .rpc() + .subscribe_chainhead_call( + self.subscription_id.clone(), + self.hash, + function, + call_parameters, + ) + .await?; + + if let Some(event) = sub.next().await { + let event = event?; + + let bytes = Vec::::try_from(event)?; + } + + Err(ChainHeadError::Other( + "Failed to execute the runtime API call".into(), + )) + } } /// A representation of a block.