use super::runtime_types::RuntimeApi;
use crate::{
backend::BlockRef,
client::OnlineClientT,
config::{Config, HashFor},
error::RuntimeApiError,
};
use derive_where::derive_where;
use std::{future::Future, marker::PhantomData};
#[derive_where(Clone; Client)]
pub struct RuntimeApiClient<T, Client> {
client: Client,
_marker: PhantomData<T>,
}
impl<T, Client> RuntimeApiClient<T, Client> {
pub fn new(client: Client) -> Self {
Self { client, _marker: PhantomData }
}
}
impl<T, Client> RuntimeApiClient<T, Client>
where
T: Config,
Client: OnlineClientT<T>,
{
pub fn at(&self, block_ref: impl Into<BlockRef<HashFor<T>>>) -> RuntimeApi<T, Client> {
RuntimeApi::new(self.client.clone(), block_ref.into())
}
pub fn at_latest(
&self,
) -> impl Future<Output = Result<RuntimeApi<T, Client>, RuntimeApiError>> + Send + 'static {
let client = self.client.clone();
async move {
let block_ref = client
.backend()
.latest_finalized_block_ref()
.await
.map_err(RuntimeApiError::CannotGetLatestFinalizedBlock)?;
Ok(RuntimeApi::new(client, block_ref))
}
}
}