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
	pub enum Test
35
	{
36
		System: frame_system,
37
		Balances: pallet_balances,
38
		Timestamp: pallet_timestamp,
39
		EVM: pallet_evm,
40
		Erc20XcmBridge: erc20_xcm_bridge,
41
	}
42
23
);
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
}
79

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

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

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

            
110
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
111
/// Block storage limit in bytes. Set to 40 KB.
112
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
113

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

            
127
pub struct HashedAddressMapping;
128

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

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

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