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_foreign_asset_migrator::ForeignAssetMigratorPrecompile;
63
use precompile_utils::precompile_set::*;
64
use sp_std::prelude::*;
65
use xcm_primitives::AsAssetType;
66

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
131
#[precompile_utils::precompile_name_from_address]
132
type MoonbasePrecompilesAt<R> = (
133
	// Ethereum precompiles:
134
	// We allow DELEGATECALL to stay compliant with Ethereum behavior.
135
	PrecompileAt<AddressU64<1>, ECRecover, EthereumPrecompilesChecks>,
136
	PrecompileAt<AddressU64<2>, Sha256, EthereumPrecompilesChecks>,
137
	PrecompileAt<AddressU64<3>, Ripemd160, EthereumPrecompilesChecks>,
138
	PrecompileAt<AddressU64<4>, Identity, EthereumPrecompilesChecks>,
139
	PrecompileAt<AddressU64<5>, Modexp, EthereumPrecompilesChecks>,
140
	PrecompileAt<AddressU64<6>, Bn128Add, EthereumPrecompilesChecks>,
141
	PrecompileAt<AddressU64<7>, Bn128Mul, EthereumPrecompilesChecks>,
142
	PrecompileAt<AddressU64<8>, Bn128Pairing, EthereumPrecompilesChecks>,
143
	PrecompileAt<AddressU64<9>, Blake2F, 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
	RemovedPrecompileAt<AddressU64<1027>>, // Previous: PrecompileAt<AddressU64<1027>, StorageCleanerPrecompile<R>, CallableByPrecompile>
151
	// Moonbeam specific precompiles:
152
	PrecompileAt<
153
		AddressU64<2048>,
154
		ParachainStakingPrecompile<R>,
155
		(CallableByContract, CallableByPrecompile),
156
	>,
157
	PrecompileAt<
158
		AddressU64<2049>,
159
		CrowdloanRewardsPrecompile<R>,
160
		(CallableByContract, CallableByPrecompile),
161
	>,
162
	PrecompileAt<
163
		AddressU64<ERC20_BALANCES_PRECOMPILE>,
164
		Erc20BalancesPrecompile<R, NativeErc20Metadata>,
165
		(CallableByContract, CallableByPrecompile),
166
	>,
167
	RemovedPrecompileAt<AddressU64<2051>>, // DemocracyPrecompile
168
	PrecompileAt<
169
		AddressU64<2052>,
170
		XtokensPrecompile<R>,
171
		(
172
			SubcallWithMaxNesting<1>,
173
			CallableByContract,
174
			CallableByPrecompile,
175
		),
176
	>,
177
	PrecompileAt<
178
		AddressU64<2053>,
179
		RelayEncoderPrecompile<R>,
180
		(CallableByContract, CallableByPrecompile),
181
	>,
182
	PrecompileAt<
183
		AddressU64<2054>,
184
		XcmTransactorPrecompileV1<R>,
185
		(CallableByContract, CallableByPrecompile),
186
	>,
187
	PrecompileAt<
188
		AddressU64<2055>,
189
		AuthorMappingPrecompile<R>,
190
		(CallableByContract, CallableByPrecompile),
191
	>,
192
	PrecompileAt<
193
		AddressU64<2056>,
194
		BatchPrecompile<R>,
195
		(
196
			SubcallWithMaxNesting<2>,
197
			// Batch is the only precompile allowed to call Batch.
198
			CallableByPrecompile<OnlyFrom<AddressU64<2056>>>,
199
		),
200
	>,
201
	PrecompileAt<
202
		AddressU64<2057>,
203
		RandomnessPrecompile<R>,
204
		(SubcallWithMaxNesting<0>, CallableByContract),
205
	>,
206
	PrecompileAt<
207
		AddressU64<2058>,
208
		CallPermitPrecompile<R>,
209
		(SubcallWithMaxNesting<0>, CallableByContract),
210
	>,
211
	PrecompileAt<
212
		AddressU64<2059>,
213
		ProxyPrecompile<R>,
214
		(
215
			CallableByContract<OnlyIsProxyAndProxy<R>>,
216
			SubcallWithMaxNesting<0>,
217
			// Batch is the only precompile allowed to call Proxy.
218
			CallableByPrecompile<OnlyFrom<AddressU64<2056>>>,
219
		),
220
	>,
221
	PrecompileAt<
222
		AddressU64<2060>,
223
		XcmUtilsPrecompile<R, XcmExecutorConfig>,
224
		CallableByContract<AllExceptXcmExecute<R, XcmExecutorConfig>>,
225
	>,
226
	PrecompileAt<
227
		AddressU64<2061>,
228
		XcmTransactorPrecompileV2<R>,
229
		(CallableByContract, CallableByPrecompile),
230
	>,
231
	// CouncilCollective precompile
232
	RemovedPrecompileAt<AddressU64<2062>>,
233
	// TechCommitteeCollective precompile
234
	RemovedPrecompileAt<AddressU64<2063>>,
235
	PrecompileAt<
236
		AddressU64<2064>,
237
		CollectivePrecompile<R, TreasuryCouncilInstance>,
238
		(CallableByContract, CallableByPrecompile),
239
	>,
240
	PrecompileAt<
241
		AddressU64<2065>,
242
		ReferendaPrecompile<R, crate::governance::custom_origins::Origin>,
243
		(CallableByContract, CallableByPrecompile),
244
	>,
245
	PrecompileAt<
246
		AddressU64<2066>,
247
		ConvictionVotingPrecompile<R>,
248
		(CallableByContract, CallableByPrecompile),
249
	>,
250
	PrecompileAt<
251
		AddressU64<2067>,
252
		PreimagePrecompile<R>,
253
		(CallableByContract, CallableByPrecompile),
254
	>,
255
	PrecompileAt<
256
		AddressU64<2068>,
257
		CollectivePrecompile<R, OpenTechCommitteeInstance>,
258
		(CallableByContract, CallableByPrecompile),
259
	>,
260
	PrecompileAt<
261
		AddressU64<2069>,
262
		PrecompileRegistry<R>,
263
		(CallableByContract, CallableByPrecompile),
264
	>,
265
	PrecompileAt<AddressU64<2070>, GmpPrecompile<R>, SubcallWithMaxNesting<0>>,
266
	PrecompileAt<
267
		AddressU64<2071>,
268
		XcmTransactorPrecompileV3<R>,
269
		(CallableByContract, CallableByPrecompile),
270
	>,
271
	PrecompileAt<
272
		AddressU64<2072>,
273
		IdentityPrecompile<R, crate::MaxAdditionalFields>,
274
		(CallableByContract, CallableByPrecompile),
275
	>,
276
	PrecompileAt<
277
		AddressU64<2073>,
278
		RelayDataVerifierPrecompile<
279
			R,
280
			moonbase_weights::pallet_precompile_benchmarks::WeightInfo<Runtime>,
281
		>,
282
		(CallableByContract, CallableByPrecompile),
283
	>,
284
	PrecompileAt<
285
		AddressU64<2074>,
286
		PalletXcmPrecompile<R, (SingleAddressMatch, ForeignAssetMatch, Erc20Match)>,
287
		(
288
			CallableByContract,
289
			CallableByPrecompile,
290
			SubcallWithMaxNesting<1>,
291
		),
292
	>,
293
	PrecompileAt<AddressU64<2075>, ForeignAssetMigratorPrecompile<R>, ()>,
294
);
295

            
296
pub struct DisabledLocalAssets<Runtime>(sp_std::marker::PhantomData<Runtime>);
297

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

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