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

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

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

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

            
17
//! Test utilities
18
use crate as erc20_xcm_bridge;
19

            
20
use frame_support::traits::Everything;
21
use frame_support::{construct_runtime, pallet_prelude::*, parameter_types};
22
use pallet_evm::{
23
	AddressMapping, EnsureAddressTruncated, FrameSystemAccountProvider, SubstrateBlockHashMapping,
24
};
25
use sp_core::{H160, H256, U256};
26
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};
27
use sp_runtime::AccountId32;
28

            
29
pub type Balance = u128;
30

            
31
type Block = frame_system::mocking::MockBlock<Test>;
32

            
33
11
construct_runtime!(
34
3
	pub enum Test
35
3
	{
36
3
		System: frame_system,
37
3
		Balances: pallet_balances,
38
3
		Timestamp: pallet_timestamp,
39
3
		EVM: pallet_evm,
40
3
		Erc20XcmBridge: erc20_xcm_bridge,
41
3
	}
42
12
);
43

            
44
parameter_types! {
45
	pub const BlockHashCount: u32 = 250;
46
}
47

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

            
81
parameter_types! {
82
	pub const ExistentialDeposit: u128 = 0;
83
}
84
impl pallet_balances::Config for Test {
85
	type MaxReserves = ();
86
	type ReserveIdentifier = [u8; 4];
87
	type MaxLocks = ();
88
	type Balance = Balance;
89
	type RuntimeEvent = RuntimeEvent;
90
	type DustRemoval = ();
91
	type ExistentialDeposit = ExistentialDeposit;
92
	type AccountStore = System;
93
	type WeightInfo = ();
94
	type RuntimeHoldReason = ();
95
	type FreezeIdentifier = ();
96
	type MaxFreezes = ();
97
	type RuntimeFreezeReason = ();
98
	type DoneSlashHandler = ();
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
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
113
/// Block storage limit in bytes. Set to 40 KB.
114
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
115

            
116
parameter_types! {
117
	pub BlockGasLimit: U256 = U256::from(u64::MAX);
118
	pub const WeightPerGas: Weight = Weight::from_parts(1, 0);
119
	pub GasLimitPovSizeRatio: u64 = {
120
		let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
121
		block_gas_limit.saturating_div(MAX_POV_SIZE)
122
	};
123
	pub GasLimitStorageGrowthRatio: u64 = {
124
		let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
125
		block_gas_limit.saturating_div(BLOCK_STORAGE_LIMIT)
126
	};
127
}
128

            
129
pub struct HashedAddressMapping;
130

            
131
impl AddressMapping<AccountId32> for HashedAddressMapping {
132
	fn into_account_id(address: H160) -> AccountId32 {
133
		let mut data = [0u8; 32];
134
		data[0..20].copy_from_slice(&address[..]);
135
		AccountId32::from(Into::<[u8; 32]>::into(data))
136
	}
137
}
138

            
139
impl pallet_evm::Config for Test {
140
	type FeeCalculator = ();
141
	type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
142
	type WeightPerGas = WeightPerGas;
143
	type CallOrigin = EnsureAddressTruncated;
144
	type WithdrawOrigin = EnsureAddressTruncated;
145
	type AddressMapping = HashedAddressMapping;
146
	type Currency = Balances;
147
	type RuntimeEvent = RuntimeEvent;
148
	type PrecompilesType = ();
149
	type PrecompilesValue = ();
150
	type Runner = pallet_evm::runner::stack::Runner<Self>;
151
	type ChainId = ();
152
	type BlockGasLimit = BlockGasLimit;
153
	type OnChargeTransaction = ();
154
	type FindAuthor = ();
155
	type BlockHashMapping = SubstrateBlockHashMapping<Self>;
156
	type OnCreate = ();
157
	type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
158
	type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
159
	type Timestamp = Timestamp;
160
	type WeightInfo = pallet_evm::weights::SubstrateWeight<Test>;
161
	type AccountProvider = FrameSystemAccountProvider<Test>;
162
}
163

            
164
parameter_types! {
165
	pub Erc20XcmBridgeTransferGasLimit: u64 = 200_000;
166
}
167
impl crate::Config for Test {
168
	type AccountIdConverter = ();
169
	type Erc20MultilocationPrefix = ();
170
	type Erc20TransferGasLimit = Erc20XcmBridgeTransferGasLimit;
171
	type EvmRunner = pallet_evm::runner::stack::Runner<Self>;
172
}