substrate_api_client/api/rpc_api/
frame_system.rsuse crate::{
api::{Api, GetStorage, Result},
rpc::Request,
};
use ac_compose_macros::rpc_params;
use ac_primitives::{config::Config, AccountInfo};
#[cfg(not(feature = "sync-api"))]
use alloc::boxed::Box;
use alloc::{string::String, vec::Vec};
use log::*;
use sp_storage::StorageKey;
#[maybe_async::maybe_async(?Send)]
pub trait GetAccountInformation {
type AccountId;
type Index;
type AccountData;
async fn get_system_account_next_index(
&self,
account_id: Self::AccountId,
) -> Result<Self::Index>;
async fn get_account_info(
&self,
address: &Self::AccountId,
) -> Result<Option<AccountInfo<Self::Index, Self::AccountData>>>;
async fn get_account_data(
&self,
address: &Self::AccountId,
) -> Result<Option<Self::AccountData>>;
async fn get_account_nonce(&self, account: &Self::AccountId) -> Result<Self::Index>;
}
#[maybe_async::maybe_async(?Send)]
impl<T, Client> GetAccountInformation for Api<T, Client>
where
T: Config,
Client: Request,
{
type AccountId = T::AccountId;
type Index = T::Index;
type AccountData = T::AccountData;
async fn get_system_account_next_index(
&self,
account_id: Self::AccountId,
) -> Result<Self::Index> {
let next_index = self
.client()
.request("system_accountNextIndex", rpc_params![account_id])
.await?;
Ok(next_index)
}
async fn get_account_info(
&self,
address: &Self::AccountId,
) -> Result<Option<AccountInfo<Self::Index, Self::AccountData>>> {
let storagekey: StorageKey = self.metadata().storage_map_key::<Self::AccountId>(
"System",
"Account",
address.clone(),
)?;
info!("storage key is: 0x{}", hex::encode(&storagekey));
self.get_storage_by_key(storagekey, None).await
}
async fn get_account_data(
&self,
address: &Self::AccountId,
) -> Result<Option<Self::AccountData>> {
self.get_account_info(address).await.map(|info| info.map(|i| i.data))
}
async fn get_account_nonce(&self, account: &Self::AccountId) -> Result<Self::Index> {
self.get_account_info(account)
.await
.map(|acc_opt| acc_opt.map_or_else(|| 0u32.into(), |acc| acc.nonce))
}
}
#[maybe_async::maybe_async(?Send)]
pub trait SystemApi {
type ChainType;
type Properties;
type Health;
async fn get_system_name(&self) -> Result<String>;
async fn get_system_version(&self) -> Result<String>;
async fn get_system_chain(&self) -> Result<String>;
async fn get_system_chain_type(&self) -> Result<Self::ChainType>;
async fn get_system_properties(&self) -> Result<Self::Properties>;
async fn get_system_health(&self) -> Result<Self::Health>;
async fn get_system_local_peer_id(&self) -> Result<String>;
async fn get_system_local_listen_addresses(&self) -> Result<Vec<String>>;
}
#[maybe_async::maybe_async(?Send)]
impl<T, Client> SystemApi for Api<T, Client>
where
T: Config,
Client: Request,
{
type ChainType = ac_primitives::ChainType;
type Properties = ac_primitives::Properties;
type Health = ac_primitives::Health;
async fn get_system_name(&self) -> Result<String> {
let res = self.client().request("system_name", rpc_params![]).await?;
Ok(res)
}
async fn get_system_version(&self) -> Result<String> {
let res = self.client().request("system_version", rpc_params![]).await?;
Ok(res)
}
async fn get_system_chain(&self) -> Result<String> {
let res = self.client().request("system_chain", rpc_params![]).await?;
Ok(res)
}
async fn get_system_chain_type(&self) -> Result<Self::ChainType> {
let res = self.client().request("system_chainType", rpc_params![]).await?;
Ok(res)
}
async fn get_system_properties(&self) -> Result<Self::Properties> {
let res = self.client().request("system_properties", rpc_params![]).await?;
Ok(res)
}
async fn get_system_health(&self) -> Result<Self::Health> {
let res = self.client().request("system_health", rpc_params![]).await?;
Ok(res)
}
async fn get_system_local_peer_id(&self) -> Result<String> {
let res = self.client().request("system_localPeerId", rpc_params![]).await?;
Ok(res)
}
async fn get_system_local_listen_addresses(&self) -> Result<Vec<String>> {
let res = self.client().request("system_localListenAddresses", rpc_params![]).await?;
Ok(res)
}
}