mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 21:01:02 +00:00
2cb39f8dc9
Implements an XCM executor `WeightTrader`, facilitating fee payments in any asset that can be exchanged for a native asset. A few constraints need to be observed: - `buy_weight` and `refund` operations must be atomic, as another weight trader implementation might be attempted in case of failure. - swap credit must be utilized since there isn’t an account to which an asset of some class can be deposited with a guarantee to meet the existential deposit requirement. Also, operating with credits enhances the efficiency of the weight trader - https://github.com/paritytech/polkadot-sdk/pull/1677 related PRs: - (depends) https://github.com/paritytech/polkadot-sdk/pull/2031 - (depends) https://github.com/paritytech/polkadot-sdk/pull/1677 - (caused) https://github.com/paritytech/polkadot-sdk/pull/1847 - (caused) https://github.com/paritytech/polkadot-sdk/pull/1876 // DONE: impl `OnUnbalanced` for a `fungible/s` credit // DONE: make the trader free from a concept of a native currency and drop few fallible conversions. related issue - https://github.com/paritytech/polkadot-sdk/issues/1842 // DONE: tests --------- Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
845 lines
26 KiB
Rust
845 lines
26 KiB
Rust
// This file is part of Cumulus.
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//! Tests for the Rococo Assets Hub chain.
|
|
|
|
use asset_hub_rococo_runtime::{
|
|
xcm_config,
|
|
xcm_config::{bridging, ForeignCreatorsSovereignAccountOf, LocationToAccountId, TokenLocation},
|
|
AllPalletsWithoutSystem, MetadataDepositBase, MetadataDepositPerByte, RuntimeCall,
|
|
RuntimeEvent, ToWestendXcmRouterInstance, XcmpQueue,
|
|
};
|
|
pub use asset_hub_rococo_runtime::{
|
|
xcm_config::{CheckingAccount, TrustBackedAssetsPalletLocation, XcmConfig},
|
|
AssetConversion, AssetDeposit, Assets, Balances, CollatorSelection, ExistentialDeposit,
|
|
ForeignAssets, ForeignAssetsInstance, ParachainSystem, Runtime, SessionKeys, System,
|
|
TrustBackedAssetsInstance,
|
|
};
|
|
use asset_test_utils::{
|
|
test_cases_over_bridge::TestBridgingConfig, CollatorSessionKey, CollatorSessionKeys, ExtBuilder,
|
|
};
|
|
use codec::{Decode, Encode};
|
|
use frame_support::{
|
|
assert_ok,
|
|
traits::{
|
|
fungible::{Inspect, Mutate},
|
|
fungibles::{
|
|
Create, Inspect as FungiblesInspect, InspectEnumerable, Mutate as FungiblesMutate,
|
|
},
|
|
},
|
|
weights::{Weight, WeightToFee as WeightToFeeT},
|
|
};
|
|
use parachains_common::{
|
|
rococo::{currency::UNITS, fee::WeightToFee},
|
|
AccountId, AssetIdForTrustBackedAssets, AuraId, Balance,
|
|
};
|
|
use sp_runtime::traits::MaybeEquivalence;
|
|
use std::convert::Into;
|
|
use xcm::latest::prelude::*;
|
|
use xcm_executor::traits::{Identity, JustTry, WeightTrader};
|
|
|
|
const ALICE: [u8; 32] = [1u8; 32];
|
|
const SOME_ASSET_ADMIN: [u8; 32] = [5u8; 32];
|
|
|
|
type AssetIdForTrustBackedAssetsConvert =
|
|
assets_common::AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>;
|
|
|
|
type RuntimeHelper = asset_test_utils::RuntimeHelper<Runtime, AllPalletsWithoutSystem>;
|
|
|
|
fn collator_session_key(account: [u8; 32]) -> CollatorSessionKey<Runtime> {
|
|
CollatorSessionKey::new(
|
|
AccountId::from(account),
|
|
AccountId::from(account),
|
|
SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(account)) },
|
|
)
|
|
}
|
|
|
|
fn collator_session_keys() -> CollatorSessionKeys<Runtime> {
|
|
CollatorSessionKeys::default().add(collator_session_key(ALICE))
|
|
}
|
|
|
|
#[test]
|
|
fn test_buy_and_refund_weight_in_native() {
|
|
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(|| {
|
|
let bob: AccountId = SOME_ASSET_ADMIN.into();
|
|
let staking_pot = CollatorSelection::account_id();
|
|
let native_location = TokenLocation::get();
|
|
let initial_balance = 200 * UNITS;
|
|
|
|
assert_ok!(Balances::mint_into(&bob, initial_balance));
|
|
assert_ok!(Balances::mint_into(&staking_pot, initial_balance));
|
|
|
|
// keep initial total issuance to assert later.
|
|
let total_issuance = Balances::total_issuance();
|
|
|
|
// prepare input to buy weight.
|
|
let weight = Weight::from_parts(4_000_000_000, 0);
|
|
let fee = WeightToFee::weight_to_fee(&weight);
|
|
let extra_amount = 100;
|
|
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };
|
|
let payment: MultiAsset = (native_location, fee + extra_amount).into();
|
|
|
|
// init trader and buy weight.
|
|
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
|
let unused_asset =
|
|
trader.buy_weight(weight, payment.into(), &ctx).expect("Expected Ok");
|
|
|
|
// assert.
|
|
let unused_amount =
|
|
unused_asset.fungible.get(&native_location.into()).map_or(0, |a| *a);
|
|
assert_eq!(unused_amount, extra_amount);
|
|
assert_eq!(Balances::total_issuance(), total_issuance);
|
|
|
|
// prepare input to refund weight.
|
|
let refund_weight = Weight::from_parts(1_000_000_000, 0);
|
|
let refund = WeightToFee::weight_to_fee(&refund_weight);
|
|
|
|
// refund.
|
|
let actual_refund = trader.refund_weight(refund_weight, &ctx).unwrap();
|
|
assert_eq!(actual_refund, (native_location, refund).into());
|
|
|
|
// assert.
|
|
assert_eq!(Balances::balance(&staking_pot), initial_balance);
|
|
// only after `trader` is dropped we expect the fee to be resolved into the treasury
|
|
// account.
|
|
drop(trader);
|
|
assert_eq!(Balances::balance(&staking_pot), initial_balance + fee - refund);
|
|
assert_eq!(Balances::total_issuance(), total_issuance + fee - refund);
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn test_buy_and_refund_weight_with_swap_local_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(|| {
|
|
let bob: AccountId = SOME_ASSET_ADMIN.into();
|
|
let staking_pot = CollatorSelection::account_id();
|
|
let asset_1: u32 = 1;
|
|
let native_location = TokenLocation::get();
|
|
let asset_1_location =
|
|
AssetIdForTrustBackedAssetsConvert::convert_back(&asset_1).unwrap();
|
|
// bob's initial balance for native and `asset1` assets.
|
|
let initial_balance = 200 * UNITS;
|
|
// liquidity for both arms of (native, asset1) pool.
|
|
let pool_liquidity = 100 * UNITS;
|
|
|
|
// init asset, balances and pool.
|
|
assert_ok!(<Assets as Create<_>>::create(asset_1, bob.clone(), true, 10));
|
|
|
|
assert_ok!(Assets::mint_into(asset_1, &bob, initial_balance));
|
|
assert_ok!(Balances::mint_into(&bob, initial_balance));
|
|
assert_ok!(Balances::mint_into(&staking_pot, initial_balance));
|
|
|
|
assert_ok!(AssetConversion::create_pool(
|
|
RuntimeHelper::origin_of(bob.clone()),
|
|
Box::new(native_location),
|
|
Box::new(asset_1_location)
|
|
));
|
|
|
|
assert_ok!(AssetConversion::add_liquidity(
|
|
RuntimeHelper::origin_of(bob.clone()),
|
|
Box::new(native_location),
|
|
Box::new(asset_1_location),
|
|
pool_liquidity,
|
|
pool_liquidity,
|
|
1,
|
|
1,
|
|
bob,
|
|
));
|
|
|
|
// keep initial total issuance to assert later.
|
|
let asset_total_issuance = Assets::total_issuance(asset_1);
|
|
let native_total_issuance = Balances::total_issuance();
|
|
|
|
// prepare input to buy weight.
|
|
let weight = Weight::from_parts(4_000_000_000, 0);
|
|
let fee = WeightToFee::weight_to_fee(&weight);
|
|
let asset_fee =
|
|
AssetConversion::get_amount_in(&fee, &pool_liquidity, &pool_liquidity).unwrap();
|
|
let extra_amount = 100;
|
|
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };
|
|
let payment: MultiAsset = (asset_1_location, asset_fee + extra_amount).into();
|
|
|
|
// init trader and buy weight.
|
|
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
|
let unused_asset =
|
|
trader.buy_weight(weight, payment.into(), &ctx).expect("Expected Ok");
|
|
|
|
// assert.
|
|
let unused_amount =
|
|
unused_asset.fungible.get(&asset_1_location.into()).map_or(0, |a| *a);
|
|
assert_eq!(unused_amount, extra_amount);
|
|
assert_eq!(Assets::total_issuance(asset_1), asset_total_issuance + asset_fee);
|
|
|
|
// prepare input to refund weight.
|
|
let refund_weight = Weight::from_parts(1_000_000_000, 0);
|
|
let refund = WeightToFee::weight_to_fee(&refund_weight);
|
|
let (reserve1, reserve2) =
|
|
AssetConversion::get_reserves(native_location, asset_1_location).unwrap();
|
|
let asset_refund =
|
|
AssetConversion::get_amount_out(&refund, &reserve1, &reserve2).unwrap();
|
|
|
|
// refund.
|
|
let actual_refund = trader.refund_weight(refund_weight, &ctx).unwrap();
|
|
assert_eq!(actual_refund, (asset_1_location, asset_refund).into());
|
|
|
|
// assert.
|
|
assert_eq!(Balances::balance(&staking_pot), initial_balance);
|
|
// only after `trader` is dropped we expect the fee to be resolved into the treasury
|
|
// account.
|
|
drop(trader);
|
|
assert_eq!(Balances::balance(&staking_pot), initial_balance + fee - refund);
|
|
assert_eq!(
|
|
Assets::total_issuance(asset_1),
|
|
asset_total_issuance + asset_fee - asset_refund
|
|
);
|
|
assert_eq!(Balances::total_issuance(), native_total_issuance);
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn test_buy_and_refund_weight_with_swap_foreign_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(|| {
|
|
let bob: AccountId = SOME_ASSET_ADMIN.into();
|
|
let staking_pot = CollatorSelection::account_id();
|
|
let native_location = TokenLocation::get();
|
|
let foreign_location =
|
|
MultiLocation { parents: 1, interior: X2(Parachain(1234), GeneralIndex(12345)) };
|
|
// bob's initial balance for native and `asset1` assets.
|
|
let initial_balance = 200 * UNITS;
|
|
// liquidity for both arms of (native, asset1) pool.
|
|
let pool_liquidity = 100 * UNITS;
|
|
|
|
// init asset, balances and pool.
|
|
assert_ok!(<ForeignAssets as Create<_>>::create(
|
|
foreign_location,
|
|
bob.clone(),
|
|
true,
|
|
10
|
|
));
|
|
|
|
assert_ok!(ForeignAssets::mint_into(foreign_location, &bob, initial_balance));
|
|
assert_ok!(Balances::mint_into(&bob, initial_balance));
|
|
assert_ok!(Balances::mint_into(&staking_pot, initial_balance));
|
|
|
|
assert_ok!(AssetConversion::create_pool(
|
|
RuntimeHelper::origin_of(bob.clone()),
|
|
Box::new(native_location),
|
|
Box::new(foreign_location)
|
|
));
|
|
|
|
assert_ok!(AssetConversion::add_liquidity(
|
|
RuntimeHelper::origin_of(bob.clone()),
|
|
Box::new(native_location),
|
|
Box::new(foreign_location),
|
|
pool_liquidity,
|
|
pool_liquidity,
|
|
1,
|
|
1,
|
|
bob,
|
|
));
|
|
|
|
// keep initial total issuance to assert later.
|
|
let asset_total_issuance = ForeignAssets::total_issuance(foreign_location);
|
|
let native_total_issuance = Balances::total_issuance();
|
|
|
|
// prepare input to buy weight.
|
|
let weight = Weight::from_parts(4_000_000_000, 0);
|
|
let fee = WeightToFee::weight_to_fee(&weight);
|
|
let asset_fee =
|
|
AssetConversion::get_amount_in(&fee, &pool_liquidity, &pool_liquidity).unwrap();
|
|
let extra_amount = 100;
|
|
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };
|
|
let payment: MultiAsset = (foreign_location, asset_fee + extra_amount).into();
|
|
|
|
// init trader and buy weight.
|
|
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
|
|
let unused_asset =
|
|
trader.buy_weight(weight, payment.into(), &ctx).expect("Expected Ok");
|
|
|
|
// assert.
|
|
let unused_amount =
|
|
unused_asset.fungible.get(&foreign_location.into()).map_or(0, |a| *a);
|
|
assert_eq!(unused_amount, extra_amount);
|
|
assert_eq!(
|
|
ForeignAssets::total_issuance(foreign_location),
|
|
asset_total_issuance + asset_fee
|
|
);
|
|
|
|
// prepare input to refund weight.
|
|
let refund_weight = Weight::from_parts(1_000_000_000, 0);
|
|
let refund = WeightToFee::weight_to_fee(&refund_weight);
|
|
let (reserve1, reserve2) =
|
|
AssetConversion::get_reserves(native_location, foreign_location).unwrap();
|
|
let asset_refund =
|
|
AssetConversion::get_amount_out(&refund, &reserve1, &reserve2).unwrap();
|
|
|
|
// refund.
|
|
let actual_refund = trader.refund_weight(refund_weight, &ctx).unwrap();
|
|
assert_eq!(actual_refund, (foreign_location, asset_refund).into());
|
|
|
|
// assert.
|
|
assert_eq!(Balances::balance(&staking_pot), initial_balance);
|
|
// only after `trader` is dropped we expect the fee to be resolved into the treasury
|
|
// account.
|
|
drop(trader);
|
|
assert_eq!(Balances::balance(&staking_pot), initial_balance + fee - refund);
|
|
assert_eq!(
|
|
ForeignAssets::total_issuance(foreign_location),
|
|
asset_total_issuance + asset_fee - asset_refund
|
|
);
|
|
assert_eq!(Balances::total_issuance(), native_total_issuance);
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn test_assets_balances_api_works() {
|
|
use assets_common::runtime_api::runtime_decl_for_fungibles_api::FungiblesApi;
|
|
|
|
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(|| {
|
|
let local_asset_id = 1;
|
|
let foreign_asset_id_multilocation =
|
|
MultiLocation { parents: 1, interior: X2(Parachain(1234), GeneralIndex(12345)) };
|
|
|
|
// check before
|
|
assert_eq!(Assets::balance(local_asset_id, AccountId::from(ALICE)), 0);
|
|
assert_eq!(
|
|
ForeignAssets::balance(foreign_asset_id_multilocation, AccountId::from(ALICE)),
|
|
0
|
|
);
|
|
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), 0);
|
|
assert!(Runtime::query_account_balances(AccountId::from(ALICE))
|
|
.unwrap()
|
|
.try_as::<MultiAssets>()
|
|
.unwrap()
|
|
.is_none());
|
|
|
|
// Drip some balance
|
|
use frame_support::traits::fungible::Mutate;
|
|
let some_currency = ExistentialDeposit::get();
|
|
Balances::mint_into(&AccountId::from(ALICE), some_currency).unwrap();
|
|
|
|
// We need root origin to create a sufficient asset
|
|
let minimum_asset_balance = 3333333_u128;
|
|
assert_ok!(Assets::force_create(
|
|
RuntimeHelper::root_origin(),
|
|
local_asset_id.into(),
|
|
AccountId::from(ALICE).into(),
|
|
true,
|
|
minimum_asset_balance
|
|
));
|
|
|
|
// We first mint enough asset for the account to exist for assets
|
|
assert_ok!(Assets::mint(
|
|
RuntimeHelper::origin_of(AccountId::from(ALICE)),
|
|
local_asset_id.into(),
|
|
AccountId::from(ALICE).into(),
|
|
minimum_asset_balance
|
|
));
|
|
|
|
// create foreign asset
|
|
let foreign_asset_minimum_asset_balance = 3333333_u128;
|
|
assert_ok!(ForeignAssets::force_create(
|
|
RuntimeHelper::root_origin(),
|
|
foreign_asset_id_multilocation,
|
|
AccountId::from(SOME_ASSET_ADMIN).into(),
|
|
false,
|
|
foreign_asset_minimum_asset_balance
|
|
));
|
|
|
|
// We first mint enough asset for the account to exist for assets
|
|
assert_ok!(ForeignAssets::mint(
|
|
RuntimeHelper::origin_of(AccountId::from(SOME_ASSET_ADMIN)),
|
|
foreign_asset_id_multilocation,
|
|
AccountId::from(ALICE).into(),
|
|
6 * foreign_asset_minimum_asset_balance
|
|
));
|
|
|
|
// check after
|
|
assert_eq!(
|
|
Assets::balance(local_asset_id, AccountId::from(ALICE)),
|
|
minimum_asset_balance
|
|
);
|
|
assert_eq!(
|
|
ForeignAssets::balance(foreign_asset_id_multilocation, AccountId::from(ALICE)),
|
|
6 * minimum_asset_balance
|
|
);
|
|
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), some_currency);
|
|
|
|
let result: MultiAssets = Runtime::query_account_balances(AccountId::from(ALICE))
|
|
.unwrap()
|
|
.try_into()
|
|
.unwrap();
|
|
assert_eq!(result.len(), 3);
|
|
|
|
// check currency
|
|
assert!(result.inner().iter().any(|asset| asset.eq(
|
|
&assets_common::fungible_conversion::convert_balance::<TokenLocation, Balance>(
|
|
some_currency
|
|
)
|
|
.unwrap()
|
|
)));
|
|
// check trusted asset
|
|
assert!(result.inner().iter().any(|asset| asset.eq(&(
|
|
AssetIdForTrustBackedAssetsConvert::convert_back(&local_asset_id).unwrap(),
|
|
minimum_asset_balance
|
|
)
|
|
.into())));
|
|
// check foreign asset
|
|
assert!(result.inner().iter().any(|asset| asset.eq(&(
|
|
Identity::convert_back(&foreign_asset_id_multilocation).unwrap(),
|
|
6 * foreign_asset_minimum_asset_balance
|
|
)
|
|
.into())));
|
|
});
|
|
}
|
|
|
|
asset_test_utils::include_teleports_for_native_asset_works!(
|
|
Runtime,
|
|
AllPalletsWithoutSystem,
|
|
XcmConfig,
|
|
CheckingAccount,
|
|
WeightToFee,
|
|
ParachainSystem,
|
|
collator_session_keys(),
|
|
ExistentialDeposit::get(),
|
|
Box::new(|runtime_event_encoded: Vec<u8>| {
|
|
match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) {
|
|
Ok(RuntimeEvent::PolkadotXcm(event)) => Some(event),
|
|
_ => None,
|
|
}
|
|
}),
|
|
1000
|
|
);
|
|
|
|
asset_test_utils::include_teleports_for_foreign_assets_works!(
|
|
Runtime,
|
|
AllPalletsWithoutSystem,
|
|
XcmConfig,
|
|
CheckingAccount,
|
|
WeightToFee,
|
|
ParachainSystem,
|
|
ForeignCreatorsSovereignAccountOf,
|
|
ForeignAssetsInstance,
|
|
collator_session_keys(),
|
|
ExistentialDeposit::get(),
|
|
Box::new(|runtime_event_encoded: Vec<u8>| {
|
|
match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) {
|
|
Ok(RuntimeEvent::PolkadotXcm(event)) => Some(event),
|
|
_ => None,
|
|
}
|
|
}),
|
|
Box::new(|runtime_event_encoded: Vec<u8>| {
|
|
match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) {
|
|
Ok(RuntimeEvent::XcmpQueue(event)) => Some(event),
|
|
_ => None,
|
|
}
|
|
})
|
|
);
|
|
|
|
asset_test_utils::include_asset_transactor_transfer_with_local_consensus_currency_works!(
|
|
Runtime,
|
|
XcmConfig,
|
|
collator_session_keys(),
|
|
ExistentialDeposit::get(),
|
|
Box::new(|| {
|
|
assert!(Assets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
assert!(ForeignAssets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
}),
|
|
Box::new(|| {
|
|
assert!(Assets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
assert!(ForeignAssets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
})
|
|
);
|
|
|
|
asset_test_utils::include_asset_transactor_transfer_with_pallet_assets_instance_works!(
|
|
asset_transactor_transfer_with_trust_backed_assets_works,
|
|
Runtime,
|
|
XcmConfig,
|
|
TrustBackedAssetsInstance,
|
|
AssetIdForTrustBackedAssets,
|
|
AssetIdForTrustBackedAssetsConvert,
|
|
collator_session_keys(),
|
|
ExistentialDeposit::get(),
|
|
12345,
|
|
Box::new(|| {
|
|
assert!(ForeignAssets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
}),
|
|
Box::new(|| {
|
|
assert!(ForeignAssets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
})
|
|
);
|
|
|
|
asset_test_utils::include_asset_transactor_transfer_with_pallet_assets_instance_works!(
|
|
asset_transactor_transfer_with_foreign_assets_works,
|
|
Runtime,
|
|
XcmConfig,
|
|
ForeignAssetsInstance,
|
|
MultiLocation,
|
|
JustTry,
|
|
collator_session_keys(),
|
|
ExistentialDeposit::get(),
|
|
MultiLocation { parents: 1, interior: X2(Parachain(1313), GeneralIndex(12345)) },
|
|
Box::new(|| {
|
|
assert!(Assets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
}),
|
|
Box::new(|| {
|
|
assert!(Assets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
})
|
|
);
|
|
|
|
asset_test_utils::include_create_and_manage_foreign_assets_for_local_consensus_parachain_assets_works!(
|
|
Runtime,
|
|
XcmConfig,
|
|
WeightToFee,
|
|
ForeignCreatorsSovereignAccountOf,
|
|
ForeignAssetsInstance,
|
|
MultiLocation,
|
|
JustTry,
|
|
collator_session_keys(),
|
|
ExistentialDeposit::get(),
|
|
AssetDeposit::get(),
|
|
MetadataDepositBase::get(),
|
|
MetadataDepositPerByte::get(),
|
|
Box::new(|pallet_asset_call| RuntimeCall::ForeignAssets(pallet_asset_call).encode()),
|
|
Box::new(|runtime_event_encoded: Vec<u8>| {
|
|
match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) {
|
|
Ok(RuntimeEvent::ForeignAssets(pallet_asset_event)) => Some(pallet_asset_event),
|
|
_ => None,
|
|
}
|
|
}),
|
|
Box::new(|| {
|
|
assert!(Assets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
assert!(ForeignAssets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
}),
|
|
Box::new(|| {
|
|
assert!(Assets::asset_ids().collect::<Vec<_>>().is_empty());
|
|
assert_eq!(ForeignAssets::asset_ids().collect::<Vec<_>>().len(), 1);
|
|
})
|
|
);
|
|
|
|
fn limited_reserve_transfer_assets_for_native_asset_over_bridge_works(
|
|
bridging_configuration: fn() -> TestBridgingConfig,
|
|
) {
|
|
asset_test_utils::test_cases_over_bridge::limited_reserve_transfer_assets_for_native_asset_works::<
|
|
Runtime,
|
|
AllPalletsWithoutSystem,
|
|
XcmConfig,
|
|
ParachainSystem,
|
|
XcmpQueue,
|
|
LocationToAccountId,
|
|
>(
|
|
collator_session_keys(),
|
|
ExistentialDeposit::get(),
|
|
AccountId::from(ALICE),
|
|
Box::new(|runtime_event_encoded: Vec<u8>| {
|
|
match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) {
|
|
Ok(RuntimeEvent::PolkadotXcm(event)) => Some(event),
|
|
_ => None,
|
|
}
|
|
}),
|
|
Box::new(|runtime_event_encoded: Vec<u8>| {
|
|
match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) {
|
|
Ok(RuntimeEvent::XcmpQueue(event)) => Some(event),
|
|
_ => None,
|
|
}
|
|
}),
|
|
bridging_configuration,
|
|
WeightLimit::Unlimited,
|
|
Some(xcm_config::bridging::XcmBridgeHubRouterFeeAssetId::get()),
|
|
Some(xcm_config::TreasuryAccount::get()),
|
|
)
|
|
}
|
|
|
|
mod asset_hub_rococo_tests {
|
|
use super::*;
|
|
use asset_hub_rococo_runtime::{PolkadotXcm, RuntimeOrigin};
|
|
|
|
fn bridging_to_asset_hub_westend() -> TestBridgingConfig {
|
|
let _ = PolkadotXcm::force_xcm_version(
|
|
RuntimeOrigin::root(),
|
|
Box::new(bridging::to_westend::AssetHubWestend::get()),
|
|
XCM_VERSION,
|
|
)
|
|
.expect("version saved!");
|
|
TestBridgingConfig {
|
|
bridged_network: bridging::to_westend::WestendNetwork::get(),
|
|
local_bridge_hub_para_id: bridging::SiblingBridgeHubParaId::get(),
|
|
local_bridge_hub_location: bridging::SiblingBridgeHub::get(),
|
|
bridged_target_location: bridging::to_westend::AssetHubWestend::get(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn limited_reserve_transfer_assets_for_native_asset_to_asset_hub_westend_works() {
|
|
limited_reserve_transfer_assets_for_native_asset_over_bridge_works(
|
|
bridging_to_asset_hub_westend,
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn receive_reserve_asset_deposited_wnd_from_asset_hub_westend_works() {
|
|
const BLOCK_AUTHOR_ACCOUNT: [u8; 32] = [13; 32];
|
|
asset_test_utils::test_cases_over_bridge::receive_reserve_asset_deposited_from_different_consensus_works::<
|
|
Runtime,
|
|
AllPalletsWithoutSystem,
|
|
XcmConfig,
|
|
LocationToAccountId,
|
|
ForeignAssetsInstance,
|
|
>(
|
|
collator_session_keys().add(collator_session_key(BLOCK_AUTHOR_ACCOUNT)),
|
|
ExistentialDeposit::get(),
|
|
AccountId::from([73; 32]),
|
|
AccountId::from(BLOCK_AUTHOR_ACCOUNT),
|
|
// receiving WNDs
|
|
(MultiLocation { parents: 2, interior: X1(GlobalConsensus(Westend)) }, 1000000000000, 1_000_000_000),
|
|
bridging_to_asset_hub_westend,
|
|
(
|
|
X1(PalletInstance(bp_bridge_hub_rococo::WITH_BRIDGE_ROCOCO_TO_WESTEND_MESSAGES_PALLET_INDEX)),
|
|
GlobalConsensus(Westend),
|
|
X1(Parachain(1000))
|
|
)
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn report_bridge_status_from_xcm_bridge_router_for_westend_works() {
|
|
asset_test_utils::test_cases_over_bridge::report_bridge_status_from_xcm_bridge_router_works::<
|
|
Runtime,
|
|
AllPalletsWithoutSystem,
|
|
XcmConfig,
|
|
LocationToAccountId,
|
|
ToWestendXcmRouterInstance,
|
|
>(
|
|
collator_session_keys(),
|
|
bridging_to_asset_hub_westend,
|
|
|| {
|
|
sp_std::vec![
|
|
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
|
|
Transact {
|
|
origin_kind: OriginKind::Xcm,
|
|
require_weight_at_most:
|
|
bp_asset_hub_rococo::XcmBridgeHubRouterTransactCallMaxWeight::get(),
|
|
call: bp_asset_hub_rococo::Call::ToWestendXcmRouter(
|
|
bp_asset_hub_rococo::XcmBridgeHubRouterCall::report_bridge_status {
|
|
bridge_id: Default::default(),
|
|
is_congested: true,
|
|
}
|
|
)
|
|
.encode()
|
|
.into(),
|
|
}
|
|
]
|
|
.into()
|
|
},
|
|
|| {
|
|
sp_std::vec![
|
|
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
|
|
Transact {
|
|
origin_kind: OriginKind::Xcm,
|
|
require_weight_at_most:
|
|
bp_asset_hub_rococo::XcmBridgeHubRouterTransactCallMaxWeight::get(),
|
|
call: bp_asset_hub_rococo::Call::ToWestendXcmRouter(
|
|
bp_asset_hub_rococo::XcmBridgeHubRouterCall::report_bridge_status {
|
|
bridge_id: Default::default(),
|
|
is_congested: false,
|
|
}
|
|
)
|
|
.encode()
|
|
.into(),
|
|
}
|
|
]
|
|
.into()
|
|
},
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn test_report_bridge_status_call_compatibility() {
|
|
// if this test fails, make sure `bp_asset_hub_rococo` has valid encoding
|
|
assert_eq!(
|
|
RuntimeCall::ToWestendXcmRouter(
|
|
pallet_xcm_bridge_hub_router::Call::report_bridge_status {
|
|
bridge_id: Default::default(),
|
|
is_congested: true,
|
|
}
|
|
)
|
|
.encode(),
|
|
bp_asset_hub_rococo::Call::ToWestendXcmRouter(
|
|
bp_asset_hub_rococo::XcmBridgeHubRouterCall::report_bridge_status {
|
|
bridge_id: Default::default(),
|
|
is_congested: true,
|
|
}
|
|
)
|
|
.encode()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn check_sane_weight_report_bridge_status_for_westend() {
|
|
use pallet_xcm_bridge_hub_router::WeightInfo;
|
|
let actual = <Runtime as pallet_xcm_bridge_hub_router::Config<
|
|
ToWestendXcmRouterInstance,
|
|
>>::WeightInfo::report_bridge_status();
|
|
let max_weight = bp_asset_hub_rococo::XcmBridgeHubRouterTransactCallMaxWeight::get();
|
|
assert!(
|
|
actual.all_lte(max_weight),
|
|
"max_weight: {:?} should be adjusted to actual {:?}",
|
|
max_weight,
|
|
actual
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn reserve_transfer_native_asset_to_non_teleport_para_works() {
|
|
asset_test_utils::test_cases::reserve_transfer_native_asset_to_non_teleport_para_works::<
|
|
Runtime,
|
|
AllPalletsWithoutSystem,
|
|
XcmConfig,
|
|
ParachainSystem,
|
|
XcmpQueue,
|
|
LocationToAccountId,
|
|
>(
|
|
collator_session_keys(),
|
|
ExistentialDeposit::get(),
|
|
AccountId::from(ALICE),
|
|
Box::new(|runtime_event_encoded: Vec<u8>| {
|
|
match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) {
|
|
Ok(RuntimeEvent::PolkadotXcm(event)) => Some(event),
|
|
_ => None,
|
|
}
|
|
}),
|
|
Box::new(|runtime_event_encoded: Vec<u8>| {
|
|
match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) {
|
|
Ok(RuntimeEvent::XcmpQueue(event)) => Some(event),
|
|
_ => None,
|
|
}
|
|
}),
|
|
WeightLimit::Unlimited,
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn change_xcm_bridge_hub_router_byte_fee_by_governance_works() {
|
|
asset_test_utils::test_cases::change_storage_constant_by_governance_works::<
|
|
Runtime,
|
|
bridging::XcmBridgeHubRouterByteFee,
|
|
Balance,
|
|
>(
|
|
collator_session_keys(),
|
|
1000,
|
|
Box::new(|call| RuntimeCall::System(call).encode()),
|
|
|| {
|
|
(
|
|
bridging::XcmBridgeHubRouterByteFee::key().to_vec(),
|
|
bridging::XcmBridgeHubRouterByteFee::get(),
|
|
)
|
|
},
|
|
|old_value| {
|
|
if let Some(new_value) = old_value.checked_add(1) {
|
|
new_value
|
|
} else {
|
|
old_value.checked_sub(1).unwrap()
|
|
}
|
|
},
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn change_xcm_bridge_hub_router_base_fee_by_governance_works() {
|
|
asset_test_utils::test_cases::change_storage_constant_by_governance_works::<
|
|
Runtime,
|
|
bridging::XcmBridgeHubRouterBaseFee,
|
|
Balance,
|
|
>(
|
|
collator_session_keys(),
|
|
1000,
|
|
Box::new(|call| RuntimeCall::System(call).encode()),
|
|
|| {
|
|
(
|
|
bridging::XcmBridgeHubRouterBaseFee::key().to_vec(),
|
|
bridging::XcmBridgeHubRouterBaseFee::get(),
|
|
)
|
|
},
|
|
|old_value| {
|
|
if let Some(new_value) = old_value.checked_add(1) {
|
|
new_value
|
|
} else {
|
|
old_value.checked_sub(1).unwrap()
|
|
}
|
|
},
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn change_xcm_bridge_hub_ethereum_base_fee_by_governance_works() {
|
|
asset_test_utils::test_cases::change_storage_constant_by_governance_works::<
|
|
Runtime,
|
|
bridging::to_ethereum::BridgeHubEthereumBaseFee,
|
|
Balance,
|
|
>(
|
|
collator_session_keys(),
|
|
1000,
|
|
Box::new(|call| RuntimeCall::System(call).encode()),
|
|
|| {
|
|
(
|
|
bridging::to_ethereum::BridgeHubEthereumBaseFee::key().to_vec(),
|
|
bridging::to_ethereum::BridgeHubEthereumBaseFee::get(),
|
|
)
|
|
},
|
|
|old_value| {
|
|
if let Some(new_value) = old_value.checked_add(1) {
|
|
new_value
|
|
} else {
|
|
old_value.checked_sub(1).unwrap()
|
|
}
|
|
},
|
|
)
|
|
}
|