subxt_core/config/
default_extrinsic_params.rs

1// Copyright 2019-2024 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or GPL-3.0.
3// see LICENSE for license details.
4
5use super::Config;
6use super::{transaction_extensions, ExtrinsicParams};
7
8/// The default [`super::ExtrinsicParams`] implementation understands common signed extensions
9/// and how to apply them to a given chain.
10pub type DefaultExtrinsicParams<T> = transaction_extensions::AnyOf<
11    T,
12    (
13        transaction_extensions::VerifySignature<T>,
14        transaction_extensions::CheckSpecVersion,
15        transaction_extensions::CheckTxVersion,
16        transaction_extensions::CheckNonce,
17        transaction_extensions::CheckGenesis<T>,
18        transaction_extensions::CheckMortality<T>,
19        transaction_extensions::ChargeAssetTxPayment<T>,
20        transaction_extensions::ChargeTransactionPayment,
21        transaction_extensions::CheckMetadataHash,
22    ),
23>;
24
25/// A builder that outputs the set of [`super::ExtrinsicParams::Params`] required for
26/// [`DefaultExtrinsicParams`]. This may expose methods that aren't applicable to the current
27/// chain; such values will simply be ignored if so.
28pub struct DefaultExtrinsicParamsBuilder<T: Config> {
29    /// `None` means the tx will be immortal, else it's mortal for N blocks (if possible).
30    mortality: Option<u64>,
31    /// `None` means the nonce will be automatically set.
32    nonce: Option<u64>,
33    /// `None` means we'll use the native token.
34    tip_of_asset_id: Option<T::AssetId>,
35    tip: u128,
36    tip_of: u128,
37}
38
39impl<T: Config> Default for DefaultExtrinsicParamsBuilder<T> {
40    fn default() -> Self {
41        Self {
42            mortality: None,
43            tip: 0,
44            tip_of: 0,
45            tip_of_asset_id: None,
46            nonce: None,
47        }
48    }
49}
50
51impl<T: Config> DefaultExtrinsicParamsBuilder<T> {
52    /// Configure new extrinsic params. We default to providing no tip
53    /// and using an immortal transaction unless otherwise configured
54    pub fn new() -> Self {
55        Default::default()
56    }
57
58    /// Make the transaction mortal, given a number of blocks it will be mortal for from
59    /// the current block at the time of submission.
60    pub fn mortal(mut self, for_n_blocks: u64) -> Self {
61        self.mortality = Some(for_n_blocks);
62        self
63    }
64
65    /// Provide a specific nonce for the submitter of the extrinsic
66    pub fn nonce(mut self, nonce: u64) -> Self {
67        self.nonce = Some(nonce);
68        self
69    }
70
71    /// Provide a tip to the block author in the chain's native token.
72    pub fn tip(mut self, tip: u128) -> Self {
73        self.tip = tip;
74        self.tip_of = tip;
75        self.tip_of_asset_id = None;
76        self
77    }
78
79    /// Provide a tip to the block author using the token denominated by the `asset_id` provided. This
80    /// is not applicable on chains which don't use the `ChargeAssetTxPayment` signed extension; in this
81    /// case, no tip will be given.
82    pub fn tip_of(mut self, tip: u128, asset_id: T::AssetId) -> Self {
83        self.tip = 0;
84        self.tip_of = tip;
85        self.tip_of_asset_id = Some(asset_id);
86        self
87    }
88
89    /// Build the extrinsic parameters.
90    pub fn build(self) -> <DefaultExtrinsicParams<T> as ExtrinsicParams<T>>::Params {
91        let check_mortality_params = if let Some(for_n_blocks) = self.mortality {
92            transaction_extensions::CheckMortalityParams::mortal(for_n_blocks)
93        } else {
94            transaction_extensions::CheckMortalityParams::immortal()
95        };
96
97        let charge_asset_tx_params = if let Some(asset_id) = self.tip_of_asset_id {
98            transaction_extensions::ChargeAssetTxPaymentParams::tip_of(self.tip, asset_id)
99        } else {
100            transaction_extensions::ChargeAssetTxPaymentParams::tip(self.tip)
101        };
102
103        let charge_transaction_params =
104            transaction_extensions::ChargeTransactionPaymentParams::tip(self.tip);
105
106        let check_nonce_params = if let Some(nonce) = self.nonce {
107            transaction_extensions::CheckNonceParams::with_nonce(nonce)
108        } else {
109            transaction_extensions::CheckNonceParams::from_chain()
110        };
111
112        (
113            (),
114            (),
115            (),
116            check_nonce_params,
117            (),
118            check_mortality_params,
119            charge_asset_tx_params,
120            charge_transaction_params,
121            (),
122        )
123    }
124}
125
126#[cfg(test)]
127mod test {
128    use super::*;
129
130    fn assert_default<T: Default>(_t: T) {}
131
132    #[test]
133    fn params_are_default() {
134        let params = DefaultExtrinsicParamsBuilder::<crate::config::PolkadotConfig>::new().build();
135        assert_default(params)
136    }
137}