Skip to content

Commit 1095b9c

Browse files
authored
Fix sibling cross-chain transfer, enable all orml-xtokens unit tests (#558)
* Fix transfer with siblings. * Update mocks; fix all unit tests. * Fix typo. * Remove unused param types.
1 parent 8b11c6e commit 1095b9c

File tree

4 files changed

+231
-183
lines changed

4 files changed

+231
-183
lines changed

xtokens/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub mod module {
208208
dest: MultiLocation,
209209
dest_weight: Weight,
210210
) -> XcmExecutionResult {
211-
let (transfer_kind, reserve, dest, recipient) = Self::transfer_kind(&asset, &dest)?;
211+
let (transfer_kind, dest, reserve, recipient) = Self::transfer_kind(&asset, &dest)?;
212212
let buy_order = BuyExecution {
213213
fees: All,
214214
// Zero weight for additional XCM (since there are none to execute)

xtokens/src/mock/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ decl_test_parachain! {
9090
}
9191
}
9292

93+
decl_test_parachain! {
94+
pub struct ParaC {
95+
Runtime = para::Runtime,
96+
new_ext = para_ext(3),
97+
}
98+
}
99+
93100
decl_test_relay_chain! {
94101
pub struct Relay {
95102
Runtime = relay::Runtime,
@@ -104,6 +111,7 @@ decl_test_network! {
104111
parachains = vec![
105112
(1, ParaA),
106113
(2, ParaB),
114+
(3, ParaC),
107115
],
108116
}
109117
}

xtokens/src/mock/para.rs

+47-14
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@ use sp_runtime::{
1717
use cumulus_primitives_core::{ChannelStatus, GetChannelInfo, ParaId};
1818
use pallet_xcm::XcmPassthrough;
1919
use polkadot_parachain::primitives::Sibling;
20-
pub use xcm::v0::{
20+
use xcm::v0::{
21+
Error as XcmError,
2122
Junction::{self, Parachain, Parent},
2223
MultiAsset,
23-
MultiLocation::{self, X1, X2, X3},
24+
MultiLocation::{self, X1, X2},
2425
NetworkId, Xcm,
2526
};
26-
pub use xcm_builder::{
27-
AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom,
28-
CurrencyAdapter as XcmCurrencyAdapter, EnsureXcmOrigin, FixedRateOfConcreteFungible, FixedWeightBounds, IsConcrete,
29-
LocationInverter, NativeAsset, ParentAsSuperuser, ParentIsDefault, RelayChainAsNative, SiblingParachainAsNative,
30-
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,
31-
TakeWeightCredit,
27+
use xcm_builder::{
28+
AccountId32Aliases, AllowTopLevelPaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds, LocationInverter,
29+
ParentIsDefault, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
30+
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
3231
};
33-
use xcm_executor::{Config, XcmExecutor};
32+
use xcm_executor::{traits::WeightTrader, Assets, Config, XcmExecutor};
3433

3534
use orml_traits::parameter_type_with_key;
36-
use orml_xcm_support::{IsNativeConcrete, MultiCurrencyAdapter};
35+
use orml_xcm_support::{IsNativeConcrete, MultiCurrencyAdapter, MultiNativeAsset};
3736

3837
pub type AccountId = AccountId32;
3938

@@ -132,7 +131,6 @@ pub type XcmOriginToCallOrigin = (
132131

133132
parameter_types! {
134133
pub const UnitWeightCost: Weight = 10;
135-
pub KsmPerSecond: (MultiLocation, u128) = (X1(Parent), 1_000);
136134
}
137135

138136
pub type LocalAssetTransactor = MultiCurrencyAdapter<
@@ -146,20 +144,55 @@ pub type LocalAssetTransactor = MultiCurrencyAdapter<
146144
>;
147145

148146
pub type XcmRouter = ParachainXcmRouter<ParachainInfo>;
149-
pub type Barrier = AllowUnpaidExecutionFrom<All<MultiLocation>>;
147+
pub type Barrier = (TakeWeightCredit, AllowTopLevelPaidExecutionFrom<All<MultiLocation>>);
148+
149+
/// A trader who believes all tokens are created equal to "weight" of any chain,
150+
/// which is not true, but good enough to mock the fee payment of XCM execution.
151+
///
152+
/// This mock will always trade `n` amount of weight to `n` amount of tokens.
153+
pub struct AllTokensAreCreatedEqualToWeight(MultiLocation);
154+
impl WeightTrader for AllTokensAreCreatedEqualToWeight {
155+
fn new() -> Self {
156+
Self(MultiLocation::Null)
157+
}
158+
159+
fn buy_weight(&mut self, weight: Weight, payment: Assets) -> Result<Assets, XcmError> {
160+
let asset_id = payment
161+
.fungible
162+
.iter()
163+
.next()
164+
.expect("Payment must be something; qed")
165+
.0;
166+
let required = asset_id.clone().into_fungible_multiasset(weight as u128);
167+
168+
if let MultiAsset::ConcreteFungible { ref id, amount: _ } = required {
169+
self.0 = id.clone();
170+
}
171+
172+
let (unused, _) = payment.less(required).map_err(|_| XcmError::TooExpensive)?;
173+
Ok(unused)
174+
}
175+
176+
fn refund_weight(&mut self, weight: Weight) -> MultiAsset {
177+
MultiAsset::ConcreteFungible {
178+
id: self.0.clone(),
179+
amount: weight as u128,
180+
}
181+
}
182+
}
150183

151184
pub struct XcmConfig;
152185
impl Config for XcmConfig {
153186
type Call = Call;
154187
type XcmSender = XcmRouter;
155188
type AssetTransactor = LocalAssetTransactor;
156189
type OriginConverter = XcmOriginToCallOrigin;
157-
type IsReserve = NativeAsset;
190+
type IsReserve = MultiNativeAsset;
158191
type IsTeleporter = ();
159192
type LocationInverter = LocationInverter<Ancestry>;
160193
type Barrier = Barrier;
161194
type Weigher = FixedWeightBounds<UnitWeightCost, Call>;
162-
type Trader = FixedRateOfConcreteFungible<KsmPerSecond, ()>;
195+
type Trader = AllTokensAreCreatedEqualToWeight;
163196
type ResponseHandler = ();
164197
}
165198

0 commit comments

Comments
 (0)