1
// Copyright 2019-2022 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 Integration Tests
18

            
19
#![cfg(test)]
20

            
21
mod common;
22
use common::*;
23

            
24
use fp_evm::{Context, IsPrecompileResult};
25
use frame_support::{
26
	assert_noop, assert_ok,
27
	dispatch::DispatchClass,
28
	traits::{
29
		fungible::Inspect, Currency as CurrencyT, EnsureOrigin, OnInitialize, PalletInfo,
30
		StorageInfo, StorageInfoTrait,
31
	},
32
	weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
33
	StorageHasher, Twox128,
34
};
35
use moonbeam_runtime::currency::{GIGAWEI, WEI};
36
use moonbeam_runtime::runtime_params::dynamic_params;
37
use moonbeam_runtime::{
38
	asset_config::ForeignAssetInstance,
39
	currency::GLMR,
40
	xcm_config::{CurrencyId, SelfReserve},
41
	AccountId, Balances, CrowdloanRewards, Executive, OpenTechCommitteeCollective,
42
	ParachainStaking, PolkadotXcm, Precompiles, Runtime, RuntimeBlockWeights, RuntimeCall,
43
	RuntimeEvent, System, TransactionPayment, TransactionPaymentAsGasPrice, Treasury,
44
	TreasuryCouncilCollective, XcmTransactor, FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX, WEEKS,
45
};
46
use moonbeam_xcm_benchmarks::weights::XcmWeight;
47
use moonkit_xcm_primitives::AccountIdAssetIdConversion;
48
use nimbus_primitives::NimbusId;
49
use pallet_evm::PrecompileSet;
50
use pallet_evm_precompileset_assets_erc20::{SELECTOR_LOG_APPROVAL, SELECTOR_LOG_TRANSFER};
51
use pallet_transaction_payment::Multiplier;
52
use pallet_xcm_transactor::{Currency, CurrencyPayment, TransactWeights};
53
use parity_scale_codec::Encode;
54
use polkadot_parachain::primitives::Sibling;
55
use precompile_utils::{
56
	precompile_set::{is_precompile_or_fail, IsActivePrecompile},
57
	prelude::*,
58
	testing::*,
59
};
60
use sha3::{Digest, Keccak256};
61
use sp_core::{ByteArray, Get, Pair, H160, U256};
62
use sp_runtime::{
63
	traits::{Convert, Dispatchable},
64
	BuildStorage, DispatchError, ModuleError,
65
};
66
use std::str::from_utf8;
67
use xcm::{latest::prelude::*, VersionedAssets, VersionedLocation};
68
use xcm_builder::{ParentIsPreset, SiblingParachainConvertsVia};
69
use xcm_executor::traits::ConvertLocation;
70
use xcm_primitives::split_location_into_chain_part_and_beneficiary;
71

            
72
type BatchPCall = pallet_evm_precompile_batch::BatchPrecompileCall<Runtime>;
73
type CrowdloanRewardsPCall =
74
	pallet_evm_precompile_crowdloan_rewards::CrowdloanRewardsPrecompileCall<Runtime>;
75
type XcmUtilsPCall = pallet_evm_precompile_xcm_utils::XcmUtilsPrecompileCall<
76
	Runtime,
77
	moonbeam_runtime::xcm_config::XcmExecutorConfig,
78
>;
79
type XtokensPCall = pallet_evm_precompile_xtokens::XtokensPrecompileCall<Runtime>;
80
type ForeignAssetsPCall = pallet_evm_precompileset_assets_erc20::Erc20AssetsPrecompileSetCall<
81
	Runtime,
82
	ForeignAssetInstance,
83
>;
84
type XcmTransactorV2PCall =
85
	pallet_evm_precompile_xcm_transactor::v2::XcmTransactorPrecompileV2Call<Runtime>;
86

            
87
const BASE_FEE_GENESIS: u128 = 10000 * GIGAWEI;
88

            
89
3
fn currency_to_asset(currency_id: CurrencyId, amount: u128) -> Asset {
90
3
	Asset {
91
3
		id: AssetId(
92
3
			<moonbeam_runtime::Runtime as pallet_xcm_transactor::Config>::CurrencyIdToLocation::convert(
93
3
				currency_id,
94
3
			)
95
3
			.unwrap(),
96
3
		),
97
3
		fun: Fungibility::Fungible(amount),
98
3
	}
99
3
}
100
#[test]
101
1
fn xcmp_queue_controller_origin_is_root() {
102
1
	// important for the XcmExecutionManager impl of PauseExecution which uses root origin
103
1
	// to suspend/resume XCM execution in xcmp_queue::on_idle
104
1
	assert_ok!(
105
1
		<moonbeam_runtime::Runtime as cumulus_pallet_xcmp_queue::Config
106
1
		>::ControllerOrigin::ensure_origin(root_origin())
107
1
	);
108
1
}
109

            
110
#[test]
111
1
fn verify_pallet_prefixes() {
112
30
	fn is_pallet_prefix<P: 'static>(name: &str) {
113
30
		// Compares the unhashed pallet prefix in the `StorageInstance` implementation by every
114
30
		// storage item in the pallet P. This pallet prefix is used in conjunction with the
115
30
		// item name to get the unique storage key: hash(PalletPrefix) + hash(StorageName)
116
30
		// https://github.com/paritytech/substrate/blob/master/frame/support/procedural/src/pallet/
117
30
		// expand/storage.rs#L389-L401
118
30
		assert_eq!(
119
30
			<moonbeam_runtime::Runtime as frame_system::Config>::PalletInfo::name::<P>(),
120
30
			Some(name)
121
30
		);
122
30
	}
123
1
	// TODO: use StorageInfoTrait once https://github.com/paritytech/substrate/pull/9246
124
1
	// is pulled in substrate deps.
125
1
	is_pallet_prefix::<moonbeam_runtime::System>("System");
126
1
	is_pallet_prefix::<moonbeam_runtime::Utility>("Utility");
127
1
	is_pallet_prefix::<moonbeam_runtime::ParachainSystem>("ParachainSystem");
128
1
	is_pallet_prefix::<moonbeam_runtime::TransactionPayment>("TransactionPayment");
129
1
	is_pallet_prefix::<moonbeam_runtime::ParachainInfo>("ParachainInfo");
130
1
	is_pallet_prefix::<moonbeam_runtime::EthereumChainId>("EthereumChainId");
131
1
	is_pallet_prefix::<moonbeam_runtime::EVM>("EVM");
132
1
	is_pallet_prefix::<moonbeam_runtime::Ethereum>("Ethereum");
133
1
	is_pallet_prefix::<moonbeam_runtime::ParachainStaking>("ParachainStaking");
134
1
	is_pallet_prefix::<moonbeam_runtime::Scheduler>("Scheduler");
135
1
	is_pallet_prefix::<moonbeam_runtime::OpenTechCommitteeCollective>(
136
1
		"OpenTechCommitteeCollective",
137
1
	);
138
1
	is_pallet_prefix::<moonbeam_runtime::Treasury>("Treasury");
139
1
	is_pallet_prefix::<moonbeam_runtime::AuthorInherent>("AuthorInherent");
140
1
	is_pallet_prefix::<moonbeam_runtime::AuthorFilter>("AuthorFilter");
141
1
	is_pallet_prefix::<moonbeam_runtime::CrowdloanRewards>("CrowdloanRewards");
142
1
	is_pallet_prefix::<moonbeam_runtime::AuthorMapping>("AuthorMapping");
143
1
	is_pallet_prefix::<moonbeam_runtime::MaintenanceMode>("MaintenanceMode");
144
1
	is_pallet_prefix::<moonbeam_runtime::Identity>("Identity");
145
1
	is_pallet_prefix::<moonbeam_runtime::XcmpQueue>("XcmpQueue");
146
1
	is_pallet_prefix::<moonbeam_runtime::CumulusXcm>("CumulusXcm");
147
1
	is_pallet_prefix::<moonbeam_runtime::PolkadotXcm>("PolkadotXcm");
148
1
	is_pallet_prefix::<moonbeam_runtime::Assets>("Assets");
149
1
	is_pallet_prefix::<moonbeam_runtime::AssetManager>("AssetManager");
150
1
	is_pallet_prefix::<moonbeam_runtime::Migrations>("Migrations");
151
1
	is_pallet_prefix::<moonbeam_runtime::XcmTransactor>("XcmTransactor");
152
1
	is_pallet_prefix::<moonbeam_runtime::ProxyGenesisCompanion>("ProxyGenesisCompanion");
153
1
	is_pallet_prefix::<moonbeam_runtime::MoonbeamOrbiters>("MoonbeamOrbiters");
154
1
	is_pallet_prefix::<moonbeam_runtime::TreasuryCouncilCollective>("TreasuryCouncilCollective");
155
1
	is_pallet_prefix::<moonbeam_runtime::MoonbeamLazyMigrations>("MoonbeamLazyMigrations");
156
1
	is_pallet_prefix::<moonbeam_runtime::RelayStorageRoots>("RelayStorageRoots");
157
1

            
158
14
	let prefix = |pallet_name, storage_name| {
159
14
		let mut res = [0u8; 32];
160
14
		res[0..16].copy_from_slice(&Twox128::hash(pallet_name));
161
14
		res[16..32].copy_from_slice(&Twox128::hash(storage_name));
162
14
		res.to_vec()
163
14
	};
164
1
	assert_eq!(
165
1
		<moonbeam_runtime::Timestamp as StorageInfoTrait>::storage_info(),
166
1
		vec![
167
1
			StorageInfo {
168
1
				pallet_name: b"Timestamp".to_vec(),
169
1
				storage_name: b"Now".to_vec(),
170
1
				prefix: prefix(b"Timestamp", b"Now"),
171
1
				max_values: Some(1),
172
1
				max_size: Some(8),
173
1
			},
174
1
			StorageInfo {
175
1
				pallet_name: b"Timestamp".to_vec(),
176
1
				storage_name: b"DidUpdate".to_vec(),
177
1
				prefix: prefix(b"Timestamp", b"DidUpdate"),
178
1
				max_values: Some(1),
179
1
				max_size: Some(1),
180
1
			}
181
1
		]
182
1
	);
183
1
	assert_eq!(
184
1
		<moonbeam_runtime::Balances as StorageInfoTrait>::storage_info(),
185
1
		vec![
186
1
			StorageInfo {
187
1
				pallet_name: b"Balances".to_vec(),
188
1
				storage_name: b"TotalIssuance".to_vec(),
189
1
				prefix: prefix(b"Balances", b"TotalIssuance"),
190
1
				max_values: Some(1),
191
1
				max_size: Some(16),
192
1
			},
193
1
			StorageInfo {
194
1
				pallet_name: b"Balances".to_vec(),
195
1
				storage_name: b"InactiveIssuance".to_vec(),
196
1
				prefix: prefix(b"Balances", b"InactiveIssuance"),
197
1
				max_values: Some(1),
198
1
				max_size: Some(16),
199
1
			},
200
1
			StorageInfo {
201
1
				pallet_name: b"Balances".to_vec(),
202
1
				storage_name: b"Account".to_vec(),
203
1
				prefix: prefix(b"Balances", b"Account"),
204
1
				max_values: None,
205
1
				max_size: Some(100),
206
1
			},
207
1
			StorageInfo {
208
1
				pallet_name: b"Balances".to_vec(),
209
1
				storage_name: b"Locks".to_vec(),
210
1
				prefix: prefix(b"Balances", b"Locks"),
211
1
				max_values: None,
212
1
				max_size: Some(1287),
213
1
			},
214
1
			StorageInfo {
215
1
				pallet_name: b"Balances".to_vec(),
216
1
				storage_name: b"Reserves".to_vec(),
217
1
				prefix: prefix(b"Balances", b"Reserves"),
218
1
				max_values: None,
219
1
				max_size: Some(1037),
220
1
			},
221
1
			StorageInfo {
222
1
				pallet_name: b"Balances".to_vec(),
223
1
				storage_name: b"Holds".to_vec(),
224
1
				prefix: prefix(b"Balances", b"Holds"),
225
1
				max_values: None,
226
1
				max_size: Some(55),
227
1
			},
228
1
			StorageInfo {
229
1
				pallet_name: b"Balances".to_vec(),
230
1
				storage_name: b"Freezes".to_vec(),
231
1
				prefix: prefix(b"Balances", b"Freezes"),
232
1
				max_values: None,
233
1
				max_size: Some(37),
234
1
			},
235
1
		]
236
1
	);
237
1
	assert_eq!(
238
1
		<moonbeam_runtime::Proxy as StorageInfoTrait>::storage_info(),
239
1
		vec![
240
1
			StorageInfo {
241
1
				pallet_name: b"Proxy".to_vec(),
242
1
				storage_name: b"Proxies".to_vec(),
243
1
				prefix: prefix(b"Proxy", b"Proxies"),
244
1
				max_values: None,
245
1
				max_size: Some(845),
246
1
			},
247
1
			StorageInfo {
248
1
				pallet_name: b"Proxy".to_vec(),
249
1
				storage_name: b"Announcements".to_vec(),
250
1
				prefix: prefix(b"Proxy", b"Announcements"),
251
1
				max_values: None,
252
1
				max_size: Some(1837),
253
1
			}
254
1
		]
255
1
	);
256
1
	assert_eq!(
257
1
		<moonbeam_runtime::MaintenanceMode as StorageInfoTrait>::storage_info(),
258
1
		vec![StorageInfo {
259
1
			pallet_name: b"MaintenanceMode".to_vec(),
260
1
			storage_name: b"MaintenanceMode".to_vec(),
261
1
			prefix: prefix(b"MaintenanceMode", b"MaintenanceMode"),
262
1
			max_values: Some(1),
263
1
			max_size: None,
264
1
		},]
265
1
	);
266
1
	assert_eq!(
267
1
		<moonbeam_runtime::RelayStorageRoots as StorageInfoTrait>::storage_info(),
268
1
		vec![
269
1
			StorageInfo {
270
1
				pallet_name: b"RelayStorageRoots".to_vec(),
271
1
				storage_name: b"RelayStorageRoot".to_vec(),
272
1
				prefix: prefix(b"RelayStorageRoots", b"RelayStorageRoot"),
273
1
				max_values: None,
274
1
				max_size: Some(44),
275
1
			},
276
1
			StorageInfo {
277
1
				pallet_name: b"RelayStorageRoots".to_vec(),
278
1
				storage_name: b"RelayStorageRootKeys".to_vec(),
279
1
				prefix: prefix(b"RelayStorageRoots", b"RelayStorageRootKeys"),
280
1
				max_values: Some(1),
281
1
				max_size: Some(121),
282
1
			},
283
1
		]
284
1
	);
285
1
}
286

            
287
#[test]
288
1
fn test_collectives_storage_item_prefixes() {
289
6
	for StorageInfo { pallet_name, .. } in
290
7
		<moonbeam_runtime::TreasuryCouncilCollective as StorageInfoTrait>::storage_info()
291
	{
292
6
		assert_eq!(pallet_name, b"TreasuryCouncilCollective".to_vec());
293
	}
294

            
295
6
	for StorageInfo { pallet_name, .. } in
296
7
		<moonbeam_runtime::OpenTechCommitteeCollective as StorageInfoTrait>::storage_info()
297
	{
298
6
		assert_eq!(pallet_name, b"OpenTechCommitteeCollective".to_vec());
299
	}
300
1
}
301

            
302
#[test]
303
1
fn collective_set_members_root_origin_works() {
304
1
	ExtBuilder::default().build().execute_with(|| {
305
1
		// TreasuryCouncilCollective
306
1
		assert_ok!(TreasuryCouncilCollective::set_members(
307
1
			<Runtime as frame_system::Config>::RuntimeOrigin::root(),
308
1
			vec![AccountId::from(ALICE), AccountId::from(BOB)],
309
1
			Some(AccountId::from(ALICE)),
310
1
			2
311
1
		));
312
		// OpenTechCommitteeCollective
313
1
		assert_ok!(OpenTechCommitteeCollective::set_members(
314
1
			<Runtime as frame_system::Config>::RuntimeOrigin::root(),
315
1
			vec![AccountId::from(ALICE), AccountId::from(BOB)],
316
1
			Some(AccountId::from(ALICE)),
317
1
			2
318
1
		));
319
1
	});
320
1
}
321

            
322
#[test]
323
1
fn collective_set_members_general_admin_origin_works() {
324
1
	use moonbeam_runtime::{
325
1
		governance::custom_origins::Origin as CustomOrigin, OriginCaller, Utility,
326
1
	};
327
1

            
328
1
	ExtBuilder::default().build().execute_with(|| {
329
1
		let root_caller = <Runtime as frame_system::Config>::RuntimeOrigin::root();
330
1
		let alice = AccountId::from(ALICE);
331
1

            
332
1
		// TreasuryCouncilCollective
333
1
		let _ = Utility::dispatch_as(
334
1
			root_caller.clone(),
335
1
			Box::new(OriginCaller::Origins(CustomOrigin::GeneralAdmin)),
336
1
			Box::new(
337
1
				pallet_collective::Call::<Runtime, pallet_collective::Instance3>::set_members {
338
1
					new_members: vec![alice, AccountId::from(BOB)],
339
1
					prime: Some(alice),
340
1
					old_count: 2,
341
1
				}
342
1
				.into(),
343
1
			),
344
1
		);
345
1
		// OpenTechCommitteeCollective
346
1
		let _ = Utility::dispatch_as(
347
1
			root_caller,
348
1
			Box::new(OriginCaller::Origins(CustomOrigin::GeneralAdmin)),
349
1
			Box::new(
350
1
				pallet_collective::Call::<Runtime, pallet_collective::Instance4>::set_members {
351
1
					new_members: vec![alice, AccountId::from(BOB)],
352
1
					prime: Some(alice),
353
1
					old_count: 2,
354
1
				}
355
1
				.into(),
356
1
			),
357
1
		);
358
1

            
359
1
		assert_eq!(
360
1
			System::events()
361
1
				.into_iter()
362
2
				.filter_map(|r| {
363
2
					match r.event {
364
2
						RuntimeEvent::Utility(pallet_utility::Event::DispatchedAs { result })
365
2
							if result.is_ok() =>
366
2
						{
367
2
							Some(true)
368
						}
369
						_ => None,
370
					}
371
2
				})
372
1
				.collect::<Vec<_>>()
373
1
				.len(),
374
1
			2
375
1
		)
376
1
	});
377
1
}
378

            
379
#[test]
380
1
fn collective_set_members_signed_origin_does_not_work() {
381
1
	let alice = AccountId::from(ALICE);
382
1
	ExtBuilder::default().build().execute_with(|| {
383
1
		// TreasuryCouncilCollective
384
1
		assert!(TreasuryCouncilCollective::set_members(
385
1
			<Runtime as frame_system::Config>::RuntimeOrigin::signed(alice),
386
1
			vec![AccountId::from(ALICE), AccountId::from(BOB)],
387
1
			Some(AccountId::from(ALICE)),
388
1
			2
389
1
		)
390
1
		.is_err());
391
		// OpenTechCommitteeCollective
392
1
		assert!(OpenTechCommitteeCollective::set_members(
393
1
			<Runtime as frame_system::Config>::RuntimeOrigin::signed(alice),
394
1
			vec![AccountId::from(ALICE), AccountId::from(BOB)],
395
1
			Some(AccountId::from(ALICE)),
396
1
			2
397
1
		)
398
1
		.is_err());
399
1
	});
400
1
}
401

            
402
#[test]
403
1
fn verify_pallet_indices() {
404
32
	fn is_pallet_index<P: 'static>(index: usize) {
405
32
		assert_eq!(
406
32
			<moonbeam_runtime::Runtime as frame_system::Config>::PalletInfo::index::<P>(),
407
32
			Some(index)
408
32
		);
409
32
	}
410
1

            
411
1
	// System support
412
1
	is_pallet_index::<moonbeam_runtime::System>(0);
413
1
	is_pallet_index::<moonbeam_runtime::ParachainSystem>(1);
414
1
	is_pallet_index::<moonbeam_runtime::Timestamp>(3);
415
1
	is_pallet_index::<moonbeam_runtime::ParachainInfo>(4);
416
1
	// Monetary
417
1
	is_pallet_index::<moonbeam_runtime::Balances>(10);
418
1
	is_pallet_index::<moonbeam_runtime::TransactionPayment>(11);
419
1
	// Consensus support
420
1
	is_pallet_index::<moonbeam_runtime::ParachainStaking>(20);
421
1
	is_pallet_index::<moonbeam_runtime::AuthorInherent>(21);
422
1
	is_pallet_index::<moonbeam_runtime::AuthorFilter>(22);
423
1
	is_pallet_index::<moonbeam_runtime::AuthorMapping>(23);
424
1
	is_pallet_index::<moonbeam_runtime::MoonbeamOrbiters>(24);
425
1
	// Handy utilities
426
1
	is_pallet_index::<moonbeam_runtime::Utility>(30);
427
1
	is_pallet_index::<moonbeam_runtime::Proxy>(31);
428
1
	is_pallet_index::<moonbeam_runtime::MaintenanceMode>(32);
429
1
	is_pallet_index::<moonbeam_runtime::Identity>(33);
430
1
	is_pallet_index::<moonbeam_runtime::Migrations>(34);
431
1
	is_pallet_index::<moonbeam_runtime::ProxyGenesisCompanion>(35);
432
1
	is_pallet_index::<moonbeam_runtime::MoonbeamLazyMigrations>(37);
433
1
	// Ethereum compatibility
434
1
	is_pallet_index::<moonbeam_runtime::EthereumChainId>(50);
435
1
	is_pallet_index::<moonbeam_runtime::EVM>(51);
436
1
	is_pallet_index::<moonbeam_runtime::Ethereum>(52);
437
1
	// Governance
438
1
	is_pallet_index::<moonbeam_runtime::Scheduler>(60);
439
1
	// is_pallet_index::<moonbeam_runtime::Democracy>(61); Removed
440
1
	// Council
441
1
	// is_pallet_index::<moonbeam_runtime::CouncilCollective>(70); Removed
442
1
	// is_pallet_index::<moonbeam_runtime::TechCommitteeCollective>(71); Removed
443
1
	is_pallet_index::<moonbeam_runtime::TreasuryCouncilCollective>(72);
444
1
	is_pallet_index::<moonbeam_runtime::OpenTechCommitteeCollective>(73);
445
1
	// Treasury
446
1
	is_pallet_index::<moonbeam_runtime::Treasury>(80);
447
1
	// Crowdloan
448
1
	is_pallet_index::<moonbeam_runtime::CrowdloanRewards>(90);
449
1
	// XCM Stuff
450
1
	is_pallet_index::<moonbeam_runtime::XcmpQueue>(100);
451
1
	is_pallet_index::<moonbeam_runtime::CumulusXcm>(101);
452
1
	is_pallet_index::<moonbeam_runtime::PolkadotXcm>(103);
453
1
	is_pallet_index::<moonbeam_runtime::Assets>(104);
454
1
	is_pallet_index::<moonbeam_runtime::AssetManager>(105);
455
1
	// is_pallet_index::<moonbeam_runtime::XTokens>(106); Removed
456
1
	is_pallet_index::<moonbeam_runtime::XcmTransactor>(107);
457
1
}
458

            
459
#[test]
460
1
fn verify_reserved_indices() {
461
1
	let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
462
1
		.build_storage()
463
1
		.unwrap()
464
1
		.into();
465
1

            
466
1
	t.execute_with(|| {
467
1
		use frame_metadata::*;
468
1
		let metadata = moonbeam_runtime::Runtime::metadata();
469
1
		let metadata = match metadata.1 {
470
1
			RuntimeMetadata::V14(metadata) => metadata,
471
			_ => panic!("metadata has been bumped, test needs to be updated"),
472
		};
473
		// 40: Sudo
474
		// 53: BaseFee
475
		// 108: pallet_assets::<Instance1>
476
1
		let reserved = vec![40, 53, 108];
477
1
		let existing = metadata
478
1
			.pallets
479
1
			.iter()
480
49
			.map(|p| p.index)
481
1
			.collect::<Vec<u8>>();
482
3
		assert!(reserved.iter().all(|index| !existing.contains(index)));
483
1
	});
484
1
}
485

            
486
#[test]
487
1
fn verify_proxy_type_indices() {
488
1
	assert_eq!(moonbeam_runtime::ProxyType::Any as u8, 0);
489
1
	assert_eq!(moonbeam_runtime::ProxyType::NonTransfer as u8, 1);
490
1
	assert_eq!(moonbeam_runtime::ProxyType::Governance as u8, 2);
491
1
	assert_eq!(moonbeam_runtime::ProxyType::Staking as u8, 3);
492
1
	assert_eq!(moonbeam_runtime::ProxyType::CancelProxy as u8, 4);
493
1
	assert_eq!(moonbeam_runtime::ProxyType::Balances as u8, 5);
494
1
	assert_eq!(moonbeam_runtime::ProxyType::AuthorMapping as u8, 6);
495
1
	assert_eq!(moonbeam_runtime::ProxyType::IdentityJudgement as u8, 7);
496
1
}
497

            
498
#[test]
499
1
fn join_collator_candidates() {
500
1
	ExtBuilder::default()
501
1
		.with_balances(vec![
502
1
			(AccountId::from(ALICE), 10_000_000 * GLMR),
503
1
			(AccountId::from(BOB), 10_000_000 * GLMR),
504
1
			(AccountId::from(CHARLIE), 10_000_000 * GLMR),
505
1
			(AccountId::from(DAVE), 10_000_000 * GLMR),
506
1
		])
507
1
		.with_collators(vec![
508
1
			(AccountId::from(ALICE), 2_000_000 * GLMR),
509
1
			(AccountId::from(BOB), 2_000_000 * GLMR),
510
1
		])
511
1
		.with_delegations(vec![
512
1
			(
513
1
				AccountId::from(CHARLIE),
514
1
				AccountId::from(ALICE),
515
1
				5_000 * GLMR,
516
1
			),
517
1
			(AccountId::from(CHARLIE), AccountId::from(BOB), 5_000 * GLMR),
518
1
		])
519
1
		.build()
520
1
		.execute_with(|| {
521
1
			assert_noop!(
522
1
				ParachainStaking::join_candidates(
523
1
					origin_of(AccountId::from(ALICE)),
524
1
					2_000_000 * GLMR,
525
1
					2u32
526
1
				),
527
1
				pallet_parachain_staking::Error::<Runtime>::CandidateExists
528
1
			);
529
1
			assert_noop!(
530
1
				ParachainStaking::join_candidates(
531
1
					origin_of(AccountId::from(CHARLIE)),
532
1
					2_000_000 * GLMR,
533
1
					2u32
534
1
				),
535
1
				pallet_parachain_staking::Error::<Runtime>::DelegatorExists
536
1
			);
537
1
			assert!(System::events().is_empty());
538
1
			assert_ok!(ParachainStaking::join_candidates(
539
1
				origin_of(AccountId::from(DAVE)),
540
1
				2_000_000 * GLMR,
541
1
				2u32
542
1
			));
543
1
			assert_eq!(
544
1
				last_event(),
545
1
				RuntimeEvent::ParachainStaking(
546
1
					pallet_parachain_staking::Event::JoinedCollatorCandidates {
547
1
						account: AccountId::from(DAVE),
548
1
						amount_locked: 2_000_000 * GLMR,
549
1
						new_total_amt_locked: 6_010_000 * GLMR
550
1
					}
551
1
				)
552
1
			);
553
1
			let candidates = ParachainStaking::candidate_pool();
554
1
			assert_eq!(candidates.0[0].owner, AccountId::from(ALICE));
555
1
			assert_eq!(candidates.0[0].amount, 2_005_000 * GLMR);
556
1
			assert_eq!(candidates.0[1].owner, AccountId::from(BOB));
557
1
			assert_eq!(candidates.0[1].amount, 2_005_000 * GLMR);
558
1
			assert_eq!(candidates.0[2].owner, AccountId::from(DAVE));
559
1
			assert_eq!(candidates.0[2].amount, 2_000_000 * GLMR);
560
1
		});
561
1
}
562

            
563
#[test]
564
1
fn transfer_through_evm_to_stake() {
565
1
	ExtBuilder::default()
566
1
		.with_balances(vec![(AccountId::from(ALICE), 10_000_000 * GLMR)])
567
1
		.build()
568
1
		.execute_with(|| {
569
1
			// Charlie has no balance => fails to stake
570
1
			assert_noop!(
571
1
				ParachainStaking::join_candidates(
572
1
					origin_of(AccountId::from(CHARLIE)),
573
1
					2_000_000 * GLMR,
574
1
					2u32
575
1
				),
576
1
				DispatchError::Module(ModuleError {
577
1
					index: 20,
578
1
					error: [8, 0, 0, 0],
579
1
					message: Some("InsufficientBalance")
580
1
				})
581
1
			);
582
			// Alice transfer from free balance 3_000_000 GLMR to Bob
583
1
			assert_ok!(Balances::transfer_allow_death(
584
1
				origin_of(AccountId::from(ALICE)),
585
1
				AccountId::from(BOB),
586
1
				3_000_000 * GLMR,
587
1
			));
588
1
			assert_eq!(
589
1
				Balances::free_balance(AccountId::from(BOB)),
590
1
				3_000_000 * GLMR
591
1
			);
592

            
593
1
			let gas_limit = 100000u64;
594
1
			let gas_price: U256 = BASE_FEE_GENESIS.into();
595
1
			// Bob transfers 2_000_000 GLMR to Charlie via EVM
596
1
			assert_ok!(RuntimeCall::EVM(pallet_evm::Call::<Runtime>::call {
597
1
				source: H160::from(BOB),
598
1
				target: H160::from(CHARLIE),
599
1
				input: vec![],
600
1
				value: (2_000_000 * GLMR).into(),
601
1
				gas_limit,
602
1
				max_fee_per_gas: gas_price,
603
1
				max_priority_fee_per_gas: None,
604
1
				nonce: None,
605
1
				access_list: Vec::new(),
606
1
			})
607
1
			.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::root()));
608
1
			assert_eq!(
609
1
				Balances::free_balance(AccountId::from(CHARLIE)),
610
1
				2_000_000 * GLMR,
611
1
			);
612

            
613
			// Charlie can stake now
614
1
			assert_ok!(ParachainStaking::join_candidates(
615
1
				origin_of(AccountId::from(CHARLIE)),
616
1
				2_000_000 * GLMR,
617
1
				2u32
618
1
			),);
619
1
			let candidates = ParachainStaking::candidate_pool();
620
1
			assert_eq!(candidates.0[0].owner, AccountId::from(CHARLIE));
621
1
			assert_eq!(candidates.0[0].amount, 2_000_000 * GLMR);
622
1
		});
623
1
}
624

            
625
#[test]
626
1
fn reward_block_authors() {
627
1
	ExtBuilder::default()
628
1
		.with_balances(vec![
629
1
			// Alice gets 10k extra tokens for her mapping deposit
630
1
			(AccountId::from(ALICE), 10_010_000 * GLMR),
631
1
			(AccountId::from(BOB), 10_000_000 * GLMR),
632
1
		])
633
1
		.with_collators(vec![(AccountId::from(ALICE), 2_000_000 * GLMR)])
634
1
		.with_delegations(vec![(
635
1
			AccountId::from(BOB),
636
1
			AccountId::from(ALICE),
637
1
			50_000 * GLMR,
638
1
		)])
639
1
		.with_mappings(vec![(
640
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
641
1
			AccountId::from(ALICE),
642
1
		)])
643
1
		.build()
644
1
		.execute_with(|| {
645
1
			increase_last_relay_slot_number(1);
646
1
			// Just before round 3
647
1
			run_to_block(7199, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
648
1
			// no rewards doled out yet
649
1
			assert_eq!(
650
1
				Balances::usable_balance(AccountId::from(ALICE)),
651
1
				8_010_000 * GLMR,
652
1
			);
653
1
			assert_eq!(
654
1
				Balances::usable_balance(AccountId::from(BOB)),
655
1
				9_950_000 * GLMR,
656
1
			);
657
1
			run_to_block(7201, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
658
1
			// rewards minted and distributed
659
1
			assert_eq!(
660
1
				Balances::usable_balance(AccountId::from(ALICE)),
661
1
				8990978048702400000000000,
662
1
			);
663
1
			assert_eq!(
664
1
				Balances::usable_balance(AccountId::from(BOB)),
665
1
				9969521950497200000000000,
666
1
			);
667
1
		});
668
1
}
669

            
670
#[test]
671
1
fn reward_block_authors_with_parachain_bond_reserved() {
672
1
	ExtBuilder::default()
673
1
		.with_balances(vec![
674
1
			// Alice gets 10k extra tokens for her mapping deposit
675
1
			(AccountId::from(ALICE), 10_010_000 * GLMR),
676
1
			(AccountId::from(BOB), 10_000_000 * GLMR),
677
1
			(AccountId::from(CHARLIE), 10_000 * GLMR),
678
1
		])
679
1
		.with_collators(vec![(AccountId::from(ALICE), 2_000_000 * GLMR)])
680
1
		.with_delegations(vec![(
681
1
			AccountId::from(BOB),
682
1
			AccountId::from(ALICE),
683
1
			50_000 * GLMR,
684
1
		)])
685
1
		.with_mappings(vec![(
686
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
687
1
			AccountId::from(ALICE),
688
1
		)])
689
1
		.build()
690
1
		.execute_with(|| {
691
1
			increase_last_relay_slot_number(1);
692
1
			assert_ok!(ParachainStaking::set_parachain_bond_account(
693
1
				root_origin(),
694
1
				AccountId::from(CHARLIE),
695
1
			),);
696

            
697
			// Stop just before round 3
698
1
			run_to_block(7199, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
699
1
			// no collators rewards doled out yet
700
1
			assert_eq!(
701
1
				Balances::usable_balance(AccountId::from(ALICE)),
702
1
				8_010_000 * GLMR,
703
1
			);
704
1
			assert_eq!(
705
1
				Balances::usable_balance(AccountId::from(BOB)),
706
1
				9_950_000 * GLMR,
707
1
			);
708
			// 30% reserved for parachain bond
709
1
			assert_eq!(
710
1
				Balances::usable_balance(AccountId::from(CHARLIE)),
711
1
				310300000000000000000000,
712
1
			);
713

            
714
			// Go to round 3
715
1
			run_to_block(7201, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
716
1

            
717
1
			// collators rewards minted and distributed
718
1
			assert_eq!(
719
1
				Balances::usable_balance(AccountId::from(ALICE)),
720
1
				8698492682878000000000000,
721
1
			);
722
1
			assert_eq!(
723
1
				Balances::usable_balance(AccountId::from(BOB)),
724
1
				9962207316621500000000000,
725
1
			);
726
			// 30% reserved for parachain bond again
727
1
			assert_eq!(
728
1
				Balances::usable_balance(AccountId::from(CHARLIE)),
729
1
				615104500000000000000000,
730
1
			);
731
1
		});
732
1
}
733

            
734
#[test]
735
1
fn initialize_crowdloan_addresses_with_batch_and_pay() {
736
1
	ExtBuilder::default()
737
1
		.with_balances(vec![
738
1
			(AccountId::from(ALICE), 200_000 * GLMR),
739
1
			(AccountId::from(BOB), 100_000 * GLMR),
740
1
		])
741
1
		.with_collators(vec![(AccountId::from(ALICE), 100_000 * GLMR)])
742
1
		.with_mappings(vec![(
743
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
744
1
			AccountId::from(ALICE),
745
1
		)])
746
1
		.with_crowdloan_fund(300_000_000 * GLMR)
747
1
		.build()
748
1
		.execute_with(|| {
749
1
			// set parachain inherent data
750
1
			set_parachain_inherent_data();
751
1
			let init_block = CrowdloanRewards::init_vesting_block();
752
1
			// This matches the previous vesting
753
1
			let end_block = init_block + 4 * WEEKS;
754
1
			// Batch calls always succeed. We just need to check the inner event
755
1
			assert_ok!(
756
1
				RuntimeCall::Utility(pallet_utility::Call::<Runtime>::batch_all {
757
1
					calls: vec![
758
1
						RuntimeCall::CrowdloanRewards(
759
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
760
1
								rewards: vec![(
761
1
									[4u8; 32].into(),
762
1
									Some(AccountId::from(CHARLIE)),
763
1
									150_000_000 * GLMR
764
1
								)]
765
1
							}
766
1
						),
767
1
						RuntimeCall::CrowdloanRewards(
768
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
769
1
								rewards: vec![(
770
1
									[5u8; 32].into(),
771
1
									Some(AccountId::from(DAVE)),
772
1
									150_000_000 * GLMR
773
1
								)]
774
1
							}
775
1
						),
776
1
						RuntimeCall::CrowdloanRewards(
777
1
							pallet_crowdloan_rewards::Call::<Runtime>::complete_initialization {
778
1
								lease_ending_block: end_block
779
1
							}
780
1
						)
781
1
					]
782
1
				})
783
1
				.dispatch(root_origin())
784
1
			);
785
			// 30 percent initial payout
786
1
			assert_eq!(
787
1
				Balances::balance(&AccountId::from(CHARLIE)),
788
1
				45_000_000 * GLMR
789
1
			);
790
			// 30 percent initial payout
791
1
			assert_eq!(Balances::balance(&AccountId::from(DAVE)), 45_000_000 * GLMR);
792
1
			let expected = RuntimeEvent::Utility(pallet_utility::Event::BatchCompleted);
793
1
			assert_eq!(last_event(), expected);
794
			// This one should fail, as we already filled our data
795
1
			assert_ok!(
796
1
				RuntimeCall::Utility(pallet_utility::Call::<Runtime>::batch {
797
1
					calls: vec![RuntimeCall::CrowdloanRewards(
798
1
						pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
799
1
							rewards: vec![(
800
1
								[4u8; 32].into(),
801
1
								Some(AccountId::from(ALICE)),
802
1
								43_200_000
803
1
							)]
804
1
						}
805
1
					)]
806
1
				})
807
1
				.dispatch(root_origin())
808
1
			);
809
1
			let expected_fail = RuntimeEvent::Utility(pallet_utility::Event::BatchInterrupted {
810
1
				index: 0,
811
1
				error: DispatchError::Module(ModuleError {
812
1
					index: 90,
813
1
					error: [8, 0, 0, 0],
814
1
					message: None,
815
1
				}),
816
1
			});
817
1
			assert_eq!(last_event(), expected_fail);
818
			// Claim 1 block.
819
1
			assert_ok!(CrowdloanRewards::claim(origin_of(AccountId::from(CHARLIE))));
820
1
			assert_ok!(CrowdloanRewards::claim(origin_of(AccountId::from(DAVE))));
821

            
822
1
			let vesting_period = 4 * WEEKS as u128;
823
1
			let per_block = (105_000_000 * GLMR) / vesting_period;
824
1

            
825
1
			assert_eq!(
826
1
				CrowdloanRewards::accounts_payable(&AccountId::from(CHARLIE))
827
1
					.unwrap()
828
1
					.claimed_reward,
829
1
				(45_000_000 * GLMR) + per_block
830
1
			);
831
1
			assert_eq!(
832
1
				CrowdloanRewards::accounts_payable(&AccountId::from(DAVE))
833
1
					.unwrap()
834
1
					.claimed_reward,
835
1
				(45_000_000 * GLMR) + per_block
836
1
			);
837
			// The total claimed reward should be equal to the account balance at this point.
838
1
			assert_eq!(
839
1
				Balances::balance(&AccountId::from(CHARLIE)),
840
1
				(45_000_000 * GLMR) + per_block
841
1
			);
842
1
			assert_eq!(
843
1
				Balances::balance(&AccountId::from(DAVE)),
844
1
				(45_000_000 * GLMR) + per_block
845
1
			);
846
1
			assert_noop!(
847
1
				CrowdloanRewards::claim(origin_of(AccountId::from(ALICE))),
848
1
				pallet_crowdloan_rewards::Error::<Runtime>::NoAssociatedClaim
849
1
			);
850
1
		});
851
1
}
852

            
853
#[test]
854
1
fn initialize_crowdloan_address_and_change_with_relay_key_sig() {
855
1
	ExtBuilder::default()
856
1
		.with_balances(vec![
857
1
			(AccountId::from(ALICE), 2_000 * GLMR),
858
1
			(AccountId::from(BOB), 1_000 * GLMR),
859
1
		])
860
1
		.with_collators(vec![(AccountId::from(ALICE), 1_000 * GLMR)])
861
1
		.with_mappings(vec![(
862
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
863
1
			AccountId::from(ALICE),
864
1
		)])
865
1
		.with_crowdloan_fund(3_000_000 * GLMR)
866
1
		.build()
867
1
		.execute_with(|| {
868
1
			// set parachain inherent data
869
1
			set_parachain_inherent_data();
870
1
			let init_block = CrowdloanRewards::init_vesting_block();
871
1
			// This matches the previous vesting
872
1
			let end_block = init_block + 4 * WEEKS;
873
1

            
874
1
			let (pair1, _) = sp_core::sr25519::Pair::generate();
875
1
			let (pair2, _) = sp_core::sr25519::Pair::generate();
876
1

            
877
1
			let public1 = pair1.public();
878
1
			let public2 = pair2.public();
879
1

            
880
1
			// signature is new_account || previous_account
881
1
			let mut message = pallet_crowdloan_rewards::WRAPPED_BYTES_PREFIX.to_vec();
882
1
			message.append(&mut b"moonbeam-".to_vec());
883
1
			message.append(&mut AccountId::from(DAVE).encode());
884
1
			message.append(&mut AccountId::from(CHARLIE).encode());
885
1
			message.append(&mut pallet_crowdloan_rewards::WRAPPED_BYTES_POSTFIX.to_vec());
886
1
			let signature1 = pair1.sign(&message);
887
1
			let signature2 = pair2.sign(&message);
888
1

            
889
1
			// Batch calls always succeed. We just need to check the inner event
890
1
			assert_ok!(
891
1
				// two relay accounts pointing at the same reward account
892
1
				RuntimeCall::Utility(pallet_utility::Call::<Runtime>::batch_all {
893
1
					calls: vec![
894
1
						RuntimeCall::CrowdloanRewards(
895
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
896
1
								rewards: vec![(
897
1
									public1.into(),
898
1
									Some(AccountId::from(CHARLIE)),
899
1
									1_500_000 * GLMR
900
1
								)]
901
1
							}
902
1
						),
903
1
						RuntimeCall::CrowdloanRewards(
904
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
905
1
								rewards: vec![(
906
1
									public2.into(),
907
1
									Some(AccountId::from(CHARLIE)),
908
1
									1_500_000 * GLMR
909
1
								)]
910
1
							}
911
1
						),
912
1
						RuntimeCall::CrowdloanRewards(
913
1
							pallet_crowdloan_rewards::Call::<Runtime>::complete_initialization {
914
1
								lease_ending_block: end_block
915
1
							}
916
1
						)
917
1
					]
918
1
				})
919
1
				.dispatch(root_origin())
920
1
			);
921
			// 30 percent initial payout
922
1
			assert_eq!(Balances::balance(&AccountId::from(CHARLIE)), 900_000 * GLMR);
923

            
924
			// this should fail, as we are only providing one signature
925
1
			assert_noop!(
926
1
				CrowdloanRewards::change_association_with_relay_keys(
927
1
					origin_of(AccountId::from(CHARLIE)),
928
1
					AccountId::from(DAVE),
929
1
					AccountId::from(CHARLIE),
930
1
					vec![(public1.into(), signature1.clone().into())]
931
1
				),
932
1
				pallet_crowdloan_rewards::Error::<Runtime>::InsufficientNumberOfValidProofs
933
1
			);
934

            
935
			// this should be valid
936
1
			assert_ok!(CrowdloanRewards::change_association_with_relay_keys(
937
1
				origin_of(AccountId::from(CHARLIE)),
938
1
				AccountId::from(DAVE),
939
1
				AccountId::from(CHARLIE),
940
1
				vec![
941
1
					(public1.into(), signature1.into()),
942
1
					(public2.into(), signature2.into())
943
1
				]
944
1
			));
945

            
946
1
			assert_eq!(
947
1
				CrowdloanRewards::accounts_payable(&AccountId::from(DAVE))
948
1
					.unwrap()
949
1
					.claimed_reward,
950
1
				(900_000 * GLMR)
951
1
			);
952
1
		});
953
1
}
954

            
955
#[test]
956
1
fn claim_via_precompile() {
957
1
	ExtBuilder::default()
958
1
		.with_balances(vec![
959
1
			(AccountId::from(ALICE), 2_000 * GLMR),
960
1
			(AccountId::from(BOB), 1_000 * GLMR),
961
1
		])
962
1
		.with_collators(vec![(AccountId::from(ALICE), 1_000 * GLMR)])
963
1
		.with_mappings(vec![(
964
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
965
1
			AccountId::from(ALICE),
966
1
		)])
967
1
		.with_crowdloan_fund(3_000_000 * GLMR)
968
1
		.build()
969
1
		.execute_with(|| {
970
1
			// set parachain inherent data
971
1
			set_parachain_inherent_data();
972
1
			let init_block = CrowdloanRewards::init_vesting_block();
973
1
			// This matches the previous vesting
974
1
			let end_block = init_block + 4 * WEEKS;
975
1
			// Batch calls always succeed. We just need to check the inner event
976
1
			assert_ok!(
977
1
				RuntimeCall::Utility(pallet_utility::Call::<Runtime>::batch_all {
978
1
					calls: vec![
979
1
						RuntimeCall::CrowdloanRewards(
980
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
981
1
								rewards: vec![(
982
1
									[4u8; 32].into(),
983
1
									Some(AccountId::from(CHARLIE)),
984
1
									1_500_000 * GLMR
985
1
								)]
986
1
							}
987
1
						),
988
1
						RuntimeCall::CrowdloanRewards(
989
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
990
1
								rewards: vec![(
991
1
									[5u8; 32].into(),
992
1
									Some(AccountId::from(DAVE)),
993
1
									1_500_000 * GLMR
994
1
								)]
995
1
							}
996
1
						),
997
1
						RuntimeCall::CrowdloanRewards(
998
1
							pallet_crowdloan_rewards::Call::<Runtime>::complete_initialization {
999
1
								lease_ending_block: end_block
1
							}
1
						)
1
					]
1
				})
1
				.dispatch(root_origin())
1
			);
1
			assert!(CrowdloanRewards::initialized());
			// 30 percent initial payout
1
			assert_eq!(Balances::balance(&AccountId::from(CHARLIE)), 450_000 * GLMR);
			// 30 percent initial payout
1
			assert_eq!(Balances::balance(&AccountId::from(DAVE)), 450_000 * GLMR);
1
			let crowdloan_precompile_address = H160::from_low_u64_be(2049);
1

            
1
			// Alice uses the crowdloan precompile to claim through the EVM
1
			let gas_limit = 100000u64;
1
			let gas_price: U256 = BASE_FEE_GENESIS.into();
1

            
1
			// Construct the call data (selector, amount)
1
			let mut call_data = Vec::<u8>::from([0u8; 4]);
1
			call_data[0..4].copy_from_slice(&Keccak256::digest(b"claim()")[0..4]);
1

            
1
			assert_ok!(RuntimeCall::EVM(pallet_evm::Call::<Runtime>::call {
1
				source: H160::from(CHARLIE),
1
				target: crowdloan_precompile_address,
1
				input: call_data,
1
				value: U256::zero(), // No value sent in EVM
1
				gas_limit,
1
				max_fee_per_gas: gas_price,
1
				max_priority_fee_per_gas: None,
1
				nonce: None, // Use the next nonce
1
				access_list: Vec::new(),
1
			})
1
			.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::root()));
1
			let vesting_period = 4 * WEEKS as u128;
1
			let per_block = (1_050_000 * GLMR) / vesting_period;
1

            
1
			assert_eq!(
1
				CrowdloanRewards::accounts_payable(&AccountId::from(CHARLIE))
1
					.unwrap()
1
					.claimed_reward,
1
				(450_000 * GLMR) + per_block
1
			);
1
		})
1
}
#[test]
1
fn is_contributor_via_precompile() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 200_000 * GLMR),
1
			(AccountId::from(BOB), 100_000 * GLMR),
1
		])
1
		.with_collators(vec![(AccountId::from(ALICE), 100_000 * GLMR)])
1
		.with_mappings(vec![(
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
1
			AccountId::from(ALICE),
1
		)])
1
		.with_crowdloan_fund(3_000_000_000 * GLMR)
1
		.build()
1
		.execute_with(|| {
1
			// set parachain inherent data
1
			set_parachain_inherent_data();
1
			let init_block = CrowdloanRewards::init_vesting_block();
1
			// This matches the previous vesting
1
			let end_block = init_block + 4 * WEEKS;
1
			// Batch calls always succeed. We just need to check the inner event
1
			assert_ok!(
1
				RuntimeCall::Utility(pallet_utility::Call::<Runtime>::batch_all {
1
					calls: vec![
1
						RuntimeCall::CrowdloanRewards(
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
1
								rewards: vec![(
1
									[4u8; 32].into(),
1
									Some(AccountId::from(CHARLIE)),
1
									1_500_000_000 * GLMR
1
								)]
1
							}
1
						),
1
						RuntimeCall::CrowdloanRewards(
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
1
								rewards: vec![(
1
									[5u8; 32].into(),
1
									Some(AccountId::from(DAVE)),
1
									1_500_000_000 * GLMR
1
								)]
1
							}
1
						),
1
						RuntimeCall::CrowdloanRewards(
1
							pallet_crowdloan_rewards::Call::<Runtime>::complete_initialization {
1
								lease_ending_block: end_block
1
							}
1
						)
1
					]
1
				})
1
				.dispatch(root_origin())
1
			);
1
			let crowdloan_precompile_address = H160::from_low_u64_be(2049);
1

            
1
			// Assert precompile reports Bob is not a contributor
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					crowdloan_precompile_address,
1
					CrowdloanRewardsPCall::is_contributor {
1
						contributor: Address(AccountId::from(BOB).into()),
1
					},
1
				)
1
				.expect_cost(1669)
1
				.expect_no_logs()
1
				.execute_returns(false);
1

            
1
			// Assert precompile reports Charlie is a nominator
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					crowdloan_precompile_address,
1
					CrowdloanRewardsPCall::is_contributor {
1
						contributor: Address(AccountId::from(CHARLIE).into()),
1
					},
1
				)
1
				.expect_cost(1669)
1
				.expect_no_logs()
1
				.execute_returns(true);
1
		})
1
}
#[test]
1
fn reward_info_via_precompile() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 200_000 * GLMR),
1
			(AccountId::from(BOB), 100_000 * GLMR),
1
		])
1
		.with_collators(vec![(AccountId::from(ALICE), 100_000 * GLMR)])
1
		.with_mappings(vec![(
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
1
			AccountId::from(ALICE),
1
		)])
1
		.with_crowdloan_fund(3_000_000 * GLMR)
1
		.build()
1
		.execute_with(|| {
1
			// set parachain inherent data
1
			set_parachain_inherent_data();
1
			let init_block = CrowdloanRewards::init_vesting_block();
1
			// This matches the previous vesting
1
			let end_block = init_block + 4 * WEEKS;
1
			// Batch calls always succeed. We just need to check the inner event
1
			assert_ok!(
1
				RuntimeCall::Utility(pallet_utility::Call::<Runtime>::batch_all {
1
					calls: vec![
1
						RuntimeCall::CrowdloanRewards(
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
1
								rewards: vec![(
1
									[4u8; 32].into(),
1
									Some(AccountId::from(CHARLIE)),
1
									1_500_000 * GLMR
1
								)]
1
							}
1
						),
1
						RuntimeCall::CrowdloanRewards(
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
1
								rewards: vec![(
1
									[5u8; 32].into(),
1
									Some(AccountId::from(DAVE)),
1
									1_500_000 * GLMR
1
								)]
1
							}
1
						),
1
						RuntimeCall::CrowdloanRewards(
1
							pallet_crowdloan_rewards::Call::<Runtime>::complete_initialization {
1
								lease_ending_block: end_block
1
							}
1
						)
1
					]
1
				})
1
				.dispatch(root_origin())
1
			);
1
			let crowdloan_precompile_address = H160::from_low_u64_be(2049);
1

            
1
			let expected_total: U256 = (1_500_000 * GLMR).into();
1
			let expected_claimed: U256 = (450_000 * GLMR).into();
1

            
1
			// Assert precompile reports correct Charlie reward info.
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					crowdloan_precompile_address,
1
					CrowdloanRewardsPCall::reward_info {
1
						contributor: Address(AccountId::from(CHARLIE).into()),
1
					},
1
				)
1
				.expect_cost(1669)
1
				.expect_no_logs()
1
				.execute_returns((expected_total, expected_claimed));
1
		})
1
}
#[test]
1
fn update_reward_address_via_precompile() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_collators(vec![(AccountId::from(ALICE), 1_000 * GLMR)])
1
		.with_mappings(vec![(
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
1
			AccountId::from(ALICE),
1
		)])
1
		.with_crowdloan_fund(3_000_000 * GLMR)
1
		.build()
1
		.execute_with(|| {
1
			// set parachain inherent data
1
			set_parachain_inherent_data();
1
			let init_block = CrowdloanRewards::init_vesting_block();
1
			// This matches the previous vesting
1
			let end_block = init_block + 4 * WEEKS;
1
			// Batch calls always succeed. We just need to check the inner event
1
			assert_ok!(
1
				RuntimeCall::Utility(pallet_utility::Call::<Runtime>::batch_all {
1
					calls: vec![
1
						RuntimeCall::CrowdloanRewards(
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
1
								rewards: vec![(
1
									[4u8; 32].into(),
1
									Some(AccountId::from(CHARLIE)),
1
									1_500_000 * GLMR
1
								)]
1
							}
1
						),
1
						RuntimeCall::CrowdloanRewards(
1
							pallet_crowdloan_rewards::Call::<Runtime>::initialize_reward_vec {
1
								rewards: vec![(
1
									[5u8; 32].into(),
1
									Some(AccountId::from(DAVE)),
1
									1_500_000 * GLMR
1
								)]
1
							}
1
						),
1
						RuntimeCall::CrowdloanRewards(
1
							pallet_crowdloan_rewards::Call::<Runtime>::complete_initialization {
1
								lease_ending_block: end_block
1
							}
1
						)
1
					]
1
				})
1
				.dispatch(root_origin())
1
			);
1
			let crowdloan_precompile_address = H160::from_low_u64_be(2049);
1

            
1
			// Charlie uses the crowdloan precompile to update address through the EVM
1
			let gas_limit = 100000u64;
1
			let gas_price: U256 = BASE_FEE_GENESIS.into();
1

            
1
			// Construct the input data to check if Bob is a contributor
1
			let mut call_data = Vec::<u8>::from([0u8; 36]);
1
			call_data[0..4]
1
				.copy_from_slice(&Keccak256::digest(b"update_reward_address(address)")[0..4]);
1
			call_data[16..36].copy_from_slice(&ALICE);
1

            
1
			assert_ok!(RuntimeCall::EVM(pallet_evm::Call::<Runtime>::call {
1
				source: H160::from(CHARLIE),
1
				target: crowdloan_precompile_address,
1
				input: call_data,
1
				value: U256::zero(), // No value sent in EVM
1
				gas_limit,
1
				max_fee_per_gas: gas_price,
1
				max_priority_fee_per_gas: None,
1
				nonce: None, // Use the next nonce
1
				access_list: Vec::new(),
1
			})
1
			.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::root()));
1
			assert!(CrowdloanRewards::accounts_payable(&AccountId::from(CHARLIE)).is_none());
1
			assert_eq!(
1
				CrowdloanRewards::accounts_payable(&AccountId::from(ALICE))
1
					.unwrap()
1
					.claimed_reward,
1
				(450_000 * GLMR)
1
			);
1
		})
1
}
1
fn run_with_system_weight<F>(w: Weight, mut assertions: F)
1
where
1
	F: FnMut() -> (),
1
{
1
	let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
1
		.build_storage()
1
		.unwrap()
1
		.into();
1
	t.execute_with(|| {
1
		System::set_block_consumed_resources(w, 0);
1
		assertions()
1
	});
1
}
#[test]
#[rustfmt::skip]
1
fn length_fee_is_sensible() {
1
	use sp_runtime::testing::TestXt;
1

            
1
	// tests that length fee is sensible for a few hypothetical transactions
1
	ExtBuilder::default().build().execute_with(|| {
1
		let call = frame_system::Call::remark::<Runtime> { remark: vec![] };
1
		let uxt: TestXt<_, ()> = TestXt::new(call, Some((1u64, ())));
1

            
9
		let calc_fee = |len: u32| -> Balance {
9
			moonbeam_runtime::TransactionPayment::query_fee_details(uxt.clone(), len)
9
				.inclusion_fee
9
				.expect("fee should be calculated")
9
				.len_fee
9
		};
		//                  left: cost of length fee, right: size in bytes
		//                             /------------- proportional component: O(N * 1B)
		//                             |           /- exponential component: O(N ** 3)
		//                             |           |
1
		assert_eq!(                    100_000_000_100, calc_fee(1));
1
		assert_eq!(                  1_000_000_100_000, calc_fee(10));
1
		assert_eq!(                 10_000_100_000_000, calc_fee(100));
1
		assert_eq!(                100_100_000_000_000, calc_fee(1_000));
1
		assert_eq!(              1_100_000_000_000_000, calc_fee(10_000)); // inflection point
1
		assert_eq!(            110_000_000_000_000_000, calc_fee(100_000));
1
		assert_eq!(        100_100_000_000_000_000_000, calc_fee(1_000_000)); // 100 GLMR, ~ 1MB
1
		assert_eq!(    100_001_000_000_000_000_000_000, calc_fee(10_000_000));
1
		assert_eq!(100_000_010_000_000_000_000_000_000, calc_fee(100_000_000));
1
	});
1
}
#[test]
1
fn multiplier_can_grow_from_zero() {
1
	use frame_support::traits::Get;
1

            
1
	let minimum_multiplier = moonbeam_runtime::MinimumMultiplier::get();
1
	let target = moonbeam_runtime::TargetBlockFullness::get()
1
		* RuntimeBlockWeights::get()
1
			.get(DispatchClass::Normal)
1
			.max_total
1
			.unwrap();
1
	// if the min is too small, then this will not change, and we are doomed forever.
1
	// the weight is 1/100th bigger than target.
1
	run_with_system_weight(target * 101 / 100, || {
1
		let next = moonbeam_runtime::SlowAdjustingFeeUpdate::<Runtime>::convert(minimum_multiplier);
1
		assert!(
1
			next > minimum_multiplier,
			"{:?} !>= {:?}",
			next,
			minimum_multiplier
		);
1
	})
1
}
#[test]
1
fn ethereum_invalid_transaction() {
1
	ExtBuilder::default().build().execute_with(|| {
1
		// Ensure an extrinsic not containing enough gas limit to store the transaction
1
		// on chain is rejected.
1
		assert_eq!(
1
			Executive::apply_extrinsic(unchecked_eth_tx(INVALID_ETH_TX)),
1
			Err(
1
				sp_runtime::transaction_validity::TransactionValidityError::Invalid(
1
					sp_runtime::transaction_validity::InvalidTransaction::Custom(0u8)
1
				)
1
			)
1
		);
1
	});
1
}
#[test]
1
fn initial_gas_fee_is_correct() {
1
	use fp_evm::FeeCalculator;
1

            
1
	ExtBuilder::default().build().execute_with(|| {
1
		let multiplier = TransactionPayment::next_fee_multiplier();
1
		assert_eq!(multiplier, Multiplier::from(1u128));
1
		assert_eq!(
1
			TransactionPaymentAsGasPrice::min_gas_price(),
1
			(
1
				31_250_000_000u128.into(),
1
				Weight::from_parts(41_742_000u64, 0)
1
			)
1
		);
1
	});
1
}
#[test]
1
fn min_gas_fee_is_correct() {
1
	use fp_evm::FeeCalculator;
1
	use frame_support::traits::Hooks;
1

            
1
	ExtBuilder::default().build().execute_with(|| {
1
		pallet_transaction_payment::NextFeeMultiplier::<Runtime>::put(Multiplier::from(0));
1
		TransactionPayment::on_finalize(System::block_number()); // should trigger min to kick in
1

            
1
		let multiplier = TransactionPayment::next_fee_multiplier();
1
		assert_eq!(multiplier, Multiplier::from(1u128));
1
		assert_eq!(
1
			TransactionPaymentAsGasPrice::min_gas_price(),
1
			(
1
				31_250_000_000u128.into(),
1
				Weight::from_parts(41_742_000u64, 0)
1
			)
1
		);
1
	});
1
}
#[test]
1
fn transfer_ed_0_substrate() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), (1 * GLMR) + (1 * WEI)),
1
			(AccountId::from(BOB), existential_deposit()),
1
		])
1
		.build()
1
		.execute_with(|| {
1
			// Substrate transfer
1
			assert_ok!(Balances::transfer_allow_death(
1
				origin_of(AccountId::from(ALICE)),
1
				AccountId::from(BOB),
1
				1 * GLMR,
1
			));
			// 1 WEI is left in the account
1
			assert_eq!(Balances::free_balance(AccountId::from(ALICE)), 1 * WEI);
1
		});
1
}
#[test]
1
fn transfer_ed_0_evm() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(
1
				AccountId::from(ALICE),
1
				((1 * GLMR) + (21_000 * BASE_FEE_GENESIS)) + (1 * WEI),
1
			),
1
			(AccountId::from(BOB), existential_deposit()),
1
		])
1
		.build()
1
		.execute_with(|| {
1
			set_parachain_inherent_data();
1
			// EVM transfer
1
			assert_ok!(RuntimeCall::EVM(pallet_evm::Call::<Runtime>::call {
1
				source: H160::from(ALICE),
1
				target: H160::from(BOB),
1
				input: Vec::new(),
1
				value: (1 * GLMR).into(),
1
				gas_limit: 21_000u64,
1
				max_fee_per_gas: BASE_FEE_GENESIS.into(),
1
				max_priority_fee_per_gas: Some(BASE_FEE_GENESIS.into()),
1
				nonce: Some(U256::from(0)),
1
				access_list: Vec::new(),
1
			})
1
			.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::root()));
			// 1 WEI is left in the account
1
			assert_eq!(Balances::free_balance(AccountId::from(ALICE)), 1 * WEI,);
1
		});
1
}
#[test]
1
fn refund_ed_0_evm() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(
1
				AccountId::from(ALICE),
1
				((1 * GLMR) + (21_777 * BASE_FEE_GENESIS) + existential_deposit()),
1
			),
1
			(AccountId::from(BOB), existential_deposit()),
1
		])
1
		.build()
1
		.execute_with(|| {
1
			set_parachain_inherent_data();
1
			// EVM transfer that zeroes ALICE
1
			assert_ok!(RuntimeCall::EVM(pallet_evm::Call::<Runtime>::call {
1
				source: H160::from(ALICE),
1
				target: H160::from(BOB),
1
				input: Vec::new(),
1
				value: (1 * GLMR).into(),
1
				gas_limit: 21_777u64,
1
				max_fee_per_gas: BASE_FEE_GENESIS.into(),
1
				max_priority_fee_per_gas: Some(BASE_FEE_GENESIS.into()),
1
				nonce: Some(U256::from(0)),
1
				access_list: Vec::new(),
1
			})
1
			.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::root()));
			// ALICE is refunded
1
			assert_eq!(
1
				Balances::free_balance(AccountId::from(ALICE)),
1
				777 * BASE_FEE_GENESIS + existential_deposit(),
1
			);
1
		});
1
}
#[test]
1
fn author_does_receive_priority_fee() {
1
	ExtBuilder::default()
1
		.with_balances(vec![(
1
			AccountId::from(BOB),
1
			(1 * GLMR) + (21_000 * (500 * GIGAWEI)),
1
		)])
1
		.build()
1
		.execute_with(|| {
1
			// Some block author as seen by pallet-evm.
1
			let author = AccountId::from(<pallet_evm::Pallet<Runtime>>::find_author());
1
			pallet_author_inherent::Author::<Runtime>::put(author);
1
			// Currently the default impl of the evm uses `deposit_into_existing`.
1
			// If we were to use this implementation, and for an author to receive eventual tips,
1
			// the account needs to be somehow initialized, otherwise the deposit would fail.
1
			Balances::make_free_balance_be(&author, 100 * GLMR);
1

            
1
			// EVM transfer.
1
			assert_ok!(RuntimeCall::EVM(pallet_evm::Call::<Runtime>::call {
1
				source: H160::from(BOB),
1
				target: H160::from(ALICE),
1
				input: Vec::new(),
1
				value: (1 * GLMR).into(),
1
				gas_limit: 21_000u64,
1
				max_fee_per_gas: U256::from(300 * GIGAWEI),
1
				max_priority_fee_per_gas: Some(U256::from(200 * GIGAWEI)),
1
				nonce: Some(U256::from(0)),
1
				access_list: Vec::new(),
1
			})
1
			.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::root()));
1
			let priority_fee = 200 * GIGAWEI * 21_000;
1
			// Author free balance increased by priority fee.
1
			assert_eq!(Balances::free_balance(author), 100 * GLMR + priority_fee,);
1
		});
1
}
#[test]
1
fn total_issuance_after_evm_transaction_with_priority_fee() {
1
	use fp_evm::FeeCalculator;
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(
1
				AccountId::from(BOB),
1
				(1 * GLMR) + (21_000 * (200 * GIGAWEI) + existential_deposit()),
1
			),
1
			(
1
				<pallet_treasury::TreasuryAccountId<Runtime> as sp_core::TypedGet>::get(),
1
				existential_deposit(),
1
			),
1
		])
1
		.build()
1
		.execute_with(|| {
1
			let issuance_before = <Runtime as pallet_evm::Config>::Currency::total_issuance();
1
			let author = AccountId::from(<pallet_evm::Pallet<Runtime>>::find_author());
1
			pallet_author_inherent::Author::<Runtime>::put(author);
1
			// EVM transfer.
1
			assert_ok!(RuntimeCall::EVM(pallet_evm::Call::<Runtime>::call {
1
				source: H160::from(BOB),
1
				target: H160::from(ALICE),
1
				input: Vec::new(),
1
				value: (1 * GLMR).into(),
1
				gas_limit: 21_000u64,
1
				max_fee_per_gas: U256::from(125 * GIGAWEI),
1
				max_priority_fee_per_gas: Some(U256::from(100 * GIGAWEI)),
1
				nonce: Some(U256::from(0)),
1
				access_list: Vec::new(),
1
			})
1
			.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::root()));
1
			let issuance_after = <Runtime as pallet_evm::Config>::Currency::total_issuance();
1

            
1
			let base_fee = TransactionPaymentAsGasPrice::min_gas_price().0.as_u128();
1

            
1
			let base_fee: Balance = base_fee * 21_000;
1

            
1
			let treasury_proportion = dynamic_params::runtime_config::FeesTreasuryProportion::get();
1

            
1
			// only base fee is split between being burned and sent to treasury
1
			let treasury_base_fee_part: Balance = treasury_proportion.mul_floor(base_fee);
1
			let burnt_base_fee_part: Balance = base_fee - treasury_base_fee_part;
1

            
1
			assert_eq!(issuance_after, issuance_before - burnt_base_fee_part);
1
			assert_eq!(moonbeam_runtime::Treasury::pot(), treasury_base_fee_part);
1
		});
1
}
#[test]
1
fn total_issuance_after_evm_transaction_without_priority_fee() {
1
	use fp_evm::FeeCalculator;
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(
1
				AccountId::from(BOB),
1
				(1 * GLMR) + (21_000 * BASE_FEE_GENESIS + existential_deposit()),
1
			),
1
			(
1
				<pallet_treasury::TreasuryAccountId<Runtime> as sp_core::TypedGet>::get(),
1
				existential_deposit(),
1
			),
1
		])
1
		.build()
1
		.execute_with(|| {
1
			set_parachain_inherent_data();
1
			let issuance_before = <Runtime as pallet_evm::Config>::Currency::total_issuance();
1
			// EVM transfer.
1
			assert_ok!(RuntimeCall::EVM(pallet_evm::Call::<Runtime>::call {
1
				source: H160::from(BOB),
1
				target: H160::from(ALICE),
1
				input: Vec::new(),
1
				value: (1 * GLMR).into(),
1
				gas_limit: 21_000u64,
1
				max_fee_per_gas: BASE_FEE_GENESIS.into(),
1
				max_priority_fee_per_gas: Some(BASE_FEE_GENESIS.into()),
1
				nonce: Some(U256::from(0)),
1
				access_list: Vec::new(),
1
			})
1
			.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::root()));
1
			let issuance_after = <Runtime as pallet_evm::Config>::Currency::total_issuance();
1

            
1
			let base_fee = TransactionPaymentAsGasPrice::min_gas_price().0.as_u128();
1

            
1
			let base_fee: Balance = base_fee * 21_000;
1

            
1
			let treasury_proportion = dynamic_params::runtime_config::FeesTreasuryProportion::get();
1

            
1
			// only base fee is split between being burned and sent to treasury
1
			let treasury_base_fee_part: Balance = treasury_proportion.mul_floor(base_fee);
1
			let burnt_base_fee_part: Balance = base_fee - treasury_base_fee_part;
1

            
1
			assert_eq!(issuance_after, issuance_before - burnt_base_fee_part);
1
			assert_eq!(moonbeam_runtime::Treasury::pot(), treasury_base_fee_part);
1
		});
1
}
#[test]
1
fn root_can_change_default_xcm_vers() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_xcm_assets(vec![XcmAssetInitialization {
1
			asset_type: AssetType::Xcm(xcm::v3::Location::parent()),
1
			metadata: AssetRegistrarMetadata {
1
				name: b"RelayToken".to_vec(),
1
				symbol: b"Relay".to_vec(),
1
				decimals: 12,
1
				is_frozen: false,
1
			},
1
			balances: vec![(AccountId::from(ALICE), 1_000_000_000_000_000)],
1
			is_sufficient: true,
1
		}])
1
		.build()
1
		.execute_with(|| {
1
			let source_location = AssetType::Xcm(xcm::v3::Location::parent());
1
			let source_id: moonbeam_runtime::AssetId = source_location.clone().into();
1
			let asset = currency_to_asset(CurrencyId::ForeignAsset(source_id), 100_000_000_000_000);
1
			// Default XCM version is not set yet, so xtokens should fail because it does not
1
			// know with which version to send
1
			assert_noop!(
1
				PolkadotXcm::transfer_assets(
1
					origin_of(AccountId::from(ALICE)),
1
					Box::new(VersionedLocation::V4(Location::parent())),
1
					Box::new(VersionedLocation::V4(Location {
1
						parents: 0,
1
						interior: [AccountId32 {
1
							network: None,
1
							id: [1u8; 32],
1
						}]
1
						.into(),
1
					})),
1
					Box::new(VersionedAssets::V4(asset.clone().into())),
1
					0,
1
					WeightLimit::Unlimited
1
				),
1
				pallet_xcm::Error::<Runtime>::SendFailure
1
			);
			// Root sets the defaultXcm
1
			assert_ok!(PolkadotXcm::force_default_xcm_version(
1
				root_origin(),
1
				Some(3)
1
			));
			// Now transferring does not fail
1
			assert_ok!(PolkadotXcm::transfer_assets(
1
				origin_of(AccountId::from(ALICE)),
1
				Box::new(VersionedLocation::V4(Location::parent())),
1
				Box::new(VersionedLocation::V4(Location {
1
					parents: 0,
1
					interior: [AccountId32 {
1
						network: None,
1
						id: [1u8; 32],
1
					}]
1
					.into(),
1
				})),
1
				Box::new(VersionedAssets::V4(asset.clone().into())),
1
				0,
1
				WeightLimit::Unlimited
1
			));
1
		})
1
}
#[test]
1
fn asset_can_be_registered() {
1
	ExtBuilder::default().build().execute_with(|| {
1
		let source_location = AssetType::Xcm(xcm::v3::Location::parent());
1
		let source_id: moonbeam_runtime::AssetId = source_location.clone().into();
1
		let asset_metadata = AssetRegistrarMetadata {
1
			name: b"RelayToken".to_vec(),
1
			symbol: b"Relay".to_vec(),
1
			decimals: 12,
1
			is_frozen: false,
1
		};
1
		assert_ok!(AssetManager::register_foreign_asset(
1
			moonbeam_runtime::RuntimeOrigin::root(),
1
			source_location,
1
			asset_metadata,
1
			1u128,
1
			true
1
		));
1
		assert!(AssetManager::asset_id_type(source_id).is_some());
1
	});
1
}
#[test]
1
fn xcm_asset_erc20_precompiles_supply_and_balance() {
1
	ExtBuilder::default()
1
		.with_xcm_assets(vec![XcmAssetInitialization {
1
			asset_type: AssetType::Xcm(xcm::v3::Location::parent()),
1
			metadata: AssetRegistrarMetadata {
1
				name: b"RelayToken".to_vec(),
1
				symbol: b"Relay".to_vec(),
1
				decimals: 12,
1
				is_frozen: false,
1
			},
1
			balances: vec![(AccountId::from(ALICE), 1_000 * GLMR)],
1
			is_sufficient: true,
1
		}])
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.build()
1
		.execute_with(|| {
1
			// We have the assetId that corresponds to the relay chain registered
1
			let relay_asset_id: moonbeam_runtime::AssetId =
1
				AssetType::Xcm(xcm::v3::Location::parent()).into();
1

            
1
			// Its address is
1
			let asset_precompile_address = Runtime::asset_id_to_account(
1
				FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX,
1
				relay_asset_id,
1
			);
1

            
1
			// Assert the asset has been created with the correct supply
1
			assert_eq!(
1
				moonbeam_runtime::Assets::total_supply(relay_asset_id),
1
				1_000 * GLMR
1
			);
			// Access totalSupply through precompile. Important that the context is correct
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					asset_precompile_address,
1
					ForeignAssetsPCall::total_supply {},
1
				)
1
				.expect_cost(3338)
1
				.expect_no_logs()
1
				.execute_returns(U256::from(1000 * GLMR));
1

            
1
			// Access balanceOf through precompile
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					asset_precompile_address,
1
					ForeignAssetsPCall::balance_of {
1
						who: Address(ALICE.into()),
1
					},
1
				)
1
				.expect_cost(3338)
1
				.expect_no_logs()
1
				.execute_returns(U256::from(1000 * GLMR));
1
		});
1
}
#[test]
1
fn xcm_asset_erc20_precompiles_transfer() {
1
	ExtBuilder::default()
1
		.with_xcm_assets(vec![XcmAssetInitialization {
1
			asset_type: AssetType::Xcm(xcm::v3::Location::parent()),
1
			metadata: AssetRegistrarMetadata {
1
				name: b"RelayToken".to_vec(),
1
				symbol: b"Relay".to_vec(),
1
				decimals: 12,
1
				is_frozen: false,
1
			},
1
			balances: vec![(AccountId::from(ALICE), 1_000 * GLMR)],
1
			is_sufficient: true,
1
		}])
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.build()
1
		.execute_with(|| {
1
			// We have the assetId that corresponds to the relay chain registered
1
			let relay_asset_id: moonbeam_runtime::AssetId =
1
				AssetType::Xcm(xcm::v3::Location::parent()).into();
1

            
1
			// Its address is
1
			let asset_precompile_address = Runtime::asset_id_to_account(
1
				FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX,
1
				relay_asset_id,
1
			);
1

            
1
			// Transfer tokens from Aice to Bob, 400 GLMR.
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					asset_precompile_address,
1
					ForeignAssetsPCall::transfer {
1
						to: Address(BOB.into()),
1
						value: { 400 * GLMR }.into(),
1
					},
1
				)
1
				.expect_cost(24978)
1
				.expect_log(log3(
1
					asset_precompile_address,
1
					SELECTOR_LOG_TRANSFER,
1
					H160::from(ALICE),
1
					H160::from(BOB),
1
					solidity::encode_event_data(U256::from(400 * GLMR)),
1
				))
1
				.execute_returns(true);
1

            
1
			// Make sure BOB has 400 GLMR
1
			Precompiles::new()
1
				.prepare_test(
1
					BOB,
1
					asset_precompile_address,
1
					ForeignAssetsPCall::balance_of {
1
						who: Address(BOB.into()),
1
					},
1
				)
1
				.expect_cost(3338)
1
				.expect_no_logs()
1
				.execute_returns(U256::from(400 * GLMR));
1
		});
1
}
#[test]
1
fn xcm_asset_erc20_precompiles_approve() {
1
	ExtBuilder::default()
1
		.with_xcm_assets(vec![XcmAssetInitialization {
1
			asset_type: AssetType::Xcm(xcm::v3::Location::parent()),
1
			metadata: AssetRegistrarMetadata {
1
				name: b"RelayToken".to_vec(),
1
				symbol: b"Relay".to_vec(),
1
				decimals: 12,
1
				is_frozen: false,
1
			},
1
			balances: vec![(AccountId::from(ALICE), 1_000 * GLMR)],
1
			is_sufficient: true,
1
		}])
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.build()
1
		.execute_with(|| {
1
			// We have the assetId that corresponds to the relay chain registered
1
			let relay_asset_id: moonbeam_runtime::AssetId =
1
				AssetType::Xcm(xcm::v3::Location::parent()).into();
1

            
1
			// Its address is
1
			let asset_precompile_address = Runtime::asset_id_to_account(
1
				FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX,
1
				relay_asset_id,
1
			);
1

            
1
			// Aprove Bob for spending 400 GLMR from Alice
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					asset_precompile_address,
1
					ForeignAssetsPCall::approve {
1
						spender: Address(BOB.into()),
1
						value: { 400 * GLMR }.into(),
1
					},
1
				)
1
				.expect_cost(15650)
1
				.expect_log(log3(
1
					asset_precompile_address,
1
					SELECTOR_LOG_APPROVAL,
1
					H160::from(ALICE),
1
					H160::from(BOB),
1
					solidity::encode_event_data(U256::from(400 * GLMR)),
1
				))
1
				.execute_returns(true);
1

            
1
			// Transfer tokens from Alice to Charlie by using BOB as origin
1
			Precompiles::new()
1
				.prepare_test(
1
					BOB,
1
					asset_precompile_address,
1
					ForeignAssetsPCall::transfer_from {
1
						from: Address(ALICE.into()),
1
						to: Address(CHARLIE.into()),
1
						value: { 400 * GLMR }.into(),
1
					},
1
				)
1
				.expect_cost(30243)
1
				.expect_log(log3(
1
					asset_precompile_address,
1
					SELECTOR_LOG_TRANSFER,
1
					H160::from(ALICE),
1
					H160::from(CHARLIE),
1
					solidity::encode_event_data(U256::from(400 * GLMR)),
1
				))
1
				.execute_returns(true);
1

            
1
			// Make sure CHARLIE has 400 GLMR
1
			Precompiles::new()
1
				.prepare_test(
1
					CHARLIE,
1
					asset_precompile_address,
1
					ForeignAssetsPCall::balance_of {
1
						who: Address(CHARLIE.into()),
1
					},
1
				)
1
				.expect_cost(3338)
1
				.expect_no_logs()
1
				.execute_returns(U256::from(400 * GLMR));
1
		});
1
}
#[test]
1
fn xtokens_precompile_transfer() {
1
	ExtBuilder::default()
1
		.with_xcm_assets(vec![XcmAssetInitialization {
1
			asset_type: AssetType::Xcm(xcm::v3::Location::parent()),
1
			metadata: AssetRegistrarMetadata {
1
				name: b"RelayToken".to_vec(),
1
				symbol: b"Relay".to_vec(),
1
				decimals: 12,
1
				is_frozen: false,
1
			},
1
			balances: vec![(AccountId::from(ALICE), 1_000_000_000_000_000)],
1
			is_sufficient: true,
1
		}])
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_safe_xcm_version(3)
1
		.build()
1
		.execute_with(|| {
1
			let xtokens_precompile_address = H160::from_low_u64_be(2052);
1

            
1
			// We have the assetId that corresponds to the relay chain registered
1
			let relay_asset_id: moonbeam_runtime::AssetId =
1
				AssetType::Xcm(xcm::v3::Location::parent()).into();
1

            
1
			// Its address is
1
			let asset_precompile_address = Runtime::asset_id_to_account(
1
				FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX,
1
				relay_asset_id,
1
			);
1

            
1
			// Alice has 1000 tokens. She should be able to send through precompile
1
			let destination = Location::new(
1
				1,
1
				[Junction::AccountId32 {
1
					network: None,
1
					id: [1u8; 32],
1
				}],
1
			);
1

            
1
			// We use the address of the asset as an identifier of the asset we want to transfer
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					xtokens_precompile_address,
1
					XtokensPCall::transfer {
1
						currency_address: Address(asset_precompile_address.into()),
1
						amount: 500_000_000_000_000u128.into(),
1
						destination,
1
						weight: 4_000_000,
1
					},
1
				)
1
				.expect_cost(25121)
1
				.expect_no_logs()
1
				.execute_returns(())
1
		})
1
}
#[test]
1
fn xtokens_precompile_transfer_multiasset() {
1
	ExtBuilder::default()
1
		.with_xcm_assets(vec![XcmAssetInitialization {
1
			asset_type: AssetType::Xcm(xcm::v3::Location::parent()),
1
			metadata: AssetRegistrarMetadata {
1
				name: b"RelayToken".to_vec(),
1
				symbol: b"Relay".to_vec(),
1
				decimals: 12,
1
				is_frozen: false,
1
			},
1
			balances: vec![(AccountId::from(ALICE), 1_000_000_000_000_000)],
1
			is_sufficient: true,
1
		}])
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_safe_xcm_version(3)
1
		.build()
1
		.execute_with(|| {
1
			let xtokens_precompile_address = H160::from_low_u64_be(2052);
1

            
1
			// Alice has 1000 tokens. She should be able to send through precompile
1
			let destination = Location::new(
1
				1,
1
				[Junction::AccountId32 {
1
					network: None,
1
					id: [1u8; 32],
1
				}],
1
			);
1

            
1
			// This time we transfer it through TransferMultiAsset
1
			// Instead of the address, we encode directly the multilocation referencing the asset
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					xtokens_precompile_address,
1
					XtokensPCall::transfer_multiasset {
1
						// We want to transfer the relay token
1
						asset: Location::parent(),
1
						amount: 500_000_000_000_000u128.into(),
1
						destination,
1
						weight: 4_000_000,
1
					},
1
				)
1
				.expect_cost(25121)
1
				.expect_no_logs()
1
				.execute_returns(());
1
		})
1
}
#[test]
1
fn make_sure_glmr_can_be_transferred_precompile() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_collators(vec![(AccountId::from(ALICE), 1_000 * GLMR)])
1
		.with_mappings(vec![(
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
1
			AccountId::from(ALICE),
1
		)])
1
		.with_safe_xcm_version(3)
1
		.build()
1
		.execute_with(|| {
1
			assert_ok!(PolkadotXcm::transfer_assets(
1
				origin_of(AccountId::from(ALICE)),
1
				Box::new(VersionedLocation::V4(Location::parent())),
1
				Box::new(VersionedLocation::V4(Location {
1
					parents: 0,
1
					interior: [AccountId32 {
1
						network: None,
1
						id: [1u8; 32],
1
					}]
1
					.into(),
1
				})),
1
				Box::new(VersionedAssets::V4(
1
					Asset {
1
						id: AssetId(moonbeam_runtime::xcm_config::SelfReserve::get()),
1
						fun: Fungible(1000)
1
					}
1
					.into()
1
				)),
1
				0,
1
				WeightLimit::Limited(40000.into())
1
			));
1
		});
1
}
#[test]
1
fn make_sure_glmr_can_be_transferred() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_collators(vec![(AccountId::from(ALICE), 1_000 * GLMR)])
1
		.with_mappings(vec![(
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
1
			AccountId::from(ALICE),
1
		)])
1
		.with_safe_xcm_version(3)
1
		.build()
1
		.execute_with(|| {
1
			let dest = Location {
1
				parents: 1,
1
				interior: [AccountId32 {
1
					network: None,
1
					id: [1u8; 32],
1
				}]
1
				.into(),
1
			};
1
			assert_ok!(PolkadotXcm::transfer_assets(
1
				origin_of(AccountId::from(ALICE)),
1
				Box::new(VersionedLocation::V4(Location::parent())),
1
				Box::new(VersionedLocation::V4(dest)),
1
				Box::new(VersionedAssets::V4(
1
					Asset {
1
						id: AssetId(moonbeam_runtime::xcm_config::SelfReserve::get()),
1
						fun: Fungible(100)
1
					}
1
					.into()
1
				)),
1
				0,
1
				WeightLimit::Limited(40000.into())
1
			));
1
		});
1
}
#[test]
1
fn make_sure_polkadot_xcm_cannot_be_called() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_collators(vec![(AccountId::from(ALICE), 1_000 * GLMR)])
1
		.with_mappings(vec![(
1
			NimbusId::from_slice(&ALICE_NIMBUS).unwrap(),
1
			AccountId::from(ALICE),
1
		)])
1
		.build()
1
		.execute_with(|| {
1
			let dest = Location {
1
				parents: 1,
1
				interior: [AccountId32 {
1
					network: None,
1
					id: [1u8; 32],
1
				}]
1
				.into(),
1
			};
1
			let assets: Assets = [Asset {
1
				id: AssetId(moonbeam_runtime::xcm_config::SelfLocation::get()),
1
				fun: Fungible(1000),
1
			}]
1
			.to_vec()
1
			.into();
1
			assert_noop!(
1
				RuntimeCall::PolkadotXcm(pallet_xcm::Call::<Runtime>::reserve_transfer_assets {
1
					dest: Box::new(VersionedLocation::V4(dest.clone())),
1
					beneficiary: Box::new(VersionedLocation::V4(dest)),
1
					assets: Box::new(VersionedAssets::V4(assets)),
1
					fee_asset_item: 0,
1
				})
1
				.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::signed(
1
					AccountId::from(ALICE)
1
				)),
1
				frame_system::Error::<Runtime>::CallFiltered
1
			);
1
		});
1
}
#[test]
1
fn transact_through_signed_precompile_works_v2() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_safe_xcm_version(3)
1
		.build()
1
		.execute_with(|| {
1
			// Destination
1
			let dest = Location::parent();
1

            
1
			let fee_payer_asset = Location::parent();
1

            
1
			let bytes = vec![1u8, 2u8, 3u8];
1

            
1
			let total_weight = 1_000_000_000u64;
1

            
1
			let xcm_transactor_v2_precompile_address = H160::from_low_u64_be(2061);
1

            
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					xcm_transactor_v2_precompile_address,
1
					XcmTransactorV2PCall::transact_through_signed_multilocation {
1
						dest,
1
						fee_asset: fee_payer_asset,
1
						weight: 4_000_000,
1
						call: bytes.into(),
1
						fee_amount: u128::from(total_weight).into(),
1
						overall_weight: total_weight,
1
					},
1
				)
1
				.expect_cost(21896)
1
				.expect_no_logs()
1
				.execute_returns(());
1
		});
1
}
#[test]
1
fn transact_through_signed_cannot_send_to_local_chain() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_safe_xcm_version(3)
1
		.build()
1
		.execute_with(|| {
1
			// Destination
1
			let dest = Location::here();
1

            
1
			let fee_payer_asset = Location::parent();
1

            
1
			let bytes = vec![1u8, 2u8, 3u8];
1

            
1
			let total_weight = 1_000_000_000u64;
1

            
1
			let xcm_transactor_v2_precompile_address = H160::from_low_u64_be(2061);
1

            
1
			Precompiles::new()
1
				.prepare_test(
1
					ALICE,
1
					xcm_transactor_v2_precompile_address,
1
					XcmTransactorV2PCall::transact_through_signed_multilocation {
1
						dest,
1
						fee_asset: fee_payer_asset,
1
						weight: 4_000_000,
1
						call: bytes.into(),
1
						fee_amount: u128::from(total_weight).into(),
1
						overall_weight: total_weight,
1
					},
1
				)
1
				.execute_reverts(|output| {
1
					from_utf8(&output)
1
						.unwrap()
1
						.contains("Dispatched call failed with error:")
1
						&& from_utf8(&output).unwrap().contains("ErrorValidating")
1
				});
1
		});
1
}
#[test]
1
fn transactor_cannot_use_more_than_max_weight() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_xcm_assets(vec![XcmAssetInitialization {
1
			asset_type: AssetType::Xcm(xcm::v3::Location::parent()),
1
			metadata: AssetRegistrarMetadata {
1
				name: b"RelayToken".to_vec(),
1
				symbol: b"Relay".to_vec(),
1
				decimals: 12,
1
				is_frozen: false,
1
			},
1
			balances: vec![(AccountId::from(ALICE), 1_000_000_000_000_000)],
1
			is_sufficient: true,
1
		}])
1
		.build()
1
		.execute_with(|| {
1
			let source_location = AssetType::Xcm(xcm::v3::Location::parent());
1
			let source_id: moonbeam_runtime::AssetId = source_location.clone().into();
1
			assert_ok!(XcmTransactor::register(
1
				root_origin(),
1
				AccountId::from(ALICE),
1
				0,
1
			));
			// Root can set transact info
1
			assert_ok!(XcmTransactor::set_transact_info(
1
				root_origin(),
1
				Box::new(xcm::VersionedLocation::V4(Location::parent())),
1
				// Relay charges 1000 for every instruction, and we have 3, so 3000
1
				3000.into(),
1
				20000.into(),
1
				None
1
			));
			// Root can set transact info
1
			assert_ok!(XcmTransactor::set_fee_per_second(
1
				root_origin(),
1
				Box::new(xcm::VersionedLocation::V4(Location::parent())),
1
				1,
1
			));
1
			assert_noop!(
1
				XcmTransactor::transact_through_derivative(
1
					origin_of(AccountId::from(ALICE)),
1
					moonbeam_runtime::xcm_config::Transactors::Relay,
1
					0,
1
					CurrencyPayment {
1
						currency: Currency::AsMultiLocation(Box::new(xcm::VersionedLocation::V4(
1
							Location::parent()
1
						))),
1
						fee_amount: None
1
					},
1
					vec![],
1
					// 2000 is the max
1
					TransactWeights {
1
						transact_required_weight_at_most: 17001.into(),
1
						overall_weight: None
1
					},
1
					false
1
				),
1
				pallet_xcm_transactor::Error::<Runtime>::MaxWeightTransactReached
1
			);
1
			assert_noop!(
1
				XcmTransactor::transact_through_derivative(
1
					origin_of(AccountId::from(ALICE)),
1
					moonbeam_runtime::xcm_config::Transactors::Relay,
1
					0,
1
					CurrencyPayment {
1
						currency: Currency::AsCurrencyId(CurrencyId::ForeignAsset(source_id)),
1
						fee_amount: None
1
					},
1
					vec![],
1
					// 20000 is the max
1
					TransactWeights {
1
						transact_required_weight_at_most: 17001.into(),
1
						overall_weight: None
1
					},
1
					false
1
				),
1
				pallet_xcm_transactor::Error::<Runtime>::MaxWeightTransactReached
1
			);
1
		})
1
}
#[test]
1
fn call_xtokens_with_fee() {
1
	ExtBuilder::default()
1
		.with_balances(vec![
1
			(AccountId::from(ALICE), 2_000 * GLMR),
1
			(AccountId::from(BOB), 1_000 * GLMR),
1
		])
1
		.with_safe_xcm_version(3)
1
		.with_xcm_assets(vec![XcmAssetInitialization {
1
			asset_type: AssetType::Xcm(xcm::v3::Location::parent()),
1
			metadata: AssetRegistrarMetadata {
1
				name: b"RelayToken".to_vec(),
1
				symbol: b"Relay".to_vec(),
1
				decimals: 12,
1
				is_frozen: false,
1
			},
1
			balances: vec![(AccountId::from(ALICE), 1_000_000_000_000_000)],
1
			is_sufficient: true,
1
		}])
1
		.build()
1
		.execute_with(|| {
1
			let source_location = AssetType::Xcm(xcm::v3::Location::parent());
1
			let dest = Location {
1
				parents: 1,
1
				interior: [AccountId32 {
1
					network: None,
1
					id: [1u8; 32],
1
				}]
1
				.into(),
1
			};
1
			let source_id: moonbeam_runtime::AssetId = source_location.clone().into();
1

            
1
			let before_balance =
1
				moonbeam_runtime::Assets::balance(source_id, &AccountId::from(ALICE));
1
			let (chain_part, beneficiary) =
1
				split_location_into_chain_part_and_beneficiary(dest).unwrap();
1
			let asset = currency_to_asset(CurrencyId::ForeignAsset(source_id), 100_000_000_000_000);
1
			let asset_fee = currency_to_asset(CurrencyId::ForeignAsset(source_id), 100);
1
			// We are able to transfer with fee
1
			assert_ok!(PolkadotXcm::transfer_assets(
1
				origin_of(AccountId::from(ALICE)),
1
				Box::new(VersionedLocation::V4(chain_part)),
1
				Box::new(VersionedLocation::V4(beneficiary)),
1
				Box::new(VersionedAssets::V4(vec![asset_fee, asset].into())),
1
				0,
1
				WeightLimit::Limited(4000000000.into())
1
			));
1
			let after_balance =
1
				moonbeam_runtime::Assets::balance(source_id, &AccountId::from(ALICE));
1
			// At least these much (plus fees) should have been charged
1
			assert_eq!(before_balance - 100_000_000_000_000 - 100, after_balance);
1
		});
1
}
#[test]
1
fn test_xcm_utils_ml_tp_account() {
1
	ExtBuilder::default().build().execute_with(|| {
1
		let xcm_utils_precompile_address = H160::from_low_u64_be(2060);
1
		let expected_address_parent: H160 =
1
			ParentIsPreset::<AccountId>::convert_location(&Location::parent())
1
				.unwrap()
1
				.into();
1

            
1
		Precompiles::new()
1
			.prepare_test(
1
				ALICE,
1
				xcm_utils_precompile_address,
1
				XcmUtilsPCall::multilocation_to_address {
1
					location: Location::parent(),
1
				},
1
			)
1
			.expect_cost(1669)
1
			.expect_no_logs()
1
			.execute_returns(Address(expected_address_parent));
1

            
1
		let parachain_2000_location = Location::new(1, [Parachain(2000)]);
1
		let expected_address_parachain: H160 =
1
			SiblingParachainConvertsVia::<Sibling, AccountId>::convert_location(
1
				&parachain_2000_location,
1
			)
1
			.unwrap()
1
			.into();
1

            
1
		Precompiles::new()
1
			.prepare_test(
1
				ALICE,
1
				xcm_utils_precompile_address,
1
				XcmUtilsPCall::multilocation_to_address {
1
					location: parachain_2000_location,
1
				},
1
			)
1
			.expect_cost(1669)
1
			.expect_no_logs()
1
			.execute_returns(Address(expected_address_parachain));
1

            
1
		let alice_in_parachain_2000_location = Location::new(
1
			1,
1
			[
1
				Parachain(2000),
1
				AccountKey20 {
1
					network: None,
1
					key: ALICE,
1
				},
1
			],
1
		);
1
		let expected_address_alice_in_parachain_2000 =
1
			xcm_builder::HashedDescription::<
1
				AccountId,
1
				xcm_builder::DescribeFamily<xcm_builder::DescribeAllTerminal>,
1
			>::convert_location(&alice_in_parachain_2000_location)
1
			.unwrap()
1
			.into();
1

            
1
		Precompiles::new()
1
			.prepare_test(
1
				ALICE,
1
				xcm_utils_precompile_address,
1
				XcmUtilsPCall::multilocation_to_address {
1
					location: alice_in_parachain_2000_location,
1
				},
1
			)
1
			.expect_cost(1669)
1
			.expect_no_logs()
1
			.execute_returns(Address(expected_address_alice_in_parachain_2000));
1
	});
1
}
#[test]
1
fn test_xcm_utils_weight_message() {
1
	ExtBuilder::default().build().execute_with(|| {
1
		let xcm_utils_precompile_address = H160::from_low_u64_be(2060);
1
		let expected_weight =
1
			XcmWeight::<moonbeam_runtime::Runtime, RuntimeCall>::clear_origin().ref_time();
1

            
1
		let message: Vec<u8> = xcm::VersionedXcm::<()>::V4(Xcm(vec![ClearOrigin])).encode();
1

            
1
		let input = XcmUtilsPCall::weight_message {
1
			message: message.into(),
1
		};
1

            
1
		Precompiles::new()
1
			.prepare_test(ALICE, xcm_utils_precompile_address, input)
1
			.expect_cost(0)
1
			.expect_no_logs()
1
			.execute_returns(expected_weight);
1
	});
1
}
#[test]
1
fn test_xcm_utils_get_units_per_second() {
1
	ExtBuilder::default().build().execute_with(|| {
1
		let xcm_utils_precompile_address = H160::from_low_u64_be(2060);
1
		let location = SelfReserve::get();
1

            
1
		let input = XcmUtilsPCall::get_units_per_second { location };
1

            
1
		let expected_units =
1
			WEIGHT_REF_TIME_PER_SECOND as u128 * moonbeam_runtime::currency::WEIGHT_FEE;
1

            
1
		Precompiles::new()
1
			.prepare_test(ALICE, xcm_utils_precompile_address, input)
1
			.expect_cost(1669)
1
			.expect_no_logs()
1
			.execute_returns(expected_units);
1
	});
1
}
#[test]
1
fn precompile_existence() {
1
	ExtBuilder::default().build().execute_with(|| {
1
		let precompiles = Precompiles::new();
1
		let precompile_addresses: std::collections::BTreeSet<_> = vec![
1
			1, 2, 3, 4, 5, 6, 7, 8, 9, 256, 1024, 1025, 1026, 2048, 2049, 2050, 2051, 2052, 2053,
1
			2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067,
1
			2068, 2069, 2070, 2071, 2072, 2073, 2074,
1
		]
1
		.into_iter()
1
		.map(H160::from_low_u64_be)
1
		.collect();
3001
		for i in 0..3000 {
3000
			let address = H160::from_low_u64_be(i);
3000

            
3000
			if precompile_addresses.contains(&address) {
40
				assert!(
40
					is_precompile_or_fail::<Runtime>(address, 100_000u64).expect("to be ok"),
					"is_precompile({}) should return true",
					i
				);
40
				assert!(
40
					precompiles
40
						.execute(&mut MockHandle::new(
40
							address,
40
							Context {
40
								address,
40
								caller: H160::zero(),
40
								apparent_value: U256::zero()
40
							}
40
						),)
40
						.is_some(),
					"execute({},..) should return Some(_)",
					i
				);
			} else {
2960
				assert!(
2960
					!is_precompile_or_fail::<Runtime>(address, 100_000u64).expect("to be ok"),
					"is_precompile({}) should return false",
					i
				);
2960
				assert!(
2960
					precompiles
2960
						.execute(&mut MockHandle::new(
2960
							address,
2960
							Context {
2960
								address,
2960
								caller: H160::zero(),
2960
								apparent_value: U256::zero()
2960
							}
2960
						),)
2960
						.is_none(),
					"execute({},..) should return None",
					i
				);
			}
		}
1
	});
1
}
#[test]
1
fn removed_precompiles() {
1
	ExtBuilder::default().build().execute_with(|| {
1
		let precompiles = Precompiles::new();
1
		let removed_precompiles = [1025, 2051, 2062, 2063];
3000
		for i in 1..3000 {
2999
			let address = H160::from_low_u64_be(i);
2999

            
2999
			if !is_precompile_or_fail::<Runtime>(address, 100_000u64).expect("to be ok") {
2959
				continue;
40
			}
40

            
40
			if !removed_precompiles.contains(&i) {
36
				assert!(
36
					match precompiles.is_active_precompile(address, 100_000u64) {
36
						IsPrecompileResult::Answer { is_precompile, .. } => is_precompile,
						_ => false,
					},
					"{i} should be an active precompile"
				);
36
				continue;
4
			}
4

            
4
			assert!(
4
				!match precompiles.is_active_precompile(address, 100_000u64) {
4
					IsPrecompileResult::Answer { is_precompile, .. } => is_precompile,
					_ => false,
				},
				"{i} shouldn't be an active precompile"
			);
4
			precompiles
4
				.prepare_test(Alice, address, [])
4
				.execute_reverts(|out| out == b"Removed precompile");
		}
1
	})
1
}
#[test]
1
fn deal_with_fees_handles_tip() {
1
	use frame_support::traits::OnUnbalanced;
1
	use moonbeam_runtime::Treasury;
1
	use moonbeam_runtime_common::deal_with_fees::DealWithSubstrateFeesAndTip;
1

            
1
	ExtBuilder::default().build().execute_with(|| {
1
		set_parachain_inherent_data();
1
		// This test validates the functionality of the `DealWithSubstrateFeesAndTip` trait implementation
1
		// in the Moonbeam runtime. It verifies that:
1
		// - The correct proportion of the fee is sent to the treasury.
1
		// - The remaining fee is burned (removed from the total supply).
1
		// - The entire tip is sent to the block author.
1

            
1
		// The test details:
1
		// 1. Simulate issuing a `fee` of 100 and a `tip` of 1000.
1
		// 2. Confirm the initial total supply is 1,100 (equal to the sum of the issued fee and tip).
1
		// 3. Confirm the treasury's balance is initially 0.
1
		// 4. Execute the `DealWithSubstrateFeesAndTip::on_unbalanceds` function with the `fee` and `tip`.
1
		// 5. Validate that the treasury's balance has increased by 20% of the fee (based on FeesTreasuryProportion).
1
		// 6. Validate that 80% of the fee is burned, and the total supply decreases accordingly.
1
		// 7. Validate that the entire tip (100%) is sent to the block author (collator).
1

            
1
		// Step 1: Issue the fee and tip amounts.
1
		let fee = <pallet_balances::Pallet<Runtime> as frame_support::traits::fungible::Balanced<
1
			AccountId,
1
		>>::issue(100);
1
		let tip = <pallet_balances::Pallet<Runtime> as frame_support::traits::fungible::Balanced<
1
			AccountId,
1
		>>::issue(1000);
1

            
1
		// Step 2: Validate the initial supply and balances.
1
		let total_supply_before = Balances::total_issuance();
1
		let block_author = pallet_author_inherent::Pallet::<Runtime>::get();
1
		let block_author_balance_before = Balances::free_balance(&block_author);
1
		assert_eq!(total_supply_before, 1_100);
1
		assert_eq!(Balances::free_balance(&Treasury::account_id()), 0);
		// Step 3: Execute the fees handling logic.
1
		DealWithSubstrateFeesAndTip::<
1
			Runtime,
1
			dynamic_params::runtime_config::FeesTreasuryProportion,
1
		>::on_unbalanceds(vec![fee, tip].into_iter());
1

            
1
		// Step 4: Compute the split between treasury and burned fees based on FeesTreasuryProportion (20%).
1
		let treasury_proportion = dynamic_params::runtime_config::FeesTreasuryProportion::get();
1

            
1
		let treasury_fee_part: Balance = treasury_proportion.mul_floor(100);
1
		let burnt_fee_part: Balance = 100 - treasury_fee_part;
1

            
1
		// Step 5: Validate the treasury received 20% of the fee.
1
		assert_eq!(
1
			Balances::free_balance(&Treasury::account_id()),
1
			treasury_fee_part,
1
		);
		// Step 6: Verify that 80% of the fee was burned (removed from the total supply).
1
		let total_supply_after = Balances::total_issuance();
1
		assert_eq!(total_supply_before - total_supply_after, burnt_fee_part,);
		// Step 7: Validate that the block author (collator) received 100% of the tip.
1
		let block_author_balance_after = Balances::free_balance(&block_author);
1
		assert_eq!(
1
			block_author_balance_after - block_author_balance_before,
1
			1000,
1
		);
1
	});
1
}
#[test]
1
fn evm_revert_substrate_events() {
1
	ExtBuilder::default()
1
		.with_balances(vec![(AccountId::from(ALICE), 100_000 * GLMR)])
1
		.build()
1
		.execute_with(|| {
1
			let batch_precompile_address = H160::from_low_u64_be(2056);
1

            
1
			// Batch a transfer followed by an invalid call to batch.
1
			// Thus BatchAll will revert the transfer.
1
			assert_ok!(RuntimeCall::EVM(pallet_evm::Call::call {
1
				source: ALICE.into(),
1
				target: batch_precompile_address,
1

            
1
				input: BatchPCall::batch_all {
1
					to: vec![Address(BOB.into()), Address(batch_precompile_address)].into(),
1
					value: vec![U256::from(1 * GLMR), U256::zero()].into(),
1
					call_data: vec![].into(),
1
					gas_limit: vec![].into()
1
				}
1
				.into(),
1
				value: U256::zero(), // No value sent in EVM
1
				gas_limit: 500_000,
1
				max_fee_per_gas: BASE_FEE_GENESIS.into(),
1
				max_priority_fee_per_gas: None,
1
				nonce: Some(U256::from(0)),
1
				access_list: Vec::new(),
1
			})
1
			.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::root()));
1
			let transfer_count = System::events()
1
				.iter()
6
				.filter(|r| match r.event {
					RuntimeEvent::Balances(pallet_balances::Event::Transfer { .. }) => true,
6
					_ => false,
6
				})
1
				.count();
1

            
1
			assert_eq!(transfer_count, 0, "there should be no transfer event");
1
		});
1
}
#[test]
1
fn evm_success_keeps_substrate_events() {
1
	ExtBuilder::default()
1
		.with_balances(vec![(AccountId::from(ALICE), 100_000 * GLMR)])
1
		.build()
1
		.execute_with(|| {
1
			let batch_precompile_address = H160::from_low_u64_be(2056);
1

            
1
			assert_ok!(RuntimeCall::EVM(pallet_evm::Call::call {
1
				source: ALICE.into(),
1
				target: batch_precompile_address,
1
				input: BatchPCall::batch_all {
1
					to: vec![Address(BOB.into())].into(),
1
					value: vec![U256::from(1 * GLMR)].into(),
1
					call_data: vec![].into(),
1
					gas_limit: vec![].into()
1
				}
1
				.into(),
1
				value: U256::zero(), // No value sent in EVM
1
				gas_limit: 500_000,
1
				max_fee_per_gas: BASE_FEE_GENESIS.into(),
1
				max_priority_fee_per_gas: None,
1
				nonce: Some(U256::from(0)),
1
				access_list: Vec::new(),
1
			})
1
			.dispatch(<Runtime as frame_system::Config>::RuntimeOrigin::root()));
1
			let transfer_count = System::events()
1
				.iter()
10
				.filter(|r| match r.event {
1
					RuntimeEvent::Balances(pallet_balances::Event::Transfer { .. }) => true,
9
					_ => false,
10
				})
1
				.count();
1

            
1
			assert_eq!(transfer_count, 1, "there should be 1 transfer event");
1
		});
1
}
#[cfg(test)]
mod treasury_tests {
	use super::*;
	use sp_runtime::traits::Hash;
2
	fn expect_events(events: Vec<RuntimeEvent>) {
2
		let block_events: Vec<RuntimeEvent> =
6
			System::events().into_iter().map(|r| r.event).collect();
2

            
4
		assert!(events.iter().all(|evt| block_events.contains(evt)))
2
	}
4
	fn next_block() {
4
		System::reset_events();
4
		System::set_block_number(System::block_number() + 1u32);
4
		System::on_initialize(System::block_number());
4
		Treasury::on_initialize(System::block_number());
4
	}
	#[test]
1
	fn test_treasury_spend_local_with_council_origin() {
1
		let initial_treasury_balance = 1_000 * GLMR;
1
		ExtBuilder::default()
1
			.with_balances(vec![
1
				(AccountId::from(ALICE), 2_000 * GLMR),
1
				(Treasury::account_id(), initial_treasury_balance),
1
			])
1
			.build()
1
			.execute_with(|| {
1
				let spend_amount = 100u128 * GLMR;
1
				let spend_beneficiary = AccountId::from(BOB);
1

            
1
				next_block();
1

            
1
				// TreasuryCouncilCollective
1
				assert_ok!(TreasuryCouncilCollective::set_members(
1
					root_origin(),
1
					vec![AccountId::from(ALICE)],
1
					Some(AccountId::from(ALICE)),
1
					1
1
				));
1
				next_block();
1

            
1
				// Perform treasury spending
1
				let proposal = RuntimeCall::Treasury(pallet_treasury::Call::spend {
1
					amount: spend_amount,
1
					asset_kind: Box::new(()),
1
					beneficiary: Box::new(AccountId::from(BOB)),
1
					valid_from: Some(5u32),
1
				});
1
				assert_ok!(TreasuryCouncilCollective::propose(
1
					origin_of(AccountId::from(ALICE)),
1
					1,
1
					Box::new(proposal.clone()),
1
					1_000
1
				));
1
				let payout_period =
1
					<<Runtime as pallet_treasury::Config>::PayoutPeriod as Get<u32>>::get();
1
				let expected_events = [
1
					RuntimeEvent::Treasury(pallet_treasury::Event::AssetSpendApproved {
1
						index: 0,
1
						asset_kind: (),
1
						amount: spend_amount,
1
						beneficiary: spend_beneficiary,
1
						valid_from: 5u32,
1
						expire_at: payout_period + 5u32,
1
					}),
1
					RuntimeEvent::TreasuryCouncilCollective(pallet_collective::Event::Executed {
1
						proposal_hash: sp_runtime::traits::BlakeTwo256::hash_of(&proposal),
1
						result: Ok(()),
1
					}),
1
				]
1
				.to_vec();
1
				expect_events(expected_events);
3
				while System::block_number() < 5u32 {
2
					next_block();
2
				}
1
				assert_ok!(Treasury::payout(origin_of(spend_beneficiary), 0));
1
				let expected_events = [
1
					RuntimeEvent::Treasury(pallet_treasury::Event::Paid {
1
						index: 0,
1
						payment_id: (),
1
					}),
1
					RuntimeEvent::Balances(pallet_balances::Event::Transfer {
1
						from: Treasury::account_id(),
1
						to: spend_beneficiary,
1
						amount: spend_amount,
1
					}),
1
				]
1
				.to_vec();
1
				expect_events(expected_events);
1
			});
1
	}
}
#[cfg(test)]
mod fee_tests {
	use super::*;
	use fp_evm::FeeCalculator;
	use frame_support::{
		traits::{ConstU128, OnFinalize},
		weights::{ConstantMultiplier, WeightToFee},
	};
	use moonbeam_runtime::{
		currency, LengthToFee, MinimumMultiplier, RuntimeBlockWeights, SlowAdjustingFeeUpdate,
		TargetBlockFullness, TransactionPaymentAsGasPrice, NORMAL_WEIGHT, WEIGHT_PER_GAS,
	};
	use sp_core::Get;
	use sp_runtime::{BuildStorage, FixedPointNumber, Perbill};
1
	fn run_with_system_weight<F>(w: Weight, mut assertions: F)
1
	where
1
		F: FnMut() -> (),
1
	{
1
		let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
1
			.build_storage()
1
			.unwrap()
1
			.into();
1
		t.execute_with(|| {
1
			System::set_block_consumed_resources(w, 0);
1
			assertions()
1
		});
1
	}
	#[test]
1
	fn test_multiplier_can_grow_from_zero() {
1
		let minimum_multiplier = MinimumMultiplier::get();
1
		let target = TargetBlockFullness::get()
1
			* RuntimeBlockWeights::get()
1
				.get(DispatchClass::Normal)
1
				.max_total
1
				.unwrap();
1
		// if the min is too small, then this will not change, and we are doomed forever.
1
		// the weight is 1/100th bigger than target.
1
		run_with_system_weight(target * 101 / 100, || {
1
			let next = SlowAdjustingFeeUpdate::<Runtime>::convert(minimum_multiplier);
1
			assert!(
1
				next > minimum_multiplier,
				"{:?} !>= {:?}",
				next,
				minimum_multiplier
			);
1
		})
1
	}
	#[test]
1
	fn test_fee_calculation() {
1
		let base_extrinsic = RuntimeBlockWeights::get()
1
			.get(DispatchClass::Normal)
1
			.base_extrinsic;
1
		let multiplier = sp_runtime::FixedU128::from_float(0.999000000000000000);
1
		let extrinsic_len = 100u32;
1
		let extrinsic_weight = 5_000u64;
1
		let tip = 42u128;
1
		type WeightToFeeImpl = ConstantMultiplier<u128, ConstU128<{ currency::WEIGHT_FEE }>>;
1
		type LengthToFeeImpl = LengthToFee;
1

            
1
		// base_fee + (multiplier * extrinsic_weight_fee) + extrinsic_length_fee + tip
1
		let expected_fee =
1
			WeightToFeeImpl::weight_to_fee(&base_extrinsic)
1
				+ multiplier.saturating_mul_int(WeightToFeeImpl::weight_to_fee(
1
					&Weight::from_parts(extrinsic_weight, 1),
1
				)) + LengthToFeeImpl::weight_to_fee(&Weight::from_parts(extrinsic_len as u64, 1))
1
				+ tip;
1

            
1
		let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
1
			.build_storage()
1
			.unwrap()
1
			.into();
1
		t.execute_with(|| {
1
			pallet_transaction_payment::NextFeeMultiplier::<Runtime>::set(multiplier);
1
			let actual_fee = TransactionPayment::compute_fee(
1
				extrinsic_len,
1
				&frame_support::dispatch::DispatchInfo {
1
					class: DispatchClass::Normal,
1
					pays_fee: frame_support::dispatch::Pays::Yes,
1
					weight: Weight::from_parts(extrinsic_weight, 1),
1
				},
1
				tip,
1
			);
1

            
1
			assert_eq!(
				expected_fee,
				actual_fee,
				"The actual fee did not match the expected fee, diff {}",
				actual_fee - expected_fee
			);
1
		});
1
	}
	#[test]
1
	fn test_min_gas_price_is_deterministic() {
1
		let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
1
			.build_storage()
1
			.unwrap()
1
			.into();
1
		t.execute_with(|| {
1
			let multiplier = sp_runtime::FixedU128::from_u32(1);
1
			pallet_transaction_payment::NextFeeMultiplier::<Runtime>::set(multiplier);
1
			let actual = TransactionPaymentAsGasPrice::min_gas_price().0;
1
			let expected: U256 = multiplier
1
				.saturating_mul_int(currency::WEIGHT_FEE.saturating_mul(WEIGHT_PER_GAS as u128))
1
				.into();
1

            
1
			assert_eq!(expected, actual);
1
		});
1
	}
	#[test]
1
	fn test_min_gas_price_has_no_precision_loss_from_saturating_mul_int() {
1
		let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
1
			.build_storage()
1
			.unwrap()
1
			.into();
1
		t.execute_with(|| {
1
			let multiplier_1 = sp_runtime::FixedU128::from_float(0.999593900000000000);
1
			let multiplier_2 = sp_runtime::FixedU128::from_float(0.999593200000000000);
1

            
1
			pallet_transaction_payment::NextFeeMultiplier::<Runtime>::set(multiplier_1);
1
			let a = TransactionPaymentAsGasPrice::min_gas_price();
1
			pallet_transaction_payment::NextFeeMultiplier::<Runtime>::set(multiplier_2);
1
			let b = TransactionPaymentAsGasPrice::min_gas_price();
1

            
1
			assert_ne!(
				a, b,
				"both gas prices were equal, unexpected precision loss incurred"
			);
1
		});
1
	}
	#[test]
1
	fn test_fee_scenarios() {
1
		use sp_runtime::FixedU128;
1
		let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
1
			.build_storage()
1
			.unwrap()
1
			.into();
1
		t.execute_with(|| {
1
			let weight_fee_per_gas = (currency::WEIGHT_FEE).saturating_mul(WEIGHT_PER_GAS as u128);
12
			let sim = |start_gas_price: u128, fullness: Perbill, num_blocks: u64| -> U256 {
12
				let start_multiplier =
12
					FixedU128::from_rational(start_gas_price, weight_fee_per_gas);
12
				pallet_transaction_payment::NextFeeMultiplier::<Runtime>::set(start_multiplier);
12

            
12
				let block_weight = NORMAL_WEIGHT * fullness;
60004
				for i in 0..num_blocks {
60004
					System::set_block_number(i as u32);
60004
					System::set_block_consumed_resources(block_weight, 0);
60004
					TransactionPayment::on_finalize(i as u32);
60004
				}
12
				TransactionPaymentAsGasPrice::min_gas_price().0
12
			};
			// The expected values are the ones observed during test execution,
			// they are expected to change when parameters that influence
			// the fee calculation are changed, and should be updated accordingly.
			// If a test fails when nothing specific to fees has changed,
			// it may indicate an unexpected collateral effect and should be investigated
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(0), 1),
1
				U256::from(31_250_000_000u128), // lower bound enforced
1
			);
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(25), 1),
1
				U256::from(31_250_000_000u128),
1
			);
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(50), 1),
1
				U256::from(31_268_755_625u128), // slightly higher than lower bound
1
			);
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(100), 1),
1
				U256::from(31_331_355_625u128), // a bit higher than before
1
			);
			// 1 "real" hour (at 12-second blocks)
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(0), 600),
1
				U256::from(31_250_000_000u128), // lower bound enforced
1
			);
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(25), 600),
1
				U256::from(31_250_000_000u128),
1
			);
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(50), 600),
1
				U256::from(44_791_543_237u128), // a bit higher than lower bound
1
			);
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(100), 600),
1
				U256::from(148_712_903_041u128), // a lot more
1
			);
			// 1 "real" day (at 12-second blocks)
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(0), 14400),
1
				U256::from(31_250_000_000u128), // lower bound enforced
1
			);
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(25), 14400),
1
				U256::from(31_250_000_000u128),
1
			);
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(50), 14400),
1
				U256::from(176_666_465_470_908u128), // significantly higher
1
			);
1
			assert_eq!(
1
				sim(1_000_000_000, Perbill::from_percent(100), 14400),
1
				U256::from(3_125_000_000_000_000u128), // upper bound enforced
1
			);
1
		});
1
	}
}