feat: Mortal extrinsic construction

This commit is contained in:
emostov
2020-12-13 20:13:16 -08:00
parent 80aff7eb6e
commit c9059d1a32
8 changed files with 112 additions and 14 deletions
+55 -2
View File
@@ -63,6 +63,12 @@ use sp_core::{
pub use sp_runtime::traits::SignedExtension;
pub use sp_version::RuntimeVersion;
use std::marker::PhantomData;
use sp_runtime::{
traits::{Block, Header},
SaturatedConversion
};
use quote::{TokenStreamExt, quote};
use proc_macro2::TokenStream;
mod error;
mod events;
@@ -84,6 +90,7 @@ pub use crate::{
SignedExtra,
Signer,
UncheckedExtrinsic,
DEFAULT_ERA_PERIOD
},
frame::*,
metadata::{
@@ -193,6 +200,37 @@ pub struct Client<T: Runtime> {
page_size: u32,
}
/// Construction options for a signed extrinsic
// TODO: tip can go in here https://github.com/paritytech/substrate-subxt/issues/187
#[derive(Clone)]
pub struct SignedOptions {
/// The period, measured in blocks, that transaction will live for, starting from a checkpoint
/// block. A good default is 64 (64 * 6secs = 6min 40sec).
///
/// `era_period == None`: immortal transaction.
/// `0 <= era_period <= 65536`: rounded up to the closest power of 2, starting at 4.
/// `65536 < era_period`: 65536.
// pub era_period: Option<u64>,
pub era_period: Option<u64>,
}
// https://github.com/dtolnay/quote/issues/129#issue-481909264
fn options_to_tokens<T : quote::ToTokens>(input: &core::option::Option<T>) -> TokenStream {
match input {
Some(value) => quote!(core::option::Option::Some(#value)),
None => quote!(core::option::Option::None)
}
}
impl quote::ToTokens for self::SignedOptions {
fn to_tokens(&self, tokens: &mut TokenStream) {
let era_period = options_to_tokens(&self.era_period);
tokens.append_all(quote!(
SignedOptions {
era_period: #era_period
}
));
}
}
impl<T: Runtime> Clone for Client<T> {
fn clone(&self) -> Self {
Self {
@@ -444,6 +482,7 @@ impl<T: Runtime> Client<T> {
&self,
call: C,
signer: &(dyn Signer<T> + Send + Sync),
opts: SignedOptions,
) -> Result<UncheckedExtrinsic<T>, Error>
where
<<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned:
@@ -455,12 +494,24 @@ impl<T: Runtime> Client<T> {
self.account(signer.account_id(), None).await?.nonce
};
let call = self.encode(call)?;
let era_opts = if opts.era_period.is_some() {
let era_period = opts.era_period.unwrap();
let current_block = self.block(None::<T::Hash>).await?.unwrap().block;
let current_number = (*current_block.header().number()).saturated_into::<u64>();
let current_hash = current_block.hash();
Some((era_period, current_number, current_hash))
} else {
None
};
let signed = extrinsic::create_signed(
&self.runtime_version,
self.genesis_hash,
account_nonce,
call,
signer,
era_opts,
)
.await?;
Ok(signed)
@@ -498,12 +549,13 @@ impl<T: Runtime> Client<T> {
&self,
call: C,
signer: &(dyn Signer<T> + Send + Sync),
opts: SignedOptions,
) -> Result<T::Hash, Error>
where
<<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned:
Send + Sync,
{
let extrinsic = self.create_signed(call, signer).await?;
let extrinsic = self.create_signed(call, signer, opts).await?;
self.submit_extrinsic(extrinsic).await
}
@@ -512,12 +564,13 @@ impl<T: Runtime> Client<T> {
&self,
call: C,
signer: &(dyn Signer<T> + Send + Sync),
opts: SignedOptions,
) -> Result<ExtrinsicSuccess<T>, Error>
where
<<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned:
Send + Sync,
{
let extrinsic = self.create_signed(call, signer).await?;
let extrinsic = self.create_signed(call, signer, opts).await?;
let decoder = self.events_decoder::<C>();
self.submit_and_watch_extrinsic(extrinsic, decoder).await
}