mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 07:31:02 +00:00
020a024ab5
* 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>
159 lines
5.3 KiB
Rust
159 lines
5.3 KiB
Rust
// 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, MatchedConvertedConcreteId};
|
|
use xcm_executor::traits::{Convert, MatchesFungibles};
|
|
|
|
/// Converting any [`(AssetId, Balance)`] to [`MultiAsset`]
|
|
pub trait MultiAssetConverter<AssetId, Balance, ConvertAssetId, ConvertBalance>:
|
|
MatchesFungibles<AssetId, Balance>
|
|
where
|
|
AssetId: Clone,
|
|
Balance: Clone,
|
|
ConvertAssetId: Convert<MultiLocation, AssetId>,
|
|
ConvertBalance: Convert<u128, Balance>,
|
|
{
|
|
fn convert_ref(
|
|
value: impl Borrow<(AssetId, Balance)>,
|
|
) -> Result<MultiAsset, FungiblesAccessError>;
|
|
}
|
|
|
|
impl<
|
|
AssetId: Clone,
|
|
Balance: Clone,
|
|
ConvertAssetId: Convert<MultiLocation, AssetId>,
|
|
ConvertBalance: Convert<u128, Balance>,
|
|
> MultiAssetConverter<AssetId, Balance, ConvertAssetId, ConvertBalance>
|
|
for ConvertedConcreteId<AssetId, Balance, 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),
|
|
}
|
|
}
|
|
}
|
|
|
|
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)>,
|
|
) -> Result<Vec<MultiAsset>, FungiblesAccessError>
|
|
where
|
|
AssetId: Clone + 'a,
|
|
Balance: Clone + 'a,
|
|
ConvertAssetId: Convert<MultiLocation, AssetId>,
|
|
ConvertBalance: Convert<u128, Balance>,
|
|
Converter: MultiAssetConverter<AssetId, Balance, ConvertAssetId, ConvertBalance>,
|
|
{
|
|
items.map(Converter::convert_ref).collect()
|
|
}
|
|
|
|
/// Helper function to convert `Balance` with MultiLocation` to `MultiAsset`
|
|
pub fn convert_balance<
|
|
T: frame_support::pallet_prelude::Get<MultiLocation>,
|
|
Balance: TryInto<u128>,
|
|
>(
|
|
balance: Balance,
|
|
) -> Result<MultiAsset, FungiblesAccessError> {
|
|
match balance.try_into() {
|
|
Ok(balance) => Ok((T::get(), balance).into()),
|
|
Err(_) => Err(FungiblesAccessError::AmountToBalanceConversionFailed),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use frame_support::traits::Everything;
|
|
|
|
use xcm::latest::prelude::*;
|
|
use xcm_executor::traits::{Identity, JustTry};
|
|
|
|
type Converter = MatchedConvertedConcreteId<MultiLocation, u64, Everything, Identity, JustTry>;
|
|
|
|
#[test]
|
|
fn converted_concrete_id_fungible_multi_asset_conversion_roundtrip_works() {
|
|
let location = MultiLocation::new(0, X1(GlobalConsensus(ByGenesis([0; 32]))));
|
|
let amount = 123456_u64;
|
|
let expected_multi_asset = MultiAsset {
|
|
id: Concrete(MultiLocation::new(0, X1(GlobalConsensus(ByGenesis([0; 32]))))),
|
|
fun: Fungible(123456_u128),
|
|
};
|
|
|
|
assert_eq!(
|
|
Converter::matches_fungibles(&expected_multi_asset).map_err(|_| ()),
|
|
Ok((location, amount))
|
|
);
|
|
|
|
assert_eq!(Converter::convert_ref((location, amount)), Ok(expected_multi_asset));
|
|
}
|
|
|
|
#[test]
|
|
fn converted_concrete_id_fungible_multi_asset_conversion_collection_works() {
|
|
let data = vec![
|
|
(MultiLocation::new(0, X1(GlobalConsensus(ByGenesis([0; 32])))), 123456_u64),
|
|
(MultiLocation::new(1, X1(GlobalConsensus(ByGenesis([1; 32])))), 654321_u64),
|
|
];
|
|
|
|
let expected_data = vec![
|
|
MultiAsset {
|
|
id: Concrete(MultiLocation::new(0, X1(GlobalConsensus(ByGenesis([0; 32]))))),
|
|
fun: Fungible(123456_u128),
|
|
},
|
|
MultiAsset {
|
|
id: Concrete(MultiLocation::new(1, X1(GlobalConsensus(ByGenesis([1; 32]))))),
|
|
fun: Fungible(654321_u128),
|
|
},
|
|
];
|
|
|
|
assert_eq!(convert::<_, _, _, _, Converter>(data.iter()), Ok(expected_data));
|
|
}
|
|
}
|