Add MortalPeriod enum

This commit is contained in:
emostov
2020-12-26 12:11:43 -08:00
parent 6908adbd1a
commit 7cae5d2bc7
2 changed files with 15 additions and 7 deletions
+12 -4
View File
@@ -121,6 +121,14 @@ use crate::{
}, },
}; };
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum MortalPeriod {
/// Create a mortal transaction with the specified period
Mortal(u64),
/// Create an immortal transaction
Immortal,
}
/// ClientBuilder for constructing a Client. /// ClientBuilder for constructing a Client.
#[derive(Default)] #[derive(Default)]
pub struct ClientBuilder<T: Runtime> { pub struct ClientBuilder<T: Runtime> {
@@ -128,7 +136,7 @@ pub struct ClientBuilder<T: Runtime> {
url: Option<String>, url: Option<String>,
client: Option<jsonrpsee::Client>, client: Option<jsonrpsee::Client>,
page_size: Option<u32>, page_size: Option<u32>,
mortal_period: Option<Option<u64>>, mortal_period: Option<MortalPeriod>,
} }
impl<T: Runtime> ClientBuilder<T> { impl<T: Runtime> ClientBuilder<T> {
@@ -162,7 +170,7 @@ impl<T: Runtime> ClientBuilder<T> {
} }
/// Set the mortal period. Must be set if `Metadata::derive_mortal_period` results in an error. /// Set the mortal period. Must be set if `Metadata::derive_mortal_period` results in an error.
pub fn set_mortal_period(mut self, mortal_period: Option<u64>) -> Self { pub fn set_mortal_period(mut self, mortal_period: MortalPeriod) -> Self {
self.mortal_period = Some(mortal_period); self.mortal_period = Some(mortal_period);
self self
} }
@@ -222,7 +230,7 @@ struct ClientSignedOptions {
/// ///
/// Substrate reference: /// Substrate reference:
/// https://docs.rs/sp-runtime/2.0.0/sp_runtime/generic/enum.Era.html#variant.Mortal /// https://docs.rs/sp-runtime/2.0.0/sp_runtime/generic/enum.Era.html#variant.Mortal
pub(crate) mortal_period: Option<u64>, pub(crate) mortal_period: MortalPeriod,
} }
/// Client to interface with a substrate node. /// Client to interface with a substrate node.
@@ -511,7 +519,7 @@ impl<T: Runtime> Client<T> {
self.account(signer.account_id(), None).await?.nonce self.account(signer.account_id(), None).await?.nonce
}; };
let call = self.encode(call)?; let call = self.encode(call)?;
let era_info = if let Some(mortal_period) = self.signed_options.mortal_period { let era_info = if let MortalPeriod::Mortal(mortal_period) = self.signed_options.mortal_period {
let current_block = match self.block(None::<T::Hash>).await? { let current_block = match self.block(None::<T::Hash>).await? {
Some(signed_block) => signed_block.block, Some(signed_block) => signed_block.block,
None => return Err("RPC chain_getBlock returned None when Some(signed_block) was expected".into()), None => return Err("RPC chain_getBlock returned None when Some(signed_block) was expected".into()),
+3 -3
View File
@@ -177,12 +177,12 @@ impl Metadata {
let block_hash_count: u64 = self let block_hash_count: u64 = self
.module("System") .module("System")
.and_then(|sys| sys.constant("BlockHashCount")) .and_then(|sys| sys.constant("BlockHashCount"))
.and_then(|count| count.value::<u32>()) .and_then(|b| b.value::<u32>())
.map(Into::into)?; .map(Into::into)?;
let expected_block_time = self let expected_block_time = self
.module("Timestamp") .module("Timestamp")
.and_then(|babe| babe.constant("MinimumPeriod")) .and_then(|time| time.constant("MinimumPeriod"))
.and_then(|e| e.value::<u64>())? .and_then(|m| m.value::<u64>())?
.checked_mul(2) .checked_mul(2)
.ok_or(MetadataError::MortalPeriodError( .ok_or(MetadataError::MortalPeriodError(
"Underflow or overflow attempting `TimeStamp::MinimumPeriod.checked_mul(2)`", "Underflow or overflow attempting `TimeStamp::MinimumPeriod.checked_mul(2)`",