From afb02cbb5875dcd95d057665dc08f82e509f9621 Mon Sep 17 00:00:00 2001 From: NachoPal Date: Fri, 5 Aug 2022 18:07:58 +0200 Subject: [PATCH] backport #1278 --- Cargo.lock | 24 ++ parachains/common/Cargo.toml | 3 + parachains/common/src/xcm_config.rs | 43 ++- .../runtimes/assets/statemine/Cargo.toml | 1 + .../assets/statemine/src/xcm_config.rs | 33 +- .../runtimes/assets/statemine/tests/tests.rs | 301 ++++++++++++++++++ .../runtimes/assets/statemint/Cargo.toml | 1 + .../assets/statemint/src/xcm_config.rs | 3 +- .../runtimes/assets/statemint/tests/tests.rs | 71 +++++ .../runtimes/assets/test-utils/src/lib.rs | 134 ++++++++ .../runtimes/assets/westmint/Cargo.toml | 1 + .../assets/westmint/src/xcm_config.rs | 33 +- .../runtimes/assets/westmint/tests/tests.rs | 300 +++++++++++++++++ primitives/utility/Cargo.toml | 2 + primitives/utility/src/lib.rs | 218 ++++++++++++- 15 files changed, 1157 insertions(+), 11 deletions(-) create mode 100644 parachains/runtimes/assets/statemine/tests/tests.rs create mode 100644 parachains/runtimes/assets/statemint/tests/tests.rs create mode 100644 parachains/runtimes/assets/test-utils/src/lib.rs create mode 100644 parachains/runtimes/assets/westmint/tests/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 2fe08a1eb01..97e65d7e075 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -165,6 +165,25 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" +[[package]] +name = "asset-test-utils" +version = "1.0.0" +dependencies = [ + "frame-support", + "frame-system", + "hex-literal", + "pallet-balances", + "pallet-collator-selection", + "pallet-session", + "parachains-common", + "sp-consensus-aura", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", + "substrate-wasm-builder", +] + [[package]] name = "async-attributes" version = "1.1.2" @@ -1869,6 +1888,7 @@ version = "0.1.0" dependencies = [ "cumulus-primitives-core", "frame-support", + "log", "parity-scale-codec", "polkadot-core-primitives", "polkadot-parachain 0.9.27", @@ -6463,6 +6483,7 @@ dependencies = [ name = "parachains-common" version = "0.9.27" dependencies = [ + "cumulus-primitives-utility", "frame-executive", "frame-support", "frame-system", @@ -11398,6 +11419,7 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" name = "statemine-runtime" version = "0.9.27" dependencies = [ + "asset-test-utils", "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", "cumulus-pallet-parachain-system", @@ -11464,6 +11486,7 @@ dependencies = [ name = "statemint-runtime" version = "0.9.27" dependencies = [ + "asset-test-utils", "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", "cumulus-pallet-parachain-system", @@ -12939,6 +12962,7 @@ dependencies = [ name = "westmint-runtime" version = "0.9.27" dependencies = [ + "asset-test-utils", "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", "cumulus-pallet-parachain-system", diff --git a/parachains/common/Cargo.toml b/parachains/common/Cargo.toml index 6b6c53ec47c..90daa0af813 100644 --- a/parachains/common/Cargo.toml +++ b/parachains/common/Cargo.toml @@ -35,6 +35,7 @@ xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features # Cumulus pallet-collator-selection = { path = "../../pallets/collator-selection", default-features = false } +cumulus-primitives-utility = { path = "../../primitives/utility", default-features = false } [dev-dependencies] pallet-authorship = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.27" } @@ -61,4 +62,6 @@ std = [ "sp-io/std", "sp-std/std", "pallet-collator-selection/std", + "cumulus-primitives-utility/std", + "xcm-builder/std" ] diff --git a/parachains/common/src/xcm_config.rs b/parachains/common/src/xcm_config.rs index d2444282175..d56876d60ca 100644 --- a/parachains/common/src/xcm_config.rs +++ b/parachains/common/src/xcm_config.rs @@ -1,5 +1,10 @@ +use crate::impls::AccountIdOf; use core::marker::PhantomData; -use frame_support::{log, weights::Weight}; +use frame_support::{ + log, + traits::{fungibles::Inspect, tokens::BalanceConversion}, + weights::{Weight, WeightToFee, WeightToFeePolynomial}, +}; use xcm::latest::prelude::*; use xcm_executor::traits::ShouldExecute; @@ -66,3 +71,39 @@ impl ShouldExecute for DenyReserveTransferToRelayChain { Ok(()) } } + +/// A `ChargeFeeInFungibles` implementation that converts the output of +/// a given WeightToFee implementation an amount charged in +/// a particular assetId from pallet-assets +pub struct AssetFeeAsExistentialDepositMultiplier( + PhantomData<(Runtime, WeightToFee, BalanceConverter)>, +); +impl + cumulus_primitives_utility::ChargeWeightInFungibles< + AccountIdOf, + pallet_assets::Pallet, + > for AssetFeeAsExistentialDepositMultiplier +where + Runtime: pallet_assets::Config, + WeightToFee: WeightToFeePolynomial, + BalanceConverter: BalanceConversion< + CurrencyBalance, + ::AssetId, + ::Balance, + >, + AccountIdOf: + From + Into, +{ + fn charge_weight_in_fungibles( + asset_id: as Inspect>>::AssetId, + weight: Weight, + ) -> Result< as Inspect>>::Balance, XcmError> + { + let amount = WeightToFee::weight_to_fee(&weight); + // If the amount gotten is not at least the ED, then make it be the ED of the asset + // This is to avoid burning assets and decreasing the supply + let asset_amount = BalanceConverter::to_asset_balance(amount, asset_id) + .map_err(|_| XcmError::TooExpensive)?; + Ok(asset_amount) + } +} diff --git a/parachains/runtimes/assets/statemine/Cargo.toml b/parachains/runtimes/assets/statemine/Cargo.toml index 49d230ba144..e61e0900340 100644 --- a/parachains/runtimes/assets/statemine/Cargo.toml +++ b/parachains/runtimes/assets/statemine/Cargo.toml @@ -75,6 +75,7 @@ parachains-common = { path = "../../../common", default-features = false } [dev-dependencies] hex-literal = "0.3.4" +asset-test-utils = { path = "../test-utils"} [build-dependencies] substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" } diff --git a/parachains/runtimes/assets/statemine/src/xcm_config.rs b/parachains/runtimes/assets/statemine/src/xcm_config.rs index ce6fd18f91d..3875cf7901b 100644 --- a/parachains/runtimes/assets/statemine/src/xcm_config.rs +++ b/parachains/runtimes/assets/statemine/src/xcm_config.rs @@ -14,7 +14,7 @@ // limitations under the License. use super::{ - AccountId, AssetId, Assets, Balance, Balances, Call, Event, Origin, ParachainInfo, + AccountId, AssetId, Assets, Authorship, Balance, Balances, Call, Event, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, WeightToFee, XcmpQueue, }; use frame_support::{ @@ -25,9 +25,12 @@ use frame_support::{ use pallet_xcm::XcmPassthrough; use parachains_common::{ impls::ToStakingPot, - xcm_config::{DenyReserveTransferToRelayChain, DenyThenTry}, + xcm_config::{ + AssetFeeAsExistentialDepositMultiplier, DenyReserveTransferToRelayChain, DenyThenTry, + }, }; use polkadot_parachain::primitives::Sibling; +use sp_runtime::traits::ConvertInto; use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom, @@ -129,6 +132,7 @@ parameter_types! { // One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate. pub UnitWeightCost: Weight = 1_000_000_000; pub const MaxInstructions: u32 = 100; + pub XcmAssetFeesReceiver: Option = Authorship::author(); } match_types! { @@ -170,8 +174,29 @@ impl xcm_executor::Config for XcmConfig { type LocationInverter = LocationInverter; type Barrier = Barrier; type Weigher = FixedWeightBounds; - type Trader = - UsingComponents>; + type Trader = ( + UsingComponents>, + cumulus_primitives_utility::TakeFirstAssetTrader< + AccountId, + AssetFeeAsExistentialDepositMultiplier< + Runtime, + WeightToFee, + pallet_assets::BalanceToAssetBalance, + >, + ConvertedConcreteAssetId< + AssetId, + Balance, + AsPrefixedGeneralIndex, + JustTry, + >, + Assets, + cumulus_primitives_utility::XcmFeesTo32ByteAccount< + FungiblesTransactor, + AccountId, + XcmAssetFeesReceiver, + >, + >, + ); type ResponseHandler = PolkadotXcm; type AssetTrap = PolkadotXcm; type AssetClaims = PolkadotXcm; diff --git a/parachains/runtimes/assets/statemine/tests/tests.rs b/parachains/runtimes/assets/statemine/tests/tests.rs new file mode 100644 index 00000000000..c99debbced4 --- /dev/null +++ b/parachains/runtimes/assets/statemine/tests/tests.rs @@ -0,0 +1,301 @@ +use asset_test_utils::{ExtBuilder, RuntimeHelper}; +use frame_support::{ + assert_noop, assert_ok, traits::PalletInfo, weights::WeightToFee as WeightToFeeT, +}; +use parachains_common::{AccountId, AuraId}; +pub use statemine_runtime::{ + constants::fee::WeightToFee, xcm_config::XcmConfig, Assets, Balances, ExistentialDeposit, + Runtime, SessionKeys, System, +}; +use xcm::latest::prelude::*; +use xcm_executor::traits::WeightTrader; +pub const ALICE: [u8; 32] = [1u8; 32]; + +#[test] +fn test_asset_xcm_trader() { + ExtBuilder::::default() + .with_collators(vec![AccountId::from(ALICE)]) + .with_session_keys(vec![( + AccountId::from(ALICE), + AccountId::from(ALICE), + SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) }, + )]) + .build() + .execute_with(|| { + // We need root origin to create a sufficient asset + // We set existential deposit to be identical to the one for Balances first + assert_ok!(Assets::force_create( + RuntimeHelper::::root_origin(), + 1, + AccountId::from(ALICE).into(), + true, + ExistentialDeposit::get() + )); + + // We first mint enough asset for the account to exist for assets + assert_ok!(Assets::mint( + RuntimeHelper::::origin_of(AccountId::from(ALICE)), + 1, + AccountId::from(ALICE).into(), + ExistentialDeposit::get() + )); + + let mut trader = ::Trader::new(); + + // Set Alice as block author, who will receive fees + RuntimeHelper::::run_to_block(2, Some(AccountId::from(ALICE))); + + // We are going to buy 4e9 weight + let bought = 4_000_000_000u64; + + // lets calculate amount needed + let amount_needed = WeightToFee::weight_to_fee(&bought); + + let asset_multilocation = MultiLocation::new( + 0, + X2( + PalletInstance( + ::PalletInfo::index::().unwrap() + as u8, + ), + GeneralIndex(1), + ), + ); + + let asset: MultiAsset = (asset_multilocation, amount_needed).into(); + + // Make sure buy_weight does not return an error + assert_ok!(trader.buy_weight(bought, asset.into())); + + // Drop trader + drop(trader); + + // Make sure author(Alice) has received the amount + assert_eq!( + Assets::balance(1, AccountId::from(ALICE)), + ExistentialDeposit::get() + amount_needed + ); + + // We also need to ensure the total supply increased + assert_eq!(Assets::total_supply(1), ExistentialDeposit::get() + amount_needed); + }); +} + +#[test] +fn test_asset_xcm_trader_with_refund() { + ExtBuilder::::default() + .with_collators(vec![AccountId::from(ALICE)]) + .with_session_keys(vec![( + AccountId::from(ALICE), + AccountId::from(ALICE), + SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) }, + )]) + .build() + .execute_with(|| { + // We need root origin to create a sufficient asset + // We set existential deposit to be identical to the one for Balances first + assert_ok!(Assets::force_create( + RuntimeHelper::::root_origin(), + 1, + AccountId::from(ALICE).into(), + true, + ExistentialDeposit::get() + )); + + // We first mint enough asset for the account to exist for assets + assert_ok!(Assets::mint( + RuntimeHelper::::origin_of(AccountId::from(ALICE)), + 1, + AccountId::from(ALICE).into(), + ExistentialDeposit::get() + )); + + let mut trader = ::Trader::new(); + + // Set Alice as block author, who will receive fees + RuntimeHelper::::run_to_block(2, Some(AccountId::from(ALICE))); + + // We are going to buy 4e9 weight + let bought = 4_000_000_000u64; + + let asset_multilocation = MultiLocation::new( + 0, + X2( + PalletInstance( + ::PalletInfo::index::().unwrap() + as u8, + ), + GeneralIndex(1), + ), + ); + + // lets calculate amount needed + let amount_bought = WeightToFee::weight_to_fee(&bought); + + let asset: MultiAsset = (asset_multilocation.clone(), amount_bought).into(); + + // Make sure buy_weight does not return an error + assert_ok!(trader.buy_weight(bought, asset.clone().into())); + + // Make sure again buy_weight does return an error + assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::NotWithdrawable); + + // We actually use half of the weight + let weight_used = bought / 2; + + // Make sure refurnd works. + let amount_refunded = WeightToFee::weight_to_fee(&(bought - weight_used)); + + assert_eq!( + trader.refund_weight(bought - weight_used), + Some((asset_multilocation, amount_refunded).into()) + ); + + // Drop trader + drop(trader); + + // We only should have paid for half of the bought weight + let fees_paid = WeightToFee::weight_to_fee(&weight_used); + + assert_eq!( + Assets::balance(1, AccountId::from(ALICE)), + ExistentialDeposit::get() + fees_paid + ); + + // We also need to ensure the total supply increased + assert_eq!(Assets::total_supply(1), ExistentialDeposit::get() + fees_paid); + }); +} + +#[test] +fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() { + ExtBuilder::::default() + .with_collators(vec![AccountId::from(ALICE)]) + .with_session_keys(vec![( + AccountId::from(ALICE), + AccountId::from(ALICE), + SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) }, + )]) + .build() + .execute_with(|| { + // We need root origin to create a sufficient asset + // We set existential deposit to be identical to the one for Balances first + assert_ok!(Assets::force_create( + RuntimeHelper::::root_origin(), + 1, + AccountId::from(ALICE).into(), + true, + ExistentialDeposit::get() + )); + + let mut trader = ::Trader::new(); + + // Set Alice as block author, who will receive fees + RuntimeHelper::::run_to_block(2, Some(AccountId::from(ALICE))); + + // We are going to buy small amount + let bought = 500_000_000u64; + + let asset_multilocation = MultiLocation::new( + 0, + X2( + PalletInstance( + ::PalletInfo::index::().unwrap() + as u8, + ), + GeneralIndex(1), + ), + ); + + let amount_bought = WeightToFee::weight_to_fee(&bought); + + assert!( + amount_bought < ExistentialDeposit::get(), + "we are testing what happens when the amount does not exceed ED" + ); + + let asset: MultiAsset = (asset_multilocation.clone(), amount_bought).into(); + + // Buy weight should return an error + assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive); + + // not credited since the ED is higher than this value + assert_eq!(Assets::balance(1, AccountId::from(ALICE)), 0); + + // We also need to ensure the total supply did not increase + assert_eq!(Assets::total_supply(1), 0); + }); +} + +#[test] +fn test_that_buying_ed_refund_does_not_refund() { + ExtBuilder::::default() + .with_collators(vec![AccountId::from(ALICE)]) + .with_session_keys(vec![( + AccountId::from(ALICE), + AccountId::from(ALICE), + SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) }, + )]) + .build() + .execute_with(|| { + // We need root origin to create a sufficient asset + // We set existential deposit to be identical to the one for Balances first + assert_ok!(Assets::force_create( + RuntimeHelper::::root_origin(), + 1, + AccountId::from(ALICE).into(), + true, + ExistentialDeposit::get() + )); + + let mut trader = ::Trader::new(); + + // Set Alice as block author, who will receive fees + RuntimeHelper::::run_to_block(2, Some(AccountId::from(ALICE))); + + // We are gonna buy ED + let bought: u64 = ExistentialDeposit::get().try_into().unwrap(); + + let asset_multilocation = MultiLocation::new( + 0, + X2( + PalletInstance( + ::PalletInfo::index::().unwrap() + as u8, + ), + GeneralIndex(1), + ), + ); + + let amount_bought = WeightToFee::weight_to_fee(&bought); + + assert!( + amount_bought < ExistentialDeposit::get(), + "we are testing what happens when the amount does not exceed ED" + ); + + // We know we will have to buy at least ED, so lets make sure first it will + // fail with a payment of less than ED + let asset: MultiAsset = (asset_multilocation.clone(), amount_bought).into(); + assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive); + + // Now lets buy ED at least + let asset: MultiAsset = (asset_multilocation.clone(), ExistentialDeposit::get()).into(); + + // Buy weight should work + assert_ok!(trader.buy_weight(bought, asset.into())); + + // Should return None. We have a specific check making sure we dont go below ED for + // drop payment + assert_eq!(trader.refund_weight(bought), None); + + // Drop trader + drop(trader); + + // Make sure author(Alice) has received the amount + assert_eq!(Assets::balance(1, AccountId::from(ALICE)), ExistentialDeposit::get()); + + // We also need to ensure the total supply increased + assert_eq!(Assets::total_supply(1), ExistentialDeposit::get()); + }); +} diff --git a/parachains/runtimes/assets/statemint/Cargo.toml b/parachains/runtimes/assets/statemint/Cargo.toml index d4a5afd398c..09a6e6c9285 100644 --- a/parachains/runtimes/assets/statemint/Cargo.toml +++ b/parachains/runtimes/assets/statemint/Cargo.toml @@ -74,6 +74,7 @@ parachains-common = { path = "../../../common", default-features = false } [dev-dependencies] hex-literal = "0.3.4" +asset-test-utils = { path = "../test-utils"} [build-dependencies] substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" } diff --git a/parachains/runtimes/assets/statemint/src/xcm_config.rs b/parachains/runtimes/assets/statemint/src/xcm_config.rs index a8c652588cb..abfe9bb9865 100644 --- a/parachains/runtimes/assets/statemint/src/xcm_config.rs +++ b/parachains/runtimes/assets/statemint/src/xcm_config.rs @@ -14,7 +14,7 @@ // limitations under the License. use super::{ - AccountId, AssetId, Assets, Balance, Balances, Call, Event, Origin, ParachainInfo, + AccountId, AssetId, Assets, Authorship, Balance, Balances, Call, Event, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, WeightToFee, XcmpQueue, }; use frame_support::{ @@ -129,6 +129,7 @@ parameter_types! { // One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate. pub UnitWeightCost: Weight = 1_000_000_000; pub const MaxInstructions: u32 = 100; + pub XcmAssetFeesReceiver: Option = Authorship::author(); } match_types! { diff --git a/parachains/runtimes/assets/statemint/tests/tests.rs b/parachains/runtimes/assets/statemint/tests/tests.rs new file mode 100644 index 00000000000..518936210f2 --- /dev/null +++ b/parachains/runtimes/assets/statemint/tests/tests.rs @@ -0,0 +1,71 @@ +use asset_test_utils::{ExtBuilder, RuntimeHelper}; +use frame_support::{ + assert_noop, assert_ok, traits::PalletInfo, weights::WeightToFee as WeightToFeeT, +}; +use parachains_common::{AccountId, StatemintAuraId as AuraId}; +pub use statemint_runtime::{ + constants::fee::WeightToFee, xcm_config::XcmConfig, Assets, Balances, ExistentialDeposit, + Runtime, SessionKeys, System, +}; +use xcm::latest::prelude::*; +use xcm_executor::traits::WeightTrader; +pub const ALICE: [u8; 32] = [1u8; 32]; + +#[test] +fn test_asset_xcm_trader_does_not_work_in_statemine() { + ExtBuilder::::default() + .with_collators(vec![AccountId::from(ALICE)]) + .with_session_keys(vec![( + AccountId::from(ALICE), + AccountId::from(ALICE), + SessionKeys { aura: AuraId::from(sp_core::ed25519::Public::from_raw(ALICE)) }, + )]) + .build() + .execute_with(|| { + // We need root origin to create a sufficient asset + // We set existential deposit to be identical to the one for Balances first + assert_ok!(Assets::force_create( + RuntimeHelper::::root_origin(), + 1, + AccountId::from(ALICE).into(), + true, + ExistentialDeposit::get() + )); + + let mut trader = ::Trader::new(); + + // Set Alice as block author, who will receive fees + RuntimeHelper::::run_to_block(2, Some(AccountId::from(ALICE))); + + // We are going to buy 400e9 weight + // Because of the ED being higher in statemine + // and not to complicate things, we use a little + // bit more of weight + let bought = 400_000_000_000u64; + + // lets calculate amount needed + let amount_needed = WeightToFee::weight_to_fee(&bought); + + let asset_multilocation = MultiLocation::new( + 0, + X2( + PalletInstance( + ::PalletInfo::index::().unwrap() + as u8, + ), + GeneralIndex(1), + ), + ); + + let asset: MultiAsset = (asset_multilocation, amount_needed).into(); + + // Buy weight should return an error, since asset trader not installed + assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive); + + // not credited since the ED is higher than this value + assert_eq!(Assets::balance(1, AccountId::from(ALICE)), 0); + + // We also need to ensure the total supply did not increase + assert_eq!(Assets::total_supply(1), 0); + }); +} diff --git a/parachains/runtimes/assets/test-utils/src/lib.rs b/parachains/runtimes/assets/test-utils/src/lib.rs new file mode 100644 index 00000000000..f37465e5bd6 --- /dev/null +++ b/parachains/runtimes/assets/test-utils/src/lib.rs @@ -0,0 +1,134 @@ +use frame_support::traits::GenesisBuild; +use sp_std::marker::PhantomData; + +use frame_support::traits::OriginTrait; +use parachains_common::AccountId; +use sp_consensus_aura::AURA_ENGINE_ID; +use sp_core::Encode; +use sp_runtime::{Digest, DigestItem}; + +pub type BalanceOf = ::Balance; +pub type AccountIdOf = ::AccountId; +pub type ValidatorIdOf = ::ValidatorId; +pub type SessionKeysOf = ::Keys; + +// Basic builder based on balances, collators and pallet_sessopm +pub struct ExtBuilder< + Runtime: frame_system::Config + pallet_balances::Config + pallet_session::Config, +> { + // endowed accounts with balances + balances: Vec<(AccountIdOf, BalanceOf)>, + // collators to test block prod + collators: Vec>, + // keys added to pallet session + keys: Vec<(AccountIdOf, ValidatorIdOf, SessionKeysOf)>, + _runtime: PhantomData, +} + +impl Default + for ExtBuilder +{ + fn default() -> ExtBuilder { + ExtBuilder { balances: vec![], collators: vec![], keys: vec![], _runtime: PhantomData } + } +} + +impl + ExtBuilder +{ + pub fn with_balances( + mut self, + balances: Vec<(AccountIdOf, BalanceOf)>, + ) -> Self { + self.balances = balances; + self + } + pub fn with_collators(mut self, collators: Vec>) -> Self { + self.collators = collators; + self + } + + pub fn with_session_keys( + mut self, + keys: Vec<(AccountIdOf, ValidatorIdOf, SessionKeysOf)>, + ) -> Self { + self.keys = keys; + self + } + + pub fn build(self) -> sp_io::TestExternalities + where + Runtime: + pallet_collator_selection::Config + pallet_balances::Config + pallet_session::Config, + ValidatorIdOf: From>, + { + let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); + + pallet_balances::GenesisConfig:: { balances: self.balances.into() } + .assimilate_storage(&mut t) + .unwrap(); + + pallet_collator_selection::GenesisConfig:: { + invulnerables: self.collators.clone().into(), + candidacy_bond: Default::default(), + desired_candidates: Default::default(), + } + .assimilate_storage(&mut t) + .unwrap(); + + pallet_session::GenesisConfig:: { keys: self.keys } + .assimilate_storage(&mut t) + .unwrap(); + + let mut ext = sp_io::TestExternalities::new(t); + + ext.execute_with(|| { + frame_system::Pallet::::set_block_number(1u32.into()); + }); + + ext + } +} + +pub struct RuntimeHelper(PhantomData); +/// Utility function that advances the chain to the desired block number. +/// If an author is provided, that author information is injected to all the blocks in the meantime. +impl RuntimeHelper +where + AccountIdOf: + Into<<::Origin as OriginTrait>::AccountId>, +{ + pub fn run_to_block(n: u32, author: Option) { + while frame_system::Pallet::::block_number() < n.into() { + // Set the new block number and author + match author { + Some(ref author) => { + let pre_digest = Digest { + logs: vec![DigestItem::PreRuntime(AURA_ENGINE_ID, author.encode())], + }; + frame_system::Pallet::::reset_events(); + frame_system::Pallet::::initialize( + &(frame_system::Pallet::::block_number() + 1u32.into()), + &frame_system::Pallet::::parent_hash(), + &pre_digest, + ); + }, + None => { + frame_system::Pallet::::set_block_number( + frame_system::Pallet::::block_number() + 1u32.into(), + ); + }, + } + } + } + + pub fn root_origin() -> ::Origin { + ::Origin::root() + } + + pub fn origin_of( + account_id: AccountIdOf, + ) -> ::Origin { + ::Origin::signed(account_id.into()) + } +} diff --git a/parachains/runtimes/assets/westmint/Cargo.toml b/parachains/runtimes/assets/westmint/Cargo.toml index 3195aa45007..52bb9f017a0 100644 --- a/parachains/runtimes/assets/westmint/Cargo.toml +++ b/parachains/runtimes/assets/westmint/Cargo.toml @@ -74,6 +74,7 @@ parachains-common = { path = "../../../common", default-features = false } [dev-dependencies] hex-literal = "0.3.4" +asset-test-utils = { path = "../test-utils"} [build-dependencies] substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" } diff --git a/parachains/runtimes/assets/westmint/src/xcm_config.rs b/parachains/runtimes/assets/westmint/src/xcm_config.rs index 6aec071de0f..4a2daa3380e 100644 --- a/parachains/runtimes/assets/westmint/src/xcm_config.rs +++ b/parachains/runtimes/assets/westmint/src/xcm_config.rs @@ -14,7 +14,7 @@ // limitations under the License. use super::{ - AccountId, AssetId, Assets, Balance, Balances, Call, Event, Origin, ParachainInfo, + AccountId, AssetId, Assets, Authorship, Balance, Balances, Call, Event, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, WeightToFee, XcmpQueue, }; use frame_support::{ @@ -25,9 +25,12 @@ use frame_support::{ use pallet_xcm::XcmPassthrough; use parachains_common::{ impls::ToStakingPot, - xcm_config::{DenyReserveTransferToRelayChain, DenyThenTry}, + xcm_config::{ + AssetFeeAsExistentialDepositMultiplier, DenyReserveTransferToRelayChain, DenyThenTry, + }, }; use polkadot_parachain::primitives::Sibling; +use sp_runtime::traits::ConvertInto; use xcm::latest::prelude::*; use xcm_builder::{ AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom, @@ -130,6 +133,7 @@ parameter_types! { // One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate. pub UnitWeightCost: Weight = 1_000_000_000; pub const MaxInstructions: u32 = 100; + pub XcmAssetFeesReceiver: Option = Authorship::author(); } match_types! { @@ -167,8 +171,29 @@ impl xcm_executor::Config for XcmConfig { type LocationInverter = LocationInverter; type Barrier = Barrier; type Weigher = FixedWeightBounds; - type Trader = - UsingComponents>; + type Trader = ( + UsingComponents>, + cumulus_primitives_utility::TakeFirstAssetTrader< + AccountId, + AssetFeeAsExistentialDepositMultiplier< + Runtime, + WeightToFee, + pallet_assets::BalanceToAssetBalance, + >, + ConvertedConcreteAssetId< + AssetId, + Balance, + AsPrefixedGeneralIndex, + JustTry, + >, + Assets, + cumulus_primitives_utility::XcmFeesTo32ByteAccount< + FungiblesTransactor, + AccountId, + XcmAssetFeesReceiver, + >, + >, + ); type ResponseHandler = PolkadotXcm; type AssetTrap = PolkadotXcm; type AssetClaims = PolkadotXcm; diff --git a/parachains/runtimes/assets/westmint/tests/tests.rs b/parachains/runtimes/assets/westmint/tests/tests.rs new file mode 100644 index 00000000000..a967c85add3 --- /dev/null +++ b/parachains/runtimes/assets/westmint/tests/tests.rs @@ -0,0 +1,300 @@ +use asset_test_utils::{ExtBuilder, RuntimeHelper}; +use frame_support::{ + assert_noop, assert_ok, traits::PalletInfo, weights::WeightToFee as WeightToFeeT, +}; +use parachains_common::{AccountId, AuraId}; +pub use westmint_runtime::{ + constants::fee::WeightToFee, xcm_config::XcmConfig, Assets, Balances, ExistentialDeposit, + Runtime, SessionKeys, System, +}; +use xcm::latest::prelude::*; +use xcm_executor::traits::WeightTrader; + +pub const ALICE: [u8; 32] = [1u8; 32]; + +#[test] +fn test_asset_xcm_trader() { + ExtBuilder::::default() + .with_collators(vec![AccountId::from(ALICE)]) + .with_session_keys(vec![( + AccountId::from(ALICE), + AccountId::from(ALICE), + SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) }, + )]) + .build() + .execute_with(|| { + // We need root origin to create a sufficient asset + // We set existential deposit to be identical to the one for Balances first + assert_ok!(Assets::force_create( + RuntimeHelper::::root_origin(), + 1, + AccountId::from(ALICE).into(), + true, + ExistentialDeposit::get() + )); + + // We first mint enough asset for the account to exist for assets + assert_ok!(Assets::mint( + RuntimeHelper::::origin_of(AccountId::from(ALICE)), + 1, + AccountId::from(ALICE).into(), + ExistentialDeposit::get() + )); + + let mut trader = ::Trader::new(); + + // Set Alice as block author, who will receive fees + RuntimeHelper::::run_to_block(2, Some(AccountId::from(ALICE))); + + // We are going to buy 4e9 weight + let bought = 4_000_000_000u64; + + // lets calculate amount needed + let amount_needed = WeightToFee::weight_to_fee(&bought); + + let asset_multilocation = MultiLocation::new( + 0, + X2( + PalletInstance( + ::PalletInfo::index::().unwrap() + as u8, + ), + GeneralIndex(1), + ), + ); + + let asset: MultiAsset = (asset_multilocation, amount_needed).into(); + + // Make sure buy_weight does not return an error + assert_ok!(trader.buy_weight(bought, asset.into())); + + // Drop trader + drop(trader); + + // Make sure author(Alice) has received the amount + assert_eq!( + Assets::balance(1, AccountId::from(ALICE)), + ExistentialDeposit::get() + amount_needed + ); + + // We also need to ensure the total supply increased + assert_eq!(Assets::total_supply(1), ExistentialDeposit::get() + amount_needed); + }); +} + +#[test] +fn test_asset_xcm_trader_with_refund() { + ExtBuilder::::default() + .with_collators(vec![AccountId::from(ALICE)]) + .with_session_keys(vec![( + AccountId::from(ALICE), + AccountId::from(ALICE), + SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) }, + )]) + .build() + .execute_with(|| { + // We need root origin to create a sufficient asset + // We set existential deposit to be identical to the one for Balances first + assert_ok!(Assets::force_create( + RuntimeHelper::::root_origin(), + 1, + AccountId::from(ALICE).into(), + true, + ExistentialDeposit::get() + )); + + // We first mint enough asset for the account to exist for assets + assert_ok!(Assets::mint( + RuntimeHelper::::origin_of(AccountId::from(ALICE)), + 1, + AccountId::from(ALICE).into(), + ExistentialDeposit::get() + )); + + let mut trader = ::Trader::new(); + + // Set Alice as block author, who will receive fees + RuntimeHelper::::run_to_block(2, Some(AccountId::from(ALICE))); + + // We are going to buy 4e9 weight + let bought = 4_000_000_000u64; + let asset_multilocation = MultiLocation::new( + 0, + X2( + PalletInstance( + ::PalletInfo::index::().unwrap() + as u8, + ), + GeneralIndex(1), + ), + ); + + // lets calculate amount needed + let amount_bought = WeightToFee::weight_to_fee(&bought); + + let asset: MultiAsset = (asset_multilocation.clone(), amount_bought).into(); + + // Make sure buy_weight does not return an error + assert_ok!(trader.buy_weight(bought, asset.clone().into())); + + // Make sure again buy_weight does return an error + assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::NotWithdrawable); + + // We actually use half of the weight + let weight_used = bought / 2; + + // Make sure refurnd works. + let amount_refunded = WeightToFee::weight_to_fee(&(bought - weight_used)); + + assert_eq!( + trader.refund_weight(bought - weight_used), + Some((asset_multilocation, amount_refunded).into()) + ); + + // Drop trader + drop(trader); + + // We only should have paid for half of the bought weight + let fees_paid = WeightToFee::weight_to_fee(&weight_used); + + assert_eq!( + Assets::balance(1, AccountId::from(ALICE)), + ExistentialDeposit::get() + fees_paid + ); + + // We also need to ensure the total supply increased + assert_eq!(Assets::total_supply(1), ExistentialDeposit::get() + fees_paid); + }); +} + +#[test] +fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() { + ExtBuilder::::default() + .with_collators(vec![AccountId::from(ALICE)]) + .with_session_keys(vec![( + AccountId::from(ALICE), + AccountId::from(ALICE), + SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) }, + )]) + .build() + .execute_with(|| { + // We need root origin to create a sufficient asset + // We set existential deposit to be identical to the one for Balances first + assert_ok!(Assets::force_create( + RuntimeHelper::::root_origin(), + 1, + AccountId::from(ALICE).into(), + true, + ExistentialDeposit::get() + )); + + let mut trader = ::Trader::new(); + + // Set Alice as block author, who will receive fees + RuntimeHelper::::run_to_block(2, Some(AccountId::from(ALICE))); + + // We are going to buy 4e9 weight + let bought = 500_000_000u64; + + let asset_multilocation = MultiLocation::new( + 0, + X2( + PalletInstance( + ::PalletInfo::index::().unwrap() + as u8, + ), + GeneralIndex(1), + ), + ); + + let amount_bought = WeightToFee::weight_to_fee(&bought); + + assert!( + amount_bought < ExistentialDeposit::get(), + "we are testing what happens when the amount does not exceed ED" + ); + + let asset: MultiAsset = (asset_multilocation.clone(), amount_bought).into(); + + // Buy weight should return an error + assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive); + + // not credited since the ED is higher than this value + assert_eq!(Assets::balance(1, AccountId::from(ALICE)), 0); + + // We also need to ensure the total supply did not increase + assert_eq!(Assets::total_supply(1), 0); + }); +} + +#[test] +fn test_that_buying_ed_refund_does_not_refund() { + ExtBuilder::::default() + .with_collators(vec![AccountId::from(ALICE)]) + .with_session_keys(vec![( + AccountId::from(ALICE), + AccountId::from(ALICE), + SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) }, + )]) + .build() + .execute_with(|| { + // We need root origin to create a sufficient asset + // We set existential deposit to be identical to the one for Balances first + assert_ok!(Assets::force_create( + RuntimeHelper::::root_origin(), + 1, + AccountId::from(ALICE).into(), + true, + ExistentialDeposit::get() + )); + + let mut trader = ::Trader::new(); + + // Set Alice as block author, who will receive fees + RuntimeHelper::::run_to_block(2, Some(AccountId::from(ALICE))); + + let bought = 500_000_000u64; + + let asset_multilocation = MultiLocation::new( + 0, + X2( + PalletInstance( + ::PalletInfo::index::().unwrap() + as u8, + ), + GeneralIndex(1), + ), + ); + + let amount_bought = WeightToFee::weight_to_fee(&bought); + + assert!( + amount_bought < ExistentialDeposit::get(), + "we are testing what happens when the amount does not exceed ED" + ); + + // We know we will have to buy at least ED, so lets make sure first it will + // fail with a payment of less than ED + let asset: MultiAsset = (asset_multilocation.clone(), amount_bought).into(); + assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive); + + // Now lets buy ED at least + let asset: MultiAsset = (asset_multilocation.clone(), ExistentialDeposit::get()).into(); + + // Buy weight should work + assert_ok!(trader.buy_weight(bought, asset.into())); + + // Should return None. We have a specific check making sure we dont go below ED for + // drop payment + assert_eq!(trader.refund_weight(bought), None); + + // Drop trader + drop(trader); + + // Make sure author(Alice) has received the amount + assert_eq!(Assets::balance(1, AccountId::from(ALICE)), ExistentialDeposit::get()); + + // We also need to ensure the total supply increased + assert_eq!(Assets::total_supply(1), ExistentialDeposit::get()); + }); +} diff --git a/primitives/utility/Cargo.toml b/primitives/utility/Cargo.toml index 628f412bca6..f5fad5d030f 100644 --- a/primitives/utility/Cargo.toml +++ b/primitives/utility/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ "derive" ] } +log = { version = "0.4.17", default-features = false } # Substrate frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.27" } @@ -37,4 +38,5 @@ std = [ "polkadot-parachain/std", "polkadot-primitives/std", "cumulus-primitives-core/std", + "xcm-executor/std", ] diff --git a/primitives/utility/src/lib.rs b/primitives/utility/src/lib.rs index 69397247a39..ea7a81f327d 100644 --- a/primitives/utility/src/lib.rs +++ b/primitives/utility/src/lib.rs @@ -21,9 +21,16 @@ use codec::Encode; use cumulus_primitives_core::UpwardMessageSender; +use frame_support::{ + traits::tokens::{fungibles, fungibles::Inspect}, + weights::Weight, +}; +use sp_runtime::{traits::Saturating, SaturatedConversion}; + use sp_std::marker::PhantomData; use xcm::{latest::prelude::*, WrapVersion}; - +use xcm_builder::TakeRevenue; +use xcm_executor::traits::{MatchesFungibles, TransactAsset, WeightTrader}; /// Xcm router which recognises the `Parent` destination and handles it by sending the message into /// the given UMP `UpwardMessageSender` implementation. Thus this essentially adapts an /// `UpwardMessageSender` trait impl into a `SendXcm` trait impl. @@ -51,3 +58,212 @@ impl SendXcm for ParentAsUmp { } } } + +/// Contains information to handle refund/payment for xcm-execution +#[derive(Clone, Eq, PartialEq, Debug)] +struct AssetTraderRefunder { + // The amount of weight bought minus the weigh already refunded + weight_outstanding: Weight, + // The concrete asset containing the asset location and outstanding balance + outstanding_concrete_asset: MultiAsset, +} + +/// Charges for exercution in the first multiasset of those selected for fee payment +/// Only succeeds for Concrete Fungible Assets +/// First tries to convert the this MultiAsset into a local assetId +/// Then charges for this assetId as described by FeeCharger +/// Weight, paid balance, local asset Id and the multilocation is stored for +/// later refund purposes +/// Important: Errors if the Trader is being called twice by 2 BuyExecution instructions +/// Alternatively we could just return payment in the aforementioned case +pub struct TakeFirstAssetTrader< + AccountId, + FeeCharger: ChargeWeightInFungibles, + Matcher: MatchesFungibles, + ConcreteAssets: fungibles::Mutate + fungibles::Transfer + fungibles::Balanced, + HandleRefund: TakeRevenue, +>( + Option, + PhantomData<(AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund)>, +); +impl< + AccountId, + FeeCharger: ChargeWeightInFungibles, + Matcher: MatchesFungibles, + ConcreteAssets: fungibles::Mutate + + fungibles::Transfer + + fungibles::Balanced, + HandleRefund: TakeRevenue, + > WeightTrader + for TakeFirstAssetTrader +{ + fn new() -> Self { + Self(None, PhantomData) + } + // We take first multiasset + // Check whether we can convert fee to asset_fee (is_sufficient, min_deposit) + // If everything goes well, we charge. + fn buy_weight( + &mut self, + weight: Weight, + payment: xcm_executor::Assets, + ) -> Result { + log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::buy_weight weight: {:?}, payment: {:?}", weight, payment); + + // Make sure we dont enter twice + if self.0.is_some() { + return Err(XcmError::NotWithdrawable) + } + + // We take the very first multiasset from payment + let multiassets: MultiAssets = payment.clone().into(); + + // Take the first multiasset from the selected MultiAssets + let first = multiassets.get(0).ok_or(XcmError::AssetNotFound)?; + + // Get the local asset id in which we can pay for fees + let (local_asset_id, _) = + Matcher::matches_fungibles(&first).map_err(|_| XcmError::AssetNotFound)?; + + // Calculate how much we should charge in the asset_id for such amount of weight + // Require at least a payment of minimum_balance + // Necessary for fully collateral-backed assets + let asset_balance: u128 = FeeCharger::charge_weight_in_fungibles(local_asset_id, weight) + .map(|amount| { + let minimum_balance = ConcreteAssets::minimum_balance(local_asset_id); + if amount < minimum_balance { + minimum_balance + } else { + amount + } + })? + .try_into() + .map_err(|_| XcmError::Overflow)?; + + // Convert to the same kind of multiasset, with the required fungible balance + let required = first.id.clone().into_multiasset(asset_balance.into()); + + // Substract payment + let unused = payment.checked_sub(required.clone()).map_err(|_| XcmError::TooExpensive)?; + + // record weight and multiasset + self.0 = Some(AssetTraderRefunder { + weight_outstanding: weight, + outstanding_concrete_asset: required, + }); + + Ok(unused) + } + + fn refund_weight(&mut self, weight: Weight) -> Option { + log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::refund_weight weight: {:?}", weight); + if let Some(AssetTraderRefunder { + mut weight_outstanding, + outstanding_concrete_asset: MultiAsset { id, fun }, + }) = self.0.clone() + { + let weight = weight.min(weight_outstanding); + + // Get the local asset id in which we can refund fees + let (local_asset_id, outstanding_balance) = + Matcher::matches_fungibles(&(id.clone(), fun).into()).ok()?; + + let minimum_balance = ConcreteAssets::minimum_balance(local_asset_id); + + // Calculate asset_balance + // This read should have already be cached in buy_weight + let (asset_balance, outstanding_minus_substracted) = + FeeCharger::charge_weight_in_fungibles(local_asset_id, weight).ok().map( + |asset_balance| { + // Require at least a drop of minimum_balance + // Necessary for fully collateral-backed assets + if outstanding_balance.saturating_sub(asset_balance) > minimum_balance { + (asset_balance, outstanding_balance.saturating_sub(asset_balance)) + } + // If the amount to be refunded leaves the remaining balance below ED, + // we just refund the exact amount that guarantees at least ED will be + // dropped + else { + (outstanding_balance.saturating_sub(minimum_balance), minimum_balance) + } + }, + )?; + + // Convert balances into u128 + let outstanding_minus_substracted: u128 = + outstanding_minus_substracted.saturated_into(); + let asset_balance: u128 = asset_balance.saturated_into(); + + // Construct outstanding_concrete_asset with the same location id and substracted balance + let outstanding_concrete_asset: MultiAsset = + (id.clone(), outstanding_minus_substracted).into(); + + // Substract from existing weight and balance + weight_outstanding = weight_outstanding.saturating_sub(weight); + + // Override AssetTraderRefunder + self.0 = Some(AssetTraderRefunder { weight_outstanding, outstanding_concrete_asset }); + + // Only refund if positive + if asset_balance > 0 { + Some((id, asset_balance).into()) + } else { + None + } + } else { + None + } + } +} + +impl< + AccountId, + FeeCharger: ChargeWeightInFungibles, + Matcher: MatchesFungibles, + ConcreteAssets: fungibles::Mutate + + fungibles::Transfer + + fungibles::Balanced, + HandleRefund: TakeRevenue, + > Drop for TakeFirstAssetTrader +{ + fn drop(&mut self) { + if let Some(asset_trader) = self.0.clone() { + HandleRefund::take_revenue(asset_trader.outstanding_concrete_asset); + } + } +} + +/// XCM fee depositor to which we implement the TakeRevenue trait +/// It receives a Transact implemented argument, a 32 byte convertible acocuntId, and the fee receiver account +/// FungiblesMutateAdapter should be identical to that implemented by WithdrawAsset +pub struct XcmFeesTo32ByteAccount( + PhantomData<(FungiblesMutateAdapter, AccountId, ReceiverAccount)>, +); +impl< + FungiblesMutateAdapter: TransactAsset, + AccountId: Clone + Into<[u8; 32]>, + ReceiverAccount: frame_support::traits::Get>, + > TakeRevenue for XcmFeesTo32ByteAccount +{ + fn take_revenue(revenue: MultiAsset) { + if let Some(receiver) = ReceiverAccount::get() { + let ok = FungiblesMutateAdapter::deposit_asset( + &revenue, + &(X1(AccountId32 { network: Any, id: receiver.into() }).into()), + ) + .is_ok(); + + debug_assert!(ok, "`deposit_asset` cannot generally fail; qed"); + } + } +} + +/// ChargeWeightInFungibles trait, which converts a given amount of weight +/// and an assetId, and it returns the balance amount that should be charged +/// in such assetId for that amount of weight +pub trait ChargeWeightInFungibles> { + fn charge_weight_in_fungibles( + asset_id: >::AssetId, + weight: Weight, + ) -> Result<>::Balance, XcmError>; +}