-
Notifications
You must be signed in to change notification settings - Fork 789
/
Copy pathlib.rs
756 lines (673 loc) · 24.7 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
//! Helper datatypes for cumulus. This includes the [`ParentAsUmp`] routing type which will route
//! messages into an [`UpwardMessageSender`] if the destination is `Parent`.
#![cfg_attr(not(feature = "std"), no_std)]
use codec::Encode;
use cumulus_primitives_core::{MessageSendError, UpwardMessageSender};
use frame_support::{
traits::{
tokens::{
fungible,
fungible::Credit as FungibleCredit,
fungibles,
fungibles::{Credit as FungiblesCredit, Inspect},
},
Get, Imbalance, OnUnbalanced as OnUnbalancedT,
},
weights::{Weight, WeightToFee as WeightToFeeT},
};
use pallet_asset_conversion::{
MultiAssetIdConverter as SwapAssetMatcherT, SwapCredit as SwapCreditT,
};
use polkadot_runtime_common::xcm_sender::ConstantPrice;
use sp_runtime::{
traits::{Saturating, Zero},
SaturatedConversion,
};
use sp_std::{marker::PhantomData, prelude::*};
use xcm::{latest::prelude::*, WrapVersion};
use xcm_builder::TakeRevenue;
use xcm_executor::traits::{MatchesFungibles, TransactAsset, WeightTrader};
pub trait PriceForParentDelivery {
fn price_for_parent_delivery(message: &Xcm<()>) -> MultiAssets;
}
impl PriceForParentDelivery for () {
fn price_for_parent_delivery(_: &Xcm<()>) -> MultiAssets {
MultiAssets::new()
}
}
impl<T: Get<MultiAssets>> PriceForParentDelivery for ConstantPrice<T> {
fn price_for_parent_delivery(_: &Xcm<()>) -> MultiAssets {
T::get()
}
}
/// Xcm router which recognises the `Parent` destination and handles it by sending the message into
/// the given UMP `UpwardMessageSender` implementation. Thus this essentially adapts an
/// `UpwardMessageSender` trait impl into a `SendXcm` trait impl.
///
/// NOTE: This is a pretty dumb "just send it" router; we will probably want to introduce queuing
/// to UMP eventually and when we do, the pallet which implements the queuing will be responsible
/// for the `SendXcm` implementation.
pub struct ParentAsUmp<T, W, P>(PhantomData<(T, W, P)>);
impl<T, W, P> SendXcm for ParentAsUmp<T, W, P>
where
T: UpwardMessageSender,
W: WrapVersion,
P: PriceForParentDelivery,
{
type Ticket = Vec<u8>;
fn validate(
dest: &mut Option<MultiLocation>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<Vec<u8>> {
let d = dest.take().ok_or(SendError::MissingArgument)?;
if d.contains_parents_only(1) {
// An upward message for the relay chain.
let xcm = msg.take().ok_or(SendError::MissingArgument)?;
let price = P::price_for_parent_delivery(&xcm);
let versioned_xcm =
W::wrap_version(&d, xcm).map_err(|()| SendError::DestinationUnsupported)?;
let data = versioned_xcm.encode();
Ok((data, price))
} else {
// Anything else is unhandled. This includes a message that is not meant for us.
// We need to make sure that dest/msg is not consumed here.
*dest = Some(d);
Err(SendError::NotApplicable)
}
}
fn deliver(data: Vec<u8>) -> Result<XcmHash, SendError> {
let (_, hash) = T::send_upward_message(data).map_err(|e| match e {
MessageSendError::TooBig => SendError::ExceedsMaxMessageSize,
e => SendError::Transport(e.into()),
})?;
Ok(hash)
}
}
/// Contains information to handle refund/payment for xcm-execution
#[derive(Clone, Eq, PartialEq, Debug)]
struct AssetTraderRefunder {
// The amount of weight bought minus the weigh already refunded
weight_outstanding: Weight,
// The concrete asset containing the asset location and outstanding balance
outstanding_concrete_asset: MultiAsset,
}
/// Charges for execution in the first multiasset of those selected for fee payment
/// Only succeeds for Concrete Fungible Assets
/// First tries to convert the this MultiAsset into a local assetId
/// Then charges for this assetId as described by FeeCharger
/// Weight, paid balance, local asset Id and the multilocation is stored for
/// later refund purposes
/// Important: Errors if the Trader is being called twice by 2 BuyExecution instructions
/// Alternatively we could just return payment in the aforementioned case
pub struct TakeFirstAssetTrader<
AccountId,
FeeCharger: ChargeWeightInFungibles<AccountId, ConcreteAssets>,
Matcher: MatchesFungibles<ConcreteAssets::AssetId, ConcreteAssets::Balance>,
ConcreteAssets: fungibles::Mutate<AccountId> + fungibles::Balanced<AccountId>,
HandleRefund: TakeRevenue,
>(
Option<AssetTraderRefunder>,
PhantomData<(AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund)>,
);
impl<
AccountId,
FeeCharger: ChargeWeightInFungibles<AccountId, ConcreteAssets>,
Matcher: MatchesFungibles<ConcreteAssets::AssetId, ConcreteAssets::Balance>,
ConcreteAssets: fungibles::Mutate<AccountId> + fungibles::Balanced<AccountId>,
HandleRefund: TakeRevenue,
> WeightTrader
for TakeFirstAssetTrader<AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund>
{
fn new() -> Self {
Self(None, PhantomData)
}
// We take first multiasset
// Check whether we can convert fee to asset_fee (is_sufficient, min_deposit)
// If everything goes well, we charge.
fn buy_weight(
&mut self,
weight: Weight,
payment: xcm_executor::Assets,
context: &XcmContext,
) -> Result<xcm_executor::Assets, XcmError> {
log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::buy_weight weight: {:?}, payment: {:?}, context: {:?}", weight, payment, context);
// Make sure we dont enter twice
if self.0.is_some() {
return Err(XcmError::NotWithdrawable)
}
// We take the very first multiasset from payment
// (assets are sorted by fungibility/amount after this conversion)
let multiassets: MultiAssets = payment.clone().into();
// Take the first multiasset from the selected MultiAssets
let first = multiassets.get(0).ok_or(XcmError::AssetNotFound)?;
// Get the local asset id in which we can pay for fees
let (local_asset_id, _) =
Matcher::matches_fungibles(first).map_err(|_| XcmError::AssetNotFound)?;
// Calculate how much we should charge in the asset_id for such amount of weight
// Require at least a payment of minimum_balance
// Necessary for fully collateral-backed assets
let asset_balance: u128 =
FeeCharger::charge_weight_in_fungibles(local_asset_id.clone(), weight)
.map(|amount| {
let minimum_balance = ConcreteAssets::minimum_balance(local_asset_id);
if amount < minimum_balance {
minimum_balance
} else {
amount
}
})?
.try_into()
.map_err(|_| XcmError::Overflow)?;
// Convert to the same kind of multiasset, with the required fungible balance
let required = first.id.into_multiasset(asset_balance.into());
// Substract payment
let unused = payment.checked_sub(required.clone()).map_err(|_| XcmError::TooExpensive)?;
// record weight and multiasset
self.0 = Some(AssetTraderRefunder {
weight_outstanding: weight,
outstanding_concrete_asset: required,
});
Ok(unused)
}
fn refund_weight(&mut self, weight: Weight, context: &XcmContext) -> Option<MultiAsset> {
log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::refund_weight weight: {:?}, context: {:?}", weight, context);
if let Some(AssetTraderRefunder {
mut weight_outstanding,
outstanding_concrete_asset: MultiAsset { id, fun },
}) = self.0.clone()
{
// Get the local asset id in which we can refund fees
let (local_asset_id, outstanding_balance) =
Matcher::matches_fungibles(&(id, fun).into()).ok()?;
let minimum_balance = ConcreteAssets::minimum_balance(local_asset_id.clone());
// Calculate asset_balance
// This read should have already be cached in buy_weight
let (asset_balance, outstanding_minus_substracted) =
FeeCharger::charge_weight_in_fungibles(local_asset_id, weight).ok().map(
|asset_balance| {
// Require at least a drop of minimum_balance
// Necessary for fully collateral-backed assets
if outstanding_balance.saturating_sub(asset_balance) > minimum_balance {
(asset_balance, outstanding_balance.saturating_sub(asset_balance))
}
// If the amount to be refunded leaves the remaining balance below ED,
// we just refund the exact amount that guarantees at least ED will be
// dropped
else {
(outstanding_balance.saturating_sub(minimum_balance), minimum_balance)
}
},
)?;
// Convert balances into u128
let outstanding_minus_substracted: u128 =
outstanding_minus_substracted.saturated_into();
let asset_balance: u128 = asset_balance.saturated_into();
// Construct outstanding_concrete_asset with the same location id and substracted
// balance
let outstanding_concrete_asset: MultiAsset = (id, outstanding_minus_substracted).into();
// Substract from existing weight and balance
weight_outstanding = weight_outstanding.saturating_sub(weight);
// Override AssetTraderRefunder
self.0 = Some(AssetTraderRefunder { weight_outstanding, outstanding_concrete_asset });
// Only refund if positive
if asset_balance > 0 {
Some((id, asset_balance).into())
} else {
None
}
} else {
None
}
}
}
impl<
AccountId,
FeeCharger: ChargeWeightInFungibles<AccountId, ConcreteAssets>,
Matcher: MatchesFungibles<ConcreteAssets::AssetId, ConcreteAssets::Balance>,
ConcreteAssets: fungibles::Mutate<AccountId> + fungibles::Balanced<AccountId>,
HandleRefund: TakeRevenue,
> Drop for TakeFirstAssetTrader<AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund>
{
fn drop(&mut self) {
if let Some(asset_trader) = self.0.clone() {
HandleRefund::take_revenue(asset_trader.outstanding_concrete_asset);
}
}
}
/// XCM fee depositor to which we implement the TakeRevenue trait
/// It receives a Transact implemented argument, a 32 byte convertible acocuntId, and the fee
/// receiver account FungiblesMutateAdapter should be identical to that implemented by WithdrawAsset
pub struct XcmFeesTo32ByteAccount<FungiblesMutateAdapter, AccountId, ReceiverAccount>(
PhantomData<(FungiblesMutateAdapter, AccountId, ReceiverAccount)>,
);
impl<
FungiblesMutateAdapter: TransactAsset,
AccountId: Clone + Into<[u8; 32]>,
ReceiverAccount: frame_support::traits::Get<Option<AccountId>>,
> TakeRevenue for XcmFeesTo32ByteAccount<FungiblesMutateAdapter, AccountId, ReceiverAccount>
{
fn take_revenue(revenue: MultiAsset) {
if let Some(receiver) = ReceiverAccount::get() {
let ok = FungiblesMutateAdapter::deposit_asset(
&revenue,
&(X1(AccountId32 { network: None, id: receiver.into() }).into()),
// We aren't able to track the XCM that initiated the fee deposit, so we create a
// fake message hash here
&XcmContext::with_message_id([0; 32]),
)
.is_ok();
debug_assert!(ok, "`deposit_asset` cannot generally fail; qed");
}
}
}
/// ChargeWeightInFungibles trait, which converts a given amount of weight
/// and an assetId, and it returns the balance amount that should be charged
/// in such assetId for that amount of weight
pub trait ChargeWeightInFungibles<AccountId, Assets: fungibles::Inspect<AccountId>> {
fn charge_weight_in_fungibles(
asset_id: <Assets as Inspect<AccountId>>::AssetId,
weight: Weight,
) -> Result<<Assets as Inspect<AccountId>>::Balance, XcmError>;
}
/// Provides an implementation of [`WeightTrader`] to charge for weight using the first asset
/// specified in the `payment` argument.
///
/// Note: The asset utilized for weight must be non-native and must be exchangeable for a native
/// asset through `SwapCredit`.
// TODO: decouple implementation from the native asset concept, allowing the `TargetAsset`
// argument as an exchange asset. Refer to /~https://github.com/paritytech/polkadot-sdk/issues/1842.
pub struct SwapFirstAssetTrader<
AccountId,
SwapCredit: SwapCreditT<AccountId>,
SwapAssetMatcher: SwapAssetMatcherT<SwapCredit::MultiAssetId, Fungibles::AssetId>,
WeightToFee: WeightToFeeT<Balance = SwapCredit::Balance>,
Fungibles: fungibles::Balanced<AccountId, Balance = SwapCredit::Balance>,
FungiblesAssetMatcher: MatchesFungibles<Fungibles::AssetId, Fungibles::Balance>,
Fungible: fungible::Balanced<AccountId, Balance = SwapCredit::Balance>,
OnUnbalanced: OnUnbalancedT<FungibleCredit<AccountId, Fungible>>,
> where
SwapCredit::MultiAssetId: From<Fungibles::AssetId>,
SwapCredit::Credit: From<FungiblesCredit<AccountId, Fungibles>>
+ From<FungibleCredit<AccountId, Fungible>>
+ TryInto<FungibleCredit<AccountId, Fungible>>
+ TryInto<FungiblesCredit<AccountId, Fungibles>>,
Fungible::Balance: Into<u128>,
Fungibles::Balance: Into<u128>,
{
/// Accumulated fee paid for XCM execution.
total_fee: FungibleCredit<AccountId, Fungible>,
/// Last asset utilized by a client to settle a fee.
last_fee_asset: Option<AssetId>,
_phantom_data: PhantomData<(
AccountId,
SwapCredit,
SwapAssetMatcher,
WeightToFee,
Fungibles,
FungiblesAssetMatcher,
Fungible,
OnUnbalanced,
)>,
}
impl<
AccountId,
SwapCredit: SwapCreditT<AccountId>,
SwapAssetMatcher: SwapAssetMatcherT<SwapCredit::MultiAssetId, Fungibles::AssetId>,
WeightToFee: WeightToFeeT<Balance = SwapCredit::Balance>,
Fungibles: fungibles::Balanced<AccountId, Balance = SwapCredit::Balance>,
FungiblesAssetMatcher: MatchesFungibles<Fungibles::AssetId, Fungibles::Balance>,
Fungible: fungible::Balanced<AccountId, Balance = SwapCredit::Balance>,
OnUnbalanced: OnUnbalancedT<FungibleCredit<AccountId, Fungible>>,
> WeightTrader
for SwapFirstAssetTrader<
AccountId,
SwapCredit,
SwapAssetMatcher,
WeightToFee,
Fungibles,
FungiblesAssetMatcher,
Fungible,
OnUnbalanced,
> where
SwapCredit::MultiAssetId: From<Fungibles::AssetId>,
SwapCredit::Credit: From<FungiblesCredit<AccountId, Fungibles>>
+ From<FungibleCredit<AccountId, Fungible>>
+ TryInto<FungibleCredit<AccountId, Fungible>>
+ TryInto<FungiblesCredit<AccountId, Fungibles>>,
Fungible::Balance: Into<u128>,
Fungibles::Balance: Into<u128>,
{
fn new() -> Self {
Self {
total_fee: FungibleCredit::<AccountId, Fungible>::zero(),
last_fee_asset: None,
_phantom_data: PhantomData,
}
}
fn buy_weight(
&mut self,
weight: Weight,
mut payment: xcm_executor::Assets,
_context: &XcmContext,
) -> Result<xcm_executor::Assets, XcmError> {
let first_asset: MultiAsset =
payment.fungible.pop_first().ok_or(XcmError::AssetNotFound)?.into();
let (fungibles_asset, balance) = FungiblesAssetMatcher::matches_fungibles(&first_asset)
.map_err(|_| XcmError::FeesNotMet)?;
let swap_asset = fungibles_asset.clone().into();
if SwapAssetMatcher::is_native(&swap_asset) {
return Err(XcmError::FeesNotMet)
}
let credit_in = Fungibles::issue(fungibles_asset, balance);
let fee = WeightToFee::weight_to_fee(&weight);
let (credit_out, credit_change) = SwapCredit::swap_tokens_for_exact_tokens(
vec![swap_asset, SwapAssetMatcher::get_native()],
credit_in.into(),
fee,
)
.map_err(|(credit_in, _)| {
drop(credit_in);
XcmError::FeesNotMet
})?;
// the execution below must be infallible to keep this operation transactional.
self.total_fee.subsume(
// should never fail since `credit_out` is `FungibleCredit`.
// TODO: make this infallible. Refer to /~https://github.com/paritytech/polkadot-sdk/issues/1842.
credit_out.try_into().map_err(|_| XcmError::FeesNotMet)?,
);
self.last_fee_asset = Some(first_asset.id);
let credit_change: FungiblesCredit<AccountId, Fungibles> =
// should never fail since `credit_change` is `FungiblesCredit`.
// TODO: make this infallible. Refer to /~https://github.com/paritytech/polkadot-sdk/issues/1842.
credit_change.try_into().map_err(|_| XcmError::FeesNotMet)?;
payment.fungible.insert(first_asset.id, credit_change.peek().into());
drop(credit_change);
Ok(payment)
}
fn refund_weight(&mut self, weight: Weight, _context: &XcmContext) -> Option<MultiAsset> {
if self.total_fee.peek().is_zero() {
return None
}
let mut refund_asset =
if let Some(asset) = &self.last_fee_asset { (*asset, 0).into() } else { return None };
let refund_amount = WeightToFee::weight_to_fee(&weight);
if refund_amount >= self.total_fee.peek() {
return None
}
let refund_swap_asset = FungiblesAssetMatcher::matches_fungibles(&refund_asset)
.map(|(a, _)| a.into())
.ok()?;
let refund = self.total_fee.extract(refund_amount);
let refund: FungibleCredit<AccountId, Fungible> =
match SwapCredit::swap_exact_tokens_for_tokens(
vec![SwapAssetMatcher::get_native(), refund_swap_asset],
refund.into(),
None,
) {
Ok(r) => r.try_into().ok()?,
Err((c, _)) => {
self.total_fee.subsume(
// should never fail since `credit_in` is `FungibleCredit`.
// TODO: make this infallible. Refer to /~https://github.com/paritytech/polkadot-sdk/issues/1842.
c.try_into().ok()?,
);
return None
},
};
// the execution below must be infallible to keep this operation atomic.
refund_asset.fun = refund.peek().into().into();
drop(refund);
Some(refund_asset)
}
}
impl<
AccountId,
SwapCredit: SwapCreditT<AccountId>,
SwapAssetMatcher: SwapAssetMatcherT<SwapCredit::MultiAssetId, Fungibles::AssetId>,
WeightToFee: WeightToFeeT<Balance = SwapCredit::Balance>,
Fungibles: fungibles::Balanced<AccountId, Balance = SwapCredit::Balance>,
FungiblesAssetMatcher: MatchesFungibles<Fungibles::AssetId, Fungibles::Balance>,
Fungible: fungible::Balanced<AccountId, Balance = SwapCredit::Balance>,
OnUnbalanced: OnUnbalancedT<FungibleCredit<AccountId, Fungible>>,
> Drop
for SwapFirstAssetTrader<
AccountId,
SwapCredit,
SwapAssetMatcher,
WeightToFee,
Fungibles,
FungiblesAssetMatcher,
Fungible,
OnUnbalanced,
> where
SwapCredit::MultiAssetId: From<Fungibles::AssetId>,
SwapCredit::Credit: From<FungiblesCredit<AccountId, Fungibles>>
+ From<FungibleCredit<AccountId, Fungible>>
+ TryInto<FungibleCredit<AccountId, Fungible>>
+ TryInto<FungiblesCredit<AccountId, Fungibles>>,
Fungible::Balance: Into<u128>,
Fungibles::Balance: Into<u128>,
{
fn drop(&mut self) {
if self.total_fee.peek().is_zero() {
return
}
let total_fee = self.total_fee.extract(self.total_fee.peek());
OnUnbalanced::on_unbalanced(total_fee);
}
}
#[cfg(test)]
mod tests {
use super::*;
use cumulus_primitives_core::UpwardMessage;
use frame_support::{
assert_ok,
traits::tokens::{
DepositConsequence, Fortitude, Preservation, Provenance, WithdrawConsequence,
},
};
use sp_runtime::DispatchError;
use xcm_executor::{traits::Error, Assets};
/// Validates [`validate`] for required Some(destination) and Some(message)
struct OkFixedXcmHashWithAssertingRequiredInputsSender;
impl OkFixedXcmHashWithAssertingRequiredInputsSender {
const FIXED_XCM_HASH: [u8; 32] = [9; 32];
fn fixed_delivery_asset() -> MultiAssets {
MultiAssets::new()
}
fn expected_delivery_result() -> Result<(XcmHash, MultiAssets), SendError> {
Ok((Self::FIXED_XCM_HASH, Self::fixed_delivery_asset()))
}
}
impl SendXcm for OkFixedXcmHashWithAssertingRequiredInputsSender {
type Ticket = ();
fn validate(
destination: &mut Option<MultiLocation>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
assert!(destination.is_some());
assert!(message.is_some());
Ok(((), OkFixedXcmHashWithAssertingRequiredInputsSender::fixed_delivery_asset()))
}
fn deliver(_: Self::Ticket) -> Result<XcmHash, SendError> {
Ok(Self::FIXED_XCM_HASH)
}
}
/// Impl [`UpwardMessageSender`] that return `Other` error
struct OtherErrorUpwardMessageSender;
impl UpwardMessageSender for OtherErrorUpwardMessageSender {
fn send_upward_message(_: UpwardMessage) -> Result<(u32, XcmHash), MessageSendError> {
Err(MessageSendError::Other)
}
}
#[test]
fn parent_as_ump_does_not_consume_dest_or_msg_on_not_applicable() {
// dummy message
let message = Xcm(vec![Trap(5)]);
// ParentAsUmp - check dest is really not applicable
let dest = (Parent, Parent, Parent);
let mut dest_wrapper = Some(dest.into());
let mut msg_wrapper = Some(message.clone());
assert_eq!(
Err(SendError::NotApplicable),
<ParentAsUmp<(), (), ()> as SendXcm>::validate(&mut dest_wrapper, &mut msg_wrapper)
);
// check wrapper were not consumed
assert_eq!(Some(dest.into()), dest_wrapper.take());
assert_eq!(Some(message.clone()), msg_wrapper.take());
// another try with router chain with asserting sender
assert_eq!(
OkFixedXcmHashWithAssertingRequiredInputsSender::expected_delivery_result(),
send_xcm::<(ParentAsUmp<(), (), ()>, OkFixedXcmHashWithAssertingRequiredInputsSender)>(
dest.into(),
message
)
);
}
#[test]
fn parent_as_ump_consumes_dest_and_msg_on_ok_validate() {
// dummy message
let message = Xcm(vec![Trap(5)]);
// ParentAsUmp - check dest/msg is valid
let dest = (Parent, Here);
let mut dest_wrapper = Some(dest.into());
let mut msg_wrapper = Some(message.clone());
assert!(<ParentAsUmp<(), (), ()> as SendXcm>::validate(
&mut dest_wrapper,
&mut msg_wrapper
)
.is_ok());
// check wrapper were consumed
assert_eq!(None, dest_wrapper.take());
assert_eq!(None, msg_wrapper.take());
// another try with router chain with asserting sender
assert_eq!(
Err(SendError::Transport("Other")),
send_xcm::<(
ParentAsUmp<OtherErrorUpwardMessageSender, (), ()>,
OkFixedXcmHashWithAssertingRequiredInputsSender
)>(dest.into(), message)
);
}
#[test]
fn take_first_asset_trader_buy_weight_called_twice_throws_error() {
const AMOUNT: u128 = 100;
// prepare prerequisites to instantiate `TakeFirstAssetTrader`
type TestAccountId = u32;
type TestAssetId = u32;
type TestBalance = u128;
struct TestAssets;
impl MatchesFungibles<TestAssetId, TestBalance> for TestAssets {
fn matches_fungibles(a: &MultiAsset) -> Result<(TestAssetId, TestBalance), Error> {
match a {
MultiAsset { fun: Fungible(amount), id: Concrete(_id) } => Ok((1, *amount)),
_ => Err(Error::AssetNotHandled),
}
}
}
impl fungibles::Inspect<TestAccountId> for TestAssets {
type AssetId = TestAssetId;
type Balance = TestBalance;
fn total_issuance(_: Self::AssetId) -> Self::Balance {
todo!()
}
fn minimum_balance(_: Self::AssetId) -> Self::Balance {
0
}
fn balance(_: Self::AssetId, _: &TestAccountId) -> Self::Balance {
todo!()
}
fn total_balance(_: Self::AssetId, _: &TestAccountId) -> Self::Balance {
todo!()
}
fn reducible_balance(
_: Self::AssetId,
_: &TestAccountId,
_: Preservation,
_: Fortitude,
) -> Self::Balance {
todo!()
}
fn can_deposit(
_: Self::AssetId,
_: &TestAccountId,
_: Self::Balance,
_: Provenance,
) -> DepositConsequence {
todo!()
}
fn can_withdraw(
_: Self::AssetId,
_: &TestAccountId,
_: Self::Balance,
) -> WithdrawConsequence<Self::Balance> {
todo!()
}
fn asset_exists(_: Self::AssetId) -> bool {
todo!()
}
}
impl fungibles::Mutate<TestAccountId> for TestAssets {}
impl fungibles::Balanced<TestAccountId> for TestAssets {
type OnDropCredit = fungibles::DecreaseIssuance<TestAccountId, Self>;
type OnDropDebt = fungibles::IncreaseIssuance<TestAccountId, Self>;
}
impl fungibles::Unbalanced<TestAccountId> for TestAssets {
fn handle_dust(_: fungibles::Dust<TestAccountId, Self>) {
todo!()
}
fn write_balance(
_: Self::AssetId,
_: &TestAccountId,
_: Self::Balance,
) -> Result<Option<Self::Balance>, DispatchError> {
todo!()
}
fn set_total_issuance(_: Self::AssetId, _: Self::Balance) {
todo!()
}
}
struct FeeChargerAssetsHandleRefund;
impl ChargeWeightInFungibles<TestAccountId, TestAssets> for FeeChargerAssetsHandleRefund {
fn charge_weight_in_fungibles(
_: <TestAssets as Inspect<TestAccountId>>::AssetId,
_: Weight,
) -> Result<<TestAssets as Inspect<TestAccountId>>::Balance, XcmError> {
Ok(AMOUNT)
}
}
impl TakeRevenue for FeeChargerAssetsHandleRefund {
fn take_revenue(_: MultiAsset) {}
}
// create new instance
type Trader = TakeFirstAssetTrader<
TestAccountId,
FeeChargerAssetsHandleRefund,
TestAssets,
TestAssets,
FeeChargerAssetsHandleRefund,
>;
let mut trader = <Trader as WeightTrader>::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };
// prepare test data
let asset: MultiAsset = (Here, AMOUNT).into();
let payment = Assets::from(asset);
let weight_to_buy = Weight::from_parts(1_000, 1_000);
// lets do first call (success)
assert_ok!(trader.buy_weight(weight_to_buy, payment.clone(), &ctx));
// lets do second call (error)
assert_eq!(trader.buy_weight(weight_to_buy, payment, &ctx), Err(XcmError::NotWithdrawable));
}
}