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

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

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

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

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

            
285
pub struct DisabledLocalAssets<Runtime>(sp_std::marker::PhantomData<Runtime>);
286

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

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