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

            
60
parameter_types! {
61
	pub P256VerifyWeight: frame_support::weights::Weight =
62
		moonbase_weights::pallet_precompile_benchmarks::WeightInfo::<Runtime>::p256_verify();
63
}
64

            
65
/// ERC20 metadata for the native token.
66
pub struct NativeErc20Metadata;
67

            
68
impl Erc20Metadata for NativeErc20Metadata {
69
	/// Returns the name of the token.
70
	fn name() -> &'static str {
71
		"DEV token"
72
	}
73

            
74
	/// Returns the symbol of the token.
75
	fn symbol() -> &'static str {
76
		"DEV"
77
	}
78

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

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

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

            
98
/// Const to identify ERC20_BALANCES_PRECOMPILE address
99
pub const ERC20_BALANCES_PRECOMPILE: u64 = 2050;
100

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

            
106
type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, CallableByPrecompile);
107

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

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

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

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

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

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