Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 6 additions & 28 deletions cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use frame_support::{
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32, ConstU64, ConstU8,
ConstantStoragePrice, EitherOfDiverse, Equals, InstanceFilter, TransformOrigin,
},
weights::{ConstantMultiplier, Weight, WeightToFee as _},
weights::{ConstantMultiplier, Weight},
BoundedVec, PalletId,
};
use frame_system::{
Expand Down Expand Up @@ -1555,33 +1555,11 @@ impl_runtime_apis! {
}

fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
let native_asset = xcm_config::TokenLocation::get();
let fee_in_native = WeightToFee::weight_to_fee(&weight);
let latest_asset_id: Result<AssetId, ()> = asset.clone().try_into();
match latest_asset_id {
Ok(asset_id) if asset_id.0 == native_asset => {
// for native token
Ok(fee_in_native)
},
Ok(asset_id) => {
// Try to get current price of `asset_id` in `native_asset`.
if let Ok(Some(swapped_in_native)) = assets_common::PoolAdapter::<Runtime>::quote_price_tokens_for_exact_tokens(
asset_id.0.clone(),
native_asset,
fee_in_native,
true, // We include the fee.
) {
Ok(swapped_in_native)
} else {
log::trace!(target: "xcm::xcm_runtime_apis", "query_weight_to_asset_fee - unhandled asset_id: {asset_id:?}!");
Err(XcmPaymentApiError::AssetNotFound)
}
},
Err(_) => {
log::trace!(target: "xcm::xcm_runtime_apis", "query_weight_to_asset_fee - failed to convert asset: {asset:?}!");
Err(XcmPaymentApiError::VersionedConversionFailed)
}
}
use crate::xcm_config::XcmConfig;

type Trader = <XcmConfig as xcm_executor::Config>::Trader;

PolkadotXcm::query_weight_to_asset_fee::<Trader>(weight, asset)
}

fn query_xcm_weight(message: VersionedXcm<()>) -> Result<Weight, XcmPaymentApiError> {
Expand Down
62 changes: 13 additions & 49 deletions cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use asset_test_utils::{
ExtBuilder, GovernanceOrigin, SlotDurations,
};
use codec::{Decode, Encode};
use core::ops::Mul;
use frame_support::{
assert_noop, assert_ok, parameter_types,
traits::{
Expand Down Expand Up @@ -91,52 +90,6 @@ fn slot_durations() -> SlotDurations {
}
}

fn setup_pool_for_paying_fees_with_foreign_assets(
(foreign_asset_owner, foreign_asset_id_location, foreign_asset_id_minimum_balance): (
AccountId,
Location,
Balance,
),
) {
let existential_deposit = ExistentialDeposit::get();

// setup a pool to pay fees with `foreign_asset_id_location` tokens
let pool_owner: AccountId = [14u8; 32].into();
let native_asset = Location::parent();
let pool_liquidity: Balance =
existential_deposit.max(foreign_asset_id_minimum_balance).mul(100_000);

let _ = Balances::force_set_balance(
RuntimeOrigin::root(),
pool_owner.clone().into(),
(existential_deposit + pool_liquidity).mul(2).into(),
);

assert_ok!(ForeignAssets::mint(
RuntimeOrigin::signed(foreign_asset_owner),
foreign_asset_id_location.clone().into(),
pool_owner.clone().into(),
(foreign_asset_id_minimum_balance + pool_liquidity).mul(2).into(),
));

assert_ok!(AssetConversion::create_pool(
RuntimeOrigin::signed(pool_owner.clone()),
Box::new(native_asset.clone().into()),
Box::new(foreign_asset_id_location.clone().into())
));

assert_ok!(AssetConversion::add_liquidity(
RuntimeOrigin::signed(pool_owner.clone()),
Box::new(native_asset.into()),
Box::new(foreign_asset_id_location.into()),
pool_liquidity,
pool_liquidity,
1,
1,
pool_owner,
));
}

#[test]
fn test_buy_and_refund_weight_in_native() {
ExtBuilder::<Runtime>::default()
Expand Down Expand Up @@ -845,7 +798,7 @@ mod asset_hub_rococo_tests {
1000000000000,
|| {
// setup pool for paying fees to touch `SwapFirstAssetTrader`
setup_pool_for_paying_fees_with_foreign_assets(foreign_asset_create_params);
asset_test_utils::test_cases::setup_pool_for_paying_fees_with_foreign_assets::<Runtime, RuntimeOrigin>(ExistentialDeposit::get(), foreign_asset_create_params);
// staking pot account for collecting local native fees from `BuyExecution`
let _ = Balances::force_set_balance(RuntimeOrigin::root(), StakingPot::get().into(), ExistentialDeposit::get());
// prepare bridge configuration
Expand Down Expand Up @@ -920,7 +873,7 @@ mod asset_hub_rococo_tests {
foreign_asset_create_params.clone(),
1000000000000,
|| {
setup_pool_for_paying_fees_with_foreign_assets(foreign_asset_create_params);
asset_test_utils::test_cases::setup_pool_for_paying_fees_with_foreign_assets::<Runtime, RuntimeOrigin>(ExistentialDeposit::get(), foreign_asset_create_params);
bridging_to_asset_hub_westend()
},
(
Expand Down Expand Up @@ -1365,11 +1318,22 @@ fn xcm_payment_api_works() {
RuntimeCall,
RuntimeOrigin,
Block,
WeightToFee,
>();
asset_test_utils::test_cases::xcm_payment_api_with_pools_works::<
Runtime,
RuntimeCall,
RuntimeOrigin,
Block,
WeightToFee,
>();

asset_test_utils::test_cases::xcm_payment_api_foreign_asset_pool_works::<
Runtime,
RuntimeCall,
RuntimeOrigin,
LocationToAccountId,
Block,
WeightToFee,
>(ExistentialDeposit::get(), WESTEND_GENESIS_HASH);
}
34 changes: 6 additions & 28 deletions cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use frame_support::{
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32, ConstU64, ConstU8,
ConstantStoragePrice, Equals, InstanceFilter, Nothing, TransformOrigin,
},
weights::{ConstantMultiplier, Weight, WeightToFee as _},
weights::{ConstantMultiplier, Weight},
BoundedVec, PalletId,
};
use frame_system::{
Expand Down Expand Up @@ -1692,33 +1692,11 @@ impl_runtime_apis! {
}

fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
let native_asset = xcm_config::WestendLocation::get();
let fee_in_native = WeightToFee::weight_to_fee(&weight);
let latest_asset_id: Result<AssetId, ()> = asset.clone().try_into();
match latest_asset_id {
Ok(asset_id) if asset_id.0 == native_asset => {
// for native asset
Ok(fee_in_native)
},
Ok(asset_id) => {
// Try to get current price of `asset_id` in `native_asset`.
if let Ok(Some(swapped_in_native)) = assets_common::PoolAdapter::<Runtime>::quote_price_tokens_for_exact_tokens(
asset_id.0.clone(),
native_asset,
fee_in_native,
true, // We include the fee.
) {
Ok(swapped_in_native)
} else {
log::trace!(target: "xcm::xcm_runtime_apis", "query_weight_to_asset_fee - unhandled asset_id: {asset_id:?}!");
Err(XcmPaymentApiError::AssetNotFound)
}
},
Err(_) => {
log::trace!(target: "xcm::xcm_runtime_apis", "query_weight_to_asset_fee - failed to convert asset: {asset:?}!");
Err(XcmPaymentApiError::VersionedConversionFailed)
}
}
use crate::xcm_config::XcmConfig;

type Trader = <XcmConfig as xcm_executor::Config>::Trader;

PolkadotXcm::query_weight_to_asset_fee::<Trader>(weight, asset)
}

fn query_xcm_weight(message: VersionedXcm<()>) -> Result<Weight, XcmPaymentApiError> {
Expand Down
63 changes: 14 additions & 49 deletions cumulus/parachains/runtimes/assets/asset-hub-westend/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use parachains_common::{AccountId, AssetIdForTrustBackedAssets, AuraId, Balance}
use sp_consensus_aura::SlotDuration;
use sp_core::crypto::Ss58Codec;
use sp_runtime::{traits::MaybeEquivalence, Either};
use std::{convert::Into, ops::Mul};
use std::convert::Into;
use testnet_parachains_constants::westend::{consensus::*, currency::UNITS, fee::WeightToFee};
use xcm::latest::{
prelude::{Assets as XcmAssets, *},
Expand Down Expand Up @@ -91,52 +91,6 @@ fn slot_durations() -> SlotDurations {
}
}

fn setup_pool_for_paying_fees_with_foreign_assets(
(foreign_asset_owner, foreign_asset_id_location, foreign_asset_id_minimum_balance): (
AccountId,
xcm::v5::Location,
Balance,
),
) {
let existential_deposit = ExistentialDeposit::get();

// setup a pool to pay fees with `foreign_asset_id_location` tokens
let pool_owner: AccountId = [14u8; 32].into();
let native_asset = xcm::v5::Location::parent();
let pool_liquidity: Balance =
existential_deposit.max(foreign_asset_id_minimum_balance).mul(100_000);

let _ = Balances::force_set_balance(
RuntimeOrigin::root(),
pool_owner.clone().into(),
(existential_deposit + pool_liquidity).mul(2).into(),
);

assert_ok!(ForeignAssets::mint(
RuntimeOrigin::signed(foreign_asset_owner),
foreign_asset_id_location.clone().into(),
pool_owner.clone().into(),
(foreign_asset_id_minimum_balance + pool_liquidity).mul(2).into(),
));

assert_ok!(AssetConversion::create_pool(
RuntimeOrigin::signed(pool_owner.clone()),
Box::new(native_asset.clone().into()),
Box::new(foreign_asset_id_location.clone().into())
));

assert_ok!(AssetConversion::add_liquidity(
RuntimeOrigin::signed(pool_owner.clone()),
Box::new(native_asset.into()),
Box::new(foreign_asset_id_location.into()),
pool_liquidity,
pool_liquidity,
1,
1,
pool_owner,
));
}

#[test]
fn test_buy_and_refund_weight_in_native() {
ExtBuilder::<Runtime>::default()
Expand Down Expand Up @@ -944,7 +898,7 @@ fn receive_reserve_asset_deposited_roc_from_asset_hub_rococo_fees_paid_by_pool_s
1000000000000,
|| {
// setup pool for paying fees to touch `SwapFirstAssetTrader`
setup_pool_for_paying_fees_with_foreign_assets(foreign_asset_create_params);
asset_test_utils::test_cases::setup_pool_for_paying_fees_with_foreign_assets::<Runtime, RuntimeOrigin>(ExistentialDeposit::get(), foreign_asset_create_params);
// staking pot account for collecting local native fees from `BuyExecution`
let _ = Balances::force_set_balance(RuntimeOrigin::root(), StakingPot::get().into(), ExistentialDeposit::get());
// prepare bridge configuration
Expand Down Expand Up @@ -1014,7 +968,7 @@ fn receive_reserve_asset_deposited_roc_from_asset_hub_rococo_fees_paid_by_suffic
foreign_asset_create_params.clone(),
1000000000000,
|| {
setup_pool_for_paying_fees_with_foreign_assets(foreign_asset_create_params);
asset_test_utils::test_cases::setup_pool_for_paying_fees_with_foreign_assets::<Runtime, RuntimeOrigin>(ExistentialDeposit::get(), foreign_asset_create_params);
bridging_to_asset_hub_rococo()
},
(
Expand Down Expand Up @@ -1423,13 +1377,24 @@ fn xcm_payment_api_works() {
RuntimeCall,
RuntimeOrigin,
Block,
WeightToFee,
>();
asset_test_utils::test_cases::xcm_payment_api_with_pools_works::<
Runtime,
RuntimeCall,
RuntimeOrigin,
Block,
WeightToFee,
>();

asset_test_utils::test_cases::xcm_payment_api_foreign_asset_pool_works::<
Runtime,
RuntimeCall,
RuntimeOrigin,
LocationToAccountId,
Block,
WeightToFee,
>(ExistentialDeposit::get(), ROCOCO_GENESIS_HASH);
}

#[test]
Expand Down
Loading
Loading