Skip to content

Fix sibling cross-chain transfer, enable all orml-xtokens unit tests #558

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 4 commits into from
Jul 19, 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
2 changes: 1 addition & 1 deletion xtokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub mod module {
dest: MultiLocation,
dest_weight: Weight,
) -> XcmExecutionResult {
let (transfer_kind, reserve, dest, recipient) = Self::transfer_kind(&asset, &dest)?;
let (transfer_kind, dest, reserve, recipient) = Self::transfer_kind(&asset, &dest)?;
let buy_order = BuyExecution {
fees: All,
// Zero weight for additional XCM (since there are none to execute)
Expand Down
8 changes: 8 additions & 0 deletions xtokens/src/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ decl_test_parachain! {
}
}

decl_test_parachain! {
pub struct ParaC {
Runtime = para::Runtime,
new_ext = para_ext(3),
}
}

decl_test_relay_chain! {
pub struct Relay {
Runtime = relay::Runtime,
Expand All @@ -104,6 +111,7 @@ decl_test_network! {
parachains = vec![
(1, ParaA),
(2, ParaB),
(3, ParaC),
],
}
}
Expand Down
61 changes: 47 additions & 14 deletions xtokens/src/mock/para.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@ use sp_runtime::{
use cumulus_primitives_core::{ChannelStatus, GetChannelInfo, ParaId};
use pallet_xcm::XcmPassthrough;
use polkadot_parachain::primitives::Sibling;
pub use xcm::v0::{
use xcm::v0::{
Error as XcmError,
Junction::{self, Parachain, Parent},
MultiAsset,
MultiLocation::{self, X1, X2, X3},
MultiLocation::{self, X1, X2},
NetworkId, Xcm,
};
pub use xcm_builder::{
AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom,
CurrencyAdapter as XcmCurrencyAdapter, EnsureXcmOrigin, FixedRateOfConcreteFungible, FixedWeightBounds, IsConcrete,
LocationInverter, NativeAsset, ParentAsSuperuser, ParentIsDefault, RelayChainAsNative, SiblingParachainAsNative,
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,
TakeWeightCredit,
use xcm_builder::{
AccountId32Aliases, AllowTopLevelPaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds, LocationInverter,
ParentIsDefault, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
};
use xcm_executor::{Config, XcmExecutor};
use xcm_executor::{traits::WeightTrader, Assets, Config, XcmExecutor};

use orml_traits::parameter_type_with_key;
use orml_xcm_support::{IsNativeConcrete, MultiCurrencyAdapter};
use orml_xcm_support::{IsNativeConcrete, MultiCurrencyAdapter, MultiNativeAsset};

pub type AccountId = AccountId32;

Expand Down Expand Up @@ -132,7 +131,6 @@ pub type XcmOriginToCallOrigin = (

parameter_types! {
pub const UnitWeightCost: Weight = 10;
pub KsmPerSecond: (MultiLocation, u128) = (X1(Parent), 1_000);
}

pub type LocalAssetTransactor = MultiCurrencyAdapter<
Expand All @@ -146,20 +144,55 @@ pub type LocalAssetTransactor = MultiCurrencyAdapter<
>;

pub type XcmRouter = ParachainXcmRouter<ParachainInfo>;
pub type Barrier = AllowUnpaidExecutionFrom<All<MultiLocation>>;
pub type Barrier = (TakeWeightCredit, AllowTopLevelPaidExecutionFrom<All<MultiLocation>>);

/// A trader who believes all tokens are created equal to "weight" of any chain,
/// which is not true, but good enough to mock the fee payment of XCM execution.
///
/// This mock will always trade `n` amount of weight to `n` amount of tokens.
pub struct AllTokensAreCreatedEqualToWeight(MultiLocation);
impl WeightTrader for AllTokensAreCreatedEqualToWeight {
fn new() -> Self {
Self(MultiLocation::Null)
}

fn buy_weight(&mut self, weight: Weight, payment: Assets) -> Result<Assets, XcmError> {
let asset_id = payment
.fungible
.iter()
.next()
.expect("Payment must be something; qed")
.0;
let required = asset_id.clone().into_fungible_multiasset(weight as u128);

if let MultiAsset::ConcreteFungible { ref id, amount: _ } = required {
self.0 = id.clone();
}

let (unused, _) = payment.less(required).map_err(|_| XcmError::TooExpensive)?;
Ok(unused)
}

fn refund_weight(&mut self, weight: Weight) -> MultiAsset {
MultiAsset::ConcreteFungible {
id: self.0.clone(),
amount: weight as u128,
}
}
}

pub struct XcmConfig;
impl Config for XcmConfig {
type Call = Call;
type XcmSender = XcmRouter;
type AssetTransactor = LocalAssetTransactor;
type OriginConverter = XcmOriginToCallOrigin;
type IsReserve = NativeAsset;
type IsReserve = MultiNativeAsset;
type IsTeleporter = ();
type LocationInverter = LocationInverter<Ancestry>;
type Barrier = Barrier;
type Weigher = FixedWeightBounds<UnitWeightCost, Call>;
type Trader = FixedRateOfConcreteFungible<KsmPerSecond, ()>;
type Trader = AllTokensAreCreatedEqualToWeight;
type ResponseHandler = ();
}

Expand Down
Loading