1
// Copyright (C) Parity Technologies (UK) Ltd.
2
// This file is part of Cumulus.
3

            
4
// Substrate is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Substrate is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Cumulus.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! Helper datatypes for cumulus. This includes the [`ParentAsUmp`] routing type which will route
18
//! messages into an [`UpwardMessageSender`] if the destination is `Parent`.
19

            
20
#![cfg_attr(not(feature = "std"), no_std)]
21

            
22
extern crate alloc;
23

            
24
use alloc::{vec, vec::Vec};
25
use codec::Encode;
26
use core::marker::PhantomData;
27
use cumulus_primitives_core::{MessageSendError, UpwardMessageSender};
28
use frame_support::{
29
	defensive,
30
	traits::{tokens::fungibles, Get, OnUnbalanced as OnUnbalancedT},
31
	weights::{Weight, WeightToFee as WeightToFeeT},
32
	CloneNoBound,
33
};
34
use pallet_asset_conversion::SwapCredit as SwapCreditT;
35
use polkadot_runtime_common::xcm_sender::PriceForMessageDelivery;
36
use sp_runtime::{
37
	traits::{Saturating, Zero},
38
	SaturatedConversion,
39
};
40
use xcm::{latest::prelude::*, VersionedLocation, VersionedXcm, WrapVersion};
41
use xcm_builder::{InspectMessageQueues, TakeRevenue};
42
use xcm_executor::{
43
	traits::{MatchesFungibles, TransactAsset, WeightTrader},
44
	AssetsInHolding,
45
};
46

            
47
#[cfg(test)]
48
mod tests;
49

            
50
/// Xcm router which recognises the `Parent` destination and handles it by sending the message into
51
/// the given UMP `UpwardMessageSender` implementation. Thus this essentially adapts an
52
/// `UpwardMessageSender` trait impl into a `SendXcm` trait impl.
53
///
54
/// NOTE: This is a pretty dumb "just send it" router; we will probably want to introduce queuing
55
/// to UMP eventually and when we do, the pallet which implements the queuing will be responsible
56
/// for the `SendXcm` implementation.
57
pub struct ParentAsUmp<T, W, P>(PhantomData<(T, W, P)>);
58
impl<T, W, P> SendXcm for ParentAsUmp<T, W, P>
59
where
60
	T: UpwardMessageSender,
61
	W: WrapVersion,
62
	P: PriceForMessageDelivery<Id = ()>,
63
{
64
	type Ticket = Vec<u8>;
65

            
66
	fn validate(dest: &mut Option<Location>, msg: &mut Option<Xcm<()>>) -> SendResult<Vec<u8>> {
67
		let d = dest.take().ok_or(SendError::MissingArgument)?;
68

            
69
		if d.contains_parents_only(1) {
70
			// An upward message for the relay chain.
71
			let xcm = msg.take().ok_or(SendError::MissingArgument)?;
72
			let price = P::price_for_delivery((), &xcm);
73
			let versioned_xcm =
74
				W::wrap_version(&d, xcm).map_err(|()| SendError::DestinationUnsupported)?;
75
			versioned_xcm
76
				.validate_xcm_nesting()
77
				.map_err(|()| SendError::ExceedsMaxMessageSize)?;
78
			let data = versioned_xcm.encode();
79

            
80
			Ok((data, price))
81
		} else {
82
			// Anything else is unhandled. This includes a message that is not meant for us.
83
			// We need to make sure that dest/msg is not consumed here.
84
			*dest = Some(d);
85
			Err(SendError::NotApplicable)
86
		}
87
	}
88

            
89
	fn deliver(data: Vec<u8>) -> Result<XcmHash, SendError> {
90
		let (_, hash) = T::send_upward_message(data).map_err(|e| match e {
91
			MessageSendError::TooBig => SendError::ExceedsMaxMessageSize,
92
			e => SendError::Transport(e.into()),
93
		})?;
94

            
95
		Ok(hash)
96
	}
97
}
98

            
99
impl<T: UpwardMessageSender + InspectMessageQueues, W, P> InspectMessageQueues
100
	for ParentAsUmp<T, W, P>
101
{
102
	fn get_messages() -> Vec<(VersionedLocation, Vec<VersionedXcm<()>>)> {
103
		T::get_messages()
104
	}
105
}
106

            
107
/// Contains information to handle refund/payment for xcm-execution
108
#[derive(Clone, Eq, PartialEq, Debug)]
109
struct AssetTraderRefunder {
110
	// The amount of weight bought minus the weigh already refunded
111
	weight_outstanding: Weight,
112
	// The concrete asset containing the asset location and outstanding balance
113
	outstanding_concrete_asset: Asset,
114
}
115

            
116
/// Charges for execution in the first asset of those selected for fee payment
117
/// Only succeeds for Concrete Fungible Assets
118
/// First tries to convert the this Asset into a local assetId
119
/// Then charges for this assetId as described by FeeCharger
120
/// Weight, paid balance, local asset Id and the location is stored for
121
/// later refund purposes
122
/// Important: Errors if the Trader is being called twice by 2 BuyExecution instructions
123
/// Alternatively we could just return payment in the aforementioned case
124
#[derive(CloneNoBound)]
125
pub struct TakeFirstAssetTrader<
126
	AccountId: Eq,
127
	FeeCharger: ChargeWeightInFungibles<AccountId, ConcreteAssets>,
128
	Matcher: MatchesFungibles<ConcreteAssets::AssetId, ConcreteAssets::Balance>,
129
	ConcreteAssets: fungibles::Mutate<AccountId> + fungibles::Balanced<AccountId>,
130
	HandleRefund: TakeRevenue,
131
>(
132
	Option<AssetTraderRefunder>,
133
	PhantomData<(AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund)>,
134
);
135
impl<
136
		AccountId: Eq,
137
		FeeCharger: ChargeWeightInFungibles<AccountId, ConcreteAssets>,
138
		Matcher: MatchesFungibles<ConcreteAssets::AssetId, ConcreteAssets::Balance>,
139
		ConcreteAssets: fungibles::Mutate<AccountId> + fungibles::Balanced<AccountId>,
140
		HandleRefund: TakeRevenue,
141
	> WeightTrader
142
	for TakeFirstAssetTrader<AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund>
143
{
144
	fn new() -> Self {
145
		Self(None, PhantomData)
146
	}
147
	// We take first asset
148
	// Check whether we can convert fee to asset_fee (is_sufficient, min_deposit)
149
	// If everything goes well, we charge.
150
	fn buy_weight(
151
		&mut self,
152
		weight: Weight,
153
		payment: xcm_executor::AssetsInHolding,
154
		context: &XcmContext,
155
	) -> Result<xcm_executor::AssetsInHolding, XcmError> {
156
		log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::buy_weight weight: {:?}, payment: {:?}, context: {:?}", weight, payment, context);
157

            
158
		// Make sure we don't enter twice
159
		if self.0.is_some() {
160
			return Err(XcmError::NotWithdrawable)
161
		}
162

            
163
		// We take the very first asset from payment
164
		// (assets are sorted by fungibility/amount after this conversion)
165
		let assets: Assets = payment.clone().into();
166

            
167
		// Take the first asset from the selected Assets
168
		let first = assets.get(0).ok_or(XcmError::AssetNotFound)?;
169

            
170
		// Get the local asset id in which we can pay for fees
171
		let (local_asset_id, _) =
172
			Matcher::matches_fungibles(first).map_err(|_| XcmError::AssetNotFound)?;
173

            
174
		// Calculate how much we should charge in the asset_id for such amount of weight
175
		// Require at least a payment of minimum_balance
176
		// Necessary for fully collateral-backed assets
177
		let asset_balance: u128 =
178
			FeeCharger::charge_weight_in_fungibles(local_asset_id.clone(), weight)
179
				.map(|amount| {
180
					let minimum_balance = ConcreteAssets::minimum_balance(local_asset_id);
181
					if amount < minimum_balance {
182
						minimum_balance
183
					} else {
184
						amount
185
					}
186
				})?
187
				.try_into()
188
				.map_err(|_| XcmError::Overflow)?;
189

            
190
		// Convert to the same kind of asset, with the required fungible balance
191
		let required = first.id.clone().into_asset(asset_balance.into());
192

            
193
		// Subtract payment
194
		let unused = payment.checked_sub(required.clone()).map_err(|_| XcmError::TooExpensive)?;
195

            
196
		// record weight and asset
197
		self.0 = Some(AssetTraderRefunder {
198
			weight_outstanding: weight,
199
			outstanding_concrete_asset: required,
200
		});
201

            
202
		Ok(unused)
203
	}
204

            
205
	fn refund_weight(&mut self, weight: Weight, context: &XcmContext) -> Option<Asset> {
206
		log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::refund_weight weight: {:?}, context: {:?}", weight, context);
207
		if let Some(AssetTraderRefunder {
208
			mut weight_outstanding,
209
			outstanding_concrete_asset: Asset { id, fun },
210
		}) = self.0.clone()
211
		{
212
			// Get the local asset id in which we can refund fees
213
			let (local_asset_id, outstanding_balance) =
214
				Matcher::matches_fungibles(&(id.clone(), fun).into()).ok()?;
215

            
216
			let minimum_balance = ConcreteAssets::minimum_balance(local_asset_id.clone());
217

            
218
			// Calculate asset_balance
219
			// This read should have already be cached in buy_weight
220
			let (asset_balance, outstanding_minus_subtracted) =
221
				FeeCharger::charge_weight_in_fungibles(local_asset_id, weight).ok().map(
222
					|asset_balance| {
223
						// Require at least a drop of minimum_balance
224
						// Necessary for fully collateral-backed assets
225
						if outstanding_balance.saturating_sub(asset_balance) > minimum_balance {
226
							(asset_balance, outstanding_balance.saturating_sub(asset_balance))
227
						}
228
						// If the amount to be refunded leaves the remaining balance below ED,
229
						// we just refund the exact amount that guarantees at least ED will be
230
						// dropped
231
						else {
232
							(outstanding_balance.saturating_sub(minimum_balance), minimum_balance)
233
						}
234
					},
235
				)?;
236

            
237
			// Convert balances into u128
238
			let outstanding_minus_subtracted: u128 = outstanding_minus_subtracted.saturated_into();
239
			let asset_balance: u128 = asset_balance.saturated_into();
240

            
241
			// Construct outstanding_concrete_asset with the same location id and subtracted
242
			// balance
243
			let outstanding_concrete_asset: Asset =
244
				(id.clone(), outstanding_minus_subtracted).into();
245

            
246
			// Subtract from existing weight and balance
247
			weight_outstanding = weight_outstanding.saturating_sub(weight);
248

            
249
			// Override AssetTraderRefunder
250
			self.0 = Some(AssetTraderRefunder { weight_outstanding, outstanding_concrete_asset });
251

            
252
			// Only refund if positive
253
			if asset_balance > 0 {
254
				Some((id, asset_balance).into())
255
			} else {
256
				None
257
			}
258
		} else {
259
			None
260
		}
261
	}
262
}
263

            
264
impl<
265
		AccountId: Eq,
266
		FeeCharger: ChargeWeightInFungibles<AccountId, ConcreteAssets>,
267
		Matcher: MatchesFungibles<ConcreteAssets::AssetId, ConcreteAssets::Balance>,
268
		ConcreteAssets: fungibles::Mutate<AccountId> + fungibles::Balanced<AccountId>,
269
		HandleRefund: TakeRevenue,
270
	> Drop for TakeFirstAssetTrader<AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund>
271
{
272
	fn drop(&mut self) {
273
		if let Some(asset_trader) = self.0.clone() {
274
			HandleRefund::take_revenue(asset_trader.outstanding_concrete_asset);
275
		}
276
	}
277
}
278

            
279
/// XCM fee depositor to which we implement the `TakeRevenue` trait.
280
/// It receives a `Transact` implemented argument and a 32 byte convertible `AccountId`, and the fee
281
/// receiver account's `FungiblesMutateAdapter` should be identical to that implemented by
282
/// `WithdrawAsset`.
283
pub struct XcmFeesTo32ByteAccount<FungiblesMutateAdapter, AccountId, ReceiverAccount>(
284
	PhantomData<(FungiblesMutateAdapter, AccountId, ReceiverAccount)>,
285
);
286
impl<
287
		FungiblesMutateAdapter: TransactAsset,
288
		AccountId: Clone + Into<[u8; 32]>,
289
		ReceiverAccount: Get<Option<AccountId>>,
290
	> TakeRevenue for XcmFeesTo32ByteAccount<FungiblesMutateAdapter, AccountId, ReceiverAccount>
291
{
292
	fn take_revenue(revenue: Asset) {
293
		if let Some(receiver) = ReceiverAccount::get() {
294
			let ok = FungiblesMutateAdapter::deposit_asset(
295
				&revenue,
296
				&([AccountId32 { network: None, id: receiver.into() }].into()),
297
				None,
298
			)
299
			.is_ok();
300

            
301
			debug_assert!(ok, "`deposit_asset` cannot generally fail; qed");
302
		}
303
	}
304
}
305

            
306
/// ChargeWeightInFungibles trait, which converts a given amount of weight
307
/// and an assetId, and it returns the balance amount that should be charged
308
/// in such assetId for that amount of weight
309
pub trait ChargeWeightInFungibles<AccountId, Assets: fungibles::Inspect<AccountId>> {
310
	fn charge_weight_in_fungibles(
311
		asset_id: <Assets as fungibles::Inspect<AccountId>>::AssetId,
312
		weight: Weight,
313
	) -> Result<<Assets as fungibles::Inspect<AccountId>>::Balance, XcmError>;
314
}
315

            
316
/// Provides an implementation of [`WeightTrader`] to charge for weight using the first asset
317
/// specified in the `payment` argument.
318
///
319
/// The asset used to pay for the weight must differ from the `Target` asset and be exchangeable for
320
/// the same `Target` asset through `SwapCredit`.
321
///
322
/// ### Parameters:
323
/// - `Target`: the asset into which the user's payment will be exchanged using `SwapCredit`.
324
/// - `SwapCredit`: mechanism used for the exchange of the user's payment asset into the `Target`.
325
/// - `WeightToFee`: weight to the `Target` asset fee calculator.
326
/// - `Fungibles`: registry of fungible assets.
327
/// - `FungiblesAssetMatcher`: utility for mapping [`Asset`] to `Fungibles::AssetId` and
328
///   `Fungibles::Balance`.
329
/// - `OnUnbalanced`: handler for the fee payment.
330
/// - `AccountId`: the account identifier type.
331
pub struct SwapFirstAssetTrader<
332
	Target: Get<Fungibles::AssetId>,
333
	SwapCredit: SwapCreditT<
334
		AccountId,
335
		Balance = Fungibles::Balance,
336
		AssetKind = Fungibles::AssetId,
337
		Credit = fungibles::Credit<AccountId, Fungibles>,
338
	>,
339
	WeightToFee: WeightToFeeT<Balance = Fungibles::Balance>,
340
	Fungibles: fungibles::Balanced<AccountId>,
341
	FungiblesAssetMatcher: MatchesFungibles<Fungibles::AssetId, Fungibles::Balance>,
342
	OnUnbalanced: OnUnbalancedT<fungibles::Credit<AccountId, Fungibles>>,
343
	AccountId,
344
> where
345
	Fungibles::Balance: Into<u128>,
346
{
347
	/// Accumulated fee paid for XCM execution.
348
	total_fee: fungibles::Credit<AccountId, Fungibles>,
349
	/// Last asset utilized by a client to settle a fee.
350
	last_fee_asset: Option<AssetId>,
351
	_phantom_data: PhantomData<(
352
		Target,
353
		SwapCredit,
354
		WeightToFee,
355
		Fungibles,
356
		FungiblesAssetMatcher,
357
		OnUnbalanced,
358
		AccountId,
359
	)>,
360
}
361

            
362
impl<
363
		Target: Get<Fungibles::AssetId>,
364
		SwapCredit: SwapCreditT<
365
			AccountId,
366
			Balance = Fungibles::Balance,
367
			AssetKind = Fungibles::AssetId,
368
			Credit = fungibles::Credit<AccountId, Fungibles>,
369
		>,
370
		WeightToFee: WeightToFeeT<Balance = Fungibles::Balance>,
371
		Fungibles: fungibles::Balanced<AccountId>,
372
		FungiblesAssetMatcher: MatchesFungibles<Fungibles::AssetId, Fungibles::Balance>,
373
		OnUnbalanced: OnUnbalancedT<fungibles::Credit<AccountId, Fungibles>>,
374
		AccountId,
375
	> WeightTrader
376
	for SwapFirstAssetTrader<
377
		Target,
378
		SwapCredit,
379
		WeightToFee,
380
		Fungibles,
381
		FungiblesAssetMatcher,
382
		OnUnbalanced,
383
		AccountId,
384
	> where
385
	Fungibles::Balance: Into<u128>,
386
{
387
	fn new() -> Self {
388
		Self {
389
			total_fee: fungibles::Credit::<AccountId, Fungibles>::zero(Target::get()),
390
			last_fee_asset: None,
391
			_phantom_data: PhantomData,
392
		}
393
	}
394

            
395
	fn buy_weight(
396
		&mut self,
397
		weight: Weight,
398
		mut payment: AssetsInHolding,
399
		_context: &XcmContext,
400
	) -> Result<AssetsInHolding, XcmError> {
401
		log::trace!(
402
			target: "xcm::weight",
403
			"SwapFirstAssetTrader::buy_weight weight: {:?}, payment: {:?}",
404
			weight,
405
			payment,
406
		);
407
		let first_asset: Asset =
408
			payment.fungible.pop_first().ok_or(XcmError::AssetNotFound)?.into();
409
		let (fungibles_asset, balance) = FungiblesAssetMatcher::matches_fungibles(&first_asset)
410
			.map_err(|_| XcmError::AssetNotFound)?;
411

            
412
		let swap_asset = fungibles_asset.clone().into();
413
		if Target::get().eq(&swap_asset) {
414
			// current trader is not applicable.
415
			return Err(XcmError::FeesNotMet)
416
		}
417

            
418
		let credit_in = Fungibles::issue(fungibles_asset, balance);
419
		let fee = WeightToFee::weight_to_fee(&weight);
420

            
421
		// swap the user's asset for the `Target` asset.
422
		let (credit_out, credit_change) = SwapCredit::swap_tokens_for_exact_tokens(
423
			vec![swap_asset, Target::get()],
424
			credit_in,
425
			fee,
426
		)
427
		.map_err(|(credit_in, _)| {
428
			drop(credit_in);
429
			XcmError::FeesNotMet
430
		})?;
431

            
432
		match self.total_fee.subsume(credit_out) {
433
			Err(credit_out) => {
434
				// error may occur if `total_fee.asset` differs from `credit_out.asset`, which does
435
				// not apply in this context.
436
				defensive!(
437
					"`total_fee.asset` must be equal to `credit_out.asset`",
438
					(self.total_fee.asset(), credit_out.asset())
439
				);
440
				return Err(XcmError::FeesNotMet)
441
			},
442
			_ => (),
443
		};
444
		self.last_fee_asset = Some(first_asset.id.clone());
445

            
446
		payment.fungible.insert(first_asset.id, credit_change.peek().into());
447
		drop(credit_change);
448
		Ok(payment)
449
	}
450

            
451
	fn refund_weight(&mut self, weight: Weight, _context: &XcmContext) -> Option<Asset> {
452
		log::trace!(
453
			target: "xcm::weight",
454
			"SwapFirstAssetTrader::refund_weight weight: {:?}, self.total_fee: {:?}",
455
			weight,
456
			self.total_fee,
457
		);
458
		if self.total_fee.peek().is_zero() {
459
			// noting yet paid to refund.
460
			return None
461
		}
462
		let mut refund_asset = if let Some(asset) = &self.last_fee_asset {
463
			// create an initial zero refund in the asset used in the last `buy_weight`.
464
			(asset.clone(), Fungible(0)).into()
465
		} else {
466
			return None
467
		};
468
		let refund_amount = WeightToFee::weight_to_fee(&weight);
469
		if refund_amount >= self.total_fee.peek() {
470
			// not enough was paid to refund the `weight`.
471
			return None
472
		}
473

            
474
		let refund_swap_asset = FungiblesAssetMatcher::matches_fungibles(&refund_asset)
475
			.map(|(a, _)| a.into())
476
			.ok()?;
477

            
478
		let refund = self.total_fee.extract(refund_amount);
479
		let refund = match SwapCredit::swap_exact_tokens_for_tokens(
480
			vec![Target::get(), refund_swap_asset],
481
			refund,
482
			None,
483
		) {
484
			Ok(refund_in_target) => refund_in_target,
485
			Err((refund, _)) => {
486
				// return an attempted refund back to the `total_fee`.
487
				let _ = self.total_fee.subsume(refund).map_err(|refund| {
488
					// error may occur if `total_fee.asset` differs from `refund.asset`, which does
489
					// not apply in this context.
490
					defensive!(
491
						"`total_fee.asset` must be equal to `refund.asset`",
492
						(self.total_fee.asset(), refund.asset())
493
					);
494
				});
495
				return None
496
			},
497
		};
498

            
499
		refund_asset.fun = refund.peek().into().into();
500
		drop(refund);
501
		Some(refund_asset)
502
	}
503
}
504

            
505
impl<
506
		Target: Get<Fungibles::AssetId>,
507
		SwapCredit: SwapCreditT<
508
			AccountId,
509
			Balance = Fungibles::Balance,
510
			AssetKind = Fungibles::AssetId,
511
			Credit = fungibles::Credit<AccountId, Fungibles>,
512
		>,
513
		WeightToFee: WeightToFeeT<Balance = Fungibles::Balance>,
514
		Fungibles: fungibles::Balanced<AccountId>,
515
		FungiblesAssetMatcher: MatchesFungibles<Fungibles::AssetId, Fungibles::Balance>,
516
		OnUnbalanced: OnUnbalancedT<fungibles::Credit<AccountId, Fungibles>>,
517
		AccountId,
518
	> Drop
519
	for SwapFirstAssetTrader<
520
		Target,
521
		SwapCredit,
522
		WeightToFee,
523
		Fungibles,
524
		FungiblesAssetMatcher,
525
		OnUnbalanced,
526
		AccountId,
527
	> where
528
	Fungibles::Balance: Into<u128>,
529
{
530
	fn drop(&mut self) {
531
		if self.total_fee.peek().is_zero() {
532
			return
533
		}
534
		let total_fee = self.total_fee.extract(self.total_fee.peek());
535
		OnUnbalanced::on_unbalanced(total_fee);
536
	}
537
}
538

            
539
#[cfg(test)]
540
mod test_xcm_router {
541
	use super::*;
542
	use cumulus_primitives_core::UpwardMessage;
543
	use frame_support::assert_ok;
544
	use xcm::MAX_XCM_DECODE_DEPTH;
545

            
546
	/// Validates [`validate`] for required Some(destination) and Some(message)
547
	struct OkFixedXcmHashWithAssertingRequiredInputsSender;
548
	impl OkFixedXcmHashWithAssertingRequiredInputsSender {
549
		const FIXED_XCM_HASH: [u8; 32] = [9; 32];
550

            
551
		fn fixed_delivery_asset() -> Assets {
552
			Assets::new()
553
		}
554

            
555
		fn expected_delivery_result() -> Result<(XcmHash, Assets), SendError> {
556
			Ok((Self::FIXED_XCM_HASH, Self::fixed_delivery_asset()))
557
		}
558
	}
559
	impl SendXcm for OkFixedXcmHashWithAssertingRequiredInputsSender {
560
		type Ticket = ();
561

            
562
		fn validate(
563
			destination: &mut Option<Location>,
564
			message: &mut Option<Xcm<()>>,
565
		) -> SendResult<Self::Ticket> {
566
			assert!(destination.is_some());
567
			assert!(message.is_some());
568
			Ok(((), OkFixedXcmHashWithAssertingRequiredInputsSender::fixed_delivery_asset()))
569
		}
570

            
571
		fn deliver(_: Self::Ticket) -> Result<XcmHash, SendError> {
572
			Ok(Self::FIXED_XCM_HASH)
573
		}
574
	}
575

            
576
	/// Impl [`UpwardMessageSender`] that return `Other` error
577
	struct OtherErrorUpwardMessageSender;
578
	impl UpwardMessageSender for OtherErrorUpwardMessageSender {
579
		fn send_upward_message(_: UpwardMessage) -> Result<(u32, XcmHash), MessageSendError> {
580
			Err(MessageSendError::Other)
581
		}
582
	}
583

            
584
	#[test]
585
	fn parent_as_ump_does_not_consume_dest_or_msg_on_not_applicable() {
586
		// dummy message
587
		let message = Xcm(vec![Trap(5)]);
588

            
589
		// ParentAsUmp - check dest is really not applicable
590
		let dest = (Parent, Parent, Parent);
591
		let mut dest_wrapper = Some(dest.into());
592
		let mut msg_wrapper = Some(message.clone());
593
		assert_eq!(
594
			Err(SendError::NotApplicable),
595
			<ParentAsUmp<(), (), ()> as SendXcm>::validate(&mut dest_wrapper, &mut msg_wrapper)
596
		);
597

            
598
		// check wrapper were not consumed
599
		assert_eq!(Some(dest.into()), dest_wrapper.take());
600
		assert_eq!(Some(message.clone()), msg_wrapper.take());
601

            
602
		// another try with router chain with asserting sender
603
		assert_eq!(
604
			OkFixedXcmHashWithAssertingRequiredInputsSender::expected_delivery_result(),
605
			send_xcm::<(ParentAsUmp<(), (), ()>, OkFixedXcmHashWithAssertingRequiredInputsSender)>(
606
				dest.into(),
607
				message
608
			)
609
		);
610
	}
611

            
612
	#[test]
613
	fn parent_as_ump_consumes_dest_and_msg_on_ok_validate() {
614
		// dummy message
615
		let message = Xcm(vec![Trap(5)]);
616

            
617
		// ParentAsUmp - check dest/msg is valid
618
		let dest = (Parent, Here);
619
		let mut dest_wrapper = Some(dest.clone().into());
620
		let mut msg_wrapper = Some(message.clone());
621
		assert!(<ParentAsUmp<(), (), ()> as SendXcm>::validate(
622
			&mut dest_wrapper,
623
			&mut msg_wrapper
624
		)
625
		.is_ok());
626

            
627
		// check wrapper were consumed
628
		assert_eq!(None, dest_wrapper.take());
629
		assert_eq!(None, msg_wrapper.take());
630

            
631
		// another try with router chain with asserting sender
632
		assert_eq!(
633
			Err(SendError::Transport("Other")),
634
			send_xcm::<(
635
				ParentAsUmp<OtherErrorUpwardMessageSender, (), ()>,
636
				OkFixedXcmHashWithAssertingRequiredInputsSender
637
			)>(dest.into(), message)
638
		);
639
	}
640

            
641
	#[test]
642
	fn parent_as_ump_validate_nested_xcm_works() {
643
		let dest = Parent;
644

            
645
		type Router = ParentAsUmp<(), (), ()>;
646

            
647
		// Message that is not too deeply nested:
648
		let mut good = Xcm(vec![ClearOrigin]);
649
		for _ in 0..MAX_XCM_DECODE_DEPTH - 1 {
650
			good = Xcm(vec![SetAppendix(good)]);
651
		}
652

            
653
		// Check that the good message is validated:
654
		assert_ok!(<Router as SendXcm>::validate(&mut Some(dest.into()), &mut Some(good.clone())));
655

            
656
		// Nesting the message one more time should reject it:
657
		let bad = Xcm(vec![SetAppendix(good)]);
658
		assert_eq!(
659
			Err(SendError::ExceedsMaxMessageSize),
660
			<Router as SendXcm>::validate(&mut Some(dest.into()), &mut Some(bad))
661
		);
662
	}
663
}
664
#[cfg(test)]
665
mod test_trader {
666
	use super::*;
667
	use frame_support::{
668
		assert_ok,
669
		traits::tokens::{
670
			DepositConsequence, Fortitude, Preservation, Provenance, WithdrawConsequence,
671
		},
672
	};
673
	use sp_runtime::DispatchError;
674
	use xcm_executor::{traits::Error, AssetsInHolding};
675

            
676
	#[test]
677
	fn take_first_asset_trader_buy_weight_called_twice_throws_error() {
678
		const AMOUNT: u128 = 100;
679

            
680
		// prepare prerequisites to instantiate `TakeFirstAssetTrader`
681
		type TestAccountId = u32;
682
		type TestAssetId = u32;
683
		type TestBalance = u128;
684
		struct TestAssets;
685
		impl MatchesFungibles<TestAssetId, TestBalance> for TestAssets {
686
			fn matches_fungibles(a: &Asset) -> Result<(TestAssetId, TestBalance), Error> {
687
				match a {
688
					Asset { fun: Fungible(amount), id: AssetId(_id) } => Ok((1, *amount)),
689
					_ => Err(Error::AssetNotHandled),
690
				}
691
			}
692
		}
693
		impl fungibles::Inspect<TestAccountId> for TestAssets {
694
			type AssetId = TestAssetId;
695
			type Balance = TestBalance;
696

            
697
			fn total_issuance(_: Self::AssetId) -> Self::Balance {
698
				todo!()
699
			}
700

            
701
			fn minimum_balance(_: Self::AssetId) -> Self::Balance {
702
				0
703
			}
704

            
705
			fn balance(_: Self::AssetId, _: &TestAccountId) -> Self::Balance {
706
				todo!()
707
			}
708

            
709
			fn total_balance(_: Self::AssetId, _: &TestAccountId) -> Self::Balance {
710
				todo!()
711
			}
712

            
713
			fn reducible_balance(
714
				_: Self::AssetId,
715
				_: &TestAccountId,
716
				_: Preservation,
717
				_: Fortitude,
718
			) -> Self::Balance {
719
				todo!()
720
			}
721

            
722
			fn can_deposit(
723
				_: Self::AssetId,
724
				_: &TestAccountId,
725
				_: Self::Balance,
726
				_: Provenance,
727
			) -> DepositConsequence {
728
				todo!()
729
			}
730

            
731
			fn can_withdraw(
732
				_: Self::AssetId,
733
				_: &TestAccountId,
734
				_: Self::Balance,
735
			) -> WithdrawConsequence<Self::Balance> {
736
				todo!()
737
			}
738

            
739
			fn asset_exists(_: Self::AssetId) -> bool {
740
				todo!()
741
			}
742
		}
743
		impl fungibles::Mutate<TestAccountId> for TestAssets {}
744
		impl fungibles::Balanced<TestAccountId> for TestAssets {
745
			type OnDropCredit = fungibles::DecreaseIssuance<TestAccountId, Self>;
746
			type OnDropDebt = fungibles::IncreaseIssuance<TestAccountId, Self>;
747
		}
748
		impl fungibles::Unbalanced<TestAccountId> for TestAssets {
749
			fn handle_dust(_: fungibles::Dust<TestAccountId, Self>) {
750
				todo!()
751
			}
752
			fn write_balance(
753
				_: Self::AssetId,
754
				_: &TestAccountId,
755
				_: Self::Balance,
756
			) -> Result<Option<Self::Balance>, DispatchError> {
757
				todo!()
758
			}
759

            
760
			fn set_total_issuance(_: Self::AssetId, _: Self::Balance) {
761
				todo!()
762
			}
763
		}
764

            
765
		struct FeeChargerAssetsHandleRefund;
766
		impl ChargeWeightInFungibles<TestAccountId, TestAssets> for FeeChargerAssetsHandleRefund {
767
			fn charge_weight_in_fungibles(
768
				_: <TestAssets as fungibles::Inspect<TestAccountId>>::AssetId,
769
				_: Weight,
770
			) -> Result<<TestAssets as fungibles::Inspect<TestAccountId>>::Balance, XcmError> {
771
				Ok(AMOUNT)
772
			}
773
		}
774
		impl TakeRevenue for FeeChargerAssetsHandleRefund {
775
			fn take_revenue(_: Asset) {}
776
		}
777

            
778
		// create new instance
779
		type Trader = TakeFirstAssetTrader<
780
			TestAccountId,
781
			FeeChargerAssetsHandleRefund,
782
			TestAssets,
783
			TestAssets,
784
			FeeChargerAssetsHandleRefund,
785
		>;
786
		let mut trader = <Trader as WeightTrader>::new();
787
		let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };
788

            
789
		// prepare test data
790
		let asset: Asset = (Here, AMOUNT).into();
791
		let payment = AssetsInHolding::from(asset);
792
		let weight_to_buy = Weight::from_parts(1_000, 1_000);
793

            
794
		// lets do first call (success)
795
		assert_ok!(trader.buy_weight(weight_to_buy, payment.clone(), &ctx));
796

            
797
		// lets do second call (error)
798
		assert_eq!(trader.buy_weight(weight_to_buy, payment, &ctx), Err(XcmError::NotWithdrawable));
799
	}
800
}
801

            
802
/// Implementation of `xcm_builder::EnsureDelivery` which helps to ensure delivery to the
803
/// parent relay chain. Deposits existential deposit for origin (if needed).
804
/// Deposits estimated fee to the origin account (if needed).
805
/// Allows triggering of additional logic for a specific `ParaId` (e.g. to open an HRMP channel) if
806
/// needed.
807
#[cfg(feature = "runtime-benchmarks")]
808
pub struct ToParentDeliveryHelper<XcmConfig, ExistentialDeposit, PriceForDelivery>(
809
	core::marker::PhantomData<(XcmConfig, ExistentialDeposit, PriceForDelivery)>,
810
);
811

            
812
#[cfg(feature = "runtime-benchmarks")]
813
impl<
814
		XcmConfig: xcm_executor::Config,
815
		ExistentialDeposit: Get<Option<Asset>>,
816
		PriceForDelivery: PriceForMessageDelivery<Id = ()>,
817
	> xcm_builder::EnsureDelivery
818
	for ToParentDeliveryHelper<XcmConfig, ExistentialDeposit, PriceForDelivery>
819
{
820
	fn ensure_successful_delivery(
821
		origin_ref: &Location,
822
		dest: &Location,
823
		fee_reason: xcm_executor::traits::FeeReason,
824
	) -> (Option<xcm_executor::FeesMode>, Option<Assets>) {
825
		use xcm::latest::{MAX_INSTRUCTIONS_TO_DECODE, MAX_ITEMS_IN_ASSETS};
826
		use xcm_executor::{traits::FeeManager, FeesMode};
827

            
828
		// check if the destination is relay/parent
829
		if dest.ne(&Location::parent()) {
830
			return (None, None);
831
		}
832

            
833
		let mut fees_mode = None;
834
		if !XcmConfig::FeeManager::is_waived(Some(origin_ref), fee_reason) {
835
			// if not waived, we need to set up accounts for paying and receiving fees
836

            
837
			// mint ED to origin if needed
838
			if let Some(ed) = ExistentialDeposit::get() {
839
				XcmConfig::AssetTransactor::deposit_asset(&ed, &origin_ref, None).unwrap();
840
			}
841

            
842
			// overestimate delivery fee
843
			let mut max_assets: Vec<Asset> = Vec::new();
844
			for i in 0..MAX_ITEMS_IN_ASSETS {
845
				max_assets.push((GeneralIndex(i as u128), 100u128).into());
846
			}
847
			let overestimated_xcm =
848
				vec![WithdrawAsset(max_assets.into()); MAX_INSTRUCTIONS_TO_DECODE as usize].into();
849
			let overestimated_fees = PriceForDelivery::price_for_delivery((), &overestimated_xcm);
850

            
851
			// mint overestimated fee to origin
852
			for fee in overestimated_fees.inner() {
853
				XcmConfig::AssetTransactor::deposit_asset(&fee, &origin_ref, None).unwrap();
854
			}
855

            
856
			// expected worst case - direct withdraw
857
			fees_mode = Some(FeesMode { jit_withdraw: true });
858
		}
859
		(fees_mode, None)
860
	}
861
}