1
// Copyright 2024 Moonbeam Foundation.
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
use super::*;
18
use crate as pallet_moonbeam_foreign_assets;
19

            
20
use frame_support::traits::Everything;
21
use frame_support::{construct_runtime, pallet_prelude::*, parameter_types};
22
use frame_system::EnsureRoot;
23
use pallet_evm::SubstrateBlockHashMapping;
24
use precompile_utils::testing::MockAccount;
25
use sp_core::{H256, U256};
26
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};
27
use sp_runtime::BuildStorage;
28
use xcm::latest::Location;
29

            
30
pub type Balance = u128;
31

            
32
type AccountId = MockAccount;
33
type Block = frame_system::mocking::MockBlock<Test>;
34

            
35
474
construct_runtime!(
36
	pub enum Test
37
	{
38
		System: frame_system,
39
		Balances: pallet_balances,
40
		Timestamp: pallet_timestamp,
41
		EVM: pallet_evm,
42
		EvmForeignAssets: pallet_moonbeam_foreign_assets,
43
	}
44
1386
);
45

            
46
parameter_types! {
47
	pub const BlockHashCount: u32 = 250;
48
}
49

            
50
impl frame_system::Config for Test {
51
	type BaseCallFilter = Everything;
52
	type DbWeight = ();
53
	type RuntimeOrigin = RuntimeOrigin;
54
	type RuntimeTask = RuntimeTask;
55
	type Nonce = u64;
56
	type Block = Block;
57
	type RuntimeCall = RuntimeCall;
58
	type Hash = H256;
59
	type Hashing = BlakeTwo256;
60
	type AccountId = AccountId;
61
	type Lookup = IdentityLookup<Self::AccountId>;
62
	type RuntimeEvent = RuntimeEvent;
63
	type BlockHashCount = BlockHashCount;
64
	type Version = ();
65
	type PalletInfo = PalletInfo;
66
	type AccountData = pallet_balances::AccountData<Balance>;
67
	type OnNewAccount = ();
68
	type OnKilledAccount = ();
69
	type SystemWeightInfo = ();
70
	type BlockWeights = ();
71
	type BlockLength = ();
72
	type SS58Prefix = ();
73
	type OnSetCode = ();
74
	type MaxConsumers = frame_support::traits::ConstU32<16>;
75
	type SingleBlockMigrations = ();
76
	type MultiBlockMigrator = ();
77
	type PreInherents = ();
78
	type PostInherents = ();
79
	type PostTransactions = ();
80
}
81

            
82
parameter_types! {
83
	pub const ExistentialDeposit: u128 = 0;
84
}
85
impl pallet_balances::Config for Test {
86
	type MaxReserves = ();
87
	type ReserveIdentifier = [u8; 4];
88
	type MaxLocks = ();
89
	type Balance = Balance;
90
	type RuntimeEvent = RuntimeEvent;
91
	type DustRemoval = ();
92
	type ExistentialDeposit = ExistentialDeposit;
93
	type AccountStore = System;
94
	type WeightInfo = ();
95
	type RuntimeHoldReason = ();
96
	type FreezeIdentifier = ();
97
	type MaxFreezes = ();
98
	type RuntimeFreezeReason = ();
99
}
100

            
101
parameter_types! {
102
	pub const MinimumPeriod: u64 = 6000 / 2;
103
}
104

            
105
impl pallet_timestamp::Config for Test {
106
	type Moment = u64;
107
	type OnTimestampSet = ();
108
	type MinimumPeriod = MinimumPeriod;
109
	type WeightInfo = ();
110
}
111

            
112
parameter_types! {
113
	pub BlockGasLimit: U256 = U256::from(u64::MAX);
114
	pub const WeightPerGas: Weight = Weight::from_parts(1, 0);
115
	pub GasLimitPovSizeRatio: u64 = 16;
116
	pub GasLimitStorageGrowthRatio: u64 = 366;
117
}
118

            
119
impl pallet_evm::Config for Test {
120
	type FeeCalculator = ();
121
	type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
122
	type WeightPerGas = WeightPerGas;
123
	type CallOrigin = pallet_evm::EnsureAddressRoot<AccountId>;
124
	type WithdrawOrigin = pallet_evm::EnsureAddressNever<AccountId>;
125
	type AddressMapping = AccountId;
126
	type Currency = Balances;
127
	type RuntimeEvent = RuntimeEvent;
128
	type PrecompilesType = ();
129
	type PrecompilesValue = ();
130
	type Runner = pallet_evm::runner::stack::Runner<Self>;
131
	type ChainId = ();
132
	type BlockGasLimit = BlockGasLimit;
133
	type OnChargeTransaction = ();
134
	type FindAuthor = ();
135
	type BlockHashMapping = SubstrateBlockHashMapping<Self>;
136
	type OnCreate = ();
137
	type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
138
	type SuicideQuickClearLimit = ConstU32<0>;
139
	type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
140
	type Timestamp = Timestamp;
141
	type WeightInfo = pallet_evm::weights::SubstrateWeight<Test>;
142
}
143

            
144
/// Gets parameters of last `ForeignAssetCreatedHook::on_asset_created` hook invocation
145
1
pub fn get_asset_created_hook_invocation<ForeignAsset: Decode>() -> Option<(ForeignAsset, AssetId)>
146
1
{
147
1
	storage::unhashed::get_raw(b"____on_foreign_asset_created")
148
1
		.map(|output| Decode::decode(&mut output.as_slice()).expect("Decoding should work"))
149
1
}
150

            
151
/// Notes down parameters of current `ForeignAssetCreatedHook::on_asset_created` hook invocation
152
5
fn note_on_asset_created_hook_invocation<ForeignAsset: Encode>(
153
5
	foreign_asset: &ForeignAsset,
154
5
	asset_id: &AssetId,
155
5
) {
156
5
	storage::unhashed::put_raw(
157
5
		b"____on_foreign_asset_created",
158
5
		(foreign_asset, asset_id).encode().as_slice(),
159
5
	);
160
5
}
161

            
162
/// Test hook that records the hook invocation with exact params
163
pub struct NoteDownHook<ForeignAsset>(PhantomData<ForeignAsset>);
164

            
165
impl<ForeignAsset: Encode> ForeignAssetCreatedHook<ForeignAsset> for NoteDownHook<ForeignAsset> {
166
5
	fn on_asset_created(foreign_asset: &ForeignAsset, asset_id: &AssetId) {
167
5
		note_on_asset_created_hook_invocation(foreign_asset, asset_id);
168
5
	}
169
}
170

            
171
pub struct AccountIdToH160;
172
impl sp_runtime::traits::Convert<AccountId, H160> for AccountIdToH160 {
173
12
	fn convert(account_id: AccountId) -> H160 {
174
12
		account_id.into()
175
12
	}
176
}
177

            
178
impl crate::Config for Test {
179
	type AccountIdToH160 = AccountIdToH160;
180
	type AssetIdFilter = Everything;
181
	type EvmRunner = pallet_evm::runner::stack::Runner<Self>;
182
	type ForeignAssetCreatorOrigin = EnsureRoot<AccountId>;
183
	type ForeignAssetFreezerOrigin = EnsureRoot<AccountId>;
184
	type ForeignAssetModifierOrigin = EnsureRoot<AccountId>;
185
	type ForeignAssetUnfreezerOrigin = EnsureRoot<AccountId>;
186
	type OnForeignAssetCreated = NoteDownHook<Location>;
187
	type MaxForeignAssets = ConstU32<3>;
188
	type RuntimeEvent = RuntimeEvent;
189
	type WeightInfo = ();
190
	type XcmLocationToH160 = ();
191
}
192

            
193
pub(crate) struct ExtBuilder {
194
	// endowed accounts with balances
195
	balances: Vec<(AccountId, Balance)>,
196
}
197

            
198
impl Default for ExtBuilder {
199
6
	fn default() -> ExtBuilder {
200
6
		ExtBuilder { balances: vec![] }
201
6
	}
202
}
203

            
204
impl ExtBuilder {
205
6
	pub(crate) fn build(self) -> sp_io::TestExternalities {
206
6
		let mut t = frame_system::GenesisConfig::<Test>::default()
207
6
			.build_storage()
208
6
			.expect("Frame system builds valid default genesis config");
209
6

            
210
6
		pallet_balances::GenesisConfig::<Test> {
211
6
			balances: self.balances,
212
6
		}
213
6
		.assimilate_storage(&mut t)
214
6
		.expect("Pallet balances storage can be assimilated");
215
6
		let mut ext = sp_io::TestExternalities::new(t);
216
6
		ext.execute_with(|| System::set_block_number(1));
217
6
		ext
218
6
	}
219
}
220

            
221
2
pub(crate) fn events() -> Vec<super::Event<Test>> {
222
2
	System::events()
223
2
		.into_iter()
224
7
		.map(|r| r.event)
225
7
		.filter_map(|e| {
226
7
			if let RuntimeEvent::EvmForeignAssets(inner) = e {
227
3
				Some(inner)
228
			} else {
229
4
				None
230
			}
231
7
		})
232
2
		.collect::<Vec<_>>()
233
2
}
234

            
235
2
pub fn expect_events(e: Vec<super::Event<Test>>) {
236
2
	assert_eq!(events(), e);
237
2
}