mod offline_client;
mod online_client;
use crate::backend::BlockRef;
use crate::config::{Config, HashFor};
use crate::constants::ConstantsClient;
use crate::custom_values::CustomValuesClient;
use crate::error::BlockError;
use crate::events::EventsClient;
use crate::extrinsics::ExtrinsicsClient;
use crate::runtime_apis::RuntimeApisClient;
use crate::storage::StorageClient;
use crate::transactions::TransactionsClient;
use crate::view_functions::ViewFunctionsClient;
use core::marker::PhantomData;
use std::borrow::Cow;
use subxt_metadata::{ArcMetadata, Metadata};
pub use offline_client::{OfflineClient, OfflineClientAtBlockImpl, OfflineClientAtBlockT};
pub use online_client::{
Block, BlockNumberOrRef, Blocks, OnlineClient, OnlineClientAtBlockImpl, OnlineClientAtBlockT,
};
#[derive(Clone, Debug)]
pub struct ClientAtBlock<T, Client> {
pub(crate) client: Client,
marker: PhantomData<T>,
}
impl<T, Client> ClientAtBlock<T, Client> {
pub(crate) fn new(client: Client) -> Self {
Self {
client,
marker: PhantomData,
}
}
}
impl<T, Client> ClientAtBlock<T, Client>
where
T: Config,
Client: OfflineClientAtBlockT<T>,
{
pub fn tx(&self) -> TransactionsClient<T, Client> {
self.transactions()
}
pub fn transactions(&self) -> TransactionsClient<T, Client> {
TransactionsClient::new(self.client.clone())
}
pub fn storage(&self) -> StorageClient<'_, T, Client> {
StorageClient::new(&self.client)
}
pub fn constants(&self) -> ConstantsClient<'_, T, Client> {
ConstantsClient::new(&self.client)
}
pub fn custom_values(&self) -> CustomValuesClient<'_, T, Client> {
CustomValuesClient::new(&self.client)
}
pub fn extrinsics(&self) -> ExtrinsicsClient<'_, T, Client> {
ExtrinsicsClient::new(Cow::Borrowed(&self.client))
}
pub fn events(&self) -> EventsClient<'_, T, Client> {
EventsClient::new(&self.client)
}
pub fn runtime_apis(&self) -> RuntimeApisClient<'_, T, Client> {
RuntimeApisClient::new(&self.client)
}
pub fn view_functions(&self) -> ViewFunctionsClient<'_, T, Client> {
ViewFunctionsClient::new(&self.client)
}
pub fn metadata(&self) -> ArcMetadata {
self.client.metadata()
}
pub fn metadata_ref(&self) -> &Metadata {
self.client.metadata_ref()
}
pub fn block_number(&self) -> u64 {
self.client.block_number()
}
pub fn spec_version(&self) -> u32 {
self.client.spec_version()
}
pub fn transaction_version(&self) -> u32 {
self.client.transaction_version()
}
pub fn genesis_hash(&self) -> Option<HashFor<T>> {
self.client.genesis_hash()
}
pub fn hasher(&self) -> &T::Hasher {
self.client.hasher()
}
}
impl<T, Client> ClientAtBlock<T, Client>
where
T: Config,
Client: OnlineClientAtBlockT<T>,
{
pub fn online_client(&self) -> OnlineClient<T> {
self.client.client()
}
pub fn block_ref(&self) -> &BlockRef<HashFor<T>> {
self.client.block_ref()
}
pub fn block_hash(&self) -> HashFor<T> {
self.client.block_ref().hash()
}
pub async fn block_header(&self) -> Result<T::Header, BlockError> {
let block_hash = self.block_hash();
let header = self
.client
.backend()
.block_header(block_hash)
.await
.map_err(|e| BlockError::CouldNotDownloadBlockHeader {
block_hash: block_hash.into(),
reason: e,
})?
.ok_or_else(|| BlockError::BlockNotFound {
block_hash: block_hash.into(),
})?;
Ok(header)
}
}
pub type OfflineClientAtBlock<T> = ClientAtBlock<T, OfflineClientAtBlockImpl<T>>;
pub type OnlineClientAtBlock<T> = ClientAtBlock<T, OnlineClientAtBlockImpl<T>>;