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
use super::moonbeam_weights;
18
use crate::{
19
	xcm_config::XcmExecutorConfig, AccountId, AssetId, Balances, Erc20XcmBridge, EvmForeignAssets,
20
	OpenTechCommitteeInstance, Runtime, TreasuryCouncilInstance,
21
};
22
use frame_support::parameter_types;
23
use moonkit_xcm_primitives::location_matcher::{
24
	Erc20PalletMatcher, ForeignAssetMatcher, SingleAddressMatcher,
25
};
26
use pallet_evm_precompile_author_mapping::AuthorMappingPrecompile;
27
use pallet_evm_precompile_balances_erc20::{Erc20BalancesPrecompile, Erc20Metadata};
28
use pallet_evm_precompile_batch::BatchPrecompile;
29
use pallet_evm_precompile_blake2::Blake2F;
30
use pallet_evm_precompile_bls12381::{
31
	Bls12381G1Add, Bls12381G1MultiExp, Bls12381G2Add, Bls12381G2MultiExp, Bls12381MapG1,
32
	Bls12381MapG2, Bls12381Pairing,
33
};
34
use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
35
use pallet_evm_precompile_call_permit::CallPermitPrecompile;
36
use pallet_evm_precompile_collective::CollectivePrecompile;
37
use pallet_evm_precompile_conviction_voting::ConvictionVotingPrecompile;
38
use pallet_evm_precompile_crowdloan_rewards::CrowdloanRewardsPrecompile;
39
use pallet_evm_precompile_gmp::GmpPrecompile;
40
use pallet_evm_precompile_identity::IdentityPrecompile;
41
use pallet_evm_precompile_modexp::Modexp;
42
use pallet_evm_precompile_p256verify::P256Verify;
43
use pallet_evm_precompile_parachain_staking::ParachainStakingPrecompile;
44
use pallet_evm_precompile_preimage::PreimagePrecompile;
45
use pallet_evm_precompile_proxy::{OnlyIsProxyAndProxy, ProxyPrecompile};
46
use pallet_evm_precompile_randomness::RandomnessPrecompile;
47
use pallet_evm_precompile_referenda::ReferendaPrecompile;
48
use pallet_evm_precompile_registry::PrecompileRegistry;
49
use pallet_evm_precompile_relay_encoder::RelayEncoderPrecompile;
50
use pallet_evm_precompile_relay_verifier::RelayDataVerifierPrecompile;
51
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
52
use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
53
use pallet_evm_precompile_xcm::PalletXcmPrecompile;
54
use pallet_evm_precompile_xcm_transactor::{
55
	v1::XcmTransactorPrecompileV1, v2::XcmTransactorPrecompileV2, v3::XcmTransactorPrecompileV3,
56
};
57
use pallet_evm_precompile_xcm_utils::XcmUtilsPrecompile;
58
use pallet_evm_precompile_xtokens::XtokensPrecompile;
59
use pallet_precompile_benchmarks::WeightInfo;
60
use precompile_utils::precompile_set::*;
61

            
62
parameter_types! {
63
	pub P256VerifyWeight: frame_support::weights::Weight =
64
		moonbeam_weights::pallet_precompile_benchmarks::WeightInfo::<Runtime>::p256_verify();
65
}
66

            
67
pub struct NativeErc20Metadata;
68

            
69
/// ERC20 metadata for the native token.
70
impl Erc20Metadata for NativeErc20Metadata {
71
	/// Returns the name of the token.
72
	fn name() -> &'static str {
73
		"GLMR token"
74
	}
75

            
76
	/// Returns the symbol of the token.
77
	fn symbol() -> &'static str {
78
		"GLMR"
79
	}
80

            
81
	/// Returns the decimals places of the token.
82
	fn decimals() -> u8 {
83
		18
84
	}
85

            
86
	/// Must return `true` only if it represents the main native currency of
87
	/// the network. It must be the currency used in `pallet_evm`.
88
6
	fn is_native_currency() -> bool {
89
6
		true
90
6
	}
91
}
92

            
93
/// The asset precompile address prefix. Addresses that match against this prefix will be routed
94
/// to Erc20AssetsPrecompileSet being marked as foreign
95
pub const FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];
96
/// The asset precompile address prefix. Addresses that match against this prefix will be routed
97
/// to Erc20AssetsPrecompileSet being marked as local
98
pub const LOCAL_ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8, 255u8, 255u8, 254u8];
99

            
100
/// Const to identify ERC20_BALANCES_PRECOMPILE address
101
pub const ERC20_BALANCES_PRECOMPILE: u64 = 2050;
102

            
103
parameter_types! {
104
	pub ForeignAssetPrefix: &'static [u8] = FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX;
105
	pub LocalAssetPrefix: &'static [u8] = LOCAL_ASSET_PRECOMPILE_ADDRESS_PREFIX;
106
}
107

            
108
type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, CallableByPrecompile);
109

            
110
// Pallet-xcm precompile types.
111
// Type that converts AssetId into Location
112
type AssetIdToLocationManager = (EvmForeignAssets,);
113

            
114
// The pallet-balances address is identified by ERC20_BALANCES_PRECOMPILE const
115
type SingleAddressMatch = SingleAddressMatcher<AccountId, ERC20_BALANCES_PRECOMPILE, Balances>;
116

            
117
// Type that matches an AccountId with a foreign asset address (if any)
118
type ForeignAssetMatch = ForeignAssetMatcher<AccountId, AssetId, Runtime, AssetIdToLocationManager>;
119

            
120
// Erc20XcmBridge pallet is used to match ERC20s
121
type Erc20Match = Erc20PalletMatcher<AccountId, Erc20XcmBridge>;
122

            
123
#[precompile_utils::precompile_name_from_address]
124
type MoonbeamPrecompilesAt<R> = (
125
	// Ethereum precompiles:
126
	// We allow DELEGATECALL to stay compliant with Ethereum behavior.
127
	PrecompileAt<AddressU64<1>, ECRecover, EthereumPrecompilesChecks>,
128
	PrecompileAt<AddressU64<2>, Sha256, EthereumPrecompilesChecks>,
129
	PrecompileAt<AddressU64<3>, Ripemd160, EthereumPrecompilesChecks>,
130
	PrecompileAt<AddressU64<4>, Identity, EthereumPrecompilesChecks>,
131
	PrecompileAt<AddressU64<5>, Modexp, EthereumPrecompilesChecks>,
132
	PrecompileAt<AddressU64<6>, Bn128Add, EthereumPrecompilesChecks>,
133
	PrecompileAt<AddressU64<7>, Bn128Mul, EthereumPrecompilesChecks>,
134
	PrecompileAt<AddressU64<8>, Bn128Pairing, EthereumPrecompilesChecks>,
135
	PrecompileAt<AddressU64<9>, Blake2F, EthereumPrecompilesChecks>,
136
	// 10 is not implemented, Ethereum uses this slot for Point Evaluation
137
	PrecompileAt<AddressU64<11>, Bls12381G1Add, EthereumPrecompilesChecks>,
138
	PrecompileAt<AddressU64<12>, Bls12381G1MultiExp, EthereumPrecompilesChecks>,
139
	PrecompileAt<AddressU64<13>, Bls12381G2Add, EthereumPrecompilesChecks>,
140
	PrecompileAt<AddressU64<14>, Bls12381G2MultiExp, EthereumPrecompilesChecks>,
141
	PrecompileAt<AddressU64<15>, Bls12381Pairing, EthereumPrecompilesChecks>,
142
	PrecompileAt<AddressU64<16>, Bls12381MapG1, EthereumPrecompilesChecks>,
143
	PrecompileAt<AddressU64<17>, Bls12381MapG2, EthereumPrecompilesChecks>,
144
	// (0x100 => 256) https://github.com/ethereum/RIPs/blob/master/RIPS/rip-7212.md
145
	PrecompileAt<AddressU64<256>, P256Verify<P256VerifyWeight>, EthereumPrecompilesChecks>,
146
	// Non-Moonbeam specific nor Ethereum precompiles :
147
	PrecompileAt<AddressU64<1024>, Sha3FIPS256, (CallableByContract, CallableByPrecompile)>,
148
	RemovedPrecompileAt<AddressU64<1025>>, // Dispatch<R>
149
	PrecompileAt<AddressU64<1026>, ECRecoverPublicKey, (CallableByContract, CallableByPrecompile)>,
150
	// Moonbeam specific precompiles:
151
	PrecompileAt<
152
		AddressU64<2048>,
153
		ParachainStakingPrecompile<R>,
154
		(CallableByContract, CallableByPrecompile),
155
	>,
156
	PrecompileAt<
157
		AddressU64<2049>,
158
		CrowdloanRewardsPrecompile<R>,
159
		(CallableByContract, CallableByPrecompile),
160
	>,
161
	PrecompileAt<
162
		AddressU64<ERC20_BALANCES_PRECOMPILE>,
163
		Erc20BalancesPrecompile<R, NativeErc20Metadata>,
164
		(CallableByContract, CallableByPrecompile),
165
	>,
166
	RemovedPrecompileAt<AddressU64<2051>>, // Democracy
167
	PrecompileAt<
168
		AddressU64<2052>,
169
		XtokensPrecompile<R>,
170
		(
171
			SubcallWithMaxNesting<1>,
172
			CallableByContract,
173
			CallableByPrecompile,
174
		),
175
	>,
176
	PrecompileAt<
177
		AddressU64<2053>,
178
		RelayEncoderPrecompile<R>,
179
		(CallableByContract, CallableByPrecompile),
180
	>,
181
	PrecompileAt<
182
		AddressU64<2054>,
183
		XcmTransactorPrecompileV1<R>,
184
		(
185
			SubcallWithMaxNesting<1>,
186
			CallableByContract,
187
			CallableByPrecompile,
188
		),
189
	>,
190
	PrecompileAt<
191
		AddressU64<2055>,
192
		AuthorMappingPrecompile<R>,
193
		(CallableByContract, CallableByPrecompile),
194
	>,
195
	PrecompileAt<
196
		AddressU64<2056>,
197
		BatchPrecompile<R>,
198
		(
199
			SubcallWithMaxNesting<2>,
200
			// Batch is the only precompile allowed to call Batch.
201
			CallableByPrecompile<OnlyFrom<AddressU64<2056>>>,
202
		),
203
	>,
204
	PrecompileAt<
205
		AddressU64<2057>,
206
		RandomnessPrecompile<R>,
207
		(SubcallWithMaxNesting<0>, CallableByContract),
208
	>,
209
	PrecompileAt<
210
		AddressU64<2058>,
211
		CallPermitPrecompile<R>,
212
		(SubcallWithMaxNesting<0>, CallableByContract),
213
	>,
214
	PrecompileAt<
215
		AddressU64<2059>,
216
		ProxyPrecompile<R>,
217
		(
218
			CallableByContract<OnlyIsProxyAndProxy<R>>,
219
			SubcallWithMaxNesting<0>,
220
			// Batch is the only precompile allowed to call Proxy.
221
			CallableByPrecompile<OnlyFrom<AddressU64<2056>>>,
222
		),
223
	>,
224
	PrecompileAt<
225
		AddressU64<2060>,
226
		XcmUtilsPrecompile<R, XcmExecutorConfig>,
227
		CallableByContract<
228
			pallet_evm_precompile_xcm_utils::AllExceptXcmExecute<R, XcmExecutorConfig>,
229
		>,
230
	>,
231
	PrecompileAt<
232
		AddressU64<2061>,
233
		XcmTransactorPrecompileV2<R>,
234
		(
235
			SubcallWithMaxNesting<1>,
236
			CallableByContract,
237
			CallableByPrecompile,
238
		),
239
	>,
240
	RemovedPrecompileAt<AddressU64<2062>>, //CouncilInstance
241
	RemovedPrecompileAt<AddressU64<2063>>, // TechCommitteeInstance
242
	PrecompileAt<
243
		AddressU64<2064>,
244
		CollectivePrecompile<R, TreasuryCouncilInstance>,
245
		(CallableByContract, CallableByPrecompile),
246
	>,
247
	PrecompileAt<
248
		AddressU64<2065>,
249
		ReferendaPrecompile<R, crate::governance::custom_origins::Origin>,
250
		(CallableByContract, CallableByPrecompile),
251
	>,
252
	PrecompileAt<
253
		AddressU64<2066>,
254
		ConvictionVotingPrecompile<R>,
255
		(CallableByContract, CallableByPrecompile),
256
	>,
257
	PrecompileAt<
258
		AddressU64<2067>,
259
		PreimagePrecompile<R>,
260
		(CallableByContract, CallableByPrecompile),
261
	>,
262
	PrecompileAt<
263
		AddressU64<2068>,
264
		CollectivePrecompile<R, OpenTechCommitteeInstance>,
265
		(CallableByContract, CallableByPrecompile),
266
	>,
267
	PrecompileAt<
268
		AddressU64<2069>,
269
		PrecompileRegistry<R>,
270
		(CallableByContract, CallableByPrecompile),
271
	>,
272
	PrecompileAt<AddressU64<2070>, GmpPrecompile<R>, SubcallWithMaxNesting<0>>,
273
	PrecompileAt<
274
		AddressU64<2071>,
275
		XcmTransactorPrecompileV3<R>,
276
		(
277
			SubcallWithMaxNesting<1>,
278
			CallableByContract,
279
			CallableByPrecompile,
280
		),
281
	>,
282
	PrecompileAt<
283
		AddressU64<2072>,
284
		IdentityPrecompile<R, crate::MaxAdditionalFields>,
285
		(CallableByContract, CallableByPrecompile),
286
	>,
287
	PrecompileAt<
288
		AddressU64<2073>,
289
		RelayDataVerifierPrecompile<
290
			R,
291
			moonbeam_weights::pallet_precompile_benchmarks::WeightInfo<Runtime>,
292
		>,
293
		(CallableByContract, CallableByPrecompile),
294
	>,
295
	PrecompileAt<
296
		AddressU64<2074>,
297
		PalletXcmPrecompile<R, (SingleAddressMatch, ForeignAssetMatch, Erc20Match)>,
298
		(
299
			CallableByContract,
300
			CallableByPrecompile,
301
			SubcallWithMaxNesting<1>,
302
		),
303
	>,
304
);
305

            
306
/// The PrecompileSet installed in the Moonbeam runtime.
307
/// We include the nine Istanbul precompiles
308
/// (https://github.com/ethereum/go-ethereum/blob/3c46f557/core/vm/contracts.go#L69)
309
/// as well as a special precompile for dispatching Substrate extrinsics
310
/// The following distribution has been decided for the precompiles
311
/// 0-1023: Ethereum Mainnet Precompiles
312
/// 1024-2047 Precompiles that are not in Ethereum Mainnet but are neither Moonbeam specific
313
/// 2048-4095 Moonbeam specific precompiles
314
pub type MoonbeamPrecompiles<R> = PrecompileSetBuilder<
315
	R,
316
	(
317
		// Skip precompiles if out of range.
318
		PrecompilesInRangeInclusive<(AddressU64<1>, AddressU64<4095>), MoonbeamPrecompilesAt<R>>,
319
	),
320
>;