substrate_api_client/api/runtime_api/
mod.rspub use self::{
account_nonce::*, api_core::*, authority_discovery::*, block_builder::*, metadata::*, mmr::*,
session_keys::*, staking::*, transaction_payment::*, transaction_payment_call::*,
};
pub mod account_nonce;
pub mod api_core;
pub mod authority_discovery;
pub mod block_builder;
pub mod metadata;
pub mod mmr;
pub mod session_keys;
pub mod staking;
pub mod transaction_payment;
pub mod transaction_payment_call;
use crate::{api::Result, rpc::Request};
use ac_compose_macros::rpc_params;
use ac_primitives::config::Config;
#[cfg(not(feature = "sync-api"))]
use alloc::boxed::Box;
use alloc::{sync::Arc, vec::Vec};
use codec::Decode;
use core::marker::PhantomData;
use sp_core::Bytes;
#[derive(Clone)]
pub struct RuntimeApiClient<T, Client> {
client: Arc<Client>,
_phantom: PhantomData<T>,
}
impl<T, Client> RuntimeApiClient<T, Client> {
pub fn new(client: Arc<Client>) -> Self {
Self { client, _phantom: PhantomData }
}
}
#[maybe_async::maybe_async(?Send)]
pub trait RuntimeApi {
type Hash;
async fn runtime_call<V: Decode>(
&self,
method: &str,
data: Vec<Vec<u8>>,
at_block: Option<Self::Hash>,
) -> Result<V>;
async fn opaque_runtime_call(
&self,
method: &str,
data: Vec<Vec<u8>>,
at_block: Option<Self::Hash>,
) -> Result<Bytes>;
async fn rpc_call(
&self,
method: &str,
data: Option<Bytes>,
at_block: Option<Self::Hash>,
) -> Result<Bytes>;
}
#[maybe_async::maybe_async(?Send)]
impl<T, Client> RuntimeApi for RuntimeApiClient<T, Client>
where
T: Config,
Client: Request,
{
type Hash = T::Hash;
async fn runtime_call<V: Decode>(
&self,
method: &str,
data: Vec<Vec<u8>>,
at_block: Option<Self::Hash>,
) -> Result<V> {
let bytes = self.opaque_runtime_call(method, data, at_block).await?;
Ok(Decode::decode(&mut bytes.0.as_slice())?)
}
async fn opaque_runtime_call(
&self,
method: &str,
data: Vec<Vec<u8>>,
at_block: Option<Self::Hash>,
) -> Result<Bytes> {
let data = match data.is_empty() {
true => None,
false => {
let mut appended_data = Vec::new();
for mut item in data {
appended_data.append(&mut item);
}
Some(appended_data.into())
},
};
self.rpc_call(method, data, at_block).await
}
async fn rpc_call(
&self,
method: &str,
data: Option<Bytes>,
at_block: Option<Self::Hash>,
) -> Result<Bytes> {
let extracted_data: Bytes = match data {
Some(data) => data,
None => Vec::new().into(),
};
let return_bytes = self
.client
.request("state_call", rpc_params![method, extracted_data, at_block])
.await?;
Ok(return_bytes)
}
}