Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Fixups for new Convert APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
gavofyork committed Jun 5, 2023
1 parent 7b6c3db commit f869904
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 67 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion parachains/integration-tests/emulated/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use xcm_emulator::{
decl_test_networks, decl_test_parachains, decl_test_relay_chains, Parachain, RelayChain,
TestExt,
};
use xcm_executor::traits::Convert;
use xcm_executor::traits::ConvertLocation;

decl_test_relay_chains! {
#[api_version(5)]
Expand Down
36 changes: 28 additions & 8 deletions parachains/runtimes/assets/asset-hub-kusama/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
// 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 Statemine (Kusama Assets Hub) chain.
use asset_hub_kusama_runtime::xcm_config::{
AssetFeeAsExistentialDepositMultiplierFeeCharger, KsmLocation, TrustBackedAssetsPalletLocation,
};
Expand All @@ -17,8 +36,9 @@ use frame_support::{
weights::{Weight, WeightToFee as WeightToFeeT},
};
use parachains_common::{AccountId, AssetIdForTrustBackedAssets, AuraId, Balance};
use sp_runtime::traits::MaybeEquivalence;
use xcm::latest::prelude::*;
use xcm_executor::traits::{Convert, Identity, JustTry, WeightTrader};
use xcm_executor::traits::{Identity, JustTry, WeightTrader};

const ALICE: [u8; 32] = [1u8; 32];
const SOME_ASSET_ADMIN: [u8; 32] = [5u8; 32];
Expand Down Expand Up @@ -66,7 +86,7 @@ fn test_asset_xcm_trader() {

// get asset id as multilocation
let asset_multilocation =
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap();
AssetIdForTrustBackedAssetsConvert::convert_back(&local_asset_id).unwrap();

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand Down Expand Up @@ -150,7 +170,7 @@ fn test_asset_xcm_trader_with_refund() {
// We are going to buy 4e9 weight
let bought = Weight::from_parts(4_000_000_000u64, 0);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

// lets calculate amount needed
let amount_bought = WeightToFee::weight_to_fee(&bought);
Expand Down Expand Up @@ -220,7 +240,7 @@ fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() {
// We are going to buy small amount
let bought = Weight::from_parts(500_000_000u64, 0);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

let amount_bought = WeightToFee::weight_to_fee(&bought);

Expand Down Expand Up @@ -271,7 +291,7 @@ fn test_that_buying_ed_refund_does_not_refund() {
// We are gonna buy ED
let bought = Weight::from_parts(ExistentialDeposit::get().try_into().unwrap(), 0);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

let amount_bought = WeightToFee::weight_to_fee(&bought);

Expand Down Expand Up @@ -346,7 +366,7 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
// lets calculate amount needed
let asset_amount_needed = WeightToFee::weight_to_fee(&bought);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

let asset: MultiAsset = (asset_multilocation, asset_amount_needed).into();

Expand Down Expand Up @@ -461,13 +481,13 @@ fn test_assets_balances_api_works() {
)));
// check trusted asset
assert!(result.inner().iter().any(|asset| asset.eq(&(
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap(),
AssetIdForTrustBackedAssetsConvert::convert_back(&local_asset_id).unwrap(),
minimum_asset_balance
)
.into())));
// check foreign asset
assert!(result.inner().iter().any(|asset| asset.eq(&(
Identity::reverse_ref(foreign_asset_id_multilocation).unwrap(),
Identity::convert_back(&foreign_asset_id_multilocation).unwrap(),
6 * foreign_asset_minimum_asset_balance
)
.into())));
Expand Down
36 changes: 28 additions & 8 deletions parachains/runtimes/assets/asset-hub-polkadot/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
// 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 Statemint (Polkadot Assets Hub) chain.
use asset_hub_polkadot_runtime::xcm_config::{
AssetFeeAsExistentialDepositMultiplierFeeCharger, CheckingAccount, DotLocation,
ForeignCreatorsSovereignAccountOf, TrustBackedAssetsPalletLocation, XcmConfig,
Expand All @@ -18,8 +37,9 @@ use frame_support::{
use parachains_common::{
AccountId, AssetHubPolkadotAuraId as AuraId, AssetIdForTrustBackedAssets, Balance,
};
use sp_runtime::traits::MaybeEquivalence;
use xcm::latest::prelude::*;
use xcm_executor::traits::{Convert, Identity, JustTry, WeightTrader};
use xcm_executor::traits::{Identity, JustTry, WeightTrader};

const ALICE: [u8; 32] = [1u8; 32];
const SOME_ASSET_ADMIN: [u8; 32] = [5u8; 32];
Expand Down Expand Up @@ -67,7 +87,7 @@ fn test_asset_xcm_trader() {

// get asset id as multilocation
let asset_multilocation =
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap();
AssetIdForTrustBackedAssetsConvert::convert_back(&local_asset_id).unwrap();

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand Down Expand Up @@ -157,7 +177,7 @@ fn test_asset_xcm_trader_with_refund() {
// bit more of weight
let bought = Weight::from_parts(400_000_000_000u64, 0);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

// lets calculate amount needed
let amount_bought = WeightToFee::weight_to_fee(&bought);
Expand Down Expand Up @@ -230,7 +250,7 @@ fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() {
// bit more of weight
let bought = Weight::from_parts(50_000_000_000u64, 0);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

let amount_bought = WeightToFee::weight_to_fee(&bought);

Expand Down Expand Up @@ -281,7 +301,7 @@ fn test_that_buying_ed_refund_does_not_refund() {
// We are gonna buy ED
let bought = Weight::from_parts(ExistentialDeposit::get().try_into().unwrap(), 0);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

let amount_bought = WeightToFee::weight_to_fee(&bought);

Expand Down Expand Up @@ -359,7 +379,7 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
// lets calculate amount needed
let asset_amount_needed = WeightToFee::weight_to_fee(&bought);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

let asset: MultiAsset = (asset_multilocation, asset_amount_needed).into();

Expand Down Expand Up @@ -474,13 +494,13 @@ fn test_assets_balances_api_works() {
)));
// check trusted asset
assert!(result.inner().iter().any(|asset| asset.eq(&(
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap(),
AssetIdForTrustBackedAssetsConvert::convert_back(&local_asset_id).unwrap(),
minimum_asset_balance
)
.into())));
// check foreign asset
assert!(result.inner().iter().any(|asset| asset.eq(&(
Identity::reverse_ref(foreign_asset_id_multilocation).unwrap(),
Identity::convert_back(&foreign_asset_id_multilocation).unwrap(),
6 * foreign_asset_minimum_asset_balance
)
.into())));
Expand Down
36 changes: 28 additions & 8 deletions parachains/runtimes/assets/asset-hub-westend/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
// 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 Westmint (Westend Assets Hub) chain.
pub use asset_hub_westend_runtime::{
constants::fee::WeightToFee,
xcm_config::{CheckingAccount, TrustBackedAssetsPalletLocation, XcmConfig},
Expand All @@ -20,10 +39,11 @@ use frame_support::{
weights::{Weight, WeightToFee as WeightToFeeT},
};
use parachains_common::{AccountId, AssetIdForTrustBackedAssets, AuraId, Balance};
use sp_runtime::traits::MaybeEquivalence;
use std::convert::Into;
use xcm::{latest::prelude::*, VersionedXcm, MAX_XCM_DECODE_DEPTH};
use xcm_executor::{
traits::{Convert, Identity, JustTry, WeightTrader},
traits::{Identity, JustTry, WeightTrader},
XcmExecutor,
};

Expand Down Expand Up @@ -73,7 +93,7 @@ fn test_asset_xcm_trader() {

// get asset id as multilocation
let asset_multilocation =
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap();
AssetIdForTrustBackedAssetsConvert::convert_back(&local_asset_id).unwrap();

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand Down Expand Up @@ -156,7 +176,7 @@ fn test_asset_xcm_trader_with_refund() {

// We are going to buy 4e9 weight
let bought = Weight::from_parts(4_000_000_000u64, 0);
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

// lets calculate amount needed
let amount_bought = WeightToFee::weight_to_fee(&bought);
Expand Down Expand Up @@ -226,7 +246,7 @@ fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() {
// We are going to buy 5e9 weight
let bought = Weight::from_parts(500_000_000u64, 0);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

let amount_bought = WeightToFee::weight_to_fee(&bought);

Expand Down Expand Up @@ -276,7 +296,7 @@ fn test_that_buying_ed_refund_does_not_refund() {

let bought = Weight::from_parts(500_000_000u64, 0);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

let amount_bought = WeightToFee::weight_to_fee(&bought);

Expand Down Expand Up @@ -351,7 +371,7 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
// lets calculate amount needed
let asset_amount_needed = WeightToFee::weight_to_fee(&bought);

let asset_multilocation = AssetIdForTrustBackedAssetsConvert::reverse_ref(1).unwrap();
let asset_multilocation = AssetIdForTrustBackedAssetsConvert::convert_back(&1).unwrap();

let asset: MultiAsset = (asset_multilocation, asset_amount_needed).into();

Expand Down Expand Up @@ -466,13 +486,13 @@ fn test_assets_balances_api_works() {
)));
// check trusted asset
assert!(result.inner().iter().any(|asset| asset.eq(&(
AssetIdForTrustBackedAssetsConvert::reverse_ref(local_asset_id).unwrap(),
AssetIdForTrustBackedAssetsConvert::convert_back(&local_asset_id).unwrap(),
minimum_asset_balance
)
.into())));
// check foreign asset
assert!(result.inner().iter().any(|asset| asset.eq(&(
Identity::reverse_ref(foreign_asset_id_multilocation).unwrap(),
Identity::convert_back(&foreign_asset_id_multilocation).unwrap(),
6 * foreign_asset_minimum_asset_balance
)
.into())));
Expand Down
2 changes: 2 additions & 0 deletions parachains/runtimes/assets/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ log = { version = "0.4.18", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-api = { 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-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }

# Polkadot
pallet-xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
Expand All @@ -38,6 +39,7 @@ std = [
"cumulus-primitives-core/std",
"sp-api/std",
"sp-std/std",
"sp-runtime/std",
"pallet-xcm/std",
"xcm/std",
"xcm-builder/std",
Expand Down
10 changes: 5 additions & 5 deletions parachains/runtimes/assets/common/src/foreign_creators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ use frame_support::traits::{
};
use pallet_xcm::{EnsureXcm, Origin as XcmOrigin};
use xcm::latest::MultiLocation;
use xcm_executor::traits::Convert;
use xcm_executor::traits::ConvertLocation;

// `EnsureOriginWithArg` impl for `CreateOrigin` that allows only XCM origins that are locations
// containing the class location.
/// `EnsureOriginWithArg` impl for `CreateOrigin` that allows only XCM origins that are locations
/// containing the class location.
pub struct ForeignCreators<IsForeign, AccountOf, AccountId>(
sp_std::marker::PhantomData<(IsForeign, AccountOf, AccountId)>,
);
impl<
IsForeign: ContainsPair<MultiLocation, MultiLocation>,
AccountOf: Convert<MultiLocation, AccountId>,
AccountOf: ConvertLocation<AccountId>,
AccountId: Clone,
RuntimeOrigin: From<XcmOrigin> + OriginTrait + Clone,
> EnsureOriginWithArg<RuntimeOrigin, MultiLocation>
Expand All @@ -46,7 +46,7 @@ where
if !IsForeign::contains(asset_location, &origin_location) {
return Err(origin)
}
AccountOf::convert(origin_location).map_err(|_| origin)
AccountOf::convert_location(&origin_location).ok_or(origin)
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down
Loading

0 comments on commit f869904

Please sign in to comment.