use crate::types::Era;
use codec::{Compact, Encode};
use scale_info::PortableRegistry;
use subxt_core::{
client::ClientState,
config::{Config, ExtrinsicParams, ExtrinsicParamsEncoder, Header, transaction_extensions},
error::ExtrinsicParamsError,
};
use crate::types::metadata::AppId;
#[derive(Debug, Clone, Copy, Default)]
pub struct CheckAppId(pub AppId);
impl<T: Config> transaction_extensions::Params<T> for AppId {}
impl<T: Config> transaction_extensions::Params<T> for CheckAppId {}
impl ExtrinsicParamsEncoder for CheckAppId {
fn encode_value_to(&self, v: &mut Vec<u8>) {
Compact::<u32>(self.0.0).encode_to(v);
}
fn encode_implicit_to(&self, _: &mut Vec<u8>) {}
}
impl<T: Config> subxt_core::config::ExtrinsicParams<T> for CheckAppId {
type Params = AppId;
fn new(_client: &ClientState<T>, id: Self::Params) -> Result<Self, ExtrinsicParamsError> {
Ok(CheckAppId(id))
}
}
impl<T: Config> transaction_extensions::TransactionExtension<T> for CheckAppId {
type Decoded = Compact<u32>;
fn matches(identifier: &str, _type_id: u32, _types: &PortableRegistry) -> bool {
identifier == "CheckAppId"
}
}
pub type OnlyCodecExtra = (
(), (), (), (), Era, Compact<u32>, (), Compact<u128>, AppId, );
pub type DefaultExtrinsicParams<T> = transaction_extensions::AnyOf<
T,
(
transaction_extensions::CheckSpecVersion,
transaction_extensions::CheckTxVersion,
transaction_extensions::CheckGenesis<T>,
transaction_extensions::CheckMortality<T>,
transaction_extensions::CheckNonce,
transaction_extensions::ChargeTransactionPayment,
CheckAppId,
),
>;
pub struct DefaultExtrinsicParamsBuilder<T: Config> {
mortality: Option<Mortality<T::Hash>>,
nonce: Option<u64>,
tip: u128,
app_id: AppId,
}
#[derive(Debug, Clone)]
struct Mortality<Hash> {
checkpoint_hash: Hash,
checkpoint_number: u64,
period: u64,
}
impl<T: Config> Default for DefaultExtrinsicParamsBuilder<T> {
fn default() -> Self {
Self {
mortality: None,
tip: 0,
nonce: None,
app_id: AppId::default(),
}
}
}
impl<T: Config> DefaultExtrinsicParamsBuilder<T> {
pub fn new() -> Self {
Default::default()
}
pub fn mortal(mut self, from_block: &T::Header, for_n_blocks: u64) -> Self {
self.mortality = Some(Mortality {
checkpoint_hash: from_block.hash(),
checkpoint_number: from_block.number().into(),
period: for_n_blocks,
});
self
}
pub fn nonce(mut self, nonce: u64) -> Self {
self.nonce = Some(nonce);
self
}
pub fn app_id(mut self, app_id: u32) -> Self {
self.app_id = AppId(app_id);
self
}
pub fn mortal_unchecked(mut self, from_block_number: u64, from_block_hash: T::Hash, for_n_blocks: u64) -> Self {
self.mortality = Some(Mortality {
checkpoint_hash: from_block_hash,
checkpoint_number: from_block_number,
period: for_n_blocks,
});
self
}
pub fn tip(mut self, tip: u128) -> Self {
self.tip = tip;
self
}
pub fn build(self) -> <DefaultExtrinsicParams<T> as ExtrinsicParams<T>>::Params {
let check_mortality_params = if let Some(mortality) = self.mortality {
transaction_extensions::CheckMortalityParams::mortal_from_unchecked(
mortality.period,
mortality.checkpoint_number,
mortality.checkpoint_hash,
)
} else {
transaction_extensions::CheckMortalityParams::immortal()
};
let charge_transaction_params = transaction_extensions::ChargeTransactionPaymentParams::tip(self.tip);
let check_nonce_params = match self.nonce {
Some(x) => transaction_extensions::CheckNonceParams::with_nonce(x),
None => transaction_extensions::CheckNonceParams::from_chain(),
};
((), (), (), check_mortality_params, check_nonce_params, charge_transaction_params, self.app_id)
}
}