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
74 changes: 51 additions & 23 deletions xcm-support/src/currency_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use sp_std::{
use xcm::v0::{Error as XcmError, MultiAsset, MultiLocation, Result};
use xcm_executor::traits::{LocationConversion, MatchesFungible, TransactAsset};

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

/// Asset transaction errors.
enum Error {
/// Asset not found.
AssetNotFound,
/// Failed to match fungible.
FailedToMatchFungible,
/// `MultiLocation` to `AccountId` Conversion failed.
AccountIdConversionFailed,
/// `CurrencyId` conversion failed.
Expand All @@ -26,16 +26,29 @@ enum Error {
impl From<Error> for XcmError {
fn from(e: Error) -> Self {
match e {
Error::AssetNotFound => XcmError::FailedToTransactAsset("AssetNotFound"),
Error::FailedToMatchFungible => XcmError::FailedToTransactAsset("FailedToMatchFungible"),
Error::AccountIdConversionFailed => XcmError::FailedToTransactAsset("AccountIdConversionFailed"),
Error::CurrencyIdConversionFailed => XcmError::FailedToTransactAsset("CurrencyIdConversionFailed"),
}
}
}

pub struct MultiCurrencyAdapter<MultiCurrency, Matcher, AccountIdConverter, AccountId, CurrencyIdConverter, CurrencyId>(
/// The `TransactAsset` implementation, to handle `MultiAsset` deposit/withdraw.
///
/// 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,
>(
PhantomData<(
MultiCurrency,
UnknownAsset,
Matcher,
AccountIdConverter,
AccountId,
Expand All @@ -46,35 +59,50 @@ pub struct MultiCurrencyAdapter<MultiCurrency, Matcher, AccountIdConverter, Acco

impl<
MultiCurrency: orml_traits::MultiCurrency<AccountId, CurrencyId = CurrencyId>,
UnknownAsset: UnknownAssetT,
Matcher: MatchesFungible<MultiCurrency::Balance>,
AccountIdConverter: LocationConversion<AccountId>,
AccountId: sp_std::fmt::Debug,
CurrencyIdConverter: CurrencyIdConversion<CurrencyId>,
CurrencyId: FullCodec + Eq + PartialEq + Copy + MaybeSerializeDeserialize + Debug,
> TransactAsset
for MultiCurrencyAdapter<MultiCurrency, Matcher, AccountIdConverter, AccountId, CurrencyIdConverter, CurrencyId>
for MultiCurrencyAdapter<
MultiCurrency,
UnknownAsset,
Matcher,
AccountIdConverter,
AccountId,
CurrencyIdConverter,
CurrencyId,
>
{
fn deposit_asset(asset: &MultiAsset, location: &MultiLocation) -> Result {
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 amount: MultiCurrency::Balance = Matcher::matches_fungible(&asset)
.ok_or_else(|| XcmError::from(Error::AssetNotFound))?
.saturated_into();
MultiCurrency::deposit(currency_id, &who, amount).map_err(|e| XcmError::FailedToTransactAsset(e.into()))?;
Ok(())
match (
AccountIdConverter::from_location(location),
CurrencyIdConverter::from_asset(asset),
Matcher::matches_fungible(&asset),
) {
// known asset
(Some(who), Some(currency_id), Some(amount)) => {
MultiCurrency::deposit(currency_id, &who, amount).map_err(|e| XcmError::FailedToTransactAsset(e.into()))
}
// unknown asset
_ => UnknownAsset::deposit(asset, location).map_err(|e| XcmError::FailedToTransactAsset(e.into())),
}
}

fn withdraw_asset(asset: &MultiAsset, location: &MultiLocation) -> result::Result<MultiAsset, XcmError> {
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 amount: MultiCurrency::Balance = Matcher::matches_fungible(&asset)
.ok_or_else(|| XcmError::from(Error::AssetNotFound))?
.saturated_into();
MultiCurrency::withdraw(currency_id, &who, amount).map_err(|e| XcmError::FailedToTransactAsset(e.into()))?;
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 amount: MultiCurrency::Balance = Matcher::matches_fungible(&asset)
.ok_or_else(|| XcmError::from(Error::FailedToMatchFungible))?
.saturated_into();
MultiCurrency::withdraw(currency_id, &who, amount).map_err(|e| XcmError::FailedToTransactAsset(e.into()))
})?;

Ok(asset.clone())
}
}
25 changes: 24 additions & 1 deletion xcm-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{dispatch::DispatchResult, traits::Get};
use frame_support::{
dispatch::{DispatchError, DispatchResult},
traits::Get,
};
use sp_runtime::traits::{CheckedConversion, Convert};
use sp_std::{
collections::btree_set::BTreeSet,
Expand Down Expand Up @@ -111,3 +114,23 @@ where
None
}
}

/// Handlers unknown asset deposit and withdraw.
pub trait UnknownAsset {
/// Deposit unknown asset.
fn deposit(asset: &MultiAsset, to: &MultiLocation) -> DispatchResult;

/// Withdraw unknown asset.
fn withdraw(asset: &MultiAsset, from: &MultiLocation) -> DispatchResult;
}

const NO_UNKNOWN_ASSET_IMPL: &str = "NoUnknownAssetImpl";

impl UnknownAsset for () {
fn deposit(_asset: &MultiAsset, _to: &MultiLocation) -> DispatchResult {
Err(DispatchError::Other(NO_UNKNOWN_ASSET_IMPL))
}
fn withdraw(_asset: &MultiAsset, _from: &MultiLocation) -> DispatchResult {
Err(DispatchError::Other(NO_UNKNOWN_ASSET_IMPL))
}
}