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

Replace XcmError::Undefined usage. #402

Merged
merged 2 commits into from
Mar 15, 2021
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
80 changes: 80 additions & 0 deletions xcm-support/src/currency_adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use codec::FullCodec;
use sp_runtime::traits::{MaybeSerializeDeserialize, SaturatedConversion};
use sp_std::{
cmp::{Eq, PartialEq},
fmt::Debug,
marker::PhantomData,
prelude::*,
result,
};

use xcm::v0::{Error as XcmError, MultiAsset, MultiLocation, Result};
use xcm_executor::traits::{LocationConversion, MatchesFungible, TransactAsset};

use crate::CurrencyIdConversion;

/// Asset transaction errors.
enum Error {
/// Asset not found.
AssetNotFound,
/// `MultiLocation` to `AccountId` Conversion failed.
AccountIdConversionFailed,
/// `CurrencyId` conversion failed.
CurrencyIdConversionFailed,
}

impl From<Error> for XcmError {
fn from(e: Error) -> Self {
match e {
Error::AssetNotFound => XcmError::FailedToTransactAsset("AssetNotFound"),
Error::AccountIdConversionFailed => XcmError::FailedToTransactAsset("AccountIdConversionFailed"),
Error::CurrencyIdConversionFailed => XcmError::FailedToTransactAsset("CurrencyIdConversionFailed"),
}
}
}

pub struct MultiCurrencyAdapter<MultiCurrency, Matcher, AccountIdConverter, AccountId, CurrencyIdConverter, CurrencyId>(
PhantomData<(
MultiCurrency,
Matcher,
AccountIdConverter,
AccountId,
CurrencyIdConverter,
CurrencyId,
)>,
);

impl<
MultiCurrency: orml_traits::MultiCurrency<AccountId, CurrencyId = CurrencyId>,
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>
{
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(())
}

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()))?;
Ok(asset.clone())
}
}
67 changes: 7 additions & 60 deletions xcm-support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::FullCodec;
use sp_runtime::traits::{CheckedConversion, Convert, MaybeSerializeDeserialize, SaturatedConversion};
use frame_support::{dispatch::DispatchResult, traits::Get};
use sp_runtime::traits::{CheckedConversion, Convert};
use sp_std::{
cmp::{Eq, PartialEq},
collections::btree_set::BTreeSet,
convert::{TryFrom, TryInto},
fmt::Debug,
marker::PhantomData,
prelude::*,
result,
};

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

use frame_support::{dispatch::DispatchResult, log, traits::Get};
pub use currency_adapter::MultiCurrencyAdapter;

mod currency_adapter;

pub trait XcmHandler<AccountId> {
fn execute_xcm(origin: AccountId, xcm: Xcm) -> DispatchResult;
Expand All @@ -25,58 +24,6 @@ pub trait CurrencyIdConversion<CurrencyId> {
fn from_asset(asset: &MultiAsset) -> Option<CurrencyId>;
}

pub struct MultiCurrencyAdapter<MultiCurrency, Matcher, AccountIdConverter, AccountId, CurrencyIdConverter, CurrencyId>(
PhantomData<(
MultiCurrency,
Matcher,
AccountIdConverter,
AccountId,
CurrencyIdConverter,
CurrencyId,
)>,
);

impl<
MultiCurrency: orml_traits::MultiCurrency<AccountId, CurrencyId = CurrencyId>,
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>
{
fn deposit_asset(asset: &MultiAsset, location: &MultiLocation) -> Result {
log::info!("------------------------------------------------");
log::info!(">>> trying deposit. asset: {:?}, location: {:?}", asset, location);
let who = AccountIdConverter::from_location(location).ok_or(())?;
log::info!("who: {:?}", who);
let currency_id = CurrencyIdConverter::from_asset(asset).ok_or(())?;
log::info!("currency_id: {:?}", currency_id);
let amount: MultiCurrency::Balance = Matcher::matches_fungible(&asset).ok_or(())?.saturated_into();
log::info!("amount: {:?}", amount);
MultiCurrency::deposit(currency_id, &who, amount).map_err(|_| ())?;
log::info!(">>> success deposit.");
log::info!("------------------------------------------------");
Ok(())
}

fn withdraw_asset(asset: &MultiAsset, location: &MultiLocation) -> result::Result<MultiAsset, Error> {
log::info!("------------------------------------------------");
log::info!(">>> trying withdraw. asset: {:?}, location: {:?}", asset, location);
let who = AccountIdConverter::from_location(location).ok_or(())?;
log::info!("who: {:?}", who);
let currency_id = CurrencyIdConverter::from_asset(asset).ok_or(())?;
log::info!("currency_id: {:?}", currency_id);
let amount: MultiCurrency::Balance = Matcher::matches_fungible(&asset).ok_or(())?.saturated_into();
log::info!("amount: {:?}", amount);
MultiCurrency::withdraw(currency_id, &who, amount).map_err(|_| ())?;
log::info!(">>> success withdraw.");
log::info!("------------------------------------------------");
Ok(asset.clone())
}
}

pub struct IsConcreteWithGeneralKey<CurrencyId, FromRelayChainBalance>(
PhantomData<(CurrencyId, FromRelayChainBalance)>,
);
Expand Down