Split subxt (#102)

* Proc macro improvements.

* Use proc-macros.

* Update examples.

* Fix build.

* Run rustfmt.

* Fix total issuance test.

* Remove gas limit from put code call.

* Handle runtime errors.

* Fix tests.

* Make test more reliable.

* Revert "Handle runtime errors."

This reverts commit 26f30a9f4cfcfddfb3e49308cded46cfe6468697.

* Use expect instead of unwrap.

* Parse marker type.

* Fetch doesn't fail.
This commit is contained in:
David Craven
2020-05-12 13:25:22 +02:00
committed by GitHub
parent 825f3ab64c
commit f861f3fac4
21 changed files with 697 additions and 564 deletions
+102 -37
View File
@@ -28,9 +28,6 @@ use synstructure::Structure;
pub fn call(s: Structure) -> TokenStream {
let subxt = utils::use_crate("substrate-subxt");
let codec = utils::use_crate("parity-scale-codec");
let sp_core = utils::use_crate("sp-core");
let sp_runtime = utils::use_crate("sp-runtime");
let ident = &s.ast().ident;
let generics = &s.ast().generics;
let params = utils::type_params(generics);
@@ -39,23 +36,35 @@ pub fn call(s: Structure) -> TokenStream {
"with_{}",
utils::path_to_ident(module).to_string().to_snake_case()
);
let call_name = ident.to_string().trim_end_matches("Call").to_snake_case();
let call = format_ident!("{}", call_name);
let call_trait = format_ident!("{}CallExt", call_name.to_camel_case());
let call_name = utils::ident_to_name(ident, "Call").to_snake_case();
let bindings = utils::bindings(&s);
let fields = bindings.iter().map(|bi| {
let ident = bi.ast().ident.as_ref().unwrap();
quote!(#ident,)
});
let args = bindings.iter().map(|bi| {
let ident = bi.ast().ident.as_ref().unwrap();
let ty = &bi.ast().ty;
quote!(#ident: #ty,)
});
let args = quote!(#(#args)*);
let ret = quote!(#subxt::ExtrinsicSuccess<T>);
let fields = utils::fields(&bindings);
let marker = utils::marker_field(&fields).unwrap_or_else(|| format_ident!("_"));
let filtered_fields = utils::filter_fields(&fields, &marker);
let args = utils::fields_to_args(&filtered_fields);
let build_struct = utils::build_struct(ident, &fields);
let xt_builder = generate_trait(
&module,
&call_name,
"XtBuilder",
quote!(&'a self),
quote!(T::Hash),
&args,
&build_struct,
&marker,
);
let events_subscriber = generate_trait(
&module,
&call_name,
"EventsSubscriber",
quote!(self),
quote!(#subxt::ExtrinsicSuccess<T>),
&args,
&build_struct,
&marker,
);
let expanded = quote! {
quote! {
impl#generics #subxt::Call<T> for #ident<#(#params),*> {
const MODULE: &'static str = MODULE;
const FUNCTION: &'static str = #call_name;
@@ -67,32 +76,55 @@ pub fn call(s: Structure) -> TokenStream {
}
}
#xt_builder
#events_subscriber
}
}
pub fn generate_trait(
module: &syn::Path,
call: &str,
ty: &str,
me: TokenStream,
ret: TokenStream,
args: &TokenStream,
build_struct: &TokenStream,
marker: &syn::Ident,
) -> TokenStream {
let subxt = utils::use_crate("substrate-subxt");
let codec = utils::use_crate("parity-scale-codec");
let call_trait = format_ident!("{}Call{}", call.to_camel_case(), ty);
let call = format_ident!("{}", call);
let ty = format_ident!("{}", ty);
quote! {
/// Call extension trait.
pub trait #call_trait<T: #module> {
/// Create and submit the extrinsic.
fn #call<'a>(
self,
#me,
#args
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>>;
}
impl<T, P, S, E> #call_trait<T> for #subxt::EventsSubscriber<T, P, S, E>
impl<T, P, S, E> #call_trait<T> for #subxt::#ty<T, P, S, E>
where
T: #module + #subxt::system::System + Send + Sync,
P: #sp_core::Pair,
S: #sp_runtime::traits::Verify + #codec::Codec + From<P::Signature> + Send + 'static,
S::Signer: From<P::Public> + #sp_runtime::traits::IdentifyAccount<AccountId = T::AccountId>,
T: #module + #subxt::system::System + Send + Sync + 'static,
P: #subxt::sp_core::Pair,
S: #subxt::sp_runtime::traits::Verify + #codec::Codec + From<P::Signature> + Send + 'static,
S::Signer: From<P::Public> + #subxt::sp_runtime::traits::IdentifyAccount<AccountId = T::AccountId>,
T::Address: From<T::AccountId>,
E: #subxt::SignedExtra<T> + #sp_runtime::traits::SignedExtension + 'static,
E: #subxt::SignedExtra<T> + #subxt::sp_runtime::traits::SignedExtension + 'static,
{
fn #call<'a>(
self,
#me,
#args
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>> {
Box::pin(self.submit(#ident { #(#fields)* }))
let #marker = core::marker::PhantomData::<T>;
Box::pin(self.submit(#build_struct))
}
}
};
TokenStream::from(expanded)
}
}
#[cfg(test)]
@@ -121,7 +153,39 @@ mod tests {
}
}
pub trait TransferCallExt<T: Balances> {
/// Call extension trait.
pub trait TransferCallXtBuilder<T: Balances> {
/// Create and submit the extrinsic.
fn transfer<'a>(
&'a self,
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>>;
}
impl<T, P, S, E> TransferCallXtBuilder<T> for substrate_subxt::XtBuilder<T, P, S, E>
where
T: Balances + substrate_subxt::system::System + Send + Sync + 'static,
P: substrate_subxt::sp_core::Pair,
S: substrate_subxt::sp_runtime::traits::Verify + codec::Codec + From<P::Signature> + Send + 'static,
S::Signer: From<P::Public> + substrate_subxt::sp_runtime::traits::IdentifyAccount<
AccountId = T::AccountId>,
T::Address: From<T::AccountId>,
E: substrate_subxt::SignedExtra<T> + substrate_subxt::sp_runtime::traits::SignedExtension + 'static,
{
fn transfer<'a>(
&'a self,
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>> {
let _ = core::marker::PhantomData::<T>;
Box::pin(self.submit(TransferCall { to, amount, }))
}
}
/// Call extension trait.
pub trait TransferCallEventsSubscriber<T: Balances> {
/// Create and submit the extrinsic.
fn transfer<'a>(
self,
to: &'a <T as System>::Address,
@@ -129,21 +193,22 @@ mod tests {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<substrate_subxt::ExtrinsicSuccess<T>, substrate_subxt::Error>> + Send + 'a>>;
}
impl<T, P, S, E> TransferCallExt<T> for substrate_subxt::EventsSubscriber<T, P, S, E>
impl<T, P, S, E> TransferCallEventsSubscriber<T> for substrate_subxt::EventsSubscriber<T, P, S, E>
where
T: Balances + substrate_subxt::system::System + Send + Sync,
P: sp_core::Pair,
S: sp_runtime::traits::Verify + codec::Codec + From<P::Signature> + Send + 'static,
S::Signer: From<P::Public> + sp_runtime::traits::IdentifyAccount<
T: Balances + substrate_subxt::system::System + Send + Sync + 'static,
P: substrate_subxt::sp_core::Pair,
S: substrate_subxt::sp_runtime::traits::Verify + codec::Codec + From<P::Signature> + Send + 'static,
S::Signer: From<P::Public> + substrate_subxt::sp_runtime::traits::IdentifyAccount<
AccountId = T::AccountId>,
T::Address: From<T::AccountId>,
E: substrate_subxt::SignedExtra<T> + sp_runtime::traits::SignedExtension + 'static,
E: substrate_subxt::SignedExtra<T> + substrate_subxt::sp_runtime::traits::SignedExtension + 'static,
{
fn transfer<'a>(
self,
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>> {
let _ = core::marker::PhantomData::<T>;
Box::pin(self.submit(TransferCall { to, amount, }))
}
}