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

            
66
parameter_types! {
67
	pub P256VerifyWeight: frame_support::weights::Weight =
68
		moonbase_weights::pallet_precompile_benchmarks::WeightInfo::<Runtime>::p256_verify();
69
}
70

            
71
/// ERC20 metadata for the native token.
72
pub struct NativeErc20Metadata;
73

            
74
impl Erc20Metadata for NativeErc20Metadata {
75
	/// Returns the name of the token.
76
	fn name() -> &'static str {
77
		"DEV token"
78
	}
79

            
80
	/// Returns the symbol of the token.
81
	fn symbol() -> &'static str {
82
		"DEV"
83
	}
84

            
85
	/// Returns the decimals places of the token.
86
	fn decimals() -> u8 {
87
		18
88
	}
89

            
90
	/// Must return `true` only if it represents the main native currency of
91
	/// the network. It must be the currency used in `pallet_evm`.
92
7
	fn is_native_currency() -> bool {
93
7
		true
94
7
	}
95
}
96

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

            
104
/// Const to identify ERC20_BALANCES_PRECOMPILE address
105
pub const ERC20_BALANCES_PRECOMPILE: u64 = 2050;
106

            
107
parameter_types! {
108
	pub ForeignAssetPrefix: &'static [u8] = FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX;
109
	pub LocalAssetPrefix: &'static [u8] = LOCAL_ASSET_PRECOMPILE_ADDRESS_PREFIX;
110
}
111

            
112
type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, CallableByPrecompile);
113

            
114
// Pallet-xcm precompile types.
115
// Type that converts AssetId into Location
116
type AssetIdToLocationManager = (
117
	AsAssetType<AssetId, AssetType, AssetManager>,
118
	EvmForeignAssets,
119
);
120

            
121
// The pallet-balances address is identified by ERC20_BALANCES_PRECOMPILE const
122
type SingleAddressMatch = SingleAddressMatcher<AccountId, ERC20_BALANCES_PRECOMPILE, Balances>;
123

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

            
127
// Erc20XcmBridge pallet is used to match ERC20s
128
type Erc20Match = Erc20PalletMatcher<AccountId, Erc20XcmBridge>;
129

            
130
#[precompile_utils::precompile_name_from_address]
131
type MoonbasePrecompilesAt<R> = (
132
	// Ethereum precompiles:
133
	// We allow DELEGATECALL to stay compliant with Ethereum behavior.
134
	PrecompileAt<AddressU64<1>, ECRecover, EthereumPrecompilesChecks>,
135
	PrecompileAt<AddressU64<2>, Sha256, EthereumPrecompilesChecks>,
136
	PrecompileAt<AddressU64<3>, Ripemd160, EthereumPrecompilesChecks>,
137
	PrecompileAt<AddressU64<4>, Identity, EthereumPrecompilesChecks>,
138
	PrecompileAt<AddressU64<5>, Modexp, EthereumPrecompilesChecks>,
139
	PrecompileAt<AddressU64<6>, Bn128Add, EthereumPrecompilesChecks>,
140
	PrecompileAt<AddressU64<7>, Bn128Mul, EthereumPrecompilesChecks>,
141
	PrecompileAt<AddressU64<8>, Bn128Pairing, EthereumPrecompilesChecks>,
142
	PrecompileAt<AddressU64<9>, Blake2F, EthereumPrecompilesChecks>,
143
	// (0x100 => 256) https://github.com/ethereum/RIPs/blob/master/RIPS/rip-7212.md
144
	PrecompileAt<AddressU64<256>, P256Verify<P256VerifyWeight>, EthereumPrecompilesChecks>,
145
	// Non-Moonbeam specific nor Ethereum precompiles :
146
	PrecompileAt<AddressU64<1024>, Sha3FIPS256, (CallableByContract, CallableByPrecompile)>,
147
	RemovedPrecompileAt<AddressU64<1025>>, // Dispatch<R>
148
	PrecompileAt<AddressU64<1026>, ECRecoverPublicKey, (CallableByContract, CallableByPrecompile)>,
149
	RemovedPrecompileAt<AddressU64<1027>>, // Previous: PrecompileAt<AddressU64<1027>, StorageCleanerPrecompile<R>, 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>>, // DemocracyPrecompile
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<AllExceptXcmExecute<R, XcmExecutorConfig>>,
228
	>,
229
	PrecompileAt<
230
		AddressU64<2061>,
231
		XcmTransactorPrecompileV2<R>,
232
		(
233
			SubcallWithMaxNesting<1>,
234
			CallableByContract,
235
			CallableByPrecompile,
236
		),
237
	>,
238
	// CouncilCollective precompile
239
	RemovedPrecompileAt<AddressU64<2062>>,
240
	// TechCommitteeCollective precompile
241
	RemovedPrecompileAt<AddressU64<2063>>,
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
			moonbase_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
pub struct DisabledLocalAssets<Runtime>(sp_std::marker::PhantomData<Runtime>);
307

            
308
impl<Runtime> sp_core::Get<Vec<H160>> for DisabledLocalAssets<Runtime>
309
where
310
	Runtime: frame_system::Config,
311
	Runtime::AccountId: Into<H160>,
312
	Runtime: AccountIdAssetIdConversion<Runtime::AccountId, AssetId>,
313
{
314
12669
	fn get() -> Vec<H160> {
315
12669
		vec![
316
12669
			// https://moonbase.subscan.io/extrinsic/5245322-6?event=5245322-22
317
12669
			182085191673801920759598290391359780050u128,
318
12669
			// https://moonbase.subscan.io/extrinsic/3244752-4?event=3244752-9
319
12669
			282223684955665977914983262584256755878u128,
320
12669
			// https://moonbase.subscan.io/extrinsic/3158280-4?event=3158280-9
321
12669
			235962050501460763853961856666389569138u128,
322
12669
			// https://moonbase.subscan.io/block/3045900?tab=event&&event=3045900-4
323
12669
			45350527686064227409532032051821627910u128,
324
12669
			// https://moonbase.subscan.io/extrinsic/3024306-4?event=3024306-9
325
12669
			199439015574556113723291251263369885338u128,
326
12669
			// https://moonbase.subscan.io/extrinsic/2921640-4?event=2921640-9
327
12669
			236426850287284823323011839750645103615u128,
328
12669
			// https://moonbase.subscan.io/extrinsic/2748867-4?event=2748867-9
329
12669
			14626673838203901761839010613793775004u128,
330
12669
			// https://moonbase.subscan.io/extrinsic/2709788-4?event=2709788-9
331
12669
			95328064580428769161981851380106820590u128,
332
12669
			// https://moonbase.subscan.io/extrinsic/2670844-4?event=2670844-9
333
12669
			339028723712074529056817184013808486301u128,
334
12669
			// https://moonbase.subscan.io/extrinsic/2555083-4?event=2555083-9
335
12669
			100481493116602214283160747599845770751u128,
336
12669
			// https://moonbase.subscan.io/extrinsic/2473880-3?event=2473880-8
337
12669
			319515966007349957795820176952936446433u128,
338
12669
			// https://moonbase.subscan.io/extrinsic/2346438-3?event=2346438-6
339
12669
			337110116006454532607322340792629567158u128,
340
12669
			// https://moonbase.subscan.io/extrinsic/2239102-3?event=2239102-6
341
12669
			255225902946708983196362678630947296516u128,
342
12669
			// https://moonbase.subscan.io/extrinsic/2142964-4?event=2142964-12
343
12669
			3356866138193769031598374869367363824u128,
344
12669
			// https://moonbase.subscan.io/extrinsic/1967538-6?event=1967538-28
345
12669
			144992676743556815849525085098140609495u128,
346
12669
		]
347
12669
		.iter()
348
190035
		.map(|id| Runtime::asset_id_to_account(LOCAL_ASSET_PRECOMPILE_ADDRESS_PREFIX, *id).into())
349
12669
		.collect()
350
12669
	}
351
}
352

            
353
/// The PrecompileSet installed in the Moonbase runtime.
354
/// We include the nine Istanbul precompiles
355
/// (https://github.com/ethereum/go-ethereum/blob/3c46f557/core/vm/contracts.go#L69)
356
/// The following distribution has been decided for the precompiles
357
/// 0-1023: Ethereum Mainnet Precompiles
358
/// 1024-2047 Precompiles that are not in Ethereum Mainnet but are neither Moonbeam specific
359
/// 2048-4095 Moonbeam specific precompiles
360
pub type MoonbasePrecompiles<R> = PrecompileSetBuilder<
361
	R,
362
	(
363
		// Skip precompiles if out of range.
364
		PrecompilesInRangeInclusive<(AddressU64<1>, AddressU64<4095>), MoonbasePrecompilesAt<R>>,
365
		// Prefixed precompile sets (XC20)
366
		PrecompileSetStartingWith<
367
			ForeignAssetPrefix,
368
			Erc20AssetsPrecompileSet<R, ForeignAssetInstance>,
369
			(CallableByContract, CallableByPrecompile),
370
		>,
371
		RemovedPrecompilesAt<DisabledLocalAssets<R>>,
372
	),
373
>;