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

Assets/ForeignAssets tests and fixes #2167

Merged
merged 41 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
fe2d626
Test for create and transfer `TrustBackedAssets` with AssetTransactor
bkontur Feb 3, 2023
8e3f62c
Test for transfer `local Currency` with AssetTransactor
bkontur Feb 6, 2023
766abea
Test for create foreign assets (covers foreign relaychain currency)
bkontur Feb 7, 2023
12d61db
Added `ForeignFungiblesTransactor` and test for transfer `ForeignAsse…
bkontur Feb 8, 2023
778f80f
Merge remote-tracking branch 'origin/joe-foreign-assets' into bko-for…
Feb 13, 2023
7772da9
Removed unused `pub const Local: MultiLocation`
bkontur Feb 13, 2023
40ccf5f
Changed `ParaId -> Sibling` for `SiblingParachainConvertsVia`
bkontur Feb 13, 2023
990e255
Test for create foreign assets (covers local sibling parachain assets)
bkontur Feb 13, 2023
a83d680
Merge remote-tracking branch 'origin/joe-foreign-assets' into bko-for…
Feb 23, 2023
2c11d66
Reverted stuff for ForeignCreators from different global consensus (m…
bkontur Feb 24, 2023
96379c3
Refactor `weight_limit` for `execute_xcm`
bkontur Feb 27, 2023
5bd9983
Added test for `set_metadata` by ForeignCreator with `xcm::Transact(s…
bkontur Feb 27, 2023
1fb6f90
Merge remote-tracking branch 'origin/joe-foreign-assets' into bko-for…
bkontur Feb 28, 2023
457f87f
Renamed `receive_teleported_asset_works` -> `receive_teleported_asset…
bkontur Feb 28, 2023
7e1da40
Allow `ForeignCreators` only for sibling parachains
bkontur Feb 28, 2023
449ba70
Merge remote-tracking branch 'origin/joe-foreign-assets' into bko-for…
bkontur Mar 3, 2023
80756d1
Merge remote-tracking branch 'origin/joe-foreign-assets' into bko-for…
Mar 3, 2023
03915e2
Unify ReservedDmpWeight/ReservedXcmpWeight usage
bkontur Mar 3, 2023
cabf0f9
Removed hack - replaced with `MatchedConvertedConcreteId`
bkontur Mar 3, 2023
92fee37
Refactor `ForeignCreators` to assets-common
bkontur Mar 3, 2023
e7cd1f5
Add `ReceiveTeleportedAsset` test
bkontur Mar 1, 2023
2aa0c8a
Change test - `Utility::batch` -> Multiple `xcm::Transact`
bkontur Mar 6, 2023
3310cd1
Reusing the same deposits as for TrustBackedAssets
bkontur Mar 6, 2023
24d0274
missing `try_successful_origin` ?
bkontur Mar 6, 2023
afbfeb0
Finished `ForeignAssets` for westmint (converter, FungiblesApi, tests)
bkontur Mar 8, 2023
ed97007
Merge remote-tracking branch 'origin/joe-foreign-assets' into bko-for…
Mar 8, 2023
47ba115
Refactoring tests - receive_teleported_asset_for_native_asset_works
bkontur Mar 9, 2023
0748e97
ForeignAssets for statemine + refactored `receive_teleported_asset_fr…
bkontur Mar 9, 2023
30acc18
Add `ForeignAssets` to statemine `FungiblesApi`
bkontur Mar 9, 2023
4fb2f85
Add `asset_transactor_transfer_with_local_consensus_currency_works` t…
bkontur Mar 9, 2023
2695886
Added `asset_transactor_transfer_with_trust_backed_assets_works` test
bkontur Mar 10, 2023
c181e0c
Added `asset_transactor_transfer_with_foreign_assets_works`
bkontur Mar 10, 2023
16ad4a4
Merge remote-tracking branch 'origin/joe-foreign-assets' into bko-for…
Mar 10, 2023
913504c
Fix `missing `try_successful_origin` in implementation`
bkontur Mar 12, 2023
4245b2f
Added `create_and_manage_foreign_assets_for_local_consensus_parachain…
bkontur Mar 13, 2023
79f9b30
Added `ExpectTransactStatus` check
bkontur Mar 13, 2023
5c97727
Small rename
bkontur Mar 13, 2023
c615788
Extended `test_assets_balances_api_works` with ForeignAssets for `sta…
bkontur Mar 13, 2023
0460e76
Merge remote-tracking branch 'origin/joe-foreign-assets' into bko-for…
Mar 15, 2023
ece1905
PR fixes
bkontur Mar 16, 2023
4b354a4
Update parachains/runtimes/assets/test-utils/src/test_cases.rs
joepetrowski Mar 16, 2023
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
9 changes: 9 additions & 0 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions parachains/runtimes/assets/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ description = "Assets common utilities"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
log = { version = "0.4.17", default-features = false }

# Substrate
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "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" }

# Polkadot
pallet-xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }

# Cumulus
parachains-common = { path = "../../../common", default-features = false }
cumulus-primitives-core = { path = "../../../../primitives/core", default-features = false }

[build-dependencies]
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand All @@ -28,11 +31,20 @@ substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", bran
default = [ "std" ]
std = [
"codec/std",
"log/std",
"frame-support/std",
"parachains-common/std",
"cumulus-primitives-core/std",
"sp-api/std",
"sp-std/std",
"pallet-xcm/std",
"xcm/std",
"xcm-builder/std",
"xcm-executor/std",
]

runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]
56 changes: 56 additions & 0 deletions parachains/runtimes/assets/common/src/foreign_creators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (C) 2023 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.

use frame_support::traits::{
ContainsPair, EnsureOrigin, EnsureOriginWithArg, Everything, OriginTrait,
};
use pallet_xcm::{EnsureXcm, Origin as XcmOrigin};
use xcm::latest::MultiLocation;
use xcm_executor::traits::Convert;

// `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>,
AccountId: Clone,
RuntimeOrigin: From<XcmOrigin> + OriginTrait + Clone,
> EnsureOriginWithArg<RuntimeOrigin, MultiLocation>
for ForeignCreators<IsForeign, AccountOf, AccountId>
where
RuntimeOrigin::PalletsOrigin:
From<XcmOrigin> + TryInto<XcmOrigin, Error = RuntimeOrigin::PalletsOrigin>,
{
type Success = AccountId;

fn try_origin(
origin: RuntimeOrigin,
asset_location: &MultiLocation,
) -> sp_std::result::Result<Self::Success, RuntimeOrigin> {
let origin_location = EnsureXcm::<Everything>::try_origin(origin.clone())?;
if !IsForeign::contains(&asset_location, &origin_location) {
return Err(origin)
}
AccountOf::convert(origin_location).map_err(|_| origin)
}

#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &MultiLocation) -> Result<RuntimeOrigin, ()> {
Ok(pallet_xcm::Origin::Xcm(a.clone()).into())
}
}
60 changes: 41 additions & 19 deletions parachains/runtimes/assets/common/src/fungible_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
// This file is part of Substrate.

// Copyright (C) 2018-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Copyright (C) 2023 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.

//! Runtime API definition for assets.

use crate::runtime_api::FungiblesAccessError;
use frame_support::traits::Contains;
use sp_std::{borrow::Borrow, vec::Vec};
use xcm::latest::{MultiAsset, MultiLocation};
use xcm_builder::ConvertedConcreteId;
use xcm_builder::{ConvertedConcreteId, MatchedConvertedConcreteId};
use xcm_executor::traits::{Convert, MatchesFungibles};

/// Converting any [`(AssetId, Balance)`] to [`MultiAsset`]
Expand Down Expand Up @@ -60,6 +58,29 @@ impl<
}
}

impl<
AssetId: Clone,
Balance: Clone,
MatchAssetId: Contains<MultiLocation>,
ConvertAssetId: Convert<MultiLocation, AssetId>,
ConvertBalance: Convert<u128, Balance>,
> MultiAssetConverter<AssetId, Balance, ConvertAssetId, ConvertBalance>
for MatchedConvertedConcreteId<AssetId, Balance, MatchAssetId, ConvertAssetId, ConvertBalance>
{
fn convert_ref(
value: impl Borrow<(AssetId, Balance)>,
) -> Result<MultiAsset, FungiblesAccessError> {
let (asset_id, balance) = value.borrow();
match ConvertAssetId::reverse_ref(asset_id) {
Ok(asset_id_as_multilocation) => match ConvertBalance::reverse_ref(balance) {
Ok(amount) => Ok((asset_id_as_multilocation, amount).into()),
Err(_) => Err(FungiblesAccessError::AmountToBalanceConversionFailed),
},
Err(_) => Err(FungiblesAccessError::AssetIdConversionFailed),
}
}
}

/// Helper function to convert collections with [`(AssetId, Balance)`] to [`MultiAsset`]
pub fn convert<'a, AssetId, Balance, ConvertAssetId, ConvertBalance, Converter>(
items: impl Iterator<Item = &'a (AssetId, Balance)>,
Expand Down Expand Up @@ -90,11 +111,12 @@ pub fn convert_balance<
#[cfg(test)]
mod tests {
use super::*;
use frame_support::traits::Everything;

use xcm::latest::prelude::*;
use xcm_executor::traits::{Identity, JustTry};

type Converter = ConvertedConcreteId<MultiLocation, u64, Identity, JustTry>;
type Converter = MatchedConvertedConcreteId<MultiLocation, u64, Everything, Identity, JustTry>;

#[test]
fn converted_concrete_id_fungible_multi_asset_conversion_roundtrip_works() {
Expand Down
Loading