// Copyright 2019-2023 Parity Technologies (UK) Ltd. // This file is dual-licensed as Apache-2.0 or GPL-3.0. // see LICENSE for license details. use super::runtime_types::RuntimeApi; use crate::{backend::BlockRef, client::OnlineClientT, error::Error, Config}; use derivative::Derivative; use std::{future::Future, marker::PhantomData}; /// Execute runtime API calls. #[derive(Derivative)] #[derivative(Clone(bound = "Client: Clone"))] pub struct RuntimeApiClient { client: Client, _marker: PhantomData, } impl RuntimeApiClient { /// Create a new [`RuntimeApiClient`] pub fn new(client: Client) -> Self { Self { client, _marker: PhantomData, } } } impl RuntimeApiClient where T: Config, Client: OnlineClientT, { /// Obtain a runtime API interface at some block hash. pub fn at(&self, block_ref: impl Into>) -> RuntimeApi { RuntimeApi::new(self.client.clone(), block_ref.into()) } /// Obtain a runtime API interface at the latest block hash. pub fn at_latest( &self, ) -> impl Future, 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 { // get the ref for the latest finalized block and use that. let block_ref = client.backend().latest_finalized_block_ref().await?; Ok(RuntimeApi::new(client, block_ref)) } } }