Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 11 additions & 26 deletions xcm-support/src/currency_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use codec::FullCodec;
use sp_runtime::traits::{MaybeSerializeDeserialize, SaturatedConversion};
use sp_std::{
cmp::{Eq, PartialEq},
convert::{TryFrom, TryInto},
fmt::Debug,
marker::PhantomData,
prelude::*,
Expand All @@ -11,7 +12,7 @@ use sp_std::{
use xcm::v0::{Error as XcmError, MultiAsset, MultiLocation, Result};
use xcm_executor::traits::{LocationConversion, MatchesFungible, TransactAsset};

use crate::{CurrencyIdConversion, UnknownAsset as UnknownAssetT};
use crate::UnknownAsset as UnknownAssetT;

/// Asset transaction errors.
enum Error {
Expand All @@ -37,22 +38,13 @@ impl From<Error> for XcmError {
///
/// If the asset is known, deposit/withdraw will be handled by `MultiCurrency`,
/// else by `UnknownAsset` if unknown.
pub struct MultiCurrencyAdapter<
MultiCurrency,
UnknownAsset,
Matcher,
AccountIdConverter,
AccountId,
CurrencyIdConverter,
CurrencyId,
>(
pub struct MultiCurrencyAdapter<MultiCurrency, UnknownAsset, Matcher, AccountIdConverter, AccountId, CurrencyId>(
PhantomData<(
MultiCurrency,
UnknownAsset,
Matcher,
AccountIdConverter,
AccountId,
CurrencyIdConverter,
CurrencyId,
)>,
);
Expand All @@ -63,27 +55,18 @@ impl<
Matcher: MatchesFungible<MultiCurrency::Balance>,
AccountIdConverter: LocationConversion<AccountId>,
AccountId: sp_std::fmt::Debug,
CurrencyIdConverter: CurrencyIdConversion<CurrencyId>,
CurrencyId: FullCodec + Eq + PartialEq + Copy + MaybeSerializeDeserialize + Debug,
CurrencyId: FullCodec + Eq + PartialEq + Copy + MaybeSerializeDeserialize + Debug + TryFrom<MultiAsset>,
> TransactAsset
for MultiCurrencyAdapter<
MultiCurrency,
UnknownAsset,
Matcher,
AccountIdConverter,
AccountId,
CurrencyIdConverter,
CurrencyId,
>
for MultiCurrencyAdapter<MultiCurrency, UnknownAsset, Matcher, AccountIdConverter, AccountId, CurrencyId>
{
fn deposit_asset(asset: &MultiAsset, location: &MultiLocation) -> Result {
match (
AccountIdConverter::from_location(location),
CurrencyIdConverter::from_asset(asset),
asset.clone().try_into(),
Matcher::matches_fungible(&asset),
) {
// known asset
(Some(who), Some(currency_id), Some(amount)) => {
(Some(who), Ok(currency_id), Some(amount)) => {
MultiCurrency::deposit(currency_id, &who, amount).map_err(|e| XcmError::FailedToTransactAsset(e.into()))
}
// unknown asset
Expand All @@ -95,8 +78,10 @@ impl<
UnknownAsset::withdraw(asset, location).or_else(|_| {
let who = AccountIdConverter::from_location(location)
.ok_or_else(|| XcmError::from(Error::AccountIdConversionFailed))?;
let currency_id = CurrencyIdConverter::from_asset(asset)
.ok_or_else(|| XcmError::from(Error::CurrencyIdConversionFailed))?;
let currency_id = asset
.clone()
.try_into()
.map_err(|_| XcmError::from(Error::CurrencyIdConversionFailed))?;
let amount: MultiCurrency::Balance = Matcher::matches_fungible(&asset)
.ok_or_else(|| XcmError::from(Error::FailedToMatchFungible))?
.saturated_into();
Expand Down
98 changes: 14 additions & 84 deletions xcm-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::unused_unit)]

use frame_support::{
dispatch::{DispatchError, DispatchResult},
traits::Get,
};
use sp_runtime::traits::{CheckedConversion, Convert};
use sp_std::{
collections::btree_set::BTreeSet,
convert::{TryFrom, TryInto},
marker::PhantomData,
prelude::*,
};
use frame_support::dispatch::{DispatchError, DispatchResult};
use sp_runtime::traits::CheckedConversion;
use sp_std::{convert::TryFrom, marker::PhantomData, prelude::*};

use xcm::v0::{Junction, MultiAsset, MultiLocation, Xcm};
use xcm_executor::traits::{FilterAssetLocation, MatchesFungible, NativeAsset};
use xcm::v0::{MultiAsset, MultiLocation, Xcm};
use xcm_executor::traits::{FilterAssetLocation, MatchesFungible};

use orml_traits::location::Reserve;

Expand All @@ -37,61 +29,24 @@ pub trait XcmHandler<AccountId> {
fn execute_xcm(origin: AccountId, xcm: Xcm) -> DispatchResult;
}

/// Convert `MultiAsset` to `CurrencyId`.
pub trait CurrencyIdConversion<CurrencyId> {
/// Get `CurrencyId` from `MultiAsset`. Returns `None` if conversion failed.
fn from_asset(asset: &MultiAsset) -> Option<CurrencyId>;
}

/// A `MatchesFungible` implementation. It matches relay chain tokens or
/// parachain tokens that could be decoded from a general key.
pub struct IsConcreteWithGeneralKey<CurrencyId, FromRelayChainBalance>(
PhantomData<(CurrencyId, FromRelayChainBalance)>,
);
impl<CurrencyId, B, FromRelayChainBalance> MatchesFungible<B>
for IsConcreteWithGeneralKey<CurrencyId, FromRelayChainBalance>
/// A `MatchesFungible` implementation. It matches concrete fungible assets
/// whose `id` could be converted into `CurrencyId`.
pub struct IsNativeConcrete<CurrencyId>(PhantomData<CurrencyId>);
impl<CurrencyId, Amount> MatchesFungible<Amount> for IsNativeConcrete<CurrencyId>
where
CurrencyId: TryFrom<Vec<u8>>,
B: TryFrom<u128>,
FromRelayChainBalance: Convert<u128, u128>,
CurrencyId: TryFrom<MultiLocation>,
Amount: TryFrom<u128>,
{
fn matches_fungible(a: &MultiAsset) -> Option<B> {
fn matches_fungible(a: &MultiAsset) -> Option<Amount> {
if let MultiAsset::ConcreteFungible { id, amount } = a {
if id == &MultiLocation::X1(Junction::Parent) {
// Convert relay chain decimals to local chain
let local_amount = FromRelayChainBalance::convert(*amount);
return CheckedConversion::checked_from(local_amount);
}
if let Some(Junction::GeneralKey(key)) = id.last() {
if TryInto::<CurrencyId>::try_into(key.clone()).is_ok() {
return CheckedConversion::checked_from(*amount);
}
if CurrencyId::try_from(id.clone()).is_ok() {
return CheckedConversion::checked_from(*amount);
}
}
None
}
}

/// A `FilterAssetLocation` implementation. Filters native assets and ORML
/// tokens via provided general key to `MultiLocation` pairs.
pub struct NativePalletAssetOr<Pairs>(PhantomData<Pairs>);
impl<Pairs: Get<BTreeSet<(Vec<u8>, MultiLocation)>>> FilterAssetLocation for NativePalletAssetOr<Pairs> {
fn filter_asset_location(asset: &MultiAsset, origin: &MultiLocation) -> bool {
if NativeAsset::filter_asset_location(asset, origin) {
return true;
}

// native orml-tokens with a general key
if let MultiAsset::ConcreteFungible { ref id, .. } = asset {
if let Some(Junction::GeneralKey(key)) = id.last() {
return Pairs::get().contains(&(key.clone(), origin.clone()));
}
}

false
}
}

/// A `FilterAssetLocation` implementation. Filters multi native assets whose
/// reserve is same with `origin`.
pub struct MultiNativeAsset;
Expand All @@ -106,31 +61,6 @@ impl FilterAssetLocation for MultiNativeAsset {
}
}

/// `CurrencyIdConversion` implementation. Converts relay chain tokens, or
/// parachain tokens that could be decoded from a general key.
pub struct CurrencyIdConverter<CurrencyId, RelayChainCurrencyId>(
PhantomData<CurrencyId>,
PhantomData<RelayChainCurrencyId>,
);
impl<CurrencyId, RelayChainCurrencyId> CurrencyIdConversion<CurrencyId>
for CurrencyIdConverter<CurrencyId, RelayChainCurrencyId>
where
CurrencyId: TryFrom<Vec<u8>>,
RelayChainCurrencyId: Get<CurrencyId>,
{
fn from_asset(asset: &MultiAsset) -> Option<CurrencyId> {
if let MultiAsset::ConcreteFungible { id: location, .. } = asset {
if location == &MultiLocation::X1(Junction::Parent) {
return Some(RelayChainCurrencyId::get());
}
if let Some(Junction::GeneralKey(key)) = location.last() {
return CurrencyId::try_from(key.clone()).ok();
}
}
None
}
}

/// Handlers unknown asset deposit and withdraw.
pub trait UnknownAsset {
/// Deposit unknown asset.
Expand Down
Loading