Improve error handling in proc-macros, handle DispatchError etc. (#123)

* Improve error handling.

* Fix build.

* Handle runtime errors.

* Add runtime trait for better type inference.

* Use runtime trait part 1.

* wip

* Add support for sudo.

* Finish error handling.

* Fix tests.

* Fix clippy warnings.
This commit is contained in:
David Craven
2020-06-22 08:39:40 +02:00
committed by GitHub
parent 21d07c6c24
commit 3080ec91a6
23 changed files with 557 additions and 373 deletions
+14 -21
View File
@@ -28,7 +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 ident = &s.ast().ident;
let generics = &s.ast().generics;
let params = utils::type_params(generics);
@@ -60,32 +59,29 @@ pub fn call(s: Structure) -> TokenStream {
}
/// Call extension trait.
pub trait #call_trait<T: #module, S: #codec::Encode, E: #subxt::SignedExtra<T>> {
pub trait #call_trait<T: #subxt::Runtime + #module> {
/// Create and submit an extrinsic.
fn #call<'a>(
&'a self,
signer: &'a (dyn #subxt::Signer<T, S, E> + Send + Sync),
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>>;
/// Create, submit and watch an extrinsic.
fn #call_and_watch<'a>(
&'a self,
signer: &'a (dyn #subxt::Signer<T, S, E> + Send + Sync),
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>>;
}
impl<T, S, E> #call_trait<T, S, E> for #subxt::Client<T, S, E>
impl<T: #subxt::Runtime + #module> #call_trait<T> for #subxt::Client<T>
where
T: #module + #subxt::system::System + Send + Sync + 'static,
S: #codec::Encode + Send + Sync + 'static,
E: #subxt::SignedExtra<T> + #subxt::sp_runtime::traits::SignedExtension + Send + Sync + 'static,
<<E as #subxt::SignedExtra<T>>::Extra as #subxt::sp_runtime::traits::SignedExtension>::AdditionalSigned: Send + Sync,
<<T::Extra as #subxt::SignedExtra<T>>::Extra as #subxt::SignedExtension>::AdditionalSigned: Send + Sync,
{
fn #call<'a>(
&'a self,
signer: &'a (dyn #subxt::Signer<T, S, E> + Send + Sync),
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>> {
let #marker = core::marker::PhantomData::<T>;
@@ -94,7 +90,7 @@ pub fn call(s: Structure) -> TokenStream {
fn #call_and_watch<'a>(
&'a self,
signer: &'a (dyn #subxt::Signer<T, S, E> + Send + Sync),
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>> {
let #marker = core::marker::PhantomData::<T>;
@@ -130,11 +126,11 @@ mod tests {
}
/// Call extension trait.
pub trait TransferCallExt<T: Balances, S: codec::Encode, E: substrate_subxt::SignedExtra<T>> {
pub trait TransferCallExt<T: substrate_subxt::Runtime + Balances> {
/// Create and submit an extrinsic.
fn transfer<'a>(
&'a self,
signer: &'a (dyn substrate_subxt::Signer<T, S, E> + Send + Sync),
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>>;
@@ -142,22 +138,19 @@ mod tests {
/// Create, submit and watch an extrinsic.
fn transfer_and_watch<'a>(
&'a self,
signer: &'a (dyn substrate_subxt::Signer<T, S, E> + Send + Sync),
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>>;
}
impl<T, S, E> TransferCallExt<T, S, E> for substrate_subxt::Client<T, S, E>
impl<T: substrate_subxt::Runtime + Balances> TransferCallExt<T> for substrate_subxt::Client<T>
where
T: Balances + substrate_subxt::system::System + Send + Sync + 'static,
S: codec::Encode + Send + Sync + 'static,
E: substrate_subxt::SignedExtra<T> + substrate_subxt::sp_runtime::traits::SignedExtension + Send + Sync + 'static,
<<E as substrate_subxt::SignedExtra<T>>::Extra as substrate_subxt::sp_runtime::traits::SignedExtension>::AdditionalSigned: Send + Sync,
<<T::Extra as substrate_subxt::SignedExtra<T>>::Extra as substrate_subxt::SignedExtension>::AdditionalSigned: Send + Sync,
{
fn transfer<'a>(
&'a self,
signer: &'a (dyn substrate_subxt::Signer<T, S, E> + Send + Sync),
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>> {
@@ -167,7 +160,7 @@ mod tests {
fn transfer_and_watch<'a>(
&'a self,
signer: &'a (dyn substrate_subxt::Signer<T, S, E> + Send + Sync),
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>> {