1
// Copyright 2019-2025 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam 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
// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! Moonbeam Runtime Xcm Tests
18

            
19
mod xcm_mock;
20

            
21
use cumulus_primitives_core::relay_chain::HrmpChannelId;
22
use frame_support::{
23
	assert_ok,
24
	traits::{PalletInfo, PalletInfoAccess},
25
	weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
26
	BoundedVec,
27
};
28
use moonbeam_runtime::xcm_config::AssetType;
29
use pallet_xcm_transactor::{
30
	Currency, CurrencyPayment, HrmpInitParams, HrmpOperation, TransactWeights,
31
};
32
use sp_core::ConstU32;
33
use sp_core::U256;
34
use sp_runtime::traits::Convert;
35
use xcm::{
36
	latest::prelude::{
37
		AccountId32, AccountKey20, All, Asset, AssetId, Assets as XcmAssets, DepositAsset,
38
		Fungibility, Fungible, GeneralIndex, Junction, Junctions, Limited, Location, OriginKind,
39
		PalletInstance, Parachain, QueryResponse, Reanchorable, Response, WeightLimit, Wild, Xcm,
40
	},
41
	IntoVersion, VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm, WrapVersion,
42
};
43
use xcm_executor::traits::{ConvertLocation, TransferType};
44
use xcm_mock::parachain::{self, EvmForeignAssets, PolkadotXcm, Treasury};
45
use xcm_mock::relay_chain;
46
use xcm_mock::*;
47
use xcm_primitives::{
48
	split_location_into_chain_part_and_beneficiary, UtilityEncodeCall, DEFAULT_PROOF_SIZE,
49
};
50
use xcm_simulator::TestExt;
51

            
52
26
fn add_supported_asset(asset_type: parachain::AssetType, units_per_second: u128) -> Result<(), ()> {
53
26
	let parachain::AssetType::Xcm(location_v3) = asset_type;
54
26
	let VersionedLocation::V5(location_v5) = VersionedLocation::V3(location_v3)
55
26
		.into_version(xcm::latest::VERSION)
56
26
		.map_err(|_| ())?
57
	else {
58
		return Err(());
59
	};
60
	use frame_support::weights::WeightToFee as _;
61
26
	let native_amount_per_second: u128 =
62
26
		<parachain::Runtime as pallet_xcm_weight_trader::Config>::WeightToFee::weight_to_fee(
63
26
			&Weight::from_parts(
64
26
				frame_support::weights::constants::WEIGHT_REF_TIME_PER_SECOND,
65
26
				0,
66
26
			),
67
		)
68
26
		.try_into()
69
26
		.map_err(|_| ())?;
70
26
	let precision_factor = 10u128.pow(pallet_xcm_weight_trader::RELATIVE_PRICE_DECIMALS);
71
26
	let relative_price: u128 = if units_per_second > 0u128 {
72
10
		native_amount_per_second
73
10
			.saturating_mul(precision_factor)
74
10
			.saturating_div(units_per_second)
75
	} else {
76
16
		0u128
77
	};
78
26
	pallet_xcm_weight_trader::SupportedAssets::<parachain::Runtime>::insert(
79
26
		location_v5,
80
26
		(true, relative_price),
81
	);
82
26
	Ok(())
83
26
}
84

            
85
/// Helper function to set fee per second for an asset location (for compatibility with old tests).
86
/// Converts fee_per_second to relative_price and adds/edits the asset in the weight-trader.
87
9
fn set_fee_per_second_for_location(location: Location, fee_per_second: u128) -> Result<(), ()> {
88
	use moonbeam_tests_primitives::MemoryFeeTrader;
89
	use xcm_primitives::XcmFeeTrader;
90

            
91
	// Configure fees for XcmTransactor via the in-memory fee trader only, so that
92
	// the initial funding XCM transfers stay free and only transactor calls pay fees.
93
9
	<MemoryFeeTrader as XcmFeeTrader>::set_asset_price(location, fee_per_second).map_err(|_| ())
94
9
}
95

            
96
23
fn currency_to_asset(currency_id: parachain::CurrencyId, amount: u128) -> Asset {
97
23
	Asset {
98
23
		id: AssetId(
99
23
			<parachain::Runtime as pallet_xcm_transactor::Config>::CurrencyIdToLocation::convert(
100
23
				currency_id,
101
23
			)
102
23
			.unwrap(),
103
23
		),
104
23
		fun: Fungibility::Fungible(amount),
105
23
	}
106
23
}
107

            
108
// Send a relay asset (like DOT) to a parachain A
109
#[test]
110
1
fn receive_relay_asset_from_relay() {
111
1
	MockNet::reset();
112

            
113
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
114
1
	let source_id: parachain::AssetId = source_location.clone().into();
115
1
	let asset_metadata = parachain::AssetMetadata {
116
1
		name: b"RelayToken".to_vec(),
117
1
		symbol: b"Relay".to_vec(),
118
1
		decimals: 12,
119
1
	};
120

            
121
	// Register relay asset in paraA
122
1
	ParaA::execute_with(|| {
123
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
124
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
125
1
			.try_into()
126
1
			.expect("v3 to latest location conversion failed");
127
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
128
1
			source_id,
129
1
			source_location_latest,
130
1
			asset_metadata.decimals,
131
1
			asset_metadata.symbol.try_into().expect("too long"),
132
1
			asset_metadata.name.try_into().expect("too long"),
133
		));
134
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
135
1
	});
136

            
137
	// Actually send relay asset to parachain
138
1
	let dest: Location = AccountKey20 {
139
1
		network: None,
140
1
		key: PARAALICE,
141
1
	}
142
1
	.into();
143

            
144
	// First send relay chain asset to Parachain
145
1
	Relay::execute_with(|| {
146
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
147
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
148
1
			assets: Wild(All),
149
1
			beneficiary: dest.clone(),
150
1
		}]);
151
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
152
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
153
1
			Box::new(Parachain(1).into()),
154
1
			Box::new(([] /* Here */, 123).into()),
155
1
			Box::new(TransferType::LocalReserve),
156
1
			Box::new(fees_id),
157
1
			Box::new(TransferType::LocalReserve),
158
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
159
1
			WeightLimit::Unlimited
160
		));
161
1
	});
162

            
163
	// Verify that parachain received the asset
164
1
	ParaA::execute_with(|| {
165
		// free execution, full amount received
166
1
		assert_eq!(
167
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
168
1
			Ok(U256::from(123))
169
		);
170
1
	});
171
1
}
172

            
173
// Send relay asset (like DOT) back from Parachain A to relaychain
174
#[test]
175
1
fn send_relay_asset_to_relay() {
176
1
	MockNet::reset();
177

            
178
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
179
1
	let source_id: parachain::AssetId = source_location.clone().into();
180

            
181
1
	let asset_metadata = parachain::AssetMetadata {
182
1
		name: b"RelayToken".to_vec(),
183
1
		symbol: b"Relay".to_vec(),
184
1
		decimals: 12,
185
1
	};
186

            
187
	// Register relay asset in paraA
188
1
	ParaA::execute_with(|| {
189
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
190
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
191
1
			.try_into()
192
1
			.expect("v3 to latest location conversion failed");
193
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
194
1
			source_id,
195
1
			source_location_latest,
196
1
			asset_metadata.decimals,
197
1
			asset_metadata.symbol.try_into().expect("too long"),
198
1
			asset_metadata.name.try_into().expect("too long"),
199
		));
200
		// Free execution
201
1
		assert_ok!(add_supported_asset(source_location, 0u128));
202
1
	});
203

            
204
1
	let dest: Location = Junction::AccountKey20 {
205
1
		network: None,
206
1
		key: PARAALICE,
207
1
	}
208
1
	.into();
209

            
210
	// First send relay chain asset to Parachain like in previous test
211
1
	Relay::execute_with(|| {
212
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
213
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
214
1
			assets: Wild(All),
215
1
			beneficiary: dest.clone(),
216
1
		}]);
217
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
218
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
219
1
			Box::new(Parachain(1).into()),
220
1
			Box::new(([] /* Here */, 123).into()),
221
1
			Box::new(TransferType::LocalReserve),
222
1
			Box::new(fees_id),
223
1
			Box::new(TransferType::LocalReserve),
224
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
225
1
			WeightLimit::Unlimited
226
		));
227
1
	});
228

            
229
1
	ParaA::execute_with(|| {
230
		// Free execution, full amount received
231
1
		assert_eq!(
232
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
233
1
			Ok(U256::from(123))
234
		);
235
1
	});
236

            
237
	// Lets gather the balance before sending back money
238
1
	let mut balance_before_sending = 0;
239
1
	Relay::execute_with(|| {
240
1
		balance_before_sending = RelayBalances::free_balance(&RELAYALICE);
241
1
	});
242

            
243
	// We now send back some money to the relay
244
1
	let dest = Location {
245
1
		parents: 1,
246
1
		interior: [AccountId32 {
247
1
			network: None,
248
1
			id: RELAYALICE.into(),
249
1
		}]
250
1
		.into(),
251
1
	};
252
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
253
1
	ParaA::execute_with(|| {
254
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_id), 123);
255
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
256
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
257
1
			assets: Wild(All),
258
1
			beneficiary: beneficiary.clone(),
259
1
		}]);
260
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
261
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
262
1
			Box::new(VersionedLocation::from(chain_part)),
263
1
			Box::new(VersionedAssets::from(vec![asset])),
264
1
			Box::new(TransferType::DestinationReserve),
265
1
			Box::new(fees_id),
266
1
			Box::new(TransferType::DestinationReserve),
267
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
268
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
269
		));
270
1
	});
271

            
272
	// The balances in paraAlice should have been substracted
273
1
	ParaA::execute_with(|| {
274
1
		assert_eq!(
275
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
276
1
			Ok(U256::from(0))
277
		);
278
1
	});
279

            
280
	// Balances in the relay should have been received
281
1
	Relay::execute_with(|| {
282
		// free execution,x	 full amount received
283
1
		assert!(RelayBalances::free_balance(&RELAYALICE) > balance_before_sending);
284
1
	});
285
1
}
286

            
287
#[test]
288
1
fn send_relay_asset_to_para_b() {
289
1
	MockNet::reset();
290

            
291
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
292
1
	let source_id: parachain::AssetId = source_location.clone().into();
293

            
294
1
	let asset_metadata = parachain::AssetMetadata {
295
1
		name: b"RelayToken".to_vec(),
296
1
		symbol: b"Relay".to_vec(),
297
1
		decimals: 12,
298
1
	};
299

            
300
	// Register asset in paraA. Free execution
301
1
	ParaA::execute_with(|| {
302
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
303
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
304
1
			.try_into()
305
1
			.expect("v3 to latest location conversion failed");
306
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
307
1
			source_id,
308
1
			source_location_latest,
309
1
			asset_metadata.decimals,
310
1
			asset_metadata.symbol.clone().try_into().expect("too long"),
311
1
			asset_metadata.name.clone().try_into().expect("too long"),
312
		));
313
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
314
1
	});
315

            
316
	// Register asset in paraB. Free execution
317
1
	ParaB::execute_with(|| {
318
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
319
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
320
1
			.try_into()
321
1
			.expect("v3 to latest location conversion failed");
322
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
323
1
			source_id,
324
1
			source_location_latest,
325
1
			asset_metadata.decimals,
326
1
			asset_metadata.symbol.try_into().expect("too long"),
327
1
			asset_metadata.name.try_into().expect("too long"),
328
		));
329
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
330
1
	});
331

            
332
1
	let dest: Location = Junction::AccountKey20 {
333
1
		network: None,
334
1
		key: PARAALICE,
335
1
	}
336
1
	.into();
337
1
	Relay::execute_with(|| {
338
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
339
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
340
1
			assets: Wild(All),
341
1
			beneficiary: dest.clone(),
342
1
		}]);
343
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
344
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
345
1
			Box::new(Parachain(1).into()),
346
1
			Box::new(([] /* Here */, 123).into()),
347
1
			Box::new(TransferType::LocalReserve),
348
1
			Box::new(fees_id),
349
1
			Box::new(TransferType::LocalReserve),
350
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
351
1
			WeightLimit::Unlimited
352
		));
353
1
	});
354

            
355
1
	ParaA::execute_with(|| {
356
		// free execution, full amount received
357
1
		assert_eq!(
358
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
359
1
			Ok(U256::from(123))
360
		);
361
1
	});
362

            
363
	// Now send relay asset from para A to para B
364
1
	let dest = Location {
365
1
		parents: 1,
366
1
		interior: [
367
1
			Parachain(2),
368
1
			AccountKey20 {
369
1
				network: None,
370
1
				key: PARAALICE.into(),
371
1
			},
372
1
		]
373
1
		.into(),
374
1
	};
375
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
376
1
	ParaA::execute_with(|| {
377
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_id), 100);
378
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
379
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
380
1
			assets: Wild(All),
381
1
			beneficiary: beneficiary.clone(),
382
1
		}]);
383
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
384
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
385
1
			Box::new(VersionedLocation::from(chain_part)),
386
1
			Box::new(VersionedAssets::from(vec![asset])),
387
1
			Box::new(TransferType::RemoteReserve(Location::parent().into())),
388
1
			Box::new(fees_id),
389
1
			Box::new(TransferType::RemoteReserve(Location::parent().into())),
390
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
391
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
392
		));
393
1
	});
394

            
395
	// Para A balances should have been substracted
396
1
	ParaA::execute_with(|| {
397
1
		assert_eq!(
398
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
399
1
			Ok(U256::from(23))
400
		);
401
1
	});
402

            
403
	// Para B balances should have been credited
404
1
	ParaB::execute_with(|| {
405
1
		assert_eq!(
406
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
407
1
			Ok(U256::from(100))
408
		);
409
1
	});
410
1
}
411

            
412
#[test]
413
1
fn send_para_a_asset_to_para_b() {
414
1
	MockNet::reset();
415

            
416
	// This represents the asset in paraA
417
1
	let para_a_balances = Location::new(1, [Parachain(1), PalletInstance(1u8)]);
418
1
	let source_location: AssetType = para_a_balances
419
1
		.try_into()
420
1
		.expect("Location convertion to AssetType should succeed");
421
1
	let source_id: parachain::AssetId = source_location.clone().into();
422

            
423
1
	let asset_metadata = parachain::AssetMetadata {
424
1
		name: b"ParaAToken".to_vec(),
425
1
		symbol: b"ParaA".to_vec(),
426
1
		decimals: 18,
427
1
	};
428

            
429
	// Register asset in paraB. Free execution
430
1
	ParaB::execute_with(|| {
431
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
432
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
433
1
			.try_into()
434
1
			.expect("v3 to latest location conversion failed");
435
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
436
1
			source_id,
437
1
			source_location_latest,
438
1
			asset_metadata.decimals,
439
1
			asset_metadata.symbol.try_into().expect("too long"),
440
1
			asset_metadata.name.try_into().expect("too long"),
441
		));
442
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
443
1
	});
444

            
445
	// Send para A asset from para A to para B
446
1
	let dest = Location {
447
1
		parents: 1,
448
1
		interior: [
449
1
			Parachain(2),
450
1
			AccountKey20 {
451
1
				network: None,
452
1
				key: PARAALICE.into(),
453
1
			},
454
1
		]
455
1
		.into(),
456
1
	};
457

            
458
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
459
	// Native token is substracted in paraA
460
1
	ParaA::execute_with(|| {
461
1
		let asset = currency_to_asset(parachain::CurrencyId::SelfReserve, 100);
462
		// Free execution, full amount received
463
1
		assert_ok!(PolkadotXcm::transfer_assets(
464
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
465
1
			Box::new(VersionedLocation::from(chain_part)),
466
1
			Box::new(VersionedLocation::from(beneficiary)),
467
1
			Box::new(VersionedAssets::from(asset)),
468
			0,
469
1
			WeightLimit::Limited(Weight::from_parts(800000u64, DEFAULT_PROOF_SIZE))
470
		));
471
1
	});
472

            
473
1
	ParaA::execute_with(|| {
474
1
		assert_eq!(
475
1
			ParaBalances::free_balance(&PARAALICE.into()),
476
			INITIAL_BALANCE - 100
477
		);
478
1
	});
479

            
480
	// Asset is minted in paraB
481
1
	ParaB::execute_with(|| {
482
		// Free execution, full amount received
483
1
		assert_eq!(
484
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
485
1
			Ok(U256::from(100))
486
		);
487
1
	});
488
1
}
489

            
490
#[test]
491
1
fn send_para_a_asset_from_para_b_to_para_c() {
492
1
	MockNet::reset();
493

            
494
	// Represents para A asset
495
1
	let para_a_balances = Location::new(1, [Parachain(1), PalletInstance(1u8)]);
496
1
	let source_location: AssetType = para_a_balances
497
1
		.try_into()
498
1
		.expect("Location convertion to AssetType should succeed");
499
1
	let source_id: parachain::AssetId = source_location.clone().into();
500

            
501
1
	let asset_metadata = parachain::AssetMetadata {
502
1
		name: b"ParaAToken".to_vec(),
503
1
		symbol: b"ParaA".to_vec(),
504
1
		decimals: 18,
505
1
	};
506

            
507
	// Register para A asset in parachain B. Free execution
508
1
	ParaB::execute_with(|| {
509
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
510
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
511
1
			.try_into()
512
1
			.expect("v3 to latest location conversion failed");
513
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
514
1
			source_id,
515
1
			source_location_latest,
516
1
			asset_metadata.decimals,
517
1
			asset_metadata.symbol.clone().try_into().expect("too long"),
518
1
			asset_metadata.name.clone().try_into().expect("too long"),
519
		));
520
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
521
1
	});
522

            
523
	// Register para A asset in parachain C. Free execution
524
1
	ParaC::execute_with(|| {
525
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
526
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
527
1
			.try_into()
528
1
			.expect("v3 to latest location conversion failed");
529
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
530
1
			source_id,
531
1
			source_location_latest,
532
1
			asset_metadata.decimals,
533
1
			asset_metadata.symbol.try_into().expect("too long"),
534
1
			asset_metadata.name.try_into().expect("too long"),
535
		));
536
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
537
1
	});
538

            
539
1
	let dest = Location {
540
1
		parents: 1,
541
1
		interior: [
542
1
			Parachain(2),
543
1
			AccountKey20 {
544
1
				network: None,
545
1
				key: PARAALICE.into(),
546
1
			},
547
1
		]
548
1
		.into(),
549
1
	};
550
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
551
1
	ParaA::execute_with(|| {
552
1
		let asset = currency_to_asset(parachain::CurrencyId::SelfReserve, 100);
553
1
		assert_ok!(PolkadotXcm::transfer_assets(
554
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
555
1
			Box::new(VersionedLocation::from(chain_part)),
556
1
			Box::new(VersionedLocation::from(beneficiary)),
557
1
			Box::new(VersionedAssets::from(asset)),
558
			0,
559
1
			WeightLimit::Limited(Weight::from_parts(80u64, DEFAULT_PROOF_SIZE))
560
		));
561
1
	});
562

            
563
	// Para A balances have been substracted
564
1
	ParaA::execute_with(|| {
565
1
		assert_eq!(
566
1
			ParaBalances::free_balance(&PARAALICE.into()),
567
			INITIAL_BALANCE - 100
568
		);
569
1
	});
570

            
571
	// Para B balances have been credited
572
1
	ParaB::execute_with(|| {
573
1
		assert_eq!(
574
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
575
1
			Ok(U256::from(100))
576
		);
577
1
	});
578

            
579
	// Send para A asset from para B to para C
580
1
	let dest = Location {
581
1
		parents: 1,
582
1
		interior: [
583
1
			Parachain(3),
584
1
			AccountKey20 {
585
1
				network: None,
586
1
				key: PARAALICE.into(),
587
1
			},
588
1
		]
589
1
		.into(),
590
1
	};
591
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
592
1
	ParaB::execute_with(|| {
593
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_id), 100);
594
1
		assert_ok!(PolkadotXcm::transfer_assets(
595
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
596
1
			Box::new(VersionedLocation::from(chain_part)),
597
1
			Box::new(VersionedLocation::from(beneficiary)),
598
1
			Box::new(VersionedAssets::from(asset)),
599
			0,
600
1
			WeightLimit::Limited(Weight::from_parts(80u64, DEFAULT_PROOF_SIZE))
601
		));
602
1
	});
603

            
604
	// The message passed through parachainA so we needed to pay since its the native token
605
1
	ParaC::execute_with(|| {
606
1
		assert_eq!(
607
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
608
1
			Ok(U256::from(95))
609
		);
610
1
	});
611
1
}
612

            
613
#[test]
614
1
fn send_para_a_asset_to_para_b_and_back_to_para_a() {
615
1
	MockNet::reset();
616

            
617
	// para A asset
618
1
	let para_a_balances = Location::new(1, [Parachain(1), PalletInstance(1u8)]);
619
1
	let source_location: AssetType = para_a_balances
620
1
		.try_into()
621
1
		.expect("Location convertion to AssetType should succeed");
622
1
	let source_id: parachain::AssetId = source_location.clone().into();
623

            
624
1
	let asset_metadata = parachain::AssetMetadata {
625
1
		name: b"ParaAToken".to_vec(),
626
1
		symbol: b"ParaA".to_vec(),
627
1
		decimals: 18,
628
1
	};
629

            
630
	// Register para A asset in para B
631
1
	ParaB::execute_with(|| {
632
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
633
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
634
1
			.try_into()
635
1
			.expect("v3 to latest location conversion failed");
636
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
637
1
			source_id,
638
1
			source_location_latest,
639
1
			asset_metadata.decimals,
640
1
			asset_metadata.symbol.try_into().expect("too long"),
641
1
			asset_metadata.name.try_into().expect("too long"),
642
		));
643
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
644
1
	});
645

            
646
	// Send para A asset to para B
647
1
	let dest = Location {
648
1
		parents: 1,
649
1
		interior: [
650
1
			Parachain(2),
651
1
			AccountKey20 {
652
1
				network: None,
653
1
				key: PARAALICE.into(),
654
1
			},
655
1
		]
656
1
		.into(),
657
1
	};
658
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
659
1
	ParaA::execute_with(|| {
660
1
		let asset = currency_to_asset(parachain::CurrencyId::SelfReserve, 100);
661
1
		assert_ok!(PolkadotXcm::transfer_assets(
662
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
663
1
			Box::new(VersionedLocation::from(chain_part)),
664
1
			Box::new(VersionedLocation::from(beneficiary)),
665
1
			Box::new(VersionedAssets::from(asset)),
666
			0,
667
1
			WeightLimit::Limited(Weight::from_parts(80u64, DEFAULT_PROOF_SIZE))
668
		));
669
1
	});
670

            
671
	// Balances have been substracted
672
1
	ParaA::execute_with(|| {
673
1
		assert_eq!(
674
1
			ParaBalances::free_balance(&PARAALICE.into()),
675
			INITIAL_BALANCE - 100
676
		);
677
1
	});
678

            
679
	// Para B balances have been credited
680
1
	ParaB::execute_with(|| {
681
1
		assert_eq!(
682
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
683
1
			Ok(U256::from(100))
684
		);
685
1
	});
686

            
687
	// Send back para A asset to para A
688
1
	let dest = Location {
689
1
		parents: 1,
690
1
		interior: [
691
1
			Parachain(1),
692
1
			AccountKey20 {
693
1
				network: None,
694
1
				key: PARAALICE.into(),
695
1
			},
696
1
		]
697
1
		.into(),
698
1
	};
699
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
700
1
	ParaB::execute_with(|| {
701
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_id), 100);
702
1
		assert_ok!(PolkadotXcm::transfer_assets(
703
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
704
1
			Box::new(VersionedLocation::from(chain_part)),
705
1
			Box::new(VersionedLocation::from(beneficiary)),
706
1
			Box::new(VersionedAssets::from(asset)),
707
			0,
708
1
			WeightLimit::Limited(Weight::from_parts(80u64, DEFAULT_PROOF_SIZE))
709
		));
710
1
	});
711

            
712
1
	ParaA::execute_with(|| {
713
		// Weight used is 4
714
1
		assert_eq!(
715
1
			ParaBalances::free_balance(&PARAALICE.into()),
716
			INITIAL_BALANCE - 4
717
		);
718
1
	});
719
1
}
720

            
721
#[test]
722
1
fn receive_relay_asset_with_trader() {
723
1
	MockNet::reset();
724

            
725
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
726
1
	let source_id: parachain::AssetId = source_location.clone().into();
727

            
728
1
	let asset_metadata = parachain::AssetMetadata {
729
1
		name: b"RelayToken".to_vec(),
730
1
		symbol: b"Relay".to_vec(),
731
1
		decimals: 12,
732
1
	};
733

            
734
	// This time we are gonna put a rather high number of units per second
735
	// we know later we will divide by 1e12
736
	// Lets put 1e6 as units per second
737
1
	ParaA::execute_with(|| {
738
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
739
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
740
1
			.try_into()
741
1
			.expect("v3 to latest location conversion failed");
742
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
743
1
			source_id,
744
1
			source_location_latest,
745
1
			asset_metadata.decimals,
746
1
			asset_metadata.symbol.try_into().expect("too long"),
747
1
			asset_metadata.name.try_into().expect("too long"),
748
		));
749
1
		assert_ok!(add_supported_asset(
750
1
			source_location.clone(),
751
			2500000000000u128
752
		));
753
1
	});
754

            
755
1
	let dest: Location = Junction::AccountKey20 {
756
1
		network: None,
757
1
		key: PARAALICE,
758
1
	}
759
1
	.into();
760
	// We are sending 100 tokens from relay.
761
	// Amount spent in fees is Units per second * weight / 1_000_000_000_000 (weight per second)
762
	// weight is 4 since we are executing 4 instructions with a unitweightcost of 1.
763
	// Units per second should be 2_500_000_000_000_000
764
	// Therefore with no refund, we should receive 10 tokens less
765
	// Native trader fails for this, and we use the asset trader
766
1
	Relay::execute_with(|| {
767
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
768
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
769
1
			assets: Wild(All),
770
1
			beneficiary: dest.clone(),
771
1
		}]);
772
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
773
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
774
1
			Box::new(Parachain(1).into()),
775
1
			Box::new(([] /* Here */, 100).into()),
776
1
			Box::new(TransferType::LocalReserve),
777
1
			Box::new(fees_id),
778
1
			Box::new(TransferType::LocalReserve),
779
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
780
1
			WeightLimit::Unlimited
781
		));
782
1
	});
783

            
784
1
	ParaA::execute_with(|| {
785
		// non-free execution, not full amount received
786
1
		assert_eq!(
787
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
788
1
			Ok(U256::from(90))
789
		);
790
		// Fee should have been received by treasury
791
1
		assert_eq!(
792
1
			EvmForeignAssets::balance(source_id, Treasury::account_id()),
793
1
			Ok(U256::from(10))
794
		);
795
1
	});
796
1
}
797

            
798
#[test]
799
1
fn send_para_a_asset_to_para_b_with_trader() {
800
1
	MockNet::reset();
801

            
802
1
	let para_a_balances = Location::new(1, [Parachain(1), PalletInstance(1u8)]);
803
1
	let source_location: AssetType = para_a_balances
804
1
		.try_into()
805
1
		.expect("Location convertion to AssetType should succeed");
806
1
	let source_id: parachain::AssetId = source_location.clone().into();
807

            
808
1
	let asset_metadata = parachain::AssetMetadata {
809
1
		name: b"ParaAToken".to_vec(),
810
1
		symbol: b"ParaA".to_vec(),
811
1
		decimals: 18,
812
1
	};
813

            
814
1
	ParaB::execute_with(|| {
815
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
816
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
817
1
			.try_into()
818
1
			.expect("v3 to latest location conversion failed");
819
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
820
1
			source_id,
821
1
			source_location_latest,
822
1
			asset_metadata.decimals,
823
1
			asset_metadata.symbol.try_into().expect("too long"),
824
1
			asset_metadata.name.try_into().expect("too long"),
825
		));
826
1
		assert_ok!(add_supported_asset(
827
1
			source_location.clone(),
828
			2500000000000u128
829
		));
830
1
	});
831

            
832
1
	let dest = Location {
833
1
		parents: 1,
834
1
		interior: [
835
1
			Parachain(2),
836
1
			AccountKey20 {
837
1
				network: None,
838
1
				key: PARAALICE.into(),
839
1
			},
840
1
		]
841
1
		.into(),
842
1
	};
843

            
844
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
845
	// In destination chain, we only need 4 weight
846
	// We put 10 weight, 6 of which should be refunded and 4 of which should go to treasury
847
1
	ParaA::execute_with(|| {
848
1
		let asset = currency_to_asset(parachain::CurrencyId::SelfReserve, 100);
849
1
		assert_ok!(PolkadotXcm::transfer_assets(
850
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
851
1
			Box::new(VersionedLocation::from(chain_part)),
852
1
			Box::new(VersionedLocation::from(beneficiary)),
853
1
			Box::new(VersionedAssets::from(asset)),
854
			0,
855
1
			WeightLimit::Limited(Weight::from_parts(10u64, DEFAULT_PROOF_SIZE))
856
		));
857
1
	});
858
1
	ParaA::execute_with(|| {
859
		// free execution, full amount received
860
1
		assert_eq!(
861
1
			ParaBalances::free_balance(&PARAALICE.into()),
862
			INITIAL_BALANCE - 100
863
		);
864
1
	});
865

            
866
	// We are sending 100 tokens from para A.
867
	// Amount spent in fees is Units per second * weight / 1_000_000_000_000 (weight per second)
868
	// weight is 4 since we are executing 4 instructions with a unitweightcost of 1.
869
	// Units per second should be 2_500_000_000_000_000
870
	// Since we set 10 weight in destination chain, 25 will be charged upfront
871
	// 15 of those will be refunded, while 10 will go to treasury as the true weight used
872
	// will be 4
873
1
	ParaB::execute_with(|| {
874
1
		assert_eq!(
875
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
876
1
			Ok(U256::from(90))
877
		);
878
		// Fee should have been received by treasury
879
1
		assert_eq!(
880
1
			EvmForeignAssets::balance(source_id, Treasury::account_id()),
881
1
			Ok(U256::from(10))
882
		);
883
1
	});
884
1
}
885

            
886
#[test]
887
1
fn send_para_a_asset_to_para_b_with_trader_and_fee() {
888
1
	MockNet::reset();
889

            
890
1
	let para_a_balances = Location::new(1, [Parachain(1), PalletInstance(1u8)]);
891
1
	let source_location: AssetType = para_a_balances
892
1
		.try_into()
893
1
		.expect("Location convertion to AssetType should succeed");
894
1
	let source_id: parachain::AssetId = source_location.clone().into();
895

            
896
1
	let asset_metadata = parachain::AssetMetadata {
897
1
		name: b"ParaAToken".to_vec(),
898
1
		symbol: b"ParaA".to_vec(),
899
1
		decimals: 18,
900
1
	};
901

            
902
1
	ParaB::execute_with(|| {
903
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
904
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
905
1
			.try_into()
906
1
			.expect("v3 to latest location conversion failed");
907
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
908
1
			source_id,
909
1
			source_location_latest,
910
1
			asset_metadata.decimals,
911
1
			asset_metadata.symbol.try_into().expect("too long"),
912
1
			asset_metadata.name.try_into().expect("too long"),
913
		));
914
		// With these units per second, 80K weight convrets to 1 asset unit
915
1
		assert_ok!(add_supported_asset(source_location.clone(), 12500000u128));
916
1
	});
917

            
918
1
	let dest = Location {
919
1
		parents: 1,
920
1
		interior: [
921
1
			Parachain(2),
922
1
			AccountKey20 {
923
1
				network: None,
924
1
				key: PARAALICE.into(),
925
1
			},
926
1
		]
927
1
		.into(),
928
1
	};
929
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
930
	// we use transfer_with_fee
931
1
	ParaA::execute_with(|| {
932
1
		let asset = currency_to_asset(parachain::CurrencyId::SelfReserve, 100);
933
1
		let asset_fee = currency_to_asset(parachain::CurrencyId::SelfReserve, 1);
934
1
		assert_ok!(PolkadotXcm::transfer_assets(
935
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
936
1
			Box::new(VersionedLocation::from(chain_part)),
937
1
			Box::new(VersionedLocation::from(beneficiary)),
938
1
			Box::new(VersionedAssets::from(vec![asset_fee, asset])),
939
			0,
940
1
			WeightLimit::Limited(Weight::from_parts(800000u64, DEFAULT_PROOF_SIZE))
941
		));
942
1
	});
943
1
	ParaA::execute_with(|| {
944
		// 100 tokens transferred plus 1 taken from fees
945
1
		assert_eq!(
946
1
			ParaBalances::free_balance(&PARAALICE.into()),
947
			INITIAL_BALANCE - 100 - 1
948
		);
949
1
	});
950

            
951
1
	ParaB::execute_with(|| {
952
		// free execution, full amount received because trully the xcm instruction does not cost
953
		// what it is specified
954
1
		assert_eq!(
955
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
956
1
			Ok(U256::from(101))
957
		);
958
1
	});
959
1
}
960

            
961
#[test]
962
1
fn error_when_not_paying_enough() {
963
1
	MockNet::reset();
964

            
965
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
966
1
	let source_id: parachain::AssetId = source_location.clone().into();
967

            
968
1
	let asset_metadata = parachain::AssetMetadata {
969
1
		name: b"RelayToken".to_vec(),
970
1
		symbol: b"Relay".to_vec(),
971
1
		decimals: 12,
972
1
	};
973

            
974
1
	let dest: Location = Junction::AccountKey20 {
975
1
		network: None,
976
1
		key: PARAALICE,
977
1
	}
978
1
	.into();
979
	// This time we are gonna put a rather high number of units per second
980
	// we know later we will divide by 1e12
981
	// Lets put 1e6 as units per second
982
1
	ParaA::execute_with(|| {
983
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
984
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
985
1
			.try_into()
986
1
			.expect("v3 to latest location conversion failed");
987
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
988
1
			source_id,
989
1
			source_location_latest,
990
1
			asset_metadata.decimals,
991
1
			asset_metadata.symbol.try_into().expect("too long"),
992
1
			asset_metadata.name.try_into().expect("too long"),
993
		));
994
1
		assert_ok!(add_supported_asset(
995
1
			source_location.clone(),
996
			2500000000000u128
997
		));
998
1
	});
999

            
	// We are sending 100 tokens from relay.
	// If we set the dest weight to be 1e7, we know the buy_execution will spend 1e7*1e6/1e12 = 10
	// Therefore with no refund, we should receive 10 tokens less
1
	Relay::execute_with(|| {
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 5).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
1
	ParaA::execute_with(|| {
		// amount not received as it is not paying enough
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(0))
		);
1
	});
1
}
#[test]
1
fn transact_through_derivative_multilocation() {
1
	MockNet::reset();
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 1u128));
		// Root can set transact info
1
		assert_ok!(XcmTransactor::set_transact_info(
1
			parachain::RuntimeOrigin::root(),
1
			Box::new(xcm::VersionedLocation::from(Location::parent())),
			// Relay charges 1000 for every instruction, and we have 3, so 3000
1
			3000.into(),
1
			20000000000.into(),
1
			None
		));
		// Root can set transact info
		// Set fee per second using weight-trader (replaces old set_fee_per_second)
1
		set_fee_per_second_for_location(Location::parent(), WEIGHT_REF_TIME_PER_SECOND as u128)
1
			.expect("must succeed");
1
	});
	// Let's construct the call to know how much weight it is going to require
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
		// 4000000000 transact + 3000 correspond to 4000003000 tokens. 100 more for the transfer call
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 4000003100u128).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000003100u64))
		);
1
	});
	// Register address
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::register(
1
			parachain::RuntimeOrigin::root(),
1
			PARAALICE.into(),
			0,
		));
1
	});
	// Send to registered address
1
	let registered_address = derivative_account_id(para_a_account(), 0);
1
	let dest = Location {
1
		parents: 1,
1
		interior: [AccountId32 {
1
			network: None,
1
			id: registered_address.clone().into(),
1
		}]
1
		.into(),
1
	};
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
1
	ParaA::execute_with(|| {
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_id), 100);
		// free execution, full amount received
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(vec![asset])),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000003000u64))
		);
1
	});
	// What we will do now is transfer this relay tokens from the derived account to the sovereign
	// again
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 4000003000);
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index = <relay_chain::Runtime as frame_system::Config>::PalletInfo::index::<
1
		relay_chain::Balances,
1
	>()
1
	.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<relay_chain::Runtime>::transfer_allow_death {
1
		dest: para_a_account(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_derivative(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			parachain::MockTransactors::Relay,
			0,
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				fee_amount: None
1
			},
1
			encoded,
			// 400000000 + 3000 we should have taken out 4000003000 tokens from the caller
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: None
1
			},
			false
		));
1
	});
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 100);
1
		assert!(RelayBalances::free_balance(&registered_address) == 0);
1
	});
1
}
#[test]
1
fn transact_through_derivative_with_custom_fee_weight() {
1
	MockNet::reset();
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 1u128));
1
	});
	// Let's construct the call to know how much weight it is going to require
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
		// 4000000000 transact + 3000 correspond to 4000003000 tokens. 100 more for the transfer call
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 4000003100u128).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000003100u64))
		);
1
	});
	// Register address
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::register(
1
			parachain::RuntimeOrigin::root(),
1
			PARAALICE.into(),
			0,
		));
1
	});
	// Send to registered address
1
	let registered_address = derivative_account_id(para_a_account(), 0);
1
	let dest = Location {
1
		parents: 1,
1
		interior: [AccountId32 {
1
			network: None,
1
			id: registered_address.clone().into(),
1
		}]
1
		.into(),
1
	};
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
1
	ParaA::execute_with(|| {
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_id), 100);
		// free execution, full amount received
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(vec![asset])),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000003000u64))
		);
1
	});
	// What we will do now is transfer this relay tokens from the derived account to the sovereign
	// again
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 4000003000);
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index = <relay_chain::Runtime as frame_system::Config>::PalletInfo::index::<
1
		relay_chain::Balances,
1
	>()
1
	.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<relay_chain::Runtime>::transfer_allow_death {
1
		dest: para_a_account(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	let overall_weight = 4000003000u64;
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_derivative(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			parachain::MockTransactors::Relay,
			0,
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				// 1-1 fee weight mapping
1
				fee_amount: Some(overall_weight as u128)
1
			},
			// 4000000000 + 3000 we should have taken out 4000003000 tokens from the caller
1
			encoded,
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: Some(Limited(overall_weight.into()))
1
			},
			false
		));
1
		let event_found: Option<parachain::RuntimeEvent> = parachain::para_events()
1
			.iter()
18
			.find_map(|event| match event.clone() {
				parachain::RuntimeEvent::PolkadotXcm(pallet_xcm::Event::AssetsTrapped {
					..
				}) => Some(event.clone()),
18
				_ => None,
18
			});
		// Assert that the events do not contain the assets being trapped
1
		assert!(event_found.is_none());
1
	});
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 100);
1
		assert!(RelayBalances::free_balance(&registered_address) == 0);
1
	});
1
}
#[test]
1
fn transact_through_derivative_with_custom_fee_weight_refund() {
1
	MockNet::reset();
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 1u128));
1
	});
	// Let's construct the call to know how much weight it is going to require
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
		// 4000000000 transact + 9000 correspond to 4000009000 tokens. 100 more for the transfer call
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 4000009100u128).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000009100u64))
		);
1
	});
	// Register address
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::register(
1
			parachain::RuntimeOrigin::root(),
1
			PARAALICE.into(),
			0,
		));
1
	});
	// Send to registered address
1
	let registered_address = derivative_account_id(para_a_account(), 0);
1
	let dest = Location {
1
		parents: 1,
1
		interior: [AccountId32 {
1
			network: None,
1
			id: registered_address.clone().into(),
1
		}]
1
		.into(),
1
	};
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
1
	ParaA::execute_with(|| {
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_id), 100);
		// free execution, full amount received
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(vec![asset])),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000009000u64))
		);
1
	});
	// What we will do now is transfer this relay tokens from the derived account to the sovereign
	// again
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 4000009000);
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index = <relay_chain::Runtime as frame_system::Config>::PalletInfo::index::<
1
		relay_chain::Balances,
1
	>()
1
	.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<relay_chain::Runtime>::transfer_allow_death {
1
		dest: para_a_account(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	let overall_weight = 4000009000u64;
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_derivative(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			parachain::MockTransactors::Relay,
			0,
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				// 1-1 fee weight mapping
1
				fee_amount: Some(overall_weight as u128)
1
			},
1
			encoded,
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: Some(Limited(overall_weight.into()))
1
			},
			true
		));
1
		let event_found: Option<parachain::RuntimeEvent> = parachain::para_events()
1
			.iter()
18
			.find_map(|event| match event.clone() {
				parachain::RuntimeEvent::PolkadotXcm(pallet_xcm::Event::AssetsTrapped {
					..
				}) => Some(event.clone()),
18
				_ => None,
18
			});
		// Assert that the events do not contain the assets being trapped
1
		assert!(event_found.is_none());
1
	});
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
		// 4000009000 refunded + 100 transferred = 4000009100
1
		assert_eq!(RelayBalances::free_balance(&para_a_account()), 4000009100);
1
		assert_eq!(RelayBalances::free_balance(&registered_address), 0);
1
	});
1
}
#[test]
1
fn transact_through_sovereign() {
1
	MockNet::reset();
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 1u128));
		// Root can set transact info
1
		assert_ok!(XcmTransactor::set_transact_info(
1
			parachain::RuntimeOrigin::root(),
1
			Box::new(xcm::VersionedLocation::from(Location::parent())),
			// Relay charges 1000 for every instruction, and we have 3, so 3000
1
			3000.into(),
1
			20000000000.into(),
1
			None
		));
		// Root can set transact info
		// Set fee per second using weight-trader (replaces old set_fee_per_second)
1
		set_fee_per_second_for_location(Location::parent(), WEIGHT_REF_TIME_PER_SECOND as u128)
1
			.expect("must succeed");
1
	});
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 4000003100u128).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000003100u64))
		);
1
	});
	// Register address
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::register(
1
			parachain::RuntimeOrigin::root(),
1
			PARAALICE.into(),
			0,
		));
1
	});
	// Send to registered address
1
	let registered_address = derivative_account_id(para_a_account(), 0);
1
	let dest = Location {
1
		parents: 1,
1
		interior: [AccountId32 {
1
			network: None,
1
			id: registered_address.clone().into(),
1
		}]
1
		.into(),
1
	};
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
1
	ParaA::execute_with(|| {
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_id), 100);
		// free execution, full amount received
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(vec![asset])),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000003000u64))
		);
1
	});
	// What we will do now is transfer this relay tokens from the derived account to the sovereign
	// again
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 4000003000);
1
		0
1
	});
	// We send the xcm transact operation to parent
1
	let dest = Location {
1
		parents: 1,
1
		interior: /* Here */ [].into(),
1
	};
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index = <relay_chain::Runtime as frame_system::Config>::PalletInfo::index::<
1
		relay_chain::Balances,
1
	>()
1
	.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<relay_chain::Runtime>::transfer_allow_death {
1
		dest: para_a_account(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
	// Root can directly pass the execution byes to the sovereign
1
	ParaA::execute_with(|| {
1
		let utility_bytes = <XcmTransactor as UtilityEncodeCall>::encode_call(
1
			parachain::MockTransactors::Relay,
1
			xcm_primitives::UtilityAvailableCalls::AsDerivative(0, encoded),
		);
1
		assert_ok!(XcmTransactor::transact_through_sovereign(
1
			parachain::RuntimeOrigin::root(),
1
			Box::new(xcm::VersionedLocation::from(dest)),
1
			Some(PARAALICE.into()),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				fee_amount: None
1
			},
1
			utility_bytes,
1
			OriginKind::SovereignAccount,
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: None
1
			},
			false
		));
1
	});
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 100);
1
		assert!(RelayBalances::free_balance(&registered_address) == 0);
1
	});
1
}
#[test]
1
fn transact_through_sovereign_fee_payer_none() {
1
	MockNet::reset();
1
	ParaA::execute_with(|| {
		// Root can set transact info
1
		assert_ok!(XcmTransactor::set_transact_info(
1
			parachain::RuntimeOrigin::root(),
1
			Box::new(xcm::VersionedLocation::from(Location::parent())),
			// Relay charges 1000 for every instruction, and we have 3, so 3000
1
			3000.into(),
1
			20000000000.into(),
1
			None
		));
		// Root can set transact info
		// Set fee per second using weight-trader (replaces old set_fee_per_second)
1
		set_fee_per_second_for_location(Location::parent(), WEIGHT_REF_TIME_PER_SECOND as u128)
1
			.expect("must succeed");
1
	});
1
	let derivative_address = derivative_account_id(para_a_account(), 0);
1
	Relay::execute_with(|| {
		// Transfer 100 tokens to derivative_address on the relay
1
		assert_ok!(RelayBalances::transfer_keep_alive(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			derivative_address.clone(),
			100u128
		));
		// Transfer the XCM execution fee amount to ParaA's sovereign account
1
		assert_ok!(RelayBalances::transfer_keep_alive(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			para_a_account(),
			4000003000u128
		));
1
	});
	// Check balances before the transact call
1
	Relay::execute_with(|| {
1
		assert_eq!(RelayBalances::free_balance(&para_a_account()), 4000003000);
1
		assert_eq!(RelayBalances::free_balance(&derivative_address), 100);
1
		assert_eq!(RelayBalances::free_balance(&RELAYBOB), 0);
1
	});
	// Encode the call. Balances transfer of 100 relay tokens to RELAYBOB
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index = <relay_chain::Runtime as frame_system::Config>::PalletInfo::index::<
1
		relay_chain::Balances,
1
	>()
1
	.unwrap() as u8;
1
	encoded.push(index);
1
	let mut call_bytes = pallet_balances::Call::<relay_chain::Runtime>::transfer_allow_death {
1
		dest: RELAYBOB,
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
	// We send the xcm transact operation to parent
1
	let dest = Location {
1
		parents: 1,
1
		interior: /* Here */ [].into(),
1
	};
	// Root can directly pass the execution byes to the sovereign
1
	ParaA::execute_with(|| {
		// The final call will be an AsDerivative using index 0
1
		let utility_bytes = <XcmTransactor as UtilityEncodeCall>::encode_call(
1
			parachain::MockTransactors::Relay,
1
			xcm_primitives::UtilityAvailableCalls::AsDerivative(0, encoded),
		);
1
		assert_ok!(XcmTransactor::transact_through_sovereign(
1
			parachain::RuntimeOrigin::root(),
1
			Box::new(xcm::VersionedLocation::from(dest)),
			// No fee_payer here. The sovereign account will pay the fees on destination.
1
			None,
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				fee_amount: None
1
			},
1
			utility_bytes,
1
			OriginKind::SovereignAccount,
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: None
1
			},
			false
		));
1
	});
	// Check balances after the transact call are correct
1
	Relay::execute_with(|| {
1
		assert_eq!(RelayBalances::free_balance(&para_a_account()), 0);
1
		assert_eq!(RelayBalances::free_balance(&derivative_address), 0);
1
		assert_eq!(RelayBalances::free_balance(&RELAYBOB), 100);
1
	});
1
}
#[test]
1
fn transact_through_sovereign_with_custom_fee_weight() {
1
	MockNet::reset();
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 1u128));
1
	});
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 4000003100u128).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000003100u64))
		);
1
	});
	// Register address
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::register(
1
			parachain::RuntimeOrigin::root(),
1
			PARAALICE.into(),
			0,
		));
1
	});
	// Send to registered address
1
	let registered_address = derivative_account_id(para_a_account(), 0);
1
	let dest = Location {
1
		parents: 1,
1
		interior: [AccountId32 {
1
			network: None,
1
			id: registered_address.clone().into(),
1
		}]
1
		.into(),
1
	};
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
1
	ParaA::execute_with(|| {
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_id), 100);
		// free execution, full amount received
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(vec![asset])),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000003000u64))
		);
1
	});
	// What we will do now is transfer this relay tokens from the derived account to the sovereign
	// again
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 4000003000);
1
		0
1
	});
	// We send the xcm transact operation to parent
1
	let dest = Location {
1
		parents: 1,
1
		interior: /* Here */ [].into(),
1
	};
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index = <relay_chain::Runtime as frame_system::Config>::PalletInfo::index::<
1
		relay_chain::Balances,
1
	>()
1
	.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<relay_chain::Runtime>::transfer_allow_death {
1
		dest: para_a_account(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	let total_weight = 4000003000u64;
	// Root can directly pass the execution byes to the sovereign
1
	ParaA::execute_with(|| {
1
		let utility_bytes = <XcmTransactor as UtilityEncodeCall>::encode_call(
1
			parachain::MockTransactors::Relay,
1
			xcm_primitives::UtilityAvailableCalls::AsDerivative(0, encoded),
		);
1
		assert_ok!(XcmTransactor::transact_through_sovereign(
1
			parachain::RuntimeOrigin::root(),
1
			Box::new(xcm::VersionedLocation::from(dest)),
1
			Some(PARAALICE.into()),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				// 1-1 fee-weight mapping
1
				fee_amount: Some(total_weight as u128)
1
			},
1
			utility_bytes,
1
			OriginKind::SovereignAccount,
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: Some(Limited(total_weight.into()))
1
			},
			false
		));
1
	});
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 100);
1
		assert!(RelayBalances::free_balance(&registered_address) == 0);
1
	});
1
}
#[test]
1
fn transact_through_sovereign_with_custom_fee_weight_refund() {
1
	MockNet::reset();
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 1u128));
1
	});
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 4000009100u128).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000009100u64))
		);
1
	});
	// Register address
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::register(
1
			parachain::RuntimeOrigin::root(),
1
			PARAALICE.into(),
			0,
		));
1
	});
	// Send to registered address
1
	let registered_address = derivative_account_id(para_a_account(), 0);
1
	let dest = Location {
1
		parents: 1,
1
		interior: [AccountId32 {
1
			network: None,
1
			id: registered_address.clone().into(),
1
		}]
1
		.into(),
1
	};
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
1
	ParaA::execute_with(|| {
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_id), 100);
		// free execution, full amount received
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(vec![asset])),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
		));
1
	});
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(4000009000u64))
		);
1
	});
	// What we will do now is transfer this relay tokens from the derived account to the sovereign
	// again
1
	Relay::execute_with(|| {
		// free execution,x	 full amount received
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 4000009000);
1
		0
1
	});
	// We send the xcm transact operation to parent
1
	let dest = Location {
1
		parents: 1,
1
		interior: /* Here */ [].into(),
1
	};
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index = <relay_chain::Runtime as frame_system::Config>::PalletInfo::index::<
1
		relay_chain::Balances,
1
	>()
1
	.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<relay_chain::Runtime>::transfer_allow_death {
1
		dest: para_a_account(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	let total_weight = 4000009000u64;
	// Root can directly pass the execution byes to the sovereign
1
	ParaA::execute_with(|| {
1
		let utility_bytes = <XcmTransactor as UtilityEncodeCall>::encode_call(
1
			parachain::MockTransactors::Relay,
1
			xcm_primitives::UtilityAvailableCalls::AsDerivative(0, encoded),
		);
1
		assert_ok!(XcmTransactor::transact_through_sovereign(
1
			parachain::RuntimeOrigin::root(),
1
			Box::new(xcm::VersionedLocation::from(dest)),
1
			Some(PARAALICE.into()),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				// 1-1 fee-weight mapping
1
				fee_amount: Some(total_weight as u128)
1
			},
1
			utility_bytes,
1
			OriginKind::SovereignAccount,
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: Some(Limited(total_weight.into()))
1
			},
			true
		));
1
	});
1
	Relay::execute_with(|| {
		// free execution, full amount received
		// 4000009000 refunded + 100 transferred = 4000009100
1
		assert_eq!(RelayBalances::free_balance(&para_a_account()), 4000009100);
1
		assert_eq!(RelayBalances::free_balance(&registered_address), 0);
1
	});
1
}
#[test]
1
fn test_automatic_versioning_on_runtime_upgrade_with_relay() {
1
	MockNet::reset();
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
	// register relay asset in parachain A and set XCM version to 1
1
	ParaA::execute_with(|| {
1
		parachain::XcmVersioner::set_version(1);
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
1
	});
1
	let response = Response::Version(2);
1
	let querier: Location = ([]/* Here */).into();
	// This is irrelevant, nothing will be done with this message,
	// but we need to pass a message as an argument to trigger the storage change
1
	let mock_message: Xcm<()> = Xcm(vec![QueryResponse {
1
		query_id: 0,
1
		response,
1
		max_weight: Weight::zero(),
1
		querier: Some(querier),
1
	}]);
	// The router is mocked, and we cannot use WrapVersion in ChildParachainRouter. So we will force
	// it directly here
	// Actually send relay asset to parachain
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
		// This sets the default version, for not known destinations
1
		assert_ok!(RelayChainPalletXcm::force_default_xcm_version(
1
			relay_chain::RuntimeOrigin::root(),
1
			Some(3)
		));
		// Wrap version, which sets VersionedStorage
		// This is necessary because the mock router does not use wrap_version, but
		// this is not necessary in prod
1
		assert_ok!(<RelayChainPalletXcm as WrapVersion>::wrap_version(
1
			&Parachain(1).into(),
1
			mock_message
		));
		// Transfer assets. Since it is an unknown destination, it will query for version
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 123).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
		// Let's advance the relay. This should trigger the subscription message
1
		relay_chain::relay_roll_to(2);
		// queries should have been updated
1
		assert!(RelayChainPalletXcm::query(&0).is_some());
1
	});
1
	let expected_supported_version: relay_chain::RuntimeEvent =
1
		pallet_xcm::Event::SupportedVersionChanged {
1
			location: Location {
1
				parents: 0,
1
				interior: [Parachain(1)].into(),
1
			},
1
			version: 1,
1
		}
1
		.into();
1
	Relay::execute_with(|| {
		// Assert that the events vector contains the version change
1
		assert!(relay_chain::relay_events().contains(&expected_supported_version));
1
	});
	// ParaA changes version to 2, and calls on_runtime_upgrade. This should notify the targets
	// of the new version change
1
	ParaA::execute_with(|| {
		// Set version
1
		parachain::XcmVersioner::set_version(2);
		// Do runtime upgrade
1
		parachain::on_runtime_upgrade();
		// Initialize block, to call on_initialize and notify targets
1
		parachain::para_roll_to(2);
		// Expect the event in the parachain
1
		assert!(parachain::para_events().iter().any(|e| matches!(
2
			e,
			parachain::RuntimeEvent::PolkadotXcm(pallet_xcm::Event::VersionChangeNotified {
				result: 2,
				..
			})
		)));
1
	});
	// This event should have been seen in the relay
1
	let expected_supported_version_2: relay_chain::RuntimeEvent =
1
		pallet_xcm::Event::SupportedVersionChanged {
1
			location: Location {
1
				parents: 0,
1
				interior: [Parachain(1)].into(),
1
			},
1
			version: 2,
1
		}
1
		.into();
1
	Relay::execute_with(|| {
		// Assert that the events vector contains the new version change
1
		assert!(relay_chain::relay_events().contains(&expected_supported_version_2));
1
	});
1
}
#[test]
1
fn receive_asset_with_no_sufficients_is_possible_for_non_existent_account() {
1
	MockNet::reset();
1
	let fresh_account = PARABOB;
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
	// register relay asset in parachain A
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
1
	});
	// Actually send relay asset to parachain
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: fresh_account,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 123).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
	// parachain should have received assets
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, fresh_account.into()),
1
			Ok(U256::from(123))
		);
1
	});
1
}
#[test]
1
fn receive_assets_with_sufficients_true_allows_non_funded_account_to_receive_assets() {
1
	MockNet::reset();
1
	let fresh_account = [2u8; 20];
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
	// register relay asset in parachain A
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
1
	});
	// Actually send relay asset to parachain
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: fresh_account,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 123).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
	// parachain should have received assets
1
	ParaA::execute_with(|| {
		// free execution, full amount received
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, fresh_account.into()),
1
			Ok(U256::from(123))
		);
1
	});
1
}
#[test]
1
fn evm_account_receiving_assets_should_handle_sufficients_ref_count() {
1
	MockNet::reset();
1
	let mut sufficient_account = [0u8; 20];
1
	sufficient_account[0..20].copy_from_slice(&evm_account()[..]);
1
	let evm_account_id = parachain::AccountId::from(sufficient_account);
	// Evm account is self sufficient
1
	ParaA::execute_with(|| {
1
		assert_eq!(parachain::System::account(evm_account_id).sufficients, 1);
1
	});
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
	// register relay asset in parachain A
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
1
	});
	// Actually send relay asset to parachain
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: sufficient_account,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 123).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
	// Evm account sufficient ref count increased by 1.
1
	ParaA::execute_with(|| {
		// TODO: since the suicided logic was introduced an smart contract account
		// is not deleted completely until it's data is deleted. Data deletion
		// will be implemented in a future release
		// assert_eq!(parachain::System::account(evm_account_id).sufficients, 2);
1
	});
1
	ParaA::execute_with(|| {
		// Remove the account from the evm context.
1
		parachain::EVM::remove_account(&evm_account());
		// Evm account sufficient ref count decreased by 1.
		// TODO: since the suicided logic was introduced an smart contract account
		// is not deleted completely until it's data is deleted. Data deletion
		// will be implemented in a future release
		// assert_eq!(parachain::System::account(evm_account_id).sufficients, 1);
1
	});
1
}
#[test]
1
fn empty_account_should_not_be_reset() {
1
	MockNet::reset();
	// Test account has nonce 1 on genesis.
1
	let sufficient_account = PARABOB;
1
	let evm_account_id = parachain::AccountId::from(sufficient_account);
1
	let source_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
	// register relay asset in parachain A
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
1
	});
	// Send native token to evm_account
1
	ParaA::execute_with(|| {
1
		assert_ok!(ParaBalances::transfer_allow_death(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			evm_account_id,
			100
		));
1
	});
	// Actually send relay asset to parachain
1
	let dest: Location = AccountKey20 {
1
		network: None,
1
		key: sufficient_account,
1
	}
1
	.into();
1
	Relay::execute_with(|| {
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(([] /* Here */, 123).into()),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
1
	ParaA::execute_with(|| {
		// Empty the assets from the account.
		// As this makes the account go below the `min_balance`, the account is considered dead
		// at eyes of pallet-assets, and the consumer reference is decreased by 1 and is now Zero.
		// Transfer using EvmForeignAssets
1
		assert_ok!(EvmForeignAssets::transfer(
1
			source_id,
1
			evm_account_id,
1
			PARAALICE.into(),
1
			U256::from(123)
		));
		// Verify account asset balance is Zero.
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, evm_account_id.into()),
1
			Ok(U256::from(0))
		);
		// Because we no longer have consumer references, we can set the balance to Zero.
		// This would reset the account if our ED were to be > than Zero.
1
		assert_ok!(ParaBalances::force_set_balance(
1
			parachain::RuntimeOrigin::root(),
1
			evm_account_id,
			0,
		));
		// Verify account native balance is Zero.
1
		assert_eq!(ParaBalances::free_balance(&evm_account_id), 0);
		// Remove the account from the evm context.
		// This decreases the sufficients reference by 1 and now is Zero.
1
		parachain::EVM::remove_account(&evm_account());
		// Verify reference count.
1
		let account = parachain::System::account(evm_account_id);
1
		assert_eq!(account.sufficients, 0);
1
		assert_eq!(account.consumers, 0);
1
		assert_eq!(account.providers, 1);
		// We expect the account to be alive in a Zero ED context.
1
		assert_eq!(parachain::System::account_nonce(evm_account_id), 1);
1
	});
1
}
#[test]
1
fn test_statemint_like() {
1
	MockNet::reset();
1
	let dest_para = Location::new(1, [Parachain(1)]);
1
	let sov = xcm_builder::SiblingParachainConvertsVia::<
1
		polkadot_parachain::primitives::Sibling,
1
		statemint_like::AccountId,
1
	>::convert_location(&dest_para)
1
	.unwrap();
1
	let statemint_asset_a_balances = Location::new(
		1,
1
		[
1
			Parachain(1000),
1
			PalletInstance(5),
1
			xcm::latest::prelude::GeneralIndex(0u128),
1
		],
	);
1
	let source_location: AssetType = statemint_asset_a_balances
1
		.try_into()
1
		.expect("Location convertion to AssetType should succeed");
1
	let source_id: parachain::AssetId = source_location.clone().into();
1
	let asset_metadata = parachain::AssetMetadata {
1
		name: b"StatemintToken".to_vec(),
1
		symbol: b"StatemintToken".to_vec(),
1
		decimals: 12,
1
	};
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = source_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_id,
1
			source_location_latest,
1
			asset_metadata.decimals,
1
			asset_metadata.symbol.try_into().expect("too long"),
1
			asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(source_location.clone(), 0u128));
1
	});
1
	Statemint::execute_with(|| {
		// Set new prefix
1
		statemint_like::PrefixChanger::set_prefix(
1
			PalletInstance(<StatemintAssets as PalletInfoAccess>::index() as u8).into(),
		);
1
		assert_ok!(StatemintAssets::create(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
			0,
			RELAYALICE,
			1
		));
1
		assert_ok!(StatemintAssets::mint(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
			0,
			RELAYALICE,
			300000000000000
		));
		// This is needed, since the asset is created as non-sufficient
1
		assert_ok!(StatemintBalances::transfer_allow_death(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
			sov,
			100000000000000
		));
		// Actually send relay asset to parachain
1
		let dest: Location = AccountKey20 {
1
			network: None,
1
			key: PARAALICE,
1
		}
1
		.into();
		// Send with new prefix
1
		let assets: VersionedAssets = (
1
			[
1
				xcm::latest::prelude::PalletInstance(
1
					<StatemintAssets as PalletInfoAccess>::index() as u8,
1
				),
1
				xcm::latest::prelude::GeneralIndex(0),
1
			],
1
			123,
1
		)
1
			.into();
1
		let fees_id: VersionedAssetId = AssetId(Location::new(
1
			0,
1
			[
1
				xcm::latest::prelude::PalletInstance(
1
					<StatemintAssets as PalletInfoAccess>::index() as u8,
1
				),
1
				xcm::latest::prelude::GeneralIndex(0),
1
			],
1
		))
1
		.into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: dest.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
1
	});
1
	ParaA::execute_with(|| {
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_id, PARAALICE.into()),
1
			Ok(U256::from(123))
		);
1
	});
1
}
#[test]
1
fn send_statemint_asset_from_para_a_to_statemint_with_relay_fee() {
1
	MockNet::reset();
	// Relay asset
1
	let relay_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_relay_id: parachain::AssetId = relay_location.clone().into();
1
	let relay_asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
	// Statemint asset
1
	let statemint_asset = Location::new(
		1,
1
		[
1
			Parachain(1000u32),
1
			PalletInstance(5u8),
1
			GeneralIndex(10u128),
1
		],
	);
1
	let statemint_location_asset: AssetType = statemint_asset
1
		.clone()
1
		.try_into()
1
		.expect("Location convertion to AssetType should succeed");
1
	let source_statemint_asset_id: parachain::AssetId = statemint_location_asset.clone().into();
1
	let asset_metadata_statemint_asset = parachain::AssetMetadata {
1
		name: b"USDC".to_vec(),
1
		symbol: b"USDC".to_vec(),
1
		decimals: 12,
1
	};
1
	let dest_para = Location::new(1, [Parachain(1)]);
1
	let sov = xcm_builder::SiblingParachainConvertsVia::<
1
		polkadot_parachain::primitives::Sibling,
1
		statemint_like::AccountId,
1
	>::convert_location(&dest_para)
1
	.unwrap();
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = relay_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_relay_id,
1
			source_location_latest,
1
			relay_asset_metadata.decimals,
1
			relay_asset_metadata.symbol.try_into().expect("too long"),
1
			relay_asset_metadata.name.try_into().expect("too long"),
		));
1
		assert_ok!(add_supported_asset(relay_location, 0u128));
1
		let parachain::AssetType::Xcm(source_location_v3) = statemint_location_asset.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_statemint_asset_id,
1
			source_location_latest,
1
			asset_metadata_statemint_asset.decimals,
1
			asset_metadata_statemint_asset
1
				.symbol
1
				.try_into()
1
				.expect("too long"),
1
			asset_metadata_statemint_asset
1
				.name
1
				.try_into()
1
				.expect("too long"),
		));
1
		assert_ok!(add_supported_asset(statemint_location_asset, 0u128));
1
	});
1
	let parachain_beneficiary_from_relay: Location = Junction::AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
	// Send relay chain asset to Alice in Parachain A
1
	Relay::execute_with(|| {
1
		let assets: VersionedAssets = ([] /* Here */, 200).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::here()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_from_relay.clone(),
1
		}]);
1
		assert_ok!(RelayChainPalletXcm::transfer_assets_using_type_and_then(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1).into()),
1
			Box::new(assets),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::LocalReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Unlimited
		));
1
	});
1
	Statemint::execute_with(|| {
		// Set new prefix
1
		statemint_like::PrefixChanger::set_prefix(
1
			PalletInstance(<StatemintAssets as PalletInfoAccess>::index() as u8).into(),
		);
1
		assert_ok!(StatemintAssets::create(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
			10,
			RELAYALICE,
			1
		));
1
		assert_ok!(StatemintAssets::mint(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
			10,
			RELAYALICE,
			300000000000000
		));
		// Send some native statemint tokens to sovereign for fees.
		// We can't pay fees with USDC as the asset is minted as non-sufficient.
1
		assert_ok!(StatemintBalances::transfer_allow_death(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
			sov,
			100000000000000
		));
		// Send statemint USDC asset to Alice in Parachain A
1
		let parachain_beneficiary_from_statemint: Location = AccountKey20 {
1
			network: None,
1
			key: PARAALICE,
1
		}
1
		.into();
		// Send with new prefix
1
		let assets: VersionedAssets = (
1
			[
1
				xcm::latest::prelude::PalletInstance(
1
					<StatemintAssets as PalletInfoAccess>::index() as u8,
1
				),
1
				GeneralIndex(10),
1
			],
1
			125,
1
		)
1
			.into();
1
		let fees_id: VersionedAssetId = AssetId(Location::new(
1
			0,
1
			[
1
				xcm::latest::prelude::PalletInstance(
1
					<StatemintAssets as PalletInfoAccess>::index() as u8,
1
				),
1
				GeneralIndex(10),
1
			],
1
		))
1
		.into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_from_statemint.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
1
	});
1
	let statemint_beneficiary = Location {
1
		parents: 1,
1
		interior: [
1
			Parachain(1000),
1
			AccountId32 {
1
				network: None,
1
				id: RELAYBOB.into(),
1
			},
1
		]
1
		.into(),
1
	};
1
	ParaA::execute_with(|| {
		// Alice has received 125 USDC
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_statemint_asset_id, PARAALICE.into()),
1
			Ok(U256::from(125))
		);
		// Alice has received 200 Relay assets
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(200))
		);
1
	});
1
	Statemint::execute_with(|| {
		// Check that BOB's balance is empty before the transfer
1
		assert_eq!(StatemintAssets::account_balances(RELAYBOB), vec![]);
1
	});
1
	let (chain_part, beneficiary) =
1
		split_location_into_chain_part_and_beneficiary(statemint_beneficiary).unwrap();
	// Transfer USDC from Parachain A to Statemint using Relay asset as fee
1
	ParaA::execute_with(|| {
1
		let asset = currency_to_asset(
1
			parachain::CurrencyId::ForeignAsset(source_statemint_asset_id),
			100,
		);
1
		let asset_fee =
1
			currency_to_asset(parachain::CurrencyId::ForeignAsset(source_relay_id), 100);
1
		let assets_to_send: XcmAssets = XcmAssets::from(vec![asset, asset_fee.clone()]);
1
		assert_eq!(assets_to_send.get(0).unwrap(), &asset_fee);
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(assets_to_send)),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(80_000_000u64, 100_000u64))
		));
1
	});
1
	ParaA::execute_with(|| {
		// Alice has 100 USDC less
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_statemint_asset_id, PARAALICE.into()),
1
			Ok(U256::from(25))
		);
		// Alice has 100 relay asset less
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(100))
		);
1
	});
1
	Statemint::execute_with(|| {
1
		println!("STATEMINT EVENTS: {:?}", parachain::para_events());
		// Check that BOB received 100 USDC on statemint
1
		assert_eq!(StatemintAssets::account_balances(RELAYBOB), vec![(10, 100)]);
1
	});
1
}
#[test]
1
fn send_dot_from_moonbeam_to_statemint_via_xtokens_transfer() {
1
	MockNet::reset();
	// Relay asset
1
	let relay_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_relay_id: parachain::AssetId = relay_location.clone().into();
1
	let relay_asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
1
	let dest_para = Location::new(1, [Parachain(1)]);
1
	let sov = xcm_builder::SiblingParachainConvertsVia::<
1
		polkadot_parachain::primitives::Sibling,
1
		statemint_like::AccountId,
1
	>::convert_location(&dest_para)
1
	.unwrap();
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = relay_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_relay_id,
1
			source_location_latest,
1
			relay_asset_metadata.decimals,
1
			relay_asset_metadata.symbol.try_into().expect("too long"),
1
			relay_asset_metadata.name.try_into().expect("too long"),
		));
1
		XcmWeightTrader::set_asset_price(Location::parent(), 0u128);
1
	});
1
	let parachain_beneficiary_absolute: Location = Junction::AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	let statemint_beneficiary_absolute: Location = Junction::AccountId32 {
1
		network: None,
1
		id: RELAYALICE.into(),
1
	}
1
	.into();
	// First we send relay chain asset to Alice in AssetHub (via teleport)
1
	Relay::execute_with(|| {
1
		assert_ok!(RelayChainPalletXcm::limited_teleport_assets(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1000).into()),
1
			Box::new(
1
				VersionedLocation::from(statemint_beneficiary_absolute)
1
					.clone()
1
					.into()
			),
1
			Box::new(([], 200).into()),
			0,
1
			WeightLimit::Unlimited
		));
1
	});
	// Send DOTs from AssetHub to ParaA (Moonbeam)
1
	Statemint::execute_with(|| {
		// Check Alice received 200 tokens on AssetHub
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYALICE),
			INITIAL_BALANCE + 200
		);
1
		assert_ok!(StatemintBalances::transfer_allow_death(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
			sov,
			110000000000000
		));
		// Now send those tokens to ParaA
1
		let assets: VersionedAssets = (Location::parent(), 200).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
1
	});
1
	ParaA::execute_with(|| {
		// Alice should have received the DOTs
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(200))
		);
1
	});
1
	let dest = Location::new(
		1,
1
		[
1
			Parachain(1000),
1
			AccountId32 {
1
				network: None,
1
				id: RELAYBOB.into(),
1
			},
1
		],
	);
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
	// Finally we test that we are able to send back the DOTs to AssetHub from the ParaA
1
	ParaA::execute_with(|| {
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_relay_id), 100);
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(asset)),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
		));
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(100))
		);
1
	});
1
	Statemint::execute_with(|| {
		// Check that Bob received the tokens back in AssetHub
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYBOB),
			INITIAL_BALANCE + 100
		);
1
	});
	// Send back tokens from AH to ParaA from Bob's account
1
	Statemint::execute_with(|| {
		// Now send those tokens to ParaA
1
		let assets: VersionedAssets = (Location::parent(), 100).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYBOB),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
		// 100 DOTs were deducted from Bob's account
1
		assert_eq!(StatemintBalances::free_balance(RELAYBOB), INITIAL_BALANCE);
1
	});
1
	ParaA::execute_with(|| {
		// Alice should have received 100 DOTs
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(200))
		);
1
	});
1
}
#[test]
1
fn send_dot_from_moonbeam_to_statemint_via_xtokens_transfer_with_fee() {
1
	MockNet::reset();
	// Relay asset
1
	let relay_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_relay_id: parachain::AssetId = relay_location.clone().into();
1
	let relay_asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
1
	let dest_para = Location::new(1, [Parachain(1)]);
1
	let sov = xcm_builder::SiblingParachainConvertsVia::<
1
		polkadot_parachain::primitives::Sibling,
1
		statemint_like::AccountId,
1
	>::convert_location(&dest_para)
1
	.unwrap();
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = relay_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_relay_id,
1
			source_location_latest,
1
			relay_asset_metadata.decimals,
1
			relay_asset_metadata.symbol.try_into().expect("too long"),
1
			relay_asset_metadata.name.try_into().expect("too long"),
		));
1
		XcmWeightTrader::set_asset_price(Location::parent(), 0u128);
1
	});
1
	let parachain_beneficiary_absolute: Location = Junction::AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	let statemint_beneficiary_absolute: Location = Junction::AccountId32 {
1
		network: None,
1
		id: RELAYALICE.into(),
1
	}
1
	.into();
	// First we send relay chain asset to Alice in AssetHub (via teleport)
1
	Relay::execute_with(|| {
1
		assert_ok!(RelayChainPalletXcm::limited_teleport_assets(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1000).into()),
1
			Box::new(
1
				VersionedLocation::from(statemint_beneficiary_absolute)
1
					.clone()
1
					.into()
			),
1
			Box::new(([], 200).into()),
			0,
1
			WeightLimit::Unlimited
		));
1
	});
	// Send DOTs from AssetHub to ParaA (Moonbeam)
1
	Statemint::execute_with(|| {
		// Check Alice received 200 tokens on AssetHub
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYALICE),
			INITIAL_BALANCE + 200
		);
1
		assert_ok!(StatemintBalances::transfer_allow_death(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
			sov,
			110000000000000
		));
		// Now send those tokens to ParaA
1
		let assets: VersionedAssets = (Location::parent(), 200).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
1
	});
1
	ParaA::execute_with(|| {
		// Alice should have received the DOTs
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(200))
		);
1
	});
1
	let dest = Location::new(
		1,
1
		[
1
			Parachain(1000),
1
			AccountId32 {
1
				network: None,
1
				id: RELAYBOB.into(),
1
			},
1
		],
	);
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
	// Finally we test that we are able to send back the DOTs to AssetHub from the ParaA
1
	ParaA::execute_with(|| {
1
		let asset = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_relay_id), 100);
1
		let asset_fee = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_relay_id), 10);
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(vec![asset_fee, asset])),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
		));
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(90))
		);
1
	});
1
	Statemint::execute_with(|| {
		// Free execution: check that Bob received the tokens back in AssetHub
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYBOB),
			INITIAL_BALANCE + 110
		);
1
	});
	// Send back tokens from AH to ParaA from Bob's account
1
	Statemint::execute_with(|| {
		// Now send those tokens to ParaA
1
		let assets: VersionedAssets = (Location::parent(), 100).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYBOB),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
		// 100 DOTs were deducted from Bob's account
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYBOB),
			INITIAL_BALANCE + 10
		);
1
	});
1
	ParaA::execute_with(|| {
		// Alice should have received 100 DOTs
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(190))
		);
1
	});
1
}
#[test]
1
fn send_dot_from_moonbeam_to_statemint_via_xtokens_transfer_multiasset() {
1
	MockNet::reset();
	// Relay asset
1
	let relay_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_relay_id: parachain::AssetId = relay_location.clone().into();
1
	let relay_asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
1
	let dest_para = Location::new(1, [Parachain(1)]);
1
	let sov = xcm_builder::SiblingParachainConvertsVia::<
1
		polkadot_parachain::primitives::Sibling,
1
		statemint_like::AccountId,
1
	>::convert_location(&dest_para)
1
	.unwrap();
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = relay_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_relay_id,
1
			source_location_latest,
1
			relay_asset_metadata.decimals,
1
			relay_asset_metadata.symbol.try_into().expect("too long"),
1
			relay_asset_metadata.name.try_into().expect("too long"),
		));
1
		XcmWeightTrader::set_asset_price(Location::parent(), 0u128);
1
	});
1
	let parachain_beneficiary_absolute: Location = Junction::AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	let statemint_beneficiary_absolute: Location = Junction::AccountId32 {
1
		network: None,
1
		id: RELAYALICE.into(),
1
	}
1
	.into();
	// First we send relay chain asset to Alice in AssetHub (via teleport)
1
	Relay::execute_with(|| {
1
		assert_ok!(RelayChainPalletXcm::limited_teleport_assets(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1000).into()),
1
			Box::new(
1
				VersionedLocation::from(statemint_beneficiary_absolute)
1
					.clone()
1
					.into()
			),
1
			Box::new(([], 200).into()),
			0,
1
			WeightLimit::Unlimited
		));
1
	});
	// Send DOTs from AssetHub to ParaA (Moonbeam)
1
	Statemint::execute_with(|| {
		// Check Alice received 200 tokens on AssetHub
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYALICE),
			INITIAL_BALANCE + 200
		);
1
		assert_ok!(StatemintBalances::transfer_allow_death(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
			sov,
			110000000000000
		));
		// Now send those tokens to ParaA
1
		let assets: VersionedAssets = (Location::parent(), 200).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
1
	});
1
	ParaA::execute_with(|| {
		// Alice should have received the DOTs
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(200))
		);
1
	});
1
	let dest = Location::new(
		1,
1
		[
1
			Parachain(1000),
1
			AccountId32 {
1
				network: None,
1
				id: RELAYBOB.into(),
1
			},
1
		],
	);
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
	// Finally we test that we are able to send back the DOTs to AssetHub from the ParaA
1
	ParaA::execute_with(|| {
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from((Location::parent(), 100))),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(40000u64, DEFAULT_PROOF_SIZE))
		));
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(100))
		);
1
	});
1
	Statemint::execute_with(|| {
		// Check that Bob received the tokens back in AssetHub
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYBOB),
			INITIAL_BALANCE + 100
		);
1
	});
	// Send back tokens from AH to ParaA from Bob's account
1
	Statemint::execute_with(|| {
		// Now send those tokens to ParaA
1
		let assets: VersionedAssets = (Location::parent(), 100).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYBOB),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
		// 100 DOTs were deducted from Bob's account
1
		assert_eq!(StatemintBalances::free_balance(RELAYBOB), INITIAL_BALANCE);
1
	});
1
	ParaA::execute_with(|| {
		// Alice should have received 100 DOTs
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(200))
		);
1
	});
1
}
#[test]
1
fn send_dot_from_moonbeam_to_statemint_via_xtokens_transfer_multicurrencies() {
1
	MockNet::reset();
	// Relay asset
1
	let relay_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_relay_id: parachain::AssetId = relay_location.clone().into();
1
	let relay_asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
	// Statemint asset
1
	let statemint_asset = Location::new(
		1,
1
		[
1
			Parachain(1000u32),
1
			PalletInstance(5u8),
1
			GeneralIndex(10u128),
1
		],
	);
1
	let statemint_location_asset: AssetType = statemint_asset
1
		.clone()
1
		.try_into()
1
		.expect("Location convertion to AssetType should succeed");
1
	let source_statemint_asset_id: parachain::AssetId = statemint_location_asset.clone().into();
1
	let asset_metadata_statemint_asset = parachain::AssetMetadata {
1
		name: b"USDC".to_vec(),
1
		symbol: b"USDC".to_vec(),
1
		decimals: 12,
1
	};
1
	let dest_para = Location::new(1, [Parachain(1)]);
1
	let sov = xcm_builder::SiblingParachainConvertsVia::<
1
		polkadot_parachain::primitives::Sibling,
1
		statemint_like::AccountId,
1
	>::convert_location(&dest_para)
1
	.unwrap();
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = relay_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_relay_id,
1
			source_location_latest,
1
			relay_asset_metadata.decimals,
1
			relay_asset_metadata.symbol.try_into().expect("too long"),
1
			relay_asset_metadata.name.try_into().expect("too long"),
		));
1
		XcmWeightTrader::set_asset_price(Location::parent(), 0u128);
1
		let parachain::AssetType::Xcm(source_location_v3) = statemint_location_asset.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_statemint_asset_id,
1
			source_location_latest,
1
			asset_metadata_statemint_asset.decimals,
1
			asset_metadata_statemint_asset
1
				.symbol
1
				.try_into()
1
				.expect("too long"),
1
			asset_metadata_statemint_asset
1
				.name
1
				.try_into()
1
				.expect("too long"),
		));
1
		XcmWeightTrader::set_asset_price(statemint_asset.clone(), 0u128);
1
	});
1
	let parachain_beneficiary_absolute: Location = Junction::AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	let statemint_beneficiary_absolute: Location = Junction::AccountId32 {
1
		network: None,
1
		id: RELAYALICE.into(),
1
	}
1
	.into();
	// First we send relay chain asset to Alice in AssetHub (via teleport)
1
	Relay::execute_with(|| {
1
		assert_ok!(RelayChainPalletXcm::limited_teleport_assets(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1000).into()),
1
			Box::new(
1
				VersionedLocation::from(statemint_beneficiary_absolute)
1
					.clone()
1
					.into()
			),
1
			Box::new(([], 200).into()),
			0,
1
			WeightLimit::Unlimited
		));
1
	});
	// Send DOTs and USDC from AssetHub to ParaA (Moonbeam)
1
	Statemint::execute_with(|| {
		// Check Alice received 200 tokens on AssetHub
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYALICE),
			INITIAL_BALANCE + 200
		);
1
		assert_ok!(StatemintBalances::transfer_allow_death(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
			sov,
			110000000000000
		));
1
		statemint_like::PrefixChanger::set_prefix(
1
			PalletInstance(<StatemintAssets as PalletInfoAccess>::index() as u8).into(),
		);
1
		assert_ok!(StatemintAssets::create(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
			10,
			RELAYALICE,
			1
		));
1
		assert_ok!(StatemintAssets::mint(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
			10,
			RELAYALICE,
			300000000000000
		));
		// Now send relay tokens to ParaA
1
		let assets: VersionedAssets = (Location::parent(), 200).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
		// Send USDC
1
		let assets: VersionedAssets = (
1
			[
1
				xcm::latest::prelude::PalletInstance(
1
					<StatemintAssets as PalletInfoAccess>::index() as u8,
1
				),
1
				GeneralIndex(10),
1
			],
1
			125,
1
		)
1
			.into();
1
		let fees_id: VersionedAssetId = AssetId(Location::new(
1
			0,
1
			[
1
				xcm::latest::prelude::PalletInstance(
1
					<StatemintAssets as PalletInfoAccess>::index() as u8,
1
				),
1
				GeneralIndex(10),
1
			],
1
		))
1
		.into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
1
	});
1
	ParaA::execute_with(|| {
		// Alice should have received the DOTs
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(200))
		);
		// Alice has received 125 USDC
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_statemint_asset_id, PARAALICE.into()),
1
			Ok(U256::from(125))
		);
1
	});
1
	let dest = Location::new(
		1,
1
		[
1
			Parachain(1000),
1
			AccountId32 {
1
				network: None,
1
				id: RELAYBOB.into(),
1
			},
1
		],
	);
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
	// Finally we test that we are able to send back the DOTs to AssetHub from the ParaA
1
	ParaA::execute_with(|| {
1
		let asset_1 = currency_to_asset(
1
			parachain::CurrencyId::ForeignAsset(source_statemint_asset_id),
			100,
		);
1
		let asset_2 = currency_to_asset(parachain::CurrencyId::ForeignAsset(source_relay_id), 100);
1
		let assets_to_send = vec![asset_1, asset_2];
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(assets_to_send)),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(80_000_000u64, 100_000u64))
		));
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(100))
		);
1
	});
1
	Statemint::execute_with(|| {
		// Check that Bob received relay tokens back in AssetHub
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYBOB),
			INITIAL_BALANCE + 100
		);
		// Check that BOB received 100 USDC on AssetHub
1
		assert_eq!(StatemintAssets::account_balances(RELAYBOB), vec![(10, 100)]);
1
	});
	// Send back tokens from AH to ParaA from Bob's account
1
	Statemint::execute_with(|| {
1
		let bob_previous_balance = StatemintBalances::free_balance(RELAYBOB);
		// Now send those tokens to ParaA
1
		let assets: VersionedAssets = (Location::parent(), 100).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYBOB),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
		// 100 DOTs were deducted from Bob's account
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYBOB),
1
			bob_previous_balance - 100
		);
1
	});
1
	ParaA::execute_with(|| {
		// Alice should have received 100 DOTs
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(200))
		);
1
	});
1
}
#[test]
1
fn send_dot_from_moonbeam_to_statemint_via_xtokens_transfer_multiassets() {
1
	MockNet::reset();
	// Relay asset
1
	let relay_location = parachain::AssetType::Xcm(xcm::v3::Location::parent());
1
	let source_relay_id: parachain::AssetId = relay_location.clone().into();
1
	let relay_asset_metadata = parachain::AssetMetadata {
1
		name: b"RelayToken".to_vec(),
1
		symbol: b"Relay".to_vec(),
1
		decimals: 12,
1
	};
	// Statemint asset
1
	let statemint_asset = Location::new(
		1,
1
		[
1
			Parachain(1000u32),
1
			PalletInstance(5u8),
1
			GeneralIndex(10u128),
1
		],
	);
1
	let statemint_location_asset: AssetType = statemint_asset
1
		.clone()
1
		.try_into()
1
		.expect("Location convertion to AssetType should succeed");
1
	let source_statemint_asset_id: parachain::AssetId = statemint_location_asset.clone().into();
1
	let asset_metadata_statemint_asset = parachain::AssetMetadata {
1
		name: b"USDC".to_vec(),
1
		symbol: b"USDC".to_vec(),
1
		decimals: 12,
1
	};
1
	let dest_para = Location::new(1, [Parachain(1)]);
1
	let sov = xcm_builder::SiblingParachainConvertsVia::<
1
		polkadot_parachain::primitives::Sibling,
1
		statemint_like::AccountId,
1
	>::convert_location(&dest_para)
1
	.unwrap();
1
	ParaA::execute_with(|| {
1
		let parachain::AssetType::Xcm(source_location_v3) = relay_location.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_relay_id,
1
			source_location_latest,
1
			relay_asset_metadata.decimals,
1
			relay_asset_metadata.symbol.try_into().expect("too long"),
1
			relay_asset_metadata.name.try_into().expect("too long"),
		));
1
		XcmWeightTrader::set_asset_price(Location::parent(), 0u128);
1
		let parachain::AssetType::Xcm(source_location_v3) = statemint_location_asset.clone();
1
		let source_location_latest: Location = xcm::VersionedLocation::V3(source_location_v3)
1
			.try_into()
1
			.expect("v3 to latest location conversion failed");
1
		assert_ok!(EvmForeignAssets::register_foreign_asset(
1
			source_statemint_asset_id,
1
			source_location_latest,
1
			asset_metadata_statemint_asset.decimals,
1
			asset_metadata_statemint_asset
1
				.symbol
1
				.try_into()
1
				.expect("too long"),
1
			asset_metadata_statemint_asset
1
				.name
1
				.try_into()
1
				.expect("too long"),
		));
1
		XcmWeightTrader::set_asset_price(statemint_asset.clone(), 0u128);
1
	});
1
	let parachain_beneficiary_absolute: Location = Junction::AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}
1
	.into();
1
	let statemint_beneficiary_absolute: Location = Junction::AccountId32 {
1
		network: None,
1
		id: RELAYALICE.into(),
1
	}
1
	.into();
	// First we send relay chain asset to Alice in AssetHub (via teleport)
1
	Relay::execute_with(|| {
1
		assert_ok!(RelayChainPalletXcm::limited_teleport_assets(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			Box::new(Parachain(1000).into()),
1
			Box::new(
1
				VersionedLocation::from(statemint_beneficiary_absolute)
1
					.clone()
1
					.into()
			),
1
			Box::new(([], 200).into()),
			0,
1
			WeightLimit::Unlimited
		));
1
	});
	// Send DOTs and USDC from AssetHub to ParaA (Moonbeam)
1
	Statemint::execute_with(|| {
		// Check Alice received 200 tokens on AssetHub
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYALICE),
			INITIAL_BALANCE + 200
		);
1
		assert_ok!(StatemintBalances::transfer_allow_death(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
			sov,
			110000000000000
		));
1
		statemint_like::PrefixChanger::set_prefix(
1
			PalletInstance(<StatemintAssets as PalletInfoAccess>::index() as u8).into(),
		);
1
		assert_ok!(StatemintAssets::create(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
			10,
			RELAYALICE,
			1
		));
1
		assert_ok!(StatemintAssets::mint(
1
			statemint_like::RuntimeOrigin::signed(RELAYALICE),
			10,
			RELAYALICE,
			300000000000000
		));
		// Now send relay tokens to ParaA
1
		let assets: VersionedAssets = (Location::parent(), 200).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
		// Send USDC
1
		let assets: VersionedAssets = (
1
			[
1
				xcm::latest::prelude::PalletInstance(
1
					<StatemintAssets as PalletInfoAccess>::index() as u8,
1
				),
1
				GeneralIndex(10),
1
			],
1
			125,
1
		)
1
			.into();
1
		let fees_id: VersionedAssetId = AssetId(Location::new(
1
			0,
1
			[
1
				xcm::latest::prelude::PalletInstance(
1
					<StatemintAssets as PalletInfoAccess>::index() as u8,
1
				),
1
				GeneralIndex(10),
1
			],
1
		))
1
		.into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYALICE),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
1
	});
1
	ParaA::execute_with(|| {
		// Alice should have received the DOTs
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(200))
		);
		// Alice has received 125 USDC
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_statemint_asset_id, PARAALICE.into()),
1
			Ok(U256::from(125))
		);
1
	});
1
	let dest = Location::new(
		1,
1
		[
1
			Parachain(1000),
1
			AccountId32 {
1
				network: None,
1
				id: RELAYBOB.into(),
1
			},
1
		],
	);
1
	let statemint_asset_to_send = Asset {
1
		id: AssetId(statemint_asset),
1
		fun: Fungible(100),
1
	};
1
	let relay_asset_to_send = Asset {
1
		id: AssetId(Location::parent()),
1
		fun: Fungible(100),
1
	};
1
	let assets_to_send: XcmAssets =
1
		XcmAssets::from(vec![statemint_asset_to_send, relay_asset_to_send.clone()]);
1
	let (chain_part, beneficiary) = split_location_into_chain_part_and_beneficiary(dest).unwrap();
	// For some reason the order of the assets is inverted when creating the array above.
	// We need to use relay asset for fees, so we pick index 0.
1
	assert_eq!(assets_to_send.get(0).unwrap(), &relay_asset_to_send);
	// Finally we test that we are able to send back the DOTs to AssetHub from the ParaA
1
	ParaA::execute_with(|| {
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: beneficiary.clone(),
1
		}]);
1
		assert_ok!(PolkadotXcm::transfer_assets_using_type_and_then(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(VersionedLocation::from(chain_part)),
1
			Box::new(VersionedAssets::from(assets_to_send)),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(fees_id),
1
			Box::new(TransferType::DestinationReserve),
1
			Box::new(VersionedXcm::V5(xcm_on_dest)),
1
			WeightLimit::Limited(Weight::from_parts(80_000_000u64, 100_000u64))
		));
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(100))
		);
1
	});
1
	Statemint::execute_with(|| {
		// Check that Bob received relay tokens back in AssetHub
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYBOB),
			INITIAL_BALANCE + 100
		);
		// Check that BOB received 100 USDC on AssetHub
1
		assert_eq!(StatemintAssets::account_balances(RELAYBOB), vec![(10, 100)]);
1
	});
	// Send back tokens from AH to ParaA from Bob's account
1
	Statemint::execute_with(|| {
1
		let bob_previous_balance = StatemintBalances::free_balance(RELAYBOB);
		// Now send those tokens to ParaA
1
		let assets: VersionedAssets = (Location::parent(), 100).into();
1
		let fees_id: VersionedAssetId = AssetId(Location::parent()).into();
1
		let xcm_on_dest = Xcm::<()>(vec![DepositAsset {
1
			assets: Wild(All),
1
			beneficiary: parachain_beneficiary_absolute.clone(),
1
		}]);
1
		assert_ok!(
1
			StatemintChainPalletXcm::transfer_assets_using_type_and_then(
1
				statemint_like::RuntimeOrigin::signed(RELAYBOB),
1
				Box::new(Location::new(1, [Parachain(1)]).into()),
1
				Box::new(assets),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(fees_id),
1
				Box::new(TransferType::LocalReserve),
1
				Box::new(VersionedXcm::V5(xcm_on_dest)),
1
				WeightLimit::Unlimited
			)
		);
		// 100 DOTs were deducted from Bob's account
1
		assert_eq!(
1
			StatemintBalances::free_balance(RELAYBOB),
1
			bob_previous_balance - 100
		);
1
	});
1
	ParaA::execute_with(|| {
		// Alice should have received 100 DOTs
1
		assert_eq!(
1
			EvmForeignAssets::balance(source_relay_id, PARAALICE.into()),
1
			Ok(U256::from(200))
		);
1
	});
1
}
#[test]
1
fn transact_through_signed_multilocation() {
1
	MockNet::reset();
1
	let mut ancestry = Location::parent();
1
	ParaA::execute_with(|| {
		// Root can set transact info
1
		assert_ok!(XcmTransactor::set_transact_info(
1
			parachain::RuntimeOrigin::root(),
1
			Box::new(xcm::VersionedLocation::from(Location::parent())),
			// Relay charges 1000 for every instruction, and we have 3, so 3000
1
			3000.into(),
1
			20000000000.into(),
			// 4 instructions in transact through signed
1
			Some(4000.into())
		));
		// Root can set transact info
		// Set fee per second using weight-trader (replaces old set_fee_per_second)
1
		set_fee_per_second_for_location(Location::parent(), WEIGHT_REF_TIME_PER_SECOND as u128)
1
			.expect("must succeed");
1
		ancestry = parachain::UniversalLocation::get().into();
1
	});
	// Let's construct the Junction that we will append with DescendOrigin
1
	let signed_origin: Junctions = [AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}]
1
	.into();
1
	let mut descend_origin_multilocation = parachain::SelfLocation::get();
1
	descend_origin_multilocation
1
		.append_with(signed_origin)
1
		.unwrap();
	// To convert it to what the relay will see instead of us
1
	descend_origin_multilocation
1
		.reanchor(&Location::parent(), &ancestry.interior)
1
		.unwrap();
1
	let derived = xcm_builder::Account32Hash::<
1
		relay_chain::KusamaNetwork,
1
		relay_chain::AccountId,
1
	>::convert_location(&descend_origin_multilocation)
1
	.unwrap();
1
	Relay::execute_with(|| {
		// free execution, full amount received
1
		assert_ok!(RelayBalances::transfer_allow_death(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			derived.clone(),
			4000004100u128,
		));
		// derived account has all funds
1
		assert!(RelayBalances::free_balance(&derived) == 4000004100);
		// sovereign account has 0 funds
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 0);
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index = <relay_chain::Runtime as frame_system::Config>::PalletInfo::index::<
1
		relay_chain::Balances,
1
	>()
1
	.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<relay_chain::Runtime>::transfer_allow_death {
1
		// 100 to sovereign
1
		dest: para_a_account(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_signed(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(xcm::VersionedLocation::from(Location::parent())),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				fee_amount: None
1
			},
1
			encoded,
			// 4000000000 for transfer + 4000 for XCM
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: None
1
			},
			false
		));
1
	});
1
	Relay::execute_with(|| {
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 100);
1
		assert!(RelayBalances::free_balance(&derived) == 0);
1
	});
1
}
#[test]
1
fn transact_through_signed_multilocation_custom_fee_and_weight() {
1
	MockNet::reset();
1
	let mut ancestry = Location::parent();
1
	ParaA::execute_with(|| {
1
		ancestry = parachain::UniversalLocation::get().into();
1
	});
	// Let's construct the Junction that we will append with DescendOrigin
1
	let signed_origin: Junctions = [AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}]
1
	.into();
1
	let mut descend_origin_multilocation = parachain::SelfLocation::get();
1
	descend_origin_multilocation
1
		.append_with(signed_origin)
1
		.unwrap();
	// To convert it to what the relay will see instead of us
1
	descend_origin_multilocation
1
		.reanchor(&Location::parent(), &ancestry.interior)
1
		.unwrap();
1
	let derived = xcm_builder::Account32Hash::<
1
		relay_chain::KusamaNetwork,
1
		relay_chain::AccountId,
1
	>::convert_location(&descend_origin_multilocation)
1
	.unwrap();
1
	Relay::execute_with(|| {
		// free execution, full amount received
1
		assert_ok!(RelayBalances::transfer_allow_death(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			derived.clone(),
			4000004100u128,
		));
		// derived account has all funds
1
		assert!(RelayBalances::free_balance(&derived) == 4000004100);
		// sovereign account has 0 funds
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 0);
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index = <relay_chain::Runtime as frame_system::Config>::PalletInfo::index::<
1
		relay_chain::Balances,
1
	>()
1
	.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<relay_chain::Runtime>::transfer_allow_death {
1
		// 100 to sovereign
1
		dest: para_a_account(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	let total_weight = 4000004000u64;
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_signed(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(xcm::VersionedLocation::from(Location::parent())),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				fee_amount: Some(total_weight as u128)
1
			},
1
			encoded,
			// 4000000000 for transfer + 4000 for XCM
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: Some(Limited(total_weight.into()))
1
			},
			false
		));
1
	});
1
	Relay::execute_with(|| {
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 100);
1
		assert!(RelayBalances::free_balance(&derived) == 0);
1
	});
1
}
#[test]
1
fn transact_through_signed_multilocation_custom_fee_and_weight_refund() {
1
	MockNet::reset();
1
	let mut ancestry = Location::parent();
1
	ParaA::execute_with(|| {
1
		ancestry = parachain::UniversalLocation::get().into();
1
	});
	// Let's construct the Junction that we will append with DescendOrigin
1
	let signed_origin: Junctions = [AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}]
1
	.into();
1
	let mut descend_origin_multilocation = parachain::SelfLocation::get();
1
	descend_origin_multilocation
1
		.append_with(signed_origin)
1
		.unwrap();
	// To convert it to what the relay will see instead of us
1
	descend_origin_multilocation
1
		.reanchor(&Location::parent(), &ancestry.interior)
1
		.unwrap();
1
	let derived = xcm_builder::Account32Hash::<
1
		relay_chain::KusamaNetwork,
1
		relay_chain::AccountId,
1
	>::convert_location(&descend_origin_multilocation)
1
	.unwrap();
1
	Relay::execute_with(|| {
		// free execution, full amount received
1
		assert_ok!(RelayBalances::transfer_allow_death(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			derived.clone(),
			4000009100u128,
		));
		// derived account has all funds
1
		assert!(RelayBalances::free_balance(&derived) == 4000009100);
		// sovereign account has 0 funds
1
		assert!(RelayBalances::free_balance(&para_a_account()) == 0);
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index = <relay_chain::Runtime as frame_system::Config>::PalletInfo::index::<
1
		relay_chain::Balances,
1
	>()
1
	.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<relay_chain::Runtime>::transfer_allow_death {
1
		// 100 to sovereign
1
		dest: para_a_account(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	let total_weight = 4000009000u64;
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_signed(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(xcm::VersionedLocation::from(Location::parent())),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				fee_amount: Some(total_weight as u128)
1
			},
1
			encoded,
			// 4000000000 for transfer + 9000 for XCM
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: Some(Limited(total_weight.into()))
1
			},
			true
		));
1
	});
1
	Relay::execute_with(|| {
		// 100 transferred
1
		assert_eq!(RelayBalances::free_balance(&para_a_account()), 100);
		// 4000009000 refunded
1
		assert_eq!(RelayBalances::free_balance(&derived), 4000009000);
1
	});
1
}
#[test]
1
fn transact_through_signed_multilocation_para_to_para() {
1
	MockNet::reset();
1
	let mut ancestry = Location::parent();
1
	let para_b_location = Location::new(1, [Parachain(2)]);
1
	let para_b_balances = Location::new(1, [Parachain(2), PalletInstance(1u8)]);
1
	ParaA::execute_with(|| {
		// Root can set transact info
1
		assert_ok!(XcmTransactor::set_transact_info(
1
			parachain::RuntimeOrigin::root(),
			// ParaB
1
			Box::new(xcm::VersionedLocation::from(para_b_location.clone())),
			// Para charges 1000 for every instruction, and we have 3, so 3
1
			3.into(),
1
			20000000000.into(),
			// 4 instructions in transact through signed
1
			Some(4.into())
		));
		// Root can set transact info
		// Set fee per second using weight-trader (replaces old set_fee_per_second)
1
		set_fee_per_second_for_location(
1
			para_b_balances.clone(),
1
			parachain::ParaTokensPerSecond::get(),
		)
1
		.expect("must succeed");
1
		ancestry = parachain::UniversalLocation::get().into();
1
	});
	// Let's construct the Junction that we will append with DescendOrigin
1
	let signed_origin: Junctions = [AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}]
1
	.into();
1
	let mut descend_origin_location = parachain::SelfLocation::get();
1
	descend_origin_location.append_with(signed_origin).unwrap();
	// To convert it to what the paraB will see instead of us
1
	descend_origin_location
1
		.reanchor(&para_b_location, &ancestry.interior)
1
		.unwrap();
1
	let derived = xcm_builder::HashedDescription::<
1
		parachain::AccountId,
1
		xcm_builder::DescribeFamily<xcm_builder::DescribeAllTerminal>,
1
	>::convert_location(&descend_origin_location)
1
	.unwrap();
1
	ParaB::execute_with(|| {
		// free execution, full amount received
1
		assert_ok!(ParaBalances::transfer_allow_death(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			derived.clone(),
			4000000104u128,
		));
		// derived account has all funds
1
		assert!(ParaBalances::free_balance(&derived) == 4000000104);
		// sovereign account has 0 funds
1
		assert!(ParaBalances::free_balance(&para_a_account_20()) == 0);
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index =
1
		<parachain::Runtime as frame_system::Config>::PalletInfo::index::<parachain::Balances>()
1
			.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<parachain::Runtime>::transfer_allow_death {
1
		// 100 to sovereign
1
		dest: para_a_account_20(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_signed(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(xcm::VersionedLocation::from(para_b_location)),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					para_b_balances
1
				))),
1
				fee_amount: None
1
			},
1
			encoded,
			// 4000000000 for transfer + 4000 for XCM
			// 1-1 to fee
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: None
1
			},
			false
		));
1
	});
1
	ParaB::execute_with(|| {
1
		assert!(ParaBalances::free_balance(&derived) == 0);
1
		assert!(ParaBalances::free_balance(&para_a_account_20()) == 100);
1
	});
1
}
#[test]
1
fn transact_through_signed_multilocation_para_to_para_refund() {
1
	MockNet::reset();
1
	let mut ancestry = Location::parent();
1
	let para_b_location = Location::new(1, [Parachain(2)]);
1
	let para_b_balances = Location::new(1, [Parachain(2), PalletInstance(1u8)]);
1
	ParaA::execute_with(|| {
		// Set fee per second using weight-trader (replaces old set_fee_per_second)
1
		set_fee_per_second_for_location(
1
			para_b_balances.clone(),
1
			parachain::ParaTokensPerSecond::get(),
		)
1
		.expect("must succeed");
1
		ancestry = parachain::UniversalLocation::get().into();
1
	});
	// Let's construct the Junction that we will append with DescendOrigin
1
	let signed_origin: Junctions = [AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}]
1
	.into();
1
	let mut descend_origin_location = parachain::SelfLocation::get();
1
	descend_origin_location.append_with(signed_origin).unwrap();
	// To convert it to what the paraB will see instead of us
1
	descend_origin_location
1
		.reanchor(&para_b_location, &ancestry.interior)
1
		.unwrap();
1
	let derived = xcm_builder::HashedDescription::<
1
		parachain::AccountId,
1
		xcm_builder::DescribeFamily<xcm_builder::DescribeAllTerminal>,
1
	>::convert_location(&descend_origin_location)
1
	.unwrap();
1
	ParaB::execute_with(|| {
		// free execution, full amount received
1
		assert_ok!(ParaBalances::transfer_allow_death(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			derived.clone(),
			4000009100u128,
		));
		// derived account has all funds
1
		assert!(ParaBalances::free_balance(&derived) == 4000009100);
		// sovereign account has 0 funds
1
		assert!(ParaBalances::free_balance(&para_a_account_20()) == 0);
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index =
1
		<parachain::Runtime as frame_system::Config>::PalletInfo::index::<parachain::Balances>()
1
			.unwrap() as u8;
1
	encoded.push(index);
	// Then call bytes
1
	let mut call_bytes = pallet_balances::Call::<parachain::Runtime>::transfer_allow_death {
1
		// 100 to sovereign
1
		dest: para_a_account_20(),
1
		value: 100u32.into(),
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	let overall_weight = 4000009000u64;
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_signed(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(xcm::VersionedLocation::from(para_b_location)),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					para_b_balances
1
				))),
1
				fee_amount: Some(overall_weight as u128)
1
			},
1
			encoded,
			// 4000000000 for transfer + 9000 for XCM
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: Some(Limited(overall_weight.into()))
1
			},
			true
		));
1
	});
1
	ParaB::execute_with(|| {
		// Check the derived account was refunded
1
		assert_eq!(ParaBalances::free_balance(&derived), 3826174993);
		// Check the transfer was executed
1
		assert_eq!(ParaBalances::free_balance(&para_a_account_20()), 100);
1
	});
1
}
#[test]
1
fn transact_through_signed_multilocation_para_to_para_ethereum() {
1
	MockNet::reset();
1
	let mut ancestry = Location::parent();
1
	let para_b_location = Location::new(1, [Parachain(2)]);
1
	let para_b_balances = Location::new(1, [Parachain(2), PalletInstance(1u8)]);
1
	ParaA::execute_with(|| {
		// Root can set transact info
1
		assert_ok!(XcmTransactor::set_transact_info(
1
			parachain::RuntimeOrigin::root(),
			// ParaB
1
			Box::new(xcm::VersionedLocation::from(para_b_location.clone())),
			// Para charges 1000 for every instruction, and we have 3, so 3
1
			3.into(),
1
			20000000000.into(),
			// 4 instructions in transact through signed
1
			Some(4.into())
		));
		// Root can set transact info
		// Set fee per second using weight-trader (replaces old set_fee_per_second)
1
		set_fee_per_second_for_location(
1
			para_b_balances.clone(),
1
			parachain::ParaTokensPerSecond::get(),
		)
1
		.expect("must succeed");
1
		ancestry = parachain::UniversalLocation::get().into();
1
	});
	// Let's construct the Junction that we will append with DescendOrigin
1
	let signed_origin: Junctions = [AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}]
1
	.into();
1
	let mut descend_origin_location = parachain::SelfLocation::get();
1
	descend_origin_location.append_with(signed_origin).unwrap();
	// To convert it to what the paraB will see instead of us
1
	descend_origin_location
1
		.reanchor(&para_b_location, &ancestry.interior)
1
		.unwrap();
1
	let derived = xcm_builder::HashedDescription::<
1
		parachain::AccountId,
1
		xcm_builder::DescribeFamily<xcm_builder::DescribeAllTerminal>,
1
	>::convert_location(&descend_origin_location)
1
	.unwrap();
1
	let mut parachain_b_alice_balances_before = 0;
1
	ParaB::execute_with(|| {
1
		assert_ok!(ParaBalances::transfer_allow_death(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			derived.clone(),
			4000000104u128,
		));
		// derived account has all funds
1
		assert!(ParaBalances::free_balance(&derived) == 4000000104);
		// sovereign account has 0 funds
1
		assert!(ParaBalances::free_balance(&para_a_account_20()) == 0);
1
		parachain_b_alice_balances_before = ParaBalances::free_balance(&PARAALICE.into())
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index =
1
		<parachain::Runtime as frame_system::Config>::PalletInfo::index::<parachain::EthereumXcm>()
1
			.unwrap() as u8;
1
	encoded.push(index);
	use sp_core::U256;
	// Let's do a EVM transfer
1
	let eth_tx =
1
		xcm_primitives::EthereumXcmTransaction::V1(xcm_primitives::EthereumXcmTransactionV1 {
1
			gas_limit: U256::from(21000),
1
			fee_payment: xcm_primitives::EthereumXcmFee::Auto,
1
			action: pallet_ethereum::TransactionAction::Call(PARAALICE.into()),
1
			value: U256::from(100),
1
			input: BoundedVec::<
1
				u8,
1
				ConstU32<{ xcm_primitives::MAX_ETHEREUM_XCM_INPUT_SIZE }>
1
			>::try_from(vec![]).unwrap(),
1
			access_list: None,
1
		});
	// Then call bytes
1
	let mut call_bytes = pallet_ethereum_xcm::Call::<parachain::Runtime>::transact {
1
		xcm_transaction: eth_tx,
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_signed(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(xcm::VersionedLocation::from(para_b_location)),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					para_b_balances
1
				))),
1
				fee_amount: None
1
			},
1
			encoded,
			// 4000000000 for transfer + 4000 for XCM
			// 1-1 to fee
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: None
1
			},
			false
		));
1
	});
1
	ParaB::execute_with(|| {
		// Make sure the EVM transfer went through
1
		assert!(
1
			ParaBalances::free_balance(&PARAALICE.into())
1
				== parachain_b_alice_balances_before + 100
		);
1
	});
1
}
#[test]
1
fn transact_through_signed_multilocation_para_to_para_ethereum_no_proxy_fails() {
1
	MockNet::reset();
1
	let mut ancestry = Location::parent();
1
	let para_b_location = Location::new(1, [Parachain(2)]);
1
	let para_b_balances = Location::new(1, [Parachain(2), PalletInstance(1u8)]);
1
	ParaA::execute_with(|| {
		// Root can set transact info
1
		assert_ok!(XcmTransactor::set_transact_info(
1
			parachain::RuntimeOrigin::root(),
			// ParaB
1
			Box::new(xcm::VersionedLocation::from(para_b_location.clone())),
			// Para charges 1000 for every instruction, and we have 3, so 3
1
			3.into(),
1
			20000000000.into(),
			// 4 instructions in transact through signed
1
			Some(4.into())
		));
		// Root can set transact info
		// Set fee per second using weight-trader (replaces old set_fee_per_second)
1
		set_fee_per_second_for_location(
1
			para_b_balances.clone(),
1
			parachain::ParaTokensPerSecond::get(),
		)
1
		.expect("must succeed");
1
		ancestry = parachain::UniversalLocation::get().into();
1
	});
	// Let's construct the Junction that we will append with DescendOrigin
1
	let signed_origin: Junctions = [AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}]
1
	.into();
1
	let mut descend_origin_location = parachain::SelfLocation::get();
1
	descend_origin_location.append_with(signed_origin).unwrap();
	// To convert it to what the paraB will see instead of us
1
	descend_origin_location
1
		.reanchor(&para_b_location, &ancestry.interior)
1
		.unwrap();
1
	let derived = xcm_builder::HashedDescription::<
1
		parachain::AccountId,
1
		xcm_builder::DescribeFamily<xcm_builder::DescribeAllTerminal>,
1
	>::convert_location(&descend_origin_location)
1
	.unwrap();
1
	let mut parachain_b_alice_balances_before = 0;
1
	ParaB::execute_with(|| {
1
		assert_ok!(ParaBalances::transfer_allow_death(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			derived.clone(),
			4000000104u128,
		));
		// derived account has all funds
1
		assert!(ParaBalances::free_balance(&derived) == 4000000104);
		// sovereign account has 0 funds
1
		assert!(ParaBalances::free_balance(&para_a_account_20()) == 0);
1
		parachain_b_alice_balances_before = ParaBalances::free_balance(&PARAALICE.into())
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index =
1
		<parachain::Runtime as frame_system::Config>::PalletInfo::index::<parachain::EthereumXcm>()
1
			.unwrap() as u8;
1
	encoded.push(index);
	use sp_core::U256;
	// Let's do a EVM transfer
1
	let eth_tx =
1
		xcm_primitives::EthereumXcmTransaction::V1(xcm_primitives::EthereumXcmTransactionV1 {
1
			gas_limit: U256::from(21000),
1
			fee_payment: xcm_primitives::EthereumXcmFee::Auto,
1
			action: pallet_ethereum::TransactionAction::Call(PARAALICE.into()),
1
			value: U256::from(100),
1
			input: BoundedVec::<
1
				u8,
1
				ConstU32<{ xcm_primitives::MAX_ETHEREUM_XCM_INPUT_SIZE }>
1
			>::try_from(vec![]).unwrap(),
1
			access_list: None,
1
		});
	// Then call bytes
1
	let mut call_bytes = pallet_ethereum_xcm::Call::<parachain::Runtime>::transact_through_proxy {
1
		transact_as: PARAALICE.into(),
1
		xcm_transaction: eth_tx,
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_signed(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(xcm::VersionedLocation::from(para_b_location)),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					para_b_balances
1
				))),
1
				fee_amount: None
1
			},
1
			encoded,
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: None
1
			},
			false
		));
1
	});
1
	ParaB::execute_with(|| {
		// Make sure the EVM transfer wasn't executed
1
		assert!(ParaBalances::free_balance(&PARAALICE.into()) == parachain_b_alice_balances_before);
1
	});
1
}
#[test]
1
fn transact_through_signed_multilocation_para_to_para_ethereum_proxy_succeeds() {
1
	MockNet::reset();
1
	let mut ancestry = Location::parent();
1
	let para_b_location = Location::new(1, [Parachain(2)]);
1
	let para_b_balances = Location::new(1, [Parachain(2), PalletInstance(1u8)]);
1
	ParaA::execute_with(|| {
		// Root can set transact info
1
		assert_ok!(XcmTransactor::set_transact_info(
1
			parachain::RuntimeOrigin::root(),
			// ParaB
1
			Box::new(xcm::VersionedLocation::from(para_b_location.clone())),
			// Para charges 1000 for every instruction, and we have 3, so 3
1
			3.into(),
1
			20000000000.into(),
			// 4 instructions in transact through signed
1
			Some(4.into())
		));
		// Root can set transact info
		// Set fee per second using weight-trader (replaces old set_fee_per_second)
1
		set_fee_per_second_for_location(
1
			para_b_balances.clone(),
1
			parachain::ParaTokensPerSecond::get(),
		)
1
		.expect("must succeed");
1
		ancestry = parachain::UniversalLocation::get().into();
1
	});
	// Let's construct the Junction that we will append with DescendOrigin
1
	let signed_origin: Junctions = [AccountKey20 {
1
		network: None,
1
		key: PARAALICE,
1
	}]
1
	.into();
1
	let mut descend_origin_location = parachain::SelfLocation::get();
1
	descend_origin_location.append_with(signed_origin).unwrap();
	// To convert it to what the paraB will see instead of us
1
	descend_origin_location
1
		.reanchor(&para_b_location, &ancestry.interior)
1
		.unwrap();
1
	let derived = xcm_builder::HashedDescription::<
1
		parachain::AccountId,
1
		xcm_builder::DescribeFamily<xcm_builder::DescribeAllTerminal>,
1
	>::convert_location(&descend_origin_location)
1
	.unwrap();
1
	let transfer_recipient = evm_account();
1
	let mut transfer_recipient_balance_before = 0;
1
	ParaB::execute_with(|| {
1
		assert_ok!(ParaBalances::transfer_allow_death(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			derived.clone(),
			4000000104u128,
		));
		// derived account has all funds
1
		assert!(ParaBalances::free_balance(&derived) == 4000000104);
		// sovereign account has 0 funds
1
		assert!(ParaBalances::free_balance(&para_a_account_20()) == 0);
1
		transfer_recipient_balance_before = ParaBalances::free_balance(&transfer_recipient.into());
		// Add proxy ALICE  -> derived
1
		let _ = parachain::Proxy::add_proxy_delegate(
1
			&PARAALICE.into(),
1
			derived,
1
			parachain::ProxyType::Any,
1
			0,
1
		);
1
	});
	// Encode the call. Balances transact to para_a_account
	// First index
1
	let mut encoded: Vec<u8> = Vec::new();
1
	let index =
1
		<parachain::Runtime as frame_system::Config>::PalletInfo::index::<parachain::EthereumXcm>()
1
			.unwrap() as u8;
1
	encoded.push(index);
	use sp_core::U256;
	// Let's do a EVM transfer
1
	let eth_tx =
1
		xcm_primitives::EthereumXcmTransaction::V2(xcm_primitives::EthereumXcmTransactionV2 {
1
			gas_limit: U256::from(21000),
1
			action: pallet_ethereum::TransactionAction::Call(transfer_recipient.into()),
1
			value: U256::from(100),
1
			input: BoundedVec::<
1
				u8,
1
				ConstU32<{ xcm_primitives::MAX_ETHEREUM_XCM_INPUT_SIZE }>
1
			>::try_from(vec![]).unwrap(),
1
			access_list: None,
1
		});
	// Then call bytes
1
	let mut call_bytes = pallet_ethereum_xcm::Call::<parachain::Runtime>::transact_through_proxy {
1
		transact_as: PARAALICE.into(),
1
		xcm_transaction: eth_tx,
1
	}
1
	.encode();
1
	encoded.append(&mut call_bytes);
1
	ParaA::execute_with(|| {
1
		assert_ok!(XcmTransactor::transact_through_signed(
1
			parachain::RuntimeOrigin::signed(PARAALICE.into()),
1
			Box::new(xcm::VersionedLocation::from(para_b_location)),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					para_b_balances
1
				))),
1
				fee_amount: None
1
			},
1
			encoded,
1
			TransactWeights {
1
				transact_required_weight_at_most: 4000000000.into(),
1
				overall_weight: None
1
			},
			false
		));
1
	});
1
	ParaB::execute_with(|| {
		// Make sure the EVM transfer was executed
1
		assert!(
1
			ParaBalances::free_balance(&transfer_recipient.into())
1
				== transfer_recipient_balance_before + 100
		);
1
	});
1
}
#[test]
1
fn hrmp_init_accept_through_root() {
1
	MockNet::reset();
1
	Relay::execute_with(|| {
1
		assert_ok!(RelayBalances::transfer_allow_death(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			para_a_account(),
			1000u128
		));
1
		assert_ok!(RelayBalances::transfer_allow_death(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			para_b_account(),
			1000u128
		));
1
	});
1
	ParaA::execute_with(|| {
1
		let total_fee = 1_000u128;
1
		let total_weight: u64 = 1_000_000_000;
1
		let tx_weight: u64 = 500_000_000;
		// Root can send hrmp init channel
1
		assert_ok!(XcmTransactor::hrmp_manage(
1
			parachain::RuntimeOrigin::root(),
1
			HrmpOperation::InitOpen(HrmpInitParams {
1
				para_id: 2u32.into(),
1
				proposed_max_capacity: 1,
1
				proposed_max_message_size: 1
1
			}),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				fee_amount: Some(total_fee)
1
			},
1
			TransactWeights {
1
				transact_required_weight_at_most: tx_weight.into(),
1
				overall_weight: Some(Limited(total_weight.into()))
1
			}
		));
1
	});
1
	Relay::execute_with(|| {
1
		let expected_event: relay_chain::RuntimeEvent =
1
			polkadot_runtime_parachains::hrmp::Event::OpenChannelRequested {
1
				sender: 1u32.into(),
1
				recipient: 2u32.into(),
1
				proposed_max_capacity: 1u32,
1
				proposed_max_message_size: 1u32,
1
			}
1
			.into();
1
		assert!(relay_chain::relay_events().contains(&expected_event));
1
	});
1
	ParaB::execute_with(|| {
1
		let total_fee = 1_000u128;
1
		let total_weight: u64 = 1_000_000_000;
1
		let tx_weight: u64 = 500_000_000;
		// Root can send hrmp accept channel
1
		assert_ok!(XcmTransactor::hrmp_manage(
1
			parachain::RuntimeOrigin::root(),
1
			HrmpOperation::Accept {
1
				para_id: 1u32.into()
1
			},
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				fee_amount: Some(total_fee)
1
			},
1
			TransactWeights {
1
				transact_required_weight_at_most: tx_weight.into(),
1
				overall_weight: Some(Limited(total_weight.into()))
1
			}
		));
1
	});
1
	Relay::execute_with(|| {
1
		let expected_event: relay_chain::RuntimeEvent =
1
			polkadot_runtime_parachains::hrmp::Event::OpenChannelAccepted {
1
				sender: 1u32.into(),
1
				recipient: 2u32.into(),
1
			}
1
			.into();
1
		assert!(relay_chain::relay_events().contains(&expected_event));
1
	});
1
}
#[test]
1
fn hrmp_close_works() {
1
	MockNet::reset();
1
	Relay::execute_with(|| {
1
		assert_ok!(RelayBalances::transfer_allow_death(
1
			relay_chain::RuntimeOrigin::signed(RELAYALICE),
1
			para_a_account(),
			1000u128
		));
1
		assert_ok!(Hrmp::force_open_hrmp_channel(
1
			relay_chain::RuntimeOrigin::root(),
1
			1u32.into(),
1
			2u32.into(),
			1u32,
			1u32
		));
1
		assert_ok!(Hrmp::force_process_hrmp_open(
1
			relay_chain::RuntimeOrigin::root(),
			1u32
		));
1
	});
1
	ParaA::execute_with(|| {
1
		let total_fee = 1_000u128;
1
		let total_weight: u64 = 1_000_000_000;
1
		let tx_weight: u64 = 500_000_000;
		// Root can send hrmp close
1
		assert_ok!(XcmTransactor::hrmp_manage(
1
			parachain::RuntimeOrigin::root(),
1
			HrmpOperation::Close(HrmpChannelId {
1
				sender: 1u32.into(),
1
				recipient: 2u32.into()
1
			}),
1
			CurrencyPayment {
1
				currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::from(
1
					Location::parent()
1
				))),
1
				fee_amount: Some(total_fee)
1
			},
1
			TransactWeights {
1
				transact_required_weight_at_most: tx_weight.into(),
1
				overall_weight: Some(Limited(total_weight.into()))
1
			}
		));
1
	});
1
	Relay::execute_with(|| {
1
		let expected_event: relay_chain::RuntimeEvent =
1
			polkadot_runtime_parachains::hrmp::Event::ChannelClosed {
1
				by_parachain: 1u32.into(),
1
				channel_id: HrmpChannelId {
1
					sender: 1u32.into(),
1
					recipient: 2u32.into(),
1
				},
1
			}
1
			.into();
1
		assert!(relay_chain::relay_events().contains(&expected_event));
1
	});
1
}
use crate::xcm_mock::parachain::XcmWeightTrader;
use parity_scale_codec::{Decode, Encode};
use sp_io::hashing::blake2_256;
// Helper to derive accountIds
7
pub fn derivative_account_id(who: sp_runtime::AccountId32, index: u16) -> sp_runtime::AccountId32 {
7
	let entropy = (b"modlpy/utilisuba", who, index).using_encoded(blake2_256);
7
	sp_runtime::AccountId32::decode(&mut &entropy[..]).expect("valid account id")
7
}