mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 07:58:02 +00:00
Statemint runtimes to accept sufficient assets as xcm fee payment (#1278)
* point to my branch * girazoki-add-TakeFirstAssetTrader-to-utility * Commit lock * point at custom branch * add new trader to statemine runtimes * compiles * Back to master * Update last tomls * Imports up * remove non-needing imports * FMT * log messages properly * Use TakeRevenue instead of HandleCredit * Introduce xcm fee handler * check total supply in tests * FMT * fix test * Start decoupling balance calculation into different traits * Make traits a bit more generic * PR suggestions * add import * import well * Place xcmfeesassethandler into parachains common * fix tests * config parameters * Min amount to fee receiver * Make minimum amount for block author to be at least the ED * Doc in AssetFeeAsExistentialDepositMultiplier * saturating sub * make sure we dont enter twice * FMT * fmt again * adapt tests * Add doc and struct for weight refund * Doc * More doc * PR suggestions * store all info related to asset payment as multiasset * return AssetNotFound instead of TooExpensive * Use asset transactor to deposit fee * uninstall from statemint * R for RUntime and CON for BalanceConverter * Rework logic to avoid unnecesary match and error * Rework ED check, also in case of refund * rework typo * In case refund makes drop below ED, just refund the difference * fix test westmint * clone id * move test imports to preamble * move test imports to preamble * test-utils with builderS * lock file updated * remove unused imports Co-authored-by: Stephen Shelton <steve@brewcraft.org> Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> Co-authored-by: joepetrowski <joe@parity.io>
This commit is contained in:
@@ -31,9 +31,11 @@ polkadot-primitives = { git = "https://github.com/paritytech/polkadot", default-
|
||||
polkadot-runtime-common = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
|
||||
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
|
||||
xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
|
||||
xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
|
||||
|
||||
# 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 = "master" }
|
||||
@@ -60,4 +62,6 @@ std = [
|
||||
"sp-io/std",
|
||||
"sp-std/std",
|
||||
"pallet-collator-selection/std",
|
||||
"cumulus-primitives-utility/std",
|
||||
"xcm-builder/std"
|
||||
]
|
||||
|
||||
@@ -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<Runtime, WeightToFee, BalanceConverter>(
|
||||
PhantomData<(Runtime, WeightToFee, BalanceConverter)>,
|
||||
);
|
||||
impl<CurrencyBalance, Runtime, WeightToFee, BalanceConverter>
|
||||
cumulus_primitives_utility::ChargeWeightInFungibles<
|
||||
AccountIdOf<Runtime>,
|
||||
pallet_assets::Pallet<Runtime>,
|
||||
> for AssetFeeAsExistentialDepositMultiplier<Runtime, WeightToFee, BalanceConverter>
|
||||
where
|
||||
Runtime: pallet_assets::Config,
|
||||
WeightToFee: WeightToFeePolynomial<Balance = CurrencyBalance>,
|
||||
BalanceConverter: BalanceConversion<
|
||||
CurrencyBalance,
|
||||
<Runtime as pallet_assets::Config>::AssetId,
|
||||
<Runtime as pallet_assets::Config>::Balance,
|
||||
>,
|
||||
AccountIdOf<Runtime>:
|
||||
From<polkadot_primitives::v2::AccountId> + Into<polkadot_primitives::v2::AccountId>,
|
||||
{
|
||||
fn charge_weight_in_fungibles(
|
||||
asset_id: <pallet_assets::Pallet<Runtime> as Inspect<AccountIdOf<Runtime>>>::AssetId,
|
||||
weight: Weight,
|
||||
) -> Result<<pallet_assets::Pallet<Runtime> as Inspect<AccountIdOf<Runtime>>>::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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 = "master" }
|
||||
|
||||
@@ -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<AccountId> = Authorship::author();
|
||||
}
|
||||
|
||||
match_types! {
|
||||
@@ -170,8 +174,29 @@ impl xcm_executor::Config for XcmConfig {
|
||||
type LocationInverter = LocationInverter<Ancestry>;
|
||||
type Barrier = Barrier;
|
||||
type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
|
||||
type Trader =
|
||||
UsingComponents<WeightToFee, KsmLocation, AccountId, Balances, ToStakingPot<Runtime>>;
|
||||
type Trader = (
|
||||
UsingComponents<WeightToFee, KsmLocation, AccountId, Balances, ToStakingPot<Runtime>>,
|
||||
cumulus_primitives_utility::TakeFirstAssetTrader<
|
||||
AccountId,
|
||||
AssetFeeAsExistentialDepositMultiplier<
|
||||
Runtime,
|
||||
WeightToFee,
|
||||
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto>,
|
||||
>,
|
||||
ConvertedConcreteAssetId<
|
||||
AssetId,
|
||||
Balance,
|
||||
AsPrefixedGeneralIndex<AssetsPalletLocation, AssetId, JustTry>,
|
||||
JustTry,
|
||||
>,
|
||||
Assets,
|
||||
cumulus_primitives_utility::XcmFeesTo32ByteAccount<
|
||||
FungiblesTransactor,
|
||||
AccountId,
|
||||
XcmAssetFeesReceiver,
|
||||
>,
|
||||
>,
|
||||
);
|
||||
type ResponseHandler = PolkadotXcm;
|
||||
type AssetTrap = PolkadotXcm;
|
||||
type AssetClaims = PolkadotXcm;
|
||||
|
||||
@@ -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::<Runtime>::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::<Runtime>::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::<Runtime>::origin_of(AccountId::from(ALICE)),
|
||||
1,
|
||||
AccountId::from(ALICE).into(),
|
||||
ExistentialDeposit::get()
|
||||
));
|
||||
|
||||
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
||||
|
||||
// Set Alice as block author, who will receive fees
|
||||
RuntimeHelper::<Runtime>::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(
|
||||
<Runtime as frame_system::Config>::PalletInfo::index::<Assets>().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::<Runtime>::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::<Runtime>::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::<Runtime>::origin_of(AccountId::from(ALICE)),
|
||||
1,
|
||||
AccountId::from(ALICE).into(),
|
||||
ExistentialDeposit::get()
|
||||
));
|
||||
|
||||
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
||||
|
||||
// Set Alice as block author, who will receive fees
|
||||
RuntimeHelper::<Runtime>::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(
|
||||
<Runtime as frame_system::Config>::PalletInfo::index::<Assets>().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::<Runtime>::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::<Runtime>::root_origin(),
|
||||
1,
|
||||
AccountId::from(ALICE).into(),
|
||||
true,
|
||||
ExistentialDeposit::get()
|
||||
));
|
||||
|
||||
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
||||
|
||||
// Set Alice as block author, who will receive fees
|
||||
RuntimeHelper::<Runtime>::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(
|
||||
<Runtime as frame_system::Config>::PalletInfo::index::<Assets>().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::<Runtime>::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::<Runtime>::root_origin(),
|
||||
1,
|
||||
AccountId::from(ALICE).into(),
|
||||
true,
|
||||
ExistentialDeposit::get()
|
||||
));
|
||||
|
||||
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
||||
|
||||
// Set Alice as block author, who will receive fees
|
||||
RuntimeHelper::<Runtime>::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(
|
||||
<Runtime as frame_system::Config>::PalletInfo::index::<Assets>().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());
|
||||
});
|
||||
}
|
||||
@@ -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 = "master" }
|
||||
|
||||
@@ -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<AccountId> = Authorship::author();
|
||||
}
|
||||
|
||||
match_types! {
|
||||
|
||||
@@ -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::<Runtime>::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::<Runtime>::root_origin(),
|
||||
1,
|
||||
AccountId::from(ALICE).into(),
|
||||
true,
|
||||
ExistentialDeposit::get()
|
||||
));
|
||||
|
||||
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
||||
|
||||
// Set Alice as block author, who will receive fees
|
||||
RuntimeHelper::<Runtime>::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(
|
||||
<Runtime as frame_system::Config>::PalletInfo::index::<Assets>().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);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
[package]
|
||||
name = "asset-test-utils"
|
||||
version = "1.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2021"
|
||||
description = "Statemint parachain runtime"
|
||||
|
||||
[dependencies]
|
||||
|
||||
# Substrate
|
||||
|
||||
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
|
||||
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
|
||||
pallet-balances = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
|
||||
pallet-session = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
|
||||
sp-consensus-aura = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
|
||||
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
|
||||
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
|
||||
|
||||
# Cumulus
|
||||
pallet-collator-selection = { path = "../../../../pallets/collator-selection", default-features = false }
|
||||
parachains-common = { path = "../../../common", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
hex-literal = "0.3.4"
|
||||
|
||||
[build-dependencies]
|
||||
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
std = [
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"pallet-balances/std",
|
||||
"pallet-collator-selection/std",
|
||||
"pallet-session/std",
|
||||
"parachains-common/std",
|
||||
"sp-consensus-aura/std",
|
||||
"sp-io/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
@@ -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<Runtime> = <Runtime as pallet_balances::Config>::Balance;
|
||||
pub type AccountIdOf<Runtime> = <Runtime as frame_system::Config>::AccountId;
|
||||
pub type ValidatorIdOf<Runtime> = <Runtime as pallet_session::Config>::ValidatorId;
|
||||
pub type SessionKeysOf<Runtime> = <Runtime as pallet_session::Config>::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<Runtime>, BalanceOf<Runtime>)>,
|
||||
// collators to test block prod
|
||||
collators: Vec<AccountIdOf<Runtime>>,
|
||||
// keys added to pallet session
|
||||
keys: Vec<(AccountIdOf<Runtime>, ValidatorIdOf<Runtime>, SessionKeysOf<Runtime>)>,
|
||||
_runtime: PhantomData<Runtime>,
|
||||
}
|
||||
|
||||
impl<Runtime: frame_system::Config + pallet_balances::Config + pallet_session::Config> Default
|
||||
for ExtBuilder<Runtime>
|
||||
{
|
||||
fn default() -> ExtBuilder<Runtime> {
|
||||
ExtBuilder { balances: vec![], collators: vec![], keys: vec![], _runtime: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<Runtime: frame_system::Config + pallet_balances::Config + pallet_session::Config>
|
||||
ExtBuilder<Runtime>
|
||||
{
|
||||
pub fn with_balances(
|
||||
mut self,
|
||||
balances: Vec<(AccountIdOf<Runtime>, BalanceOf<Runtime>)>,
|
||||
) -> Self {
|
||||
self.balances = balances;
|
||||
self
|
||||
}
|
||||
pub fn with_collators(mut self, collators: Vec<AccountIdOf<Runtime>>) -> Self {
|
||||
self.collators = collators;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_session_keys(
|
||||
mut self,
|
||||
keys: Vec<(AccountIdOf<Runtime>, ValidatorIdOf<Runtime>, SessionKeysOf<Runtime>)>,
|
||||
) -> 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<Runtime>: From<AccountIdOf<Runtime>>,
|
||||
{
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
|
||||
|
||||
pallet_balances::GenesisConfig::<Runtime> { balances: self.balances.into() }
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
pallet_collator_selection::GenesisConfig::<Runtime> {
|
||||
invulnerables: self.collators.clone().into(),
|
||||
candidacy_bond: Default::default(),
|
||||
desired_candidates: Default::default(),
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
pallet_session::GenesisConfig::<Runtime> { keys: self.keys }
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
let mut ext = sp_io::TestExternalities::new(t);
|
||||
|
||||
ext.execute_with(|| {
|
||||
frame_system::Pallet::<Runtime>::set_block_number(1u32.into());
|
||||
});
|
||||
|
||||
ext
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RuntimeHelper<Runtime>(PhantomData<Runtime>);
|
||||
/// 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<Runtime: frame_system::Config> RuntimeHelper<Runtime>
|
||||
where
|
||||
AccountIdOf<Runtime>:
|
||||
Into<<<Runtime as frame_system::Config>::Origin as OriginTrait>::AccountId>,
|
||||
{
|
||||
pub fn run_to_block(n: u32, author: Option<AccountId>) {
|
||||
while frame_system::Pallet::<Runtime>::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::<Runtime>::reset_events();
|
||||
frame_system::Pallet::<Runtime>::initialize(
|
||||
&(frame_system::Pallet::<Runtime>::block_number() + 1u32.into()),
|
||||
&frame_system::Pallet::<Runtime>::parent_hash(),
|
||||
&pre_digest,
|
||||
);
|
||||
},
|
||||
None => {
|
||||
frame_system::Pallet::<Runtime>::set_block_number(
|
||||
frame_system::Pallet::<Runtime>::block_number() + 1u32.into(),
|
||||
);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn root_origin() -> <Runtime as frame_system::Config>::Origin {
|
||||
<Runtime as frame_system::Config>::Origin::root()
|
||||
}
|
||||
|
||||
pub fn origin_of(
|
||||
account_id: AccountIdOf<Runtime>,
|
||||
) -> <Runtime as frame_system::Config>::Origin {
|
||||
<Runtime as frame_system::Config>::Origin::signed(account_id.into())
|
||||
}
|
||||
}
|
||||
@@ -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 = "master" }
|
||||
|
||||
@@ -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<AccountId> = Authorship::author();
|
||||
}
|
||||
|
||||
match_types! {
|
||||
@@ -167,8 +171,29 @@ impl xcm_executor::Config for XcmConfig {
|
||||
type LocationInverter = LocationInverter<Ancestry>;
|
||||
type Barrier = Barrier;
|
||||
type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
|
||||
type Trader =
|
||||
UsingComponents<WeightToFee, WestendLocation, AccountId, Balances, ToStakingPot<Runtime>>;
|
||||
type Trader = (
|
||||
UsingComponents<WeightToFee, WestendLocation, AccountId, Balances, ToStakingPot<Runtime>>,
|
||||
cumulus_primitives_utility::TakeFirstAssetTrader<
|
||||
AccountId,
|
||||
AssetFeeAsExistentialDepositMultiplier<
|
||||
Runtime,
|
||||
WeightToFee,
|
||||
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto>,
|
||||
>,
|
||||
ConvertedConcreteAssetId<
|
||||
AssetId,
|
||||
Balance,
|
||||
AsPrefixedGeneralIndex<AssetsPalletLocation, AssetId, JustTry>,
|
||||
JustTry,
|
||||
>,
|
||||
Assets,
|
||||
cumulus_primitives_utility::XcmFeesTo32ByteAccount<
|
||||
FungiblesTransactor,
|
||||
AccountId,
|
||||
XcmAssetFeesReceiver,
|
||||
>,
|
||||
>,
|
||||
);
|
||||
type ResponseHandler = PolkadotXcm;
|
||||
type AssetTrap = PolkadotXcm;
|
||||
type AssetClaims = PolkadotXcm;
|
||||
|
||||
@@ -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::<Runtime>::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::<Runtime>::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::<Runtime>::origin_of(AccountId::from(ALICE)),
|
||||
1,
|
||||
AccountId::from(ALICE).into(),
|
||||
ExistentialDeposit::get()
|
||||
));
|
||||
|
||||
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
||||
|
||||
// Set Alice as block author, who will receive fees
|
||||
RuntimeHelper::<Runtime>::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(
|
||||
<Runtime as frame_system::Config>::PalletInfo::index::<Assets>().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::<Runtime>::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::<Runtime>::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::<Runtime>::origin_of(AccountId::from(ALICE)),
|
||||
1,
|
||||
AccountId::from(ALICE).into(),
|
||||
ExistentialDeposit::get()
|
||||
));
|
||||
|
||||
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
||||
|
||||
// Set Alice as block author, who will receive fees
|
||||
RuntimeHelper::<Runtime>::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(
|
||||
<Runtime as frame_system::Config>::PalletInfo::index::<Assets>().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::<Runtime>::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::<Runtime>::root_origin(),
|
||||
1,
|
||||
AccountId::from(ALICE).into(),
|
||||
true,
|
||||
ExistentialDeposit::get()
|
||||
));
|
||||
|
||||
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
||||
|
||||
// Set Alice as block author, who will receive fees
|
||||
RuntimeHelper::<Runtime>::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(
|
||||
<Runtime as frame_system::Config>::PalletInfo::index::<Assets>().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::<Runtime>::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::<Runtime>::root_origin(),
|
||||
1,
|
||||
AccountId::from(ALICE).into(),
|
||||
true,
|
||||
ExistentialDeposit::get()
|
||||
));
|
||||
|
||||
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
||||
|
||||
// Set Alice as block author, who will receive fees
|
||||
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
|
||||
|
||||
let bought = 500_000_000u64;
|
||||
|
||||
let asset_multilocation = MultiLocation::new(
|
||||
0,
|
||||
X2(
|
||||
PalletInstance(
|
||||
<Runtime as frame_system::Config>::PalletInfo::index::<Assets>().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());
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user