From b5945ee528b1ee1647cb98a23b92ec2e48553783 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Fri, 17 Feb 2023 10:02:13 +0100 Subject: [PATCH 1/6] Change ParaId->Sibling for `SiblingParachainConvertsVia` --- xcm/xcm-simulator/example/src/parachain.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xcm/xcm-simulator/example/src/parachain.rs b/xcm/xcm-simulator/example/src/parachain.rs index 029771bce10d..e2a5364e9080 100644 --- a/xcm/xcm-simulator/example/src/parachain.rs +++ b/xcm/xcm-simulator/example/src/parachain.rs @@ -49,7 +49,7 @@ use xcm_executor::{ }; pub type SovereignAccountOf = ( - SiblingParachainConvertsVia, + SiblingParachainConvertsVia, AccountId32Aliases, ParentIsPreset, ); From ea32b5760380c63f6534a5e92cd3fe5c5b5f32fb Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Fri, 17 Feb 2023 12:17:05 +0100 Subject: [PATCH 2/6] [XCM] Multiple `FungiblesAdapter`s support + `WeightTrader::buy_weight` more accurate error --- xcm/xcm-executor/src/traits/token_matching.rs | 4 ++-- xcm/xcm-executor/src/traits/weight.rs | 21 +++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/xcm/xcm-executor/src/traits/token_matching.rs b/xcm/xcm-executor/src/traits/token_matching.rs index befff6b1b726..7f2652f8fb7e 100644 --- a/xcm/xcm-executor/src/traits/token_matching.rs +++ b/xcm/xcm-executor/src/traits/token_matching.rs @@ -49,7 +49,7 @@ impl MatchesNonFungible for Tuple { /// Errors associated with [`MatchesFungibles`] operation. pub enum Error { - /// Asset not found. + /// The given asset is not handled. (According to [`XcmError::AssetNotFound`]) AssetNotFound, /// `MultiLocation` to `AccountId` conversion failed. AccountIdConversionFailed, @@ -69,7 +69,7 @@ impl From for XcmError { Error::AccountIdConversionFailed => FailedToTransactAsset("AccountIdConversionFailed"), Error::AmountToBalanceConversionFailed => FailedToTransactAsset("AmountToBalanceConversionFailed"), - Error::AssetIdConversionFailed => FailedToTransactAsset("AssetIdConversionFailed"), + Error::AssetIdConversionFailed => XcmError::AssetNotFound, Error::InstanceConversionFailed => FailedToTransactAsset("InstanceConversionFailed"), } } diff --git a/xcm/xcm-executor/src/traits/weight.rs b/xcm/xcm-executor/src/traits/weight.rs index 76f411e5c1f5..6e111f93e806 100644 --- a/xcm/xcm-executor/src/traits/weight.rs +++ b/xcm/xcm-executor/src/traits/weight.rs @@ -67,16 +67,29 @@ impl WeightTrader for Tuple { } fn buy_weight(&mut self, weight: Weight, payment: Assets) -> Result { + let mut too_expensive_error_found = false; let mut last_error = None; for_tuples!( #( match Tuple.buy_weight(weight, payment.clone()) { Ok(assets) => return Ok(assets), - Err(e) => { last_error = Some(e) } + Err(e) => { + if let XcmError::TooExpensive = e { + too_expensive_error_found = true; + } + last_error = Some(e) + } } )* ); - let last_error = last_error.unwrap_or(XcmError::TooExpensive); - log::trace!(target: "xcm::buy_weight", "last_error: {:?}", last_error); - Err(last_error) + + log::trace!(target: "xcm::buy_weight", "last_error: {:?}, too_expensive_error_found: {}", last_error, too_expensive_error_found); + + // if we have multiple traders, and first one returns `TooExpensive` and others fail e.g. `AssetNotFound` + // then it is more accurate to return `TooExpensive` then `AssetNotFound` + Err(if too_expensive_error_found { + XcmError::TooExpensive + } else { + last_error.unwrap_or(XcmError::TooExpensive) + }) } fn refund_weight(&mut self, weight: Weight) -> Option { From a50366fe259c269b39c061582a9abfb4dff5e6cd Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Thu, 23 Feb 2023 21:59:06 +0100 Subject: [PATCH 3/6] Added test for `ConvertedConcreteId` with `AsPrefixedGeneralIndex` --- xcm/xcm-builder/src/asset_conversion.rs | 52 +++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/xcm/xcm-builder/src/asset_conversion.rs b/xcm/xcm-builder/src/asset_conversion.rs index 1a13f1ffb444..74ae6c844feb 100644 --- a/xcm/xcm-builder/src/asset_conversion.rs +++ b/xcm/xcm-builder/src/asset_conversion.rs @@ -147,3 +147,55 @@ impl< pub type ConvertedConcreteAssetId = ConvertedConcreteId; #[deprecated = "Use `ConvertedAbstractId` instead"] pub type ConvertedAbstractAssetId = ConvertedAbstractId; + +#[cfg(test)] +mod tests { + use super::*; + + use xcm_executor::traits::JustTry; + + #[test] + fn converted_concrete_id_with_as_prefixed_general_index_works() { + type AssetIdForTrustBackedAssets = u32; + type Balance = u128; + frame_support::parameter_types! { + pub TrustBackedAssetsPalletLocation: MultiLocation = PalletInstance(50).into(); + } + + // ConvertedConcreteId cfg + type Converter = ConvertedConcreteId< + AssetIdForTrustBackedAssets, + Balance, + AsPrefixedGeneralIndex< + TrustBackedAssetsPalletLocation, + AssetIdForTrustBackedAssets, + JustTry, + >, + JustTry, + >; + assert_eq!( + TrustBackedAssetsPalletLocation::get(), + MultiLocation { parents: 0, interior: X1(PalletInstance(50)) } + ); + + // ok scenario (MultiLocation matches AsPrefixedGeneralIndex cfg) + let multi_asset_ok = MultiAsset { + id: Concrete(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1)))), + fun: Fungible(12345), + }; + assert!(matches!(Converter::matches_fungibles(&multi_asset_ok), Ok((1, 12345)))); + + // throw AssetIdConversionFailed (MultiLocation DOES NOT match AsPrefixedGeneralIndex cfg) + let multi_asset_throw_aicf = MultiAsset { + id: Concrete(MultiLocation::new( + 1, + X3(Parachain(2000), PalletInstance(50), GeneralIndex(1)), + )), + fun: Fungible(12345), + }; + assert!(matches!( + Converter::matches_fungibles(&multi_asset_throw_aicf), + Err(MatchError::AssetIdConversionFailed) + )); + } +} From b1858f899319f65898fb9c72974fef81563400c0 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Fri, 24 Feb 2023 22:37:47 +0100 Subject: [PATCH 4/6] Solution 3. - new MatchedConvertedConcreteId with matching capabilities --- xcm/xcm-builder/src/asset_conversion.rs | 93 ++++++++++++++----- xcm/xcm-executor/src/traits/mod.rs | 9 +- xcm/xcm-executor/src/traits/token_matching.rs | 18 +++- 3 files changed, 94 insertions(+), 26 deletions(-) diff --git a/xcm/xcm-builder/src/asset_conversion.rs b/xcm/xcm-builder/src/asset_conversion.rs index 74ae6c844feb..b6201372b60c 100644 --- a/xcm/xcm-builder/src/asset_conversion.rs +++ b/xcm/xcm-builder/src/asset_conversion.rs @@ -19,7 +19,9 @@ use frame_support::traits::Get; use sp_std::{borrow::Borrow, marker::PhantomData, prelude::*, result}; use xcm::latest::prelude::*; -use xcm_executor::traits::{Convert, Error as MatchError, MatchesFungibles, MatchesNonFungibles}; +use xcm_executor::traits::{ + Convert, Error as MatchError, MatchesFungibles, MatchesMultiAssetId, MatchesNonFungibles, +}; /// Converter struct implementing `AssetIdConversion` converting a numeric asset ID (must be `TryFrom/TryInto`) into /// a `GeneralIndex` junction, prefixed by some `MultiLocation` value. The `MultiLocation` value will typically be a @@ -148,14 +150,52 @@ pub type ConvertedConcreteAssetId = ConvertedConcreteId; #[deprecated = "Use `ConvertedAbstractId` instead"] pub type ConvertedAbstractAssetId = ConvertedAbstractId; +pub struct MatchedConvertedConcreteId( + PhantomData<(AssetId, Balance, MatchAssetId, ConvertAssetId, ConvertOther)>, +); +impl< + AssetId: Clone, + Balance: Clone, + MatchAssetId: MatchesMultiAssetId, + ConvertAssetId: Convert, + ConvertBalance: Convert, + > MatchesFungibles + for MatchedConvertedConcreteId +{ + fn matches_fungibles(a: &MultiAsset) -> result::Result<(AssetId, Balance), MatchError> { + let (amount, id) = match (&a.fun, &a.id) { + (Fungible(ref amount), Concrete(ref id)) => match MatchAssetId::matches(id) { + true => (amount, id), + false => return Err(MatchError::AssetNotFound), + }, + _ => return Err(MatchError::AssetNotFound), + }; + let what = + ConvertAssetId::convert_ref(id).map_err(|_| MatchError::AssetIdConversionFailed)?; + let amount = ConvertBalance::convert_ref(amount) + .map_err(|_| MatchError::AmountToBalanceConversionFailed)?; + Ok((what, amount)) + } +} + #[cfg(test)] mod tests { use super::*; use xcm_executor::traits::JustTry; + struct OnlyParentZero; + impl MatchesMultiAssetId for OnlyParentZero { + fn matches(a: &MultiLocation) -> bool { + match a { + MultiLocation { parents: 0, .. } => true, + _ => false, + } + } + } + #[test] - fn converted_concrete_id_with_as_prefixed_general_index_works() { + fn matched_converted_concrete_id_works() { type AssetIdForTrustBackedAssets = u32; type Balance = u128; frame_support::parameter_types! { @@ -163,9 +203,10 @@ mod tests { } // ConvertedConcreteId cfg - type Converter = ConvertedConcreteId< + type Converter = MatchedConvertedConcreteId< AssetIdForTrustBackedAssets, Balance, + OnlyParentZero, AsPrefixedGeneralIndex< TrustBackedAssetsPalletLocation, AssetIdForTrustBackedAssets, @@ -178,24 +219,34 @@ mod tests { MultiLocation { parents: 0, interior: X1(PalletInstance(50)) } ); - // ok scenario (MultiLocation matches AsPrefixedGeneralIndex cfg) - let multi_asset_ok = MultiAsset { - id: Concrete(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1)))), - fun: Fungible(12345), - }; - assert!(matches!(Converter::matches_fungibles(&multi_asset_ok), Ok((1, 12345)))); - - // throw AssetIdConversionFailed (MultiLocation DOES NOT match AsPrefixedGeneralIndex cfg) - let multi_asset_throw_aicf = MultiAsset { - id: Concrete(MultiLocation::new( - 1, - X3(Parachain(2000), PalletInstance(50), GeneralIndex(1)), - )), - fun: Fungible(12345), - }; - assert!(matches!( - Converter::matches_fungibles(&multi_asset_throw_aicf), + // err - matches, but convert failed + assert_eq!( + Converter::matches_fungibles(&MultiAsset { + id: Concrete(MultiLocation::new( + 0, + X2(PalletInstance(50), GeneralKey { length: 1, data: [1; 32] }) + )), + fun: Fungible(12345), + }), Err(MatchError::AssetIdConversionFailed) - )); + ); + + // err - does not match + assert_eq!( + Converter::matches_fungibles(&MultiAsset { + id: Concrete(MultiLocation::new(1, X2(PalletInstance(50), GeneralIndex(1)))), + fun: Fungible(12345), + }), + Err(MatchError::AssetNotFound) + ); + + // ok + assert_eq!( + Converter::matches_fungibles(&MultiAsset { + id: Concrete(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1)))), + fun: Fungible(12345), + }), + Ok((1, 12345)) + ); } } diff --git a/xcm/xcm-executor/src/traits/mod.rs b/xcm/xcm-executor/src/traits/mod.rs index 94fe7e98a7db..3d2f096bc0a6 100644 --- a/xcm/xcm-executor/src/traits/mod.rs +++ b/xcm/xcm-executor/src/traits/mod.rs @@ -35,7 +35,8 @@ mod filter_asset_location; pub use filter_asset_location::FilterAssetLocation; mod token_matching; pub use token_matching::{ - Error, MatchesFungible, MatchesFungibles, MatchesNonFungible, MatchesNonFungibles, + Error, MatchesFungible, MatchesFungibles, MatchesMultiAssetId, MatchesNonFungible, + MatchesNonFungibles, }; mod on_response; pub use on_response::{OnResponse, VersionChangeNotifier}; @@ -50,8 +51,8 @@ pub mod prelude { pub use super::{ export_xcm, validate_export, AssetExchange, AssetLock, ClaimAssets, Convert, ConvertOrigin, Decoded, DropAssets, Enact, Encoded, Error, ExportXcm, FeeManager, FeeReason, Identity, - JustTry, LockError, MatchesFungible, MatchesFungibles, MatchesNonFungible, - MatchesNonFungibles, OnResponse, ShouldExecute, TransactAsset, VersionChangeNotifier, - WeightBounds, WeightTrader, WithOriginFilter, + JustTry, LockError, MatchesFungible, MatchesFungibles, MatchesMultiAssetId, + MatchesNonFungible, MatchesNonFungibles, OnResponse, ShouldExecute, TransactAsset, + VersionChangeNotifier, WeightBounds, WeightTrader, WithOriginFilter, }; } diff --git a/xcm/xcm-executor/src/traits/token_matching.rs b/xcm/xcm-executor/src/traits/token_matching.rs index 7f2652f8fb7e..a0ea9f7b5735 100644 --- a/xcm/xcm-executor/src/traits/token_matching.rs +++ b/xcm/xcm-executor/src/traits/token_matching.rs @@ -48,6 +48,7 @@ impl MatchesNonFungible for Tuple { } /// Errors associated with [`MatchesFungibles`] operation. +#[derive(Debug, PartialEq, Eq)] pub enum Error { /// The given asset is not handled. (According to [`XcmError::AssetNotFound`]) AssetNotFound, @@ -69,7 +70,7 @@ impl From for XcmError { Error::AccountIdConversionFailed => FailedToTransactAsset("AccountIdConversionFailed"), Error::AmountToBalanceConversionFailed => FailedToTransactAsset("AmountToBalanceConversionFailed"), - Error::AssetIdConversionFailed => XcmError::AssetNotFound, + Error::AssetIdConversionFailed => FailedToTransactAsset("AssetIdConversionFailed"), Error::InstanceConversionFailed => FailedToTransactAsset("InstanceConversionFailed"), } } @@ -104,3 +105,18 @@ impl MatchesNonFungibles for Tuple { Err(Error::AssetNotFound) } } + +pub trait MatchesMultiAssetId { + fn matches(a: &Id) -> bool; +} + +#[impl_trait_for_tuples::impl_for_tuples(30)] +impl MatchesMultiAssetId for Tuple { + fn matches(a: &Id) -> bool { + for_tuples!( #( + match Tuple::matches(a) { o @ true => return o, _ => () } + )* ); + log::trace!(target: "xcm::matches", "did not match asset location: {:?}", &a); + false + } +} From b43f230413618d25b6426244efc9fa02cd03a419 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Sun, 26 Feb 2023 20:58:24 +0100 Subject: [PATCH 5/6] Review fixes --- xcm/xcm-builder/src/asset_conversion.rs | 130 +++++++++++++++--- xcm/xcm-executor/src/traits/mod.rs | 9 +- xcm/xcm-executor/src/traits/token_matching.rs | 15 -- 3 files changed, 118 insertions(+), 36 deletions(-) diff --git a/xcm/xcm-builder/src/asset_conversion.rs b/xcm/xcm-builder/src/asset_conversion.rs index b6201372b60c..65f3b70b81d8 100644 --- a/xcm/xcm-builder/src/asset_conversion.rs +++ b/xcm/xcm-builder/src/asset_conversion.rs @@ -16,12 +16,10 @@ //! Adapters to work with `frame_support::traits::tokens::fungibles` through XCM. -use frame_support::traits::Get; +use frame_support::traits::{Contains, Get}; use sp_std::{borrow::Borrow, marker::PhantomData, prelude::*, result}; use xcm::latest::prelude::*; -use xcm_executor::traits::{ - Convert, Error as MatchError, MatchesFungibles, MatchesMultiAssetId, MatchesNonFungibles, -}; +use xcm_executor::traits::{Convert, Error as MatchError, MatchesFungibles, MatchesNonFungibles}; /// Converter struct implementing `AssetIdConversion` converting a numeric asset ID (must be `TryFrom/TryInto`) into /// a `GeneralIndex` junction, prefixed by some `MultiLocation` value. The `MultiLocation` value will typically be a @@ -156,7 +154,7 @@ pub struct MatchedConvertedConcreteId, + MatchAssetId: Contains, ConvertAssetId: Convert, ConvertBalance: Convert, > MatchesFungibles @@ -164,10 +162,7 @@ impl< { fn matches_fungibles(a: &MultiAsset) -> result::Result<(AssetId, Balance), MatchError> { let (amount, id) = match (&a.fun, &a.id) { - (Fungible(ref amount), Concrete(ref id)) => match MatchAssetId::matches(id) { - true => (amount, id), - false => return Err(MatchError::AssetNotFound), - }, + (Fungible(ref amount), Concrete(ref id)) if MatchAssetId::contains(id) => (amount, id), _ => return Err(MatchError::AssetNotFound), }; let what = @@ -177,6 +172,28 @@ impl< Ok((what, amount)) } } +impl< + ClassId: Clone, + InstanceId: Clone, + MatchClassId: Contains, + ConvertClassId: Convert, + ConvertInstanceId: Convert, + > MatchesNonFungibles + for MatchedConvertedConcreteId +{ + fn matches_nonfungibles(a: &MultiAsset) -> result::Result<(ClassId, InstanceId), MatchError> { + let (instance, class) = match (&a.fun, &a.id) { + (NonFungible(ref instance), Concrete(ref class)) if MatchClassId::contains(class) => + (instance, class), + _ => return Err(MatchError::AssetNotFound), + }; + let what = + ConvertClassId::convert_ref(class).map_err(|_| MatchError::AssetIdConversionFailed)?; + let instance = ConvertInstanceId::convert_ref(instance) + .map_err(|_| MatchError::InstanceConversionFailed)?; + Ok((what, instance)) + } +} #[cfg(test)] mod tests { @@ -185,8 +202,8 @@ mod tests { use xcm_executor::traits::JustTry; struct OnlyParentZero; - impl MatchesMultiAssetId for OnlyParentZero { - fn matches(a: &MultiLocation) -> bool { + impl Contains for OnlyParentZero { + fn contains(a: &MultiLocation) -> bool { match a { MultiLocation { parents: 0, .. } => true, _ => false, @@ -195,7 +212,7 @@ mod tests { } #[test] - fn matched_converted_concrete_id_works() { + fn matched_converted_concrete_id_for_fungibles_works() { type AssetIdForTrustBackedAssets = u32; type Balance = u128; frame_support::parameter_types! { @@ -219,7 +236,16 @@ mod tests { MultiLocation { parents: 0, interior: X1(PalletInstance(50)) } ); - // err - matches, but convert failed + // err - does not match + assert_eq!( + Converter::matches_fungibles(&MultiAsset { + id: Concrete(MultiLocation::new(1, X2(PalletInstance(50), GeneralIndex(1)))), + fun: Fungible(12345), + }), + Err(MatchError::AssetNotFound) + ); + + // err - matches, but convert fails assert_eq!( Converter::matches_fungibles(&MultiAsset { id: Concrete(MultiLocation::new( @@ -231,11 +257,11 @@ mod tests { Err(MatchError::AssetIdConversionFailed) ); - // err - does not match + // err - matches, but NonFungible assert_eq!( Converter::matches_fungibles(&MultiAsset { - id: Concrete(MultiLocation::new(1, X2(PalletInstance(50), GeneralIndex(1)))), - fun: Fungible(12345), + id: Concrete(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1)))), + fun: NonFungible(Index(54321)), }), Err(MatchError::AssetNotFound) ); @@ -249,4 +275,76 @@ mod tests { Ok((1, 12345)) ); } + + #[test] + fn matched_converted_concrete_id_for_nonfungibles_works() { + type ClassId = u32; + type ClassInstanceId = u64; + frame_support::parameter_types! { + pub TrustBackedAssetsPalletLocation: MultiLocation = PalletInstance(50).into(); + } + + // ConvertedConcreteId cfg + struct ClassInstanceIdConverter; + impl Convert for ClassInstanceIdConverter { + fn convert_ref(value: impl Borrow) -> Result { + value.borrow().clone().try_into().map_err(|_| ()) + } + + fn reverse_ref(value: impl Borrow) -> Result { + Ok(AssetInstance::from(value.borrow().clone())) + } + } + + type Converter = MatchedConvertedConcreteId< + ClassId, + ClassInstanceId, + OnlyParentZero, + AsPrefixedGeneralIndex, + ClassInstanceIdConverter, + >; + assert_eq!( + TrustBackedAssetsPalletLocation::get(), + MultiLocation { parents: 0, interior: X1(PalletInstance(50)) } + ); + + // err - does not match + assert_eq!( + Converter::matches_nonfungibles(&MultiAsset { + id: Concrete(MultiLocation::new(1, X2(PalletInstance(50), GeneralIndex(1)))), + fun: NonFungible(Index(54321)), + }), + Err(MatchError::AssetNotFound) + ); + + // err - matches, but convert fails + assert_eq!( + Converter::matches_nonfungibles(&MultiAsset { + id: Concrete(MultiLocation::new( + 0, + X2(PalletInstance(50), GeneralKey { length: 1, data: [1; 32] }) + )), + fun: NonFungible(Index(54321)), + }), + Err(MatchError::AssetIdConversionFailed) + ); + + // err - matches, but Fungible vs NonFungible + assert_eq!( + Converter::matches_nonfungibles(&MultiAsset { + id: Concrete(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1)))), + fun: Fungible(12345), + }), + Err(MatchError::AssetNotFound) + ); + + // ok + assert_eq!( + Converter::matches_nonfungibles(&MultiAsset { + id: Concrete(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1)))), + fun: NonFungible(Index(54321)), + }), + Ok((1, 54321)) + ); + } } diff --git a/xcm/xcm-executor/src/traits/mod.rs b/xcm/xcm-executor/src/traits/mod.rs index 3d2f096bc0a6..94fe7e98a7db 100644 --- a/xcm/xcm-executor/src/traits/mod.rs +++ b/xcm/xcm-executor/src/traits/mod.rs @@ -35,8 +35,7 @@ mod filter_asset_location; pub use filter_asset_location::FilterAssetLocation; mod token_matching; pub use token_matching::{ - Error, MatchesFungible, MatchesFungibles, MatchesMultiAssetId, MatchesNonFungible, - MatchesNonFungibles, + Error, MatchesFungible, MatchesFungibles, MatchesNonFungible, MatchesNonFungibles, }; mod on_response; pub use on_response::{OnResponse, VersionChangeNotifier}; @@ -51,8 +50,8 @@ pub mod prelude { pub use super::{ export_xcm, validate_export, AssetExchange, AssetLock, ClaimAssets, Convert, ConvertOrigin, Decoded, DropAssets, Enact, Encoded, Error, ExportXcm, FeeManager, FeeReason, Identity, - JustTry, LockError, MatchesFungible, MatchesFungibles, MatchesMultiAssetId, - MatchesNonFungible, MatchesNonFungibles, OnResponse, ShouldExecute, TransactAsset, - VersionChangeNotifier, WeightBounds, WeightTrader, WithOriginFilter, + JustTry, LockError, MatchesFungible, MatchesFungibles, MatchesNonFungible, + MatchesNonFungibles, OnResponse, ShouldExecute, TransactAsset, VersionChangeNotifier, + WeightBounds, WeightTrader, WithOriginFilter, }; } diff --git a/xcm/xcm-executor/src/traits/token_matching.rs b/xcm/xcm-executor/src/traits/token_matching.rs index a0ea9f7b5735..85553e2727d9 100644 --- a/xcm/xcm-executor/src/traits/token_matching.rs +++ b/xcm/xcm-executor/src/traits/token_matching.rs @@ -105,18 +105,3 @@ impl MatchesNonFungibles for Tuple { Err(Error::AssetNotFound) } } - -pub trait MatchesMultiAssetId { - fn matches(a: &Id) -> bool; -} - -#[impl_trait_for_tuples::impl_for_tuples(30)] -impl MatchesMultiAssetId for Tuple { - fn matches(a: &Id) -> bool { - for_tuples!( #( - match Tuple::matches(a) { o @ true => return o, _ => () } - )* ); - log::trace!(target: "xcm::matches", "did not match asset location: {:?}", &a); - false - } -} From 4d832697a3ef35df5a4684ad9c64226bf008b462 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Tue, 28 Feb 2023 09:56:16 +0100 Subject: [PATCH 6/6] Renamed `AssetNotFound` -> `AssetNotHandled` --- xcm/xcm-builder/src/asset_conversion.rs | 20 +++++++++---------- xcm/xcm-builder/src/currency_adapter.rs | 16 +++++++-------- xcm/xcm-executor/src/traits/token_matching.rs | 8 ++++---- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/xcm/xcm-builder/src/asset_conversion.rs b/xcm/xcm-builder/src/asset_conversion.rs index 65f3b70b81d8..cadbb8381cfa 100644 --- a/xcm/xcm-builder/src/asset_conversion.rs +++ b/xcm/xcm-builder/src/asset_conversion.rs @@ -69,7 +69,7 @@ impl< fn matches_fungibles(a: &MultiAsset) -> result::Result<(AssetId, Balance), MatchError> { let (amount, id) = match (&a.fun, &a.id) { (Fungible(ref amount), Concrete(ref id)) => (amount, id), - _ => return Err(MatchError::AssetNotFound), + _ => return Err(MatchError::AssetNotHandled), }; let what = ConvertAssetId::convert_ref(id).map_err(|_| MatchError::AssetIdConversionFailed)?; @@ -89,7 +89,7 @@ impl< fn matches_nonfungibles(a: &MultiAsset) -> result::Result<(ClassId, InstanceId), MatchError> { let (instance, class) = match (&a.fun, &a.id) { (NonFungible(ref instance), Concrete(ref class)) => (instance, class), - _ => return Err(MatchError::AssetNotFound), + _ => return Err(MatchError::AssetNotHandled), }; let what = ConvertClassId::convert_ref(class).map_err(|_| MatchError::AssetIdConversionFailed)?; @@ -113,7 +113,7 @@ impl< fn matches_fungibles(a: &MultiAsset) -> result::Result<(AssetId, Balance), MatchError> { let (amount, id) = match (&a.fun, &a.id) { (Fungible(ref amount), Abstract(ref id)) => (amount, id), - _ => return Err(MatchError::AssetNotFound), + _ => return Err(MatchError::AssetNotHandled), }; let what = ConvertAssetId::convert_ref(id).map_err(|_| MatchError::AssetIdConversionFailed)?; @@ -133,7 +133,7 @@ impl< fn matches_nonfungibles(a: &MultiAsset) -> result::Result<(ClassId, InstanceId), MatchError> { let (instance, class) = match (&a.fun, &a.id) { (NonFungible(ref instance), Abstract(ref class)) => (instance, class), - _ => return Err(MatchError::AssetNotFound), + _ => return Err(MatchError::AssetNotHandled), }; let what = ConvertClassId::convert_ref(class).map_err(|_| MatchError::AssetIdConversionFailed)?; @@ -163,7 +163,7 @@ impl< fn matches_fungibles(a: &MultiAsset) -> result::Result<(AssetId, Balance), MatchError> { let (amount, id) = match (&a.fun, &a.id) { (Fungible(ref amount), Concrete(ref id)) if MatchAssetId::contains(id) => (amount, id), - _ => return Err(MatchError::AssetNotFound), + _ => return Err(MatchError::AssetNotHandled), }; let what = ConvertAssetId::convert_ref(id).map_err(|_| MatchError::AssetIdConversionFailed)?; @@ -185,7 +185,7 @@ impl< let (instance, class) = match (&a.fun, &a.id) { (NonFungible(ref instance), Concrete(ref class)) if MatchClassId::contains(class) => (instance, class), - _ => return Err(MatchError::AssetNotFound), + _ => return Err(MatchError::AssetNotHandled), }; let what = ConvertClassId::convert_ref(class).map_err(|_| MatchError::AssetIdConversionFailed)?; @@ -242,7 +242,7 @@ mod tests { id: Concrete(MultiLocation::new(1, X2(PalletInstance(50), GeneralIndex(1)))), fun: Fungible(12345), }), - Err(MatchError::AssetNotFound) + Err(MatchError::AssetNotHandled) ); // err - matches, but convert fails @@ -263,7 +263,7 @@ mod tests { id: Concrete(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1)))), fun: NonFungible(Index(54321)), }), - Err(MatchError::AssetNotFound) + Err(MatchError::AssetNotHandled) ); // ok @@ -314,7 +314,7 @@ mod tests { id: Concrete(MultiLocation::new(1, X2(PalletInstance(50), GeneralIndex(1)))), fun: NonFungible(Index(54321)), }), - Err(MatchError::AssetNotFound) + Err(MatchError::AssetNotHandled) ); // err - matches, but convert fails @@ -335,7 +335,7 @@ mod tests { id: Concrete(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1)))), fun: Fungible(12345), }), - Err(MatchError::AssetNotFound) + Err(MatchError::AssetNotHandled) ); // ok diff --git a/xcm/xcm-builder/src/currency_adapter.rs b/xcm/xcm-builder/src/currency_adapter.rs index 2815d8bd3c47..bb7ac7a648da 100644 --- a/xcm/xcm-builder/src/currency_adapter.rs +++ b/xcm/xcm-builder/src/currency_adapter.rs @@ -28,8 +28,8 @@ use xcm_executor::{ /// Asset transaction errors. enum Error { - /// Asset not found. - AssetNotFound, + /// The given asset is not handled. (According to [`XcmError::AssetNotFound`]) + AssetNotHandled, /// `MultiLocation` to `AccountId` conversion failed. AccountIdConversionFailed, } @@ -38,7 +38,7 @@ impl From for XcmError { fn from(e: Error) -> Self { use XcmError::FailedToTransactAsset; match e { - Error::AssetNotFound => XcmError::AssetNotFound, + Error::AssetNotHandled => XcmError::AssetNotFound, Error::AccountIdConversionFailed => FailedToTransactAsset("AccountIdConversionFailed"), } } @@ -143,7 +143,7 @@ impl< log::trace!(target: "xcm::currency_adapter", "can_check_in origin: {:?}, what: {:?}", _origin, what); // Check we handle this asset. let amount: Currency::Balance = - Matcher::matches_fungible(what).ok_or(Error::AssetNotFound)?; + Matcher::matches_fungible(what).ok_or(Error::AssetNotHandled)?; match CheckedAccount::get() { Some((checked_account, MintLocation::Local)) => Self::can_reduce_checked(checked_account, amount), @@ -168,7 +168,7 @@ impl< fn can_check_out(_dest: &MultiLocation, what: &MultiAsset, _context: &XcmContext) -> Result { log::trace!(target: "xcm::currency_adapter", "check_out dest: {:?}, what: {:?}", _dest, what); - let amount = Matcher::matches_fungible(what).ok_or(Error::AssetNotFound)?; + let amount = Matcher::matches_fungible(what).ok_or(Error::AssetNotHandled)?; match CheckedAccount::get() { Some((checked_account, MintLocation::Local)) => Self::can_accrue_checked(checked_account, amount), @@ -194,7 +194,7 @@ impl< fn deposit_asset(what: &MultiAsset, who: &MultiLocation, _context: &XcmContext) -> Result { log::trace!(target: "xcm::currency_adapter", "deposit_asset what: {:?}, who: {:?}", what, who); // Check we handle this asset. - let amount = Matcher::matches_fungible(&what).ok_or(Error::AssetNotFound)?; + let amount = Matcher::matches_fungible(&what).ok_or(Error::AssetNotHandled)?; let who = AccountIdConverter::convert_ref(who).map_err(|()| Error::AccountIdConversionFailed)?; let _imbalance = Currency::deposit_creating(&who, amount); @@ -208,7 +208,7 @@ impl< ) -> result::Result { log::trace!(target: "xcm::currency_adapter", "withdraw_asset what: {:?}, who: {:?}", what, who); // Check we handle this asset. - let amount = Matcher::matches_fungible(what).ok_or(Error::AssetNotFound)?; + let amount = Matcher::matches_fungible(what).ok_or(Error::AssetNotHandled)?; let who = AccountIdConverter::convert_ref(who).map_err(|()| Error::AccountIdConversionFailed)?; Currency::withdraw(&who, amount, WithdrawReasons::TRANSFER, AllowDeath) @@ -223,7 +223,7 @@ impl< _context: &XcmContext, ) -> result::Result { log::trace!(target: "xcm::currency_adapter", "internal_transfer_asset asset: {:?}, from: {:?}, to: {:?}", asset, from, to); - let amount = Matcher::matches_fungible(asset).ok_or(Error::AssetNotFound)?; + let amount = Matcher::matches_fungible(asset).ok_or(Error::AssetNotHandled)?; let from = AccountIdConverter::convert_ref(from).map_err(|()| Error::AccountIdConversionFailed)?; let to = diff --git a/xcm/xcm-executor/src/traits/token_matching.rs b/xcm/xcm-executor/src/traits/token_matching.rs index 85553e2727d9..f1bbbe68025b 100644 --- a/xcm/xcm-executor/src/traits/token_matching.rs +++ b/xcm/xcm-executor/src/traits/token_matching.rs @@ -51,7 +51,7 @@ impl MatchesNonFungible for Tuple { #[derive(Debug, PartialEq, Eq)] pub enum Error { /// The given asset is not handled. (According to [`XcmError::AssetNotFound`]) - AssetNotFound, + AssetNotHandled, /// `MultiLocation` to `AccountId` conversion failed. AccountIdConversionFailed, /// `u128` amount to currency `Balance` conversion failed. @@ -66,7 +66,7 @@ impl From for XcmError { fn from(e: Error) -> Self { use XcmError::FailedToTransactAsset; match e { - Error::AssetNotFound => XcmError::AssetNotFound, + Error::AssetNotHandled => XcmError::AssetNotFound, Error::AccountIdConversionFailed => FailedToTransactAsset("AccountIdConversionFailed"), Error::AmountToBalanceConversionFailed => FailedToTransactAsset("AmountToBalanceConversionFailed"), @@ -87,7 +87,7 @@ impl MatchesFungibles for Tuple { match Tuple::matches_fungibles(a) { o @ Ok(_) => return o, _ => () } )* ); log::trace!(target: "xcm::matches_fungibles", "did not match fungibles asset: {:?}", &a); - Err(Error::AssetNotFound) + Err(Error::AssetNotHandled) } } @@ -102,6 +102,6 @@ impl MatchesNonFungibles for Tuple { match Tuple::matches_nonfungibles(a) { o @ Ok(_) => return o, _ => () } )* ); log::trace!(target: "xcm::matches_non_fungibles", "did not match fungibles asset: {:?}", &a); - Err(Error::AssetNotFound) + Err(Error::AssetNotHandled) } }