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
		(CallableByContract, CallableByPrecompile),
185
	>,
186
	PrecompileAt<
187
		AddressU64<2055>,
188
		AuthorMappingPrecompile<R>,
189
		(CallableByContract, CallableByPrecompile),
190
	>,
191
	PrecompileAt<
192
		AddressU64<2056>,
193
		BatchPrecompile<R>,
194
		(
195
			SubcallWithMaxNesting<2>,
196
			// Batch is the only precompile allowed to call Batch.
197
			CallableByPrecompile<OnlyFrom<AddressU64<2056>>>,
198
		),
199
	>,
200
	PrecompileAt<
201
		AddressU64<2057>,
202
		RandomnessPrecompile<R>,
203
		(SubcallWithMaxNesting<0>, CallableByContract),
204
	>,
205
	PrecompileAt<
206
		AddressU64<2058>,
207
		CallPermitPrecompile<R>,
208
		(SubcallWithMaxNesting<0>, CallableByContract),
209
	>,
210
	PrecompileAt<
211
		AddressU64<2059>,
212
		ProxyPrecompile<R>,
213
		(
214
			CallableByContract<OnlyIsProxyAndProxy<R>>,
215
			SubcallWithMaxNesting<0>,
216
			// Batch is the only precompile allowed to call Proxy.
217
			CallableByPrecompile<OnlyFrom<AddressU64<2056>>>,
218
		),
219
	>,
220
	PrecompileAt<
221
		AddressU64<2060>,
222
		XcmUtilsPrecompile<R, XcmExecutorConfig>,
223
		CallableByContract<AllExceptXcmExecute<R, XcmExecutorConfig>>,
224
	>,
225
	PrecompileAt<
226
		AddressU64<2061>,
227
		XcmTransactorPrecompileV2<R>,
228
		(CallableByContract, CallableByPrecompile),
229
	>,
230
	// CouncilCollective precompile
231
	RemovedPrecompileAt<AddressU64<2062>>,
232
	// TechCommitteeCollective precompile
233
	RemovedPrecompileAt<AddressU64<2063>>,
234
	PrecompileAt<
235
		AddressU64<2064>,
236
		CollectivePrecompile<R, TreasuryCouncilInstance>,
237
		(CallableByContract, CallableByPrecompile),
238
	>,
239
	PrecompileAt<
240
		AddressU64<2065>,
241
		ReferendaPrecompile<R, crate::governance::custom_origins::Origin>,
242
		(CallableByContract, CallableByPrecompile),
243
	>,
244
	PrecompileAt<
245
		AddressU64<2066>,
246
		ConvictionVotingPrecompile<R>,
247
		(CallableByContract, CallableByPrecompile),
248
	>,
249
	PrecompileAt<
250
		AddressU64<2067>,
251
		PreimagePrecompile<R>,
252
		(CallableByContract, CallableByPrecompile),
253
	>,
254
	PrecompileAt<
255
		AddressU64<2068>,
256
		CollectivePrecompile<R, OpenTechCommitteeInstance>,
257
		(CallableByContract, CallableByPrecompile),
258
	>,
259
	PrecompileAt<
260
		AddressU64<2069>,
261
		PrecompileRegistry<R>,
262
		(CallableByContract, CallableByPrecompile),
263
	>,
264
	PrecompileAt<AddressU64<2070>, GmpPrecompile<R>, SubcallWithMaxNesting<0>>,
265
	PrecompileAt<
266
		AddressU64<2071>,
267
		XcmTransactorPrecompileV3<R>,
268
		(CallableByContract, CallableByPrecompile),
269
	>,
270
	PrecompileAt<
271
		AddressU64<2072>,
272
		IdentityPrecompile<R, crate::MaxAdditionalFields>,
273
		(CallableByContract, CallableByPrecompile),
274
	>,
275
	PrecompileAt<
276
		AddressU64<2073>,
277
		RelayDataVerifierPrecompile<
278
			R,
279
			moonbase_weights::pallet_precompile_benchmarks::WeightInfo<Runtime>,
280
		>,
281
		(CallableByContract, CallableByPrecompile),
282
	>,
283
	PrecompileAt<
284
		AddressU64<2074>,
285
		PalletXcmPrecompile<R, (SingleAddressMatch, ForeignAssetMatch, Erc20Match)>,
286
		(
287
			CallableByContract,
288
			CallableByPrecompile,
289
			SubcallWithMaxNesting<1>,
290
		),
291
	>,
292
);
293

            
294
pub struct DisabledLocalAssets<Runtime>(sp_std::marker::PhantomData<Runtime>);
295

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

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