Skip to content

Commit

Permalink
at_latest for runtime API too (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdw authored Apr 11, 2023
1 parent 3b9fd72 commit 9bcfff3
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions subxt/src/runtime_api/runtime_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,25 @@ where
T: Config,
Client: OnlineClientT<T>,
{
/// Obtain a runtime API at some block hash.
pub fn at(
/// Obtain a runtime API interface at some block hash.
pub fn at(&self, block_hash: T::Hash) -> RuntimeApi<T, Client> {
RuntimeApi::new(self.client.clone(), block_hash)
}

/// Obtain a runtime API interface at the latest block hash.
pub fn at_latest(
&self,
block_hash: Option<T::Hash>,
) -> impl Future<Output = Result<RuntimeApi<T, Client>, Error>> + Send + 'static {
// Clone and pass the client in like this so that we can explicitly
// return a Future that's Send + 'static, rather than tied to &self.
let client = self.client.clone();
async move {
// If block hash is not provided, get the hash
// for the latest block and use that.
let block_hash = match block_hash {
Some(hash) => hash,
None => client.rpc().block_hash(None).await?.expect(
"substrate RPC returns the best block when no block number is provided; qed",
),
};
// get the hash for the latest block and use that.
let block_hash = client
.rpc()
.block_hash(None)
.await?
.expect("didn't pass a block number; qed");

Ok(RuntimeApi::new(client, block_hash))
}
Expand Down

0 comments on commit 9bcfff3

Please sign in to comment.