Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChargeAssetTxPayment: support providing u32 or MultiLocation in default impl #1227

Merged
merged 11 commits into from
Nov 3, 2023
4 changes: 3 additions & 1 deletion subxt/examples/setup_config_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use subxt::client::OfflineClientT;
use subxt::config::{Config, ExtrinsicParams, ExtrinsicParamsEncoder};
use subxt_signer::sr25519::dev;

#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale")]
pub mod runtime {}
use runtime::runtime_types::xcm::v2::multilocation::MultiLocation;

// We don't need to construct this at runtime,
// so an empty enum is appropriate:
Expand All @@ -18,6 +19,7 @@ impl Config for CustomConfig {
type Hasher = subxt::config::substrate::BlakeTwo256;
type Header = subxt::config::substrate::SubstrateHeader<u32, Self::Hasher>;
type ExtrinsicParams = CustomExtrinsicParams<Self>;
type AssetId = MultiLocation;
}

// This represents some arbitrary (and nonsensical) custom parameters that
Expand Down
3 changes: 2 additions & 1 deletion subxt/examples/setup_config_signed_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ impl Config for CustomConfig {
signed_extensions::CheckNonce,
signed_extensions::CheckGenesis<Self>,
signed_extensions::CheckMortality<Self>,
signed_extensions::ChargeAssetTxPayment,
signed_extensions::ChargeAssetTxPayment<Self>,
signed_extensions::ChargeTransactionPayment,
// And add a new one of our own:
CustomSignedExtension,
),
>;
type AssetId = u32;
}

// Our custom signed extension doesn't do much:
Expand Down
6 changes: 3 additions & 3 deletions subxt/src/config/default_extrinsic_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub type DefaultExtrinsicParams<T> = signed_extensions::AnyOf<
signed_extensions::CheckNonce,
signed_extensions::CheckGenesis<T>,
signed_extensions::CheckMortality<T>,
signed_extensions::ChargeAssetTxPayment,
signed_extensions::ChargeAssetTxPayment<T>,
signed_extensions::ChargeTransactionPayment,
),
>;
Expand All @@ -27,7 +27,7 @@ pub struct DefaultExtrinsicParamsBuilder<T: Config> {
/// `None` means the tx will be immortal.
mortality: Option<Mortality<T::Hash>>,
/// `None` means we'll use the native token.
tip_of_asset_id: Option<u32>,
tip_of_asset_id: Option<T::AssetId>,
tip: u128,
tip_of: u128,
}
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<T: Config> DefaultExtrinsicParamsBuilder<T> {
/// Provide a tip to the block auther using the token denominated by the `asset_id` provided. This
/// is not applicable on chains which don't use the `ChargeAssetTxPayment` signed extension; in this
/// case, no tip will be given.
pub fn tip_of(mut self, tip: u128, asset_id: u32) -> Self {
pub fn tip_of(mut self, tip: u128, asset_id: T::AssetId) -> Self {
self.tip = 0;
self.tip_of = tip;
self.tip_of_asset_id = Some(asset_id);
Expand Down
3 changes: 3 additions & 0 deletions subxt/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub trait Config: Sized + Send + Sync + 'static {

/// This type defines the extrinsic extra and additional parameters.
type ExtrinsicParams: ExtrinsicParams<Self>;

/// This is used to identify an asset in the `ChargeAssetTxPayment` signed extension.
type AssetId: Debug + Encode;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also impl EncodeAsType possibly (same as a couple of the others above like singature I think), which would at least mean that if somebody tried setting a tip with an asset ID, and it was configured wrong, we'd get an EncodeAsType error back (eg because it failed to encode an Option<u32> into an Option<MultiLocation>) rather than have some error from the node that it failed to decode the tx provided.

For now I'd leave it as is, but we should probably consider moving some things in Config to EncodeAsType instead of Encode so that we get this extra protection.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth an issue to not forget about it 🙏

}

/// given some [`Config`], this return the other params needed for its `ExtrinsicParams`.
Expand Down
1 change: 1 addition & 0 deletions subxt/src/config/polkadot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl Config for PolkadotConfig {
type Hasher = <SubstrateConfig as Config>::Hasher;
type Header = <SubstrateConfig as Config>::Header;
type ExtrinsicParams = PolkadotExtrinsicParams<Self>;
type AssetId = u32;
}

/// A struct representing the signed extra and additional parameters required
Expand Down
32 changes: 20 additions & 12 deletions subxt/src/config/signed_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,27 @@ impl<T: Config> SignedExtension<T> for CheckMortality<T> {

/// The [`ChargeAssetTxPayment`] signed extension.
#[derive(Debug)]
pub struct ChargeAssetTxPayment {
pub struct ChargeAssetTxPayment<T: Config> {
tip: Compact<u128>,
asset_id: Option<u32>,
asset_id: Option<T::AssetId>,
}

/// Parameters to configure the [`ChargeAssetTxPayment`] signed extension.
#[derive(Default)]
pub struct ChargeAssetTxPaymentParams {
pub struct ChargeAssetTxPaymentParams<T: Config> {
tip: u128,
asset_id: Option<u32>,
asset_id: Option<T::AssetId>,
}

impl<T: Config> Default for ChargeAssetTxPaymentParams<T> {
fn default() -> Self {
ChargeAssetTxPaymentParams {
tip: Default::default(),
asset_id: Default::default(),
}
}
}

impl ChargeAssetTxPaymentParams {
impl<T: Config> ChargeAssetTxPaymentParams<T> {
/// Don't provide a tip to the extrinsic author.
pub fn no_tip() -> Self {
ChargeAssetTxPaymentParams {
Expand All @@ -245,16 +253,16 @@ impl ChargeAssetTxPaymentParams {
}
}
/// Tip the extrinsic author using the asset ID given.
pub fn tip_of(tip: u128, asset_id: u32) -> Self {
pub fn tip_of(tip: u128, asset_id: T::AssetId) -> Self {
ChargeAssetTxPaymentParams {
tip,
asset_id: Some(asset_id),
}
}
}

impl<T: Config> ExtrinsicParams<T> for ChargeAssetTxPayment {
type OtherParams = ChargeAssetTxPaymentParams;
impl<T: Config> ExtrinsicParams<T> for ChargeAssetTxPayment<T> {
type OtherParams = ChargeAssetTxPaymentParams<T>;
type Error = std::convert::Infallible;

fn new<Client: OfflineClientT<T>>(
Expand All @@ -269,13 +277,13 @@ impl<T: Config> ExtrinsicParams<T> for ChargeAssetTxPayment {
}
}

impl ExtrinsicParamsEncoder for ChargeAssetTxPayment {
impl<T: Config> ExtrinsicParamsEncoder for ChargeAssetTxPayment<T> {
fn encode_extra_to(&self, v: &mut Vec<u8>) {
(self.tip, self.asset_id).encode_to(v);
(self.tip, &self.asset_id).encode_to(v);
}
}

impl<T: Config> SignedExtension<T> for ChargeAssetTxPayment {
impl<T: Config> SignedExtension<T> for ChargeAssetTxPayment<T> {
const NAME: &'static str = "ChargeAssetTxPayment";
}

Expand Down
1 change: 1 addition & 0 deletions subxt/src/config/substrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Config for SubstrateConfig {
type Hasher = BlakeTwo256;
type Header = SubstrateHeader<u32, BlakeTwo256>;
type ExtrinsicParams = SubstrateExtrinsicParams<Self>;
type AssetId = u32;
}

/// A struct representing the signed extra and additional parameters required
Expand Down