mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-01 15:57:55 +00:00
Use async-trait (#264)
* Use async-trait * Use async-trait for store trait too
This commit is contained in:
+24
-20
@@ -50,42 +50,44 @@ pub fn call(s: Structure) -> TokenStream {
|
||||
}
|
||||
|
||||
/// Call extension trait.
|
||||
#[async_trait::async_trait]
|
||||
pub trait #call_trait<T: #subxt::Runtime + #module> {
|
||||
/// Create and submit an extrinsic.
|
||||
fn #call<'a>(
|
||||
async fn #call<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
|
||||
#args
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<T::Hash, #subxt::Error>> + Send + 'a>>;
|
||||
) -> Result<T::Hash, #subxt::Error>;
|
||||
|
||||
/// Create, submit and watch an extrinsic.
|
||||
fn #call_and_watch<'a>(
|
||||
async fn #call_and_watch<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
|
||||
#args
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#subxt::ExtrinsicSuccess<T>, #subxt::Error>> + Send + 'a>>;
|
||||
) -> Result<#subxt::ExtrinsicSuccess<T>, #subxt::Error>;
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: #subxt::Runtime + #module> #call_trait<T> for #subxt::Client<T>
|
||||
where
|
||||
<<T::Extra as #subxt::SignedExtra<T>>::Extra as #subxt::SignedExtension>::AdditionalSigned: Send + Sync,
|
||||
{
|
||||
fn #call<'a>(
|
||||
async fn #call<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
|
||||
#args
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<T::Hash, #subxt::Error>> + Send + 'a>> {
|
||||
) -> Result<T::Hash, #subxt::Error> {
|
||||
let #marker = core::marker::PhantomData::<T>;
|
||||
Box::pin(self.submit(#build_struct, signer))
|
||||
self.submit(#build_struct, signer).await
|
||||
}
|
||||
|
||||
fn #call_and_watch<'a>(
|
||||
async fn #call_and_watch<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
|
||||
#args
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#subxt::ExtrinsicSuccess<T>, #subxt::Error>> + Send + 'a>> {
|
||||
) -> Result<#subxt::ExtrinsicSuccess<T>, #subxt::Error> {
|
||||
let #marker = core::marker::PhantomData::<T>;
|
||||
Box::pin(self.watch(#build_struct, signer))
|
||||
self.watch(#build_struct, signer).await
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,46 +114,48 @@ mod tests {
|
||||
}
|
||||
|
||||
/// Call extension trait.
|
||||
#[async_trait::async_trait]
|
||||
pub trait TransferCallExt<T: substrate_subxt::Runtime + Balances> {
|
||||
/// Create and submit an extrinsic.
|
||||
fn transfer<'a>(
|
||||
async fn transfer<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn substrate_subxt::Signer<T> + Send + Sync),
|
||||
to: &'a <T as System>::Address,
|
||||
amount: T::Balance,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<T::Hash, substrate_subxt::Error>> + Send + 'a>>;
|
||||
) -> Result<T::Hash, substrate_subxt::Error>;
|
||||
|
||||
/// Create, submit and watch an extrinsic.
|
||||
fn transfer_and_watch<'a>(
|
||||
async fn transfer_and_watch<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn substrate_subxt::Signer<T> + Send + Sync),
|
||||
to: &'a <T as System>::Address,
|
||||
amount: T::Balance,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<substrate_subxt::ExtrinsicSuccess<T>, substrate_subxt::Error>> + Send + 'a>>;
|
||||
) -> Result<substrate_subxt::ExtrinsicSuccess<T>, substrate_subxt::Error>;
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: substrate_subxt::Runtime + Balances> TransferCallExt<T> for substrate_subxt::Client<T>
|
||||
where
|
||||
<<T::Extra as substrate_subxt::SignedExtra<T>>::Extra as substrate_subxt::SignedExtension>::AdditionalSigned: Send + Sync,
|
||||
{
|
||||
fn transfer<'a>(
|
||||
async fn transfer<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn substrate_subxt::Signer<T> + Send + Sync),
|
||||
to: &'a <T as System>::Address,
|
||||
amount: T::Balance,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<T::Hash, substrate_subxt::Error>> + Send + 'a>> {
|
||||
) -> Result<T::Hash, substrate_subxt::Error> {
|
||||
let _ = core::marker::PhantomData::<T>;
|
||||
Box::pin(self.submit(TransferCall { to, amount, }, signer))
|
||||
self.submit(TransferCall { to, amount, }, signer).await
|
||||
}
|
||||
|
||||
fn transfer_and_watch<'a>(
|
||||
async fn transfer_and_watch<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn substrate_subxt::Signer<T> + Send + Sync),
|
||||
to: &'a <T as System>::Address,
|
||||
amount: T::Balance,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<substrate_subxt::ExtrinsicSuccess<T>, substrate_subxt::Error>> + Send + 'a>> {
|
||||
) -> Result<substrate_subxt::ExtrinsicSuccess<T>, substrate_subxt::Error> {
|
||||
let _ = core::marker::PhantomData::<T>;
|
||||
Box::pin(self.watch(TransferCall { to, amount, }, signer))
|
||||
self.watch(TransferCall { to, amount, }, signer).await
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
+24
-20
@@ -141,36 +141,38 @@ pub fn store(s: Structure) -> TokenStream {
|
||||
}
|
||||
|
||||
/// Store extension trait.
|
||||
#[async_trait::async_trait]
|
||||
pub trait #store_trait<T: #subxt::Runtime + #module> {
|
||||
/// Retrieve the store element.
|
||||
fn #store<'a>(
|
||||
async fn #store<'a>(
|
||||
&'a self,
|
||||
#args
|
||||
hash: Option<T::Hash>,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>>;
|
||||
) -> Result<#ret, #subxt::Error>;
|
||||
|
||||
/// Iterate over the store element.
|
||||
fn #store_iter<'a>(
|
||||
async fn #store_iter<'a>(
|
||||
&'a self,
|
||||
hash: Option<T::Hash>,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#key_iter, #subxt::Error>> + Send + 'a>>;
|
||||
) -> Result<#key_iter, #subxt::Error>;
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: #subxt::Runtime + #module> #store_trait<T> for #subxt::Client<T> {
|
||||
fn #store<'a>(
|
||||
async fn #store<'a>(
|
||||
&'a self,
|
||||
#args
|
||||
hash: Option<T::Hash>,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>> {
|
||||
) -> Result<#ret, #subxt::Error> {
|
||||
let #marker = core::marker::PhantomData::<T>;
|
||||
Box::pin(async move { self.#fetch(&#build_struct, hash).await })
|
||||
self.#fetch(&#build_struct, hash).await
|
||||
}
|
||||
|
||||
fn #store_iter<'a>(
|
||||
async fn #store_iter<'a>(
|
||||
&'a self,
|
||||
hash: Option<T::Hash>,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#key_iter, #subxt::Error>> + Send + 'a>> {
|
||||
Box::pin(self.iter(hash))
|
||||
) -> Result<#key_iter, #subxt::Error> {
|
||||
self.iter(hash).await
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -217,36 +219,38 @@ mod tests {
|
||||
}
|
||||
|
||||
/// Store extension trait.
|
||||
#[async_trait::async_trait]
|
||||
pub trait AccountStoreExt<T: substrate_subxt::Runtime + Balances> {
|
||||
/// Retrieve the store element.
|
||||
fn account<'a>(
|
||||
async fn account<'a>(
|
||||
&'a self,
|
||||
account_id: &'a <T as System>::AccountId,
|
||||
hash: Option<T::Hash>,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<AccountData<T::Balance>, substrate_subxt::Error>> + Send + 'a>>;
|
||||
) -> Result<AccountData<T::Balance>, substrate_subxt::Error>;
|
||||
/// Iterate over the store element.
|
||||
fn account_iter<'a>(
|
||||
async fn account_iter<'a>(
|
||||
&'a self,
|
||||
hash: Option<T::Hash>,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<substrate_subxt::KeyIter<T, AccountStore<'a, T>>, substrate_subxt::Error>> + Send + 'a>>;
|
||||
) -> Result<substrate_subxt::KeyIter<T, AccountStore<'a, T>>, substrate_subxt::Error>;
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: substrate_subxt::Runtime + Balances> AccountStoreExt<T> for substrate_subxt::Client<T> {
|
||||
fn account<'a>(
|
||||
async fn account<'a>(
|
||||
&'a self,
|
||||
account_id: &'a <T as System>::AccountId,
|
||||
hash: Option<T::Hash>,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<AccountData<T::Balance>, substrate_subxt::Error>> + Send + 'a>>
|
||||
) -> Result<AccountData<T::Balance>, substrate_subxt::Error>
|
||||
{
|
||||
let _ = core::marker::PhantomData::<T>;
|
||||
Box::pin(async move { self.fetch_or_default(&AccountStore { account_id, }, hash).await })
|
||||
self.fetch_or_default(&AccountStore { account_id, }, hash).await
|
||||
}
|
||||
|
||||
fn account_iter<'a>(
|
||||
async fn account_iter<'a>(
|
||||
&'a self,
|
||||
hash: Option<T::Hash>,
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<substrate_subxt::KeyIter<T, AccountStore<'a, T>>, substrate_subxt::Error>> + Send + 'a>> {
|
||||
Box::pin(self.iter(hash))
|
||||
) -> Result<substrate_subxt::KeyIter<T, AccountStore<'a, T>>, substrate_subxt::Error> {
|
||||
self.iter(hash).await
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user