Skip to content

Commit 020a024

Browse files
joepetrowskimuharembkonturgavofyork
authored
Add Support for Foreign Assets (#2133)
* add foreign assets to westmint * add foreign assets to statemine * use updated api for ensure origin trait * Assets/ForeignAssets tests and fixes (#2167) * Test for create and transfer `TrustBackedAssets` with AssetTransactor * Test for transfer `local Currency` with AssetTransactor * Test for create foreign assets (covers foreign relaychain currency) * Added `ForeignFungiblesTransactor` and test for transfer `ForeignAssets` with AssetTransactor * Removed unused `pub const Local: MultiLocation` * Changed `ParaId -> Sibling` for `SiblingParachainConvertsVia` * Test for create foreign assets (covers local sibling parachain assets) * Reverted stuff for ForeignCreators from different global consensus (moved to transfer asset branch) * Refactor `weight_limit` for `execute_xcm` * Added test for `set_metadata` by ForeignCreator with `xcm::Transact(set_metadata)` * Renamed `receive_teleported_asset_works` -> `receive_teleported_asset_for_native_asset_works` * Allow `ForeignCreators` only for sibling parachains * Unify ReservedDmpWeight/ReservedXcmpWeight usage * Removed hack - replaced with `MatchedConvertedConcreteId` * Refactor `ForeignCreators` to assets-common * Add `ReceiveTeleportedAsset` test * Change test - `Utility::batch` -> Multiple `xcm::Transact` * Reusing the same deposits as for TrustBackedAssets * missing `try_successful_origin` ? * Finished `ForeignAssets` for westmint (converter, FungiblesApi, tests) * Refactoring tests - receive_teleported_asset_for_native_asset_works * ForeignAssets for statemine + refactored `receive_teleported_asset_from_foreign_creator_works` * Add `ForeignAssets` to statemine `FungiblesApi` * Add `asset_transactor_transfer_with_local_consensus_currency_works` to all runtimes * Added `asset_transactor_transfer_with_trust_backed_assets_works` test * Added `asset_transactor_transfer_with_foreign_assets_works` * Fix `missing `try_successful_origin` in implementation` * Added `create_and_manage_foreign_assets_for_local_consensus_parachain_assets_works` * Added `ExpectTransactStatus` check * Small rename * Extended `test_assets_balances_api_works` with ForeignAssets for `statemine` * PR fixes * Update parachains/runtimes/assets/test-utils/src/test_cases.rs --------- Co-authored-by: parity-processbot <> Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Added `StartsWithExplicitGlobalConsensus` to ignores (#2338) * Update parachains/runtimes/assets/common/src/lib.rs Co-authored-by: Gavin Wood <gavin@parity.io> * include mint and burn in SafeCallFilter * include mint and burn in SafeCallFilter (statemine) * clarify doc * Fix compilation (moved trait `InspectMetadata`) * Fix test * Extended test for `teleport` from/to relaychain + `CheckingAccount` (Part I) * Extended test for `teleport` from/to foreign parachain + `CheckingAccount` (Part II) * Fixed TODO - `NonLocal` for `ForeignAssets` * Changed `NonLocal` to `NoChecking` * Fix weight in test --------- Co-authored-by: parity-processbot <> Co-authored-by: muharem <ismailov.m.h@gmail.com> Co-authored-by: Branislav Kontur <bkontur@gmail.com> Co-authored-by: Gavin Wood <gavin@parity.io>
1 parent 9f2b54e commit 020a024

File tree

25 files changed

+2872
-244
lines changed

25 files changed

+2872
-244
lines changed

Cargo.lock

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parachains/common/src/impls.rs

+12
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ where
9595
}
9696
}
9797

98+
/// Allow checking in assets that exists.
99+
pub struct AssetExists<AccountId, Assets>(PhantomData<(AccountId, Assets)>);
100+
impl<AccountId, Assets> Contains<<Assets as fungibles::Inspect<AccountId>>::AssetId>
101+
for AssetExists<AccountId, Assets>
102+
where
103+
Assets: fungibles::Inspect<AccountId>,
104+
{
105+
fn contains(id: &<Assets as fungibles::Inspect<AccountId>>::AssetId) -> bool {
106+
Assets::asset_exists(*id)
107+
}
108+
}
109+
98110
/// Asset filter that allows all assets from a certain location.
99111
pub struct AssetsFrom<T>(PhantomData<T>);
100112
impl<T: Get<MultiLocation>> ContainsPair<MultiAsset, MultiLocation> for AssetsFrom<T> {

parachains/runtimes/assets/common/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ description = "Assets common utilities"
77

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

1112
# Substrate
1213
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
1314
sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
1415
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
1516

1617
# Polkadot
18+
pallet-xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
1719
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
1820
xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
1921
xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
2022

2123
# Cumulus
2224
parachains-common = { path = "../../../common", default-features = false }
25+
cumulus-primitives-core = { path = "../../../../primitives/core", default-features = false }
2326

2427
[build-dependencies]
2528
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }
@@ -28,11 +31,20 @@ substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", bran
2831
default = [ "std" ]
2932
std = [
3033
"codec/std",
34+
"log/std",
3135
"frame-support/std",
3236
"parachains-common/std",
37+
"cumulus-primitives-core/std",
3338
"sp-api/std",
3439
"sp-std/std",
40+
"pallet-xcm/std",
3541
"xcm/std",
3642
"xcm-builder/std",
3743
"xcm-executor/std",
3844
]
45+
46+
runtime-benchmarks = [
47+
"frame-support/runtime-benchmarks",
48+
"pallet-xcm/runtime-benchmarks",
49+
"xcm-builder/runtime-benchmarks",
50+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (C) 2023 Parity Technologies (UK) Ltd.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
use frame_support::traits::{
17+
ContainsPair, EnsureOrigin, EnsureOriginWithArg, Everything, OriginTrait,
18+
};
19+
use pallet_xcm::{EnsureXcm, Origin as XcmOrigin};
20+
use xcm::latest::MultiLocation;
21+
use xcm_executor::traits::Convert;
22+
23+
// `EnsureOriginWithArg` impl for `CreateOrigin` that allows only XCM origins that are locations
24+
// containing the class location.
25+
pub struct ForeignCreators<IsForeign, AccountOf, AccountId>(
26+
sp_std::marker::PhantomData<(IsForeign, AccountOf, AccountId)>,
27+
);
28+
impl<
29+
IsForeign: ContainsPair<MultiLocation, MultiLocation>,
30+
AccountOf: Convert<MultiLocation, AccountId>,
31+
AccountId: Clone,
32+
RuntimeOrigin: From<XcmOrigin> + OriginTrait + Clone,
33+
> EnsureOriginWithArg<RuntimeOrigin, MultiLocation>
34+
for ForeignCreators<IsForeign, AccountOf, AccountId>
35+
where
36+
RuntimeOrigin::PalletsOrigin:
37+
From<XcmOrigin> + TryInto<XcmOrigin, Error = RuntimeOrigin::PalletsOrigin>,
38+
{
39+
type Success = AccountId;
40+
41+
fn try_origin(
42+
origin: RuntimeOrigin,
43+
asset_location: &MultiLocation,
44+
) -> sp_std::result::Result<Self::Success, RuntimeOrigin> {
45+
let origin_location = EnsureXcm::<Everything>::try_origin(origin.clone())?;
46+
if !IsForeign::contains(&asset_location, &origin_location) {
47+
return Err(origin)
48+
}
49+
AccountOf::convert(origin_location).map_err(|_| origin)
50+
}
51+
52+
#[cfg(feature = "runtime-benchmarks")]
53+
fn try_successful_origin(a: &MultiLocation) -> Result<RuntimeOrigin, ()> {
54+
Ok(pallet_xcm::Origin::Xcm(a.clone()).into())
55+
}
56+
}

parachains/runtimes/assets/common/src/fungible_conversion.rs

+41-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
// This file is part of Substrate.
2-
3-
// Copyright (C) 2018-2022 Parity Technologies (UK) Ltd.
4-
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5-
6-
// This program is free software: you can redistribute it and/or modify
7-
// it under the terms of the GNU General Public License as published by
8-
// the Free Software Foundation, either version 3 of the License, or
9-
// (at your option) any later version.
10-
11-
// This program is distributed in the hope that it will be useful,
12-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
// GNU General Public License for more details.
15-
16-
// You should have received a copy of the GNU General Public License
17-
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1+
// Copyright (C) 2023 Parity Technologies (UK) Ltd.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
1815

1916
//! Runtime API definition for assets.
2017
2118
use crate::runtime_api::FungiblesAccessError;
19+
use frame_support::traits::Contains;
2220
use sp_std::{borrow::Borrow, vec::Vec};
2321
use xcm::latest::{MultiAsset, MultiLocation};
24-
use xcm_builder::ConvertedConcreteId;
22+
use xcm_builder::{ConvertedConcreteId, MatchedConvertedConcreteId};
2523
use xcm_executor::traits::{Convert, MatchesFungibles};
2624

2725
/// Converting any [`(AssetId, Balance)`] to [`MultiAsset`]
@@ -60,6 +58,29 @@ impl<
6058
}
6159
}
6260

61+
impl<
62+
AssetId: Clone,
63+
Balance: Clone,
64+
MatchAssetId: Contains<MultiLocation>,
65+
ConvertAssetId: Convert<MultiLocation, AssetId>,
66+
ConvertBalance: Convert<u128, Balance>,
67+
> MultiAssetConverter<AssetId, Balance, ConvertAssetId, ConvertBalance>
68+
for MatchedConvertedConcreteId<AssetId, Balance, MatchAssetId, ConvertAssetId, ConvertBalance>
69+
{
70+
fn convert_ref(
71+
value: impl Borrow<(AssetId, Balance)>,
72+
) -> Result<MultiAsset, FungiblesAccessError> {
73+
let (asset_id, balance) = value.borrow();
74+
match ConvertAssetId::reverse_ref(asset_id) {
75+
Ok(asset_id_as_multilocation) => match ConvertBalance::reverse_ref(balance) {
76+
Ok(amount) => Ok((asset_id_as_multilocation, amount).into()),
77+
Err(_) => Err(FungiblesAccessError::AmountToBalanceConversionFailed),
78+
},
79+
Err(_) => Err(FungiblesAccessError::AssetIdConversionFailed),
80+
}
81+
}
82+
}
83+
6384
/// Helper function to convert collections with [`(AssetId, Balance)`] to [`MultiAsset`]
6485
pub fn convert<'a, AssetId, Balance, ConvertAssetId, ConvertBalance, Converter>(
6586
items: impl Iterator<Item = &'a (AssetId, Balance)>,
@@ -90,11 +111,12 @@ pub fn convert_balance<
90111
#[cfg(test)]
91112
mod tests {
92113
use super::*;
114+
use frame_support::traits::Everything;
93115

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

97-
type Converter = ConvertedConcreteId<MultiLocation, u64, Identity, JustTry>;
119+
type Converter = MatchedConvertedConcreteId<MultiLocation, u64, Everything, Identity, JustTry>;
98120

99121
#[test]
100122
fn converted_concrete_id_fungible_multi_asset_conversion_roundtrip_works() {

0 commit comments

Comments
 (0)