1
// Copyright 2019-2022 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, xcm_config::XcmExecutorConfig, OpenTechCommitteeInstance,
20
	TreasuryCouncilInstance,
21
};
22
use crate::{AccountId, AssetId, Balances, Erc20XcmBridge, EvmForeignAssets, Runtime, H160};
23
use frame_support::parameter_types;
24
use moonkit_xcm_primitives::{
25
	location_matcher::{Erc20PalletMatcher, ForeignAssetMatcher, SingleAddressMatcher},
26
	AccountIdAssetIdConversion,
27
};
28
use pallet_evm_precompile_author_mapping::AuthorMappingPrecompile;
29
use pallet_evm_precompile_balances_erc20::{Erc20BalancesPrecompile, Erc20Metadata};
30
use pallet_evm_precompile_batch::BatchPrecompile;
31
use pallet_evm_precompile_blake2::Blake2F;
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_storage_cleaner::StorageCleanerPrecompile;
52
use pallet_evm_precompile_xcm::PalletXcmPrecompile;
53
use pallet_evm_precompile_xcm_transactor::{
54
	v1::XcmTransactorPrecompileV1, v2::XcmTransactorPrecompileV2, v3::XcmTransactorPrecompileV3,
55
};
56
use pallet_evm_precompile_xcm_utils::{AllExceptXcmExecute, XcmUtilsPrecompile};
57
use pallet_evm_precompile_xtokens::XtokensPrecompile;
58
use pallet_evm_precompileset_assets_erc20::Erc20AssetsPrecompileSet;
59
use pallet_precompile_benchmarks::WeightInfo;
60
use precompile_foreign_asset_migrator::ForeignAssetMigratorPrecompile;
61
use precompile_utils::precompile_set::*;
62
use sp_std::prelude::*;
63

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

            
69
/// ERC20 metadata for the native token.
70
pub struct NativeErc20Metadata;
71

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

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

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

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

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

            
102
/// Const to identify ERC20_BALANCES_PRECOMPILE address
103
pub const ERC20_BALANCES_PRECOMPILE: u64 = 2050;
104

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

            
110
type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, CallableByPrecompile);
111

            
112
// Pallet-xcm precompile types.
113
// Type that converts AssetId into Location
114
type AssetIdToLocationManager = EvmForeignAssets;
115

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

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

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

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

            
290
pub struct DisabledLocalAssets<Runtime>(sp_std::marker::PhantomData<Runtime>);
291

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

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