Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add at_latest for runtime API interface too #904

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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