Skip to content

Use xcm-handler to execute XCM locally #401

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

Merged
merged 2 commits into from
Mar 14, 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
8 changes: 6 additions & 2 deletions xcm-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ use sp_std::{
result,
};

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

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

pub trait XcmHandler<AccountId> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be in cumulus / polkadot?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no usage of this trait in Cumulus, not sure if it's good to add it there. We can probably open a PR to add it later.

fn execute_xcm(origin: AccountId, xcm: Xcm) -> DispatchResult;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn execute_xcm(origin: AccountId, xcm: Xcm) -> DispatchResult;
fn execute_xcm(origin: &AccountId, xcm: Xcm) -> DispatchResult;

Copy link
Member Author

@shaunxw shaunxw Mar 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The xcm-handler requires AccountId param.

}

pub trait CurrencyIdConversion<CurrencyId> {
fn from_asset(asset: &MultiAsset) -> Option<CurrencyId>;
Expand Down
4 changes: 3 additions & 1 deletion xtokens/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ frame-system = { git = "https://github.com/paritytech/substrate", branch = "roco
cumulus-primitives-core = { git = "https://github.com/paritytech/cumulus", branch = "rococo-v1", default-features = false }

xcm = { git = "https://github.com/paritytech/polkadot", branch = "rococo-v1", default-features = false }
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "rococo-v1", default-features = false }

orml-xcm-support = { path = "../xcm-support", default-features = false }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "rococo-v1" }
Expand All @@ -39,4 +40,5 @@ std = [
"frame-system/std",
"cumulus-primitives-core/std",
"xcm/std",
"orml-xcm-support/std",
]
68 changes: 21 additions & 47 deletions xtokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ pub mod module {
use sp_std::prelude::*;

use cumulus_primitives_core::{relay_chain::Balance as RelayChainBalance, ParaId};
use xcm::v0::{Error as XcmError, ExecuteXcm, Junction, MultiAsset, MultiLocation, NetworkId, Order, Xcm};
use xcm_executor::traits::LocationConversion;
use xcm::v0::{Junction, MultiAsset, MultiLocation, NetworkId, Order, Xcm};

use orml_xcm_support::XcmHandler;

#[derive(Encode, Decode, Eq, PartialEq, Clone, Copy, RuntimeDebug)]
/// Identity of chain.
Expand Down Expand Up @@ -64,19 +65,21 @@ pub mod module {
+ MaybeSerializeDeserialize
+ Into<u128>;

/// Convertor `Balance` to `RelayChainBalance`.
/// Convert `Balance` to `RelayChainBalance`.
type ToRelayChainBalance: Convert<Self::Balance, RelayChainBalance>;

/// Convert `Self::Account` to `AccountId32`
type AccountId32Convert: Convert<Self::AccountId, [u8; 32]>;

/// The network id of relay chain. Typically `NetworkId::Polkadot` or
/// `NetworkId::Kusama`.
type RelayChainNetworkId: Get<NetworkId>;

/// Parachain ID.
/// Self parachain ID.
type ParaId: Get<ParaId>;

type AccountIdConverter: LocationConversion<Self::AccountId>;

type XcmExecutor: ExecuteXcm;
/// Xcm handler to execute XCM.
type XcmHandler: XcmHandler<Self::AccountId>;
}

#[pallet::event]
Expand All @@ -85,23 +88,13 @@ pub mod module {
/// Transferred to relay chain. \[src, dest, amount\]
TransferredToRelayChain(T::AccountId, T::AccountId, T::Balance),

/// Transfer to relay chain failed. \[src, dest, amount, error\]
TransferToRelayChainFailed(T::AccountId, T::AccountId, T::Balance, XcmError),

/// Transferred to parachain. \[x_currency_id, src, para_id, dest,
/// dest_network, amount\]
TransferredToParachain(XCurrencyId, T::AccountId, ParaId, MultiLocation, T::Balance),

/// Transfer to parachain failed. \[x_currency_id, src, para_id, dest,
/// dest_network, amount, error\]
TransferToParachainFailed(XCurrencyId, T::AccountId, ParaId, MultiLocation, T::Balance, XcmError),
}

#[pallet::error]
pub enum Error<T> {
/// Bad location.
BadLocation,
}
pub enum Error<T> {}

#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}
Expand Down Expand Up @@ -138,15 +131,9 @@ pub mod module {
}],
}],
};
T::XcmHandler::execute_xcm(who.clone(), xcm)?;

let xcm_origin =
T::AccountIdConverter::try_into_location(who.clone()).map_err(|_| Error::<T>::BadLocation)?;
// TODO: revert state on xcm execution failure.
match T::XcmExecutor::execute_xcm(xcm_origin, xcm) {
Ok(_) => Self::deposit_event(Event::<T>::TransferredToRelayChain(who, dest, amount)),
Err(err) => Self::deposit_event(Event::<T>::TransferToRelayChainFailed(who, dest, amount, err)),
}

Self::deposit_event(Event::<T>::TransferredToRelayChain(who, dest, amount));
Ok(().into())
}

Expand Down Expand Up @@ -182,28 +169,15 @@ pub mod module {
}
}
};
T::XcmHandler::execute_xcm(who.clone(), xcm)?;

let xcm_origin =
T::AccountIdConverter::try_into_location(who.clone()).map_err(|_| Error::<T>::BadLocation)?;
// TODO: revert state on xcm execution failure.
match T::XcmExecutor::execute_xcm(xcm_origin, xcm) {
Ok(_) => Self::deposit_event(Event::<T>::TransferredToParachain(
x_currency_id,
who,
para_id,
dest,
amount,
)),
Err(err) => Self::deposit_event(Event::<T>::TransferToParachainFailed(
x_currency_id,
who,
para_id,
dest,
amount,
err,
)),
}

Self::deposit_event(Event::<T>::TransferredToParachain(
x_currency_id,
who,
para_id,
dest,
amount,
));
Ok(().into())
}
}
Expand Down