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::{FrameSystemAccountProvider, 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
	type AccountProvider = FrameSystemAccountProvider<Test>;
143
}
144

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

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

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

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

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

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

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

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

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

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

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

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