Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unknown assets in TransactAsset impl #409

Merged
merged 7 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
77 changes: 54 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,34 @@ 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`,
/// or by `UnknownAsset` if unknown.
///
/// The implementation will try deposit or withdraw on unknown asset first, so
/// that detailed error info of known asset failures could be returned if any.
/// Thus known asset deposit/withdraw failures imply unknown asset failures as
/// well.
pub struct MultiCurrencyAdapter<
MultiCurrency,
UnknownAsset,
Matcher,
AccountIdConverter,
AccountId,
CurrencyIdConverter,
CurrencyId,
>(
PhantomData<(
MultiCurrency,
UnknownAsset,
Matcher,
AccountIdConverter,
AccountId,
Expand All @@ -46,35 +64,48 @@ 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(())
UnknownAsset::deposit(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::deposit(currency_id, &who, amount).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())
}
}
18 changes: 18 additions & 0 deletions xcm-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,21 @@ 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;
}

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