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
	AccountId, AssetId, AssetManager, Balances, Erc20XcmBridge, OpenTechCommitteeInstance, Runtime,
21
	TreasuryCouncilInstance,
22
};
23
use frame_support::parameter_types;
24
use moonbeam_runtime_common::weights as moonriver_weights;
25
use moonkit_xcm_primitives::location_matcher::{
26
	Erc20PalletMatcher, ForeignAssetMatcher, SingleAddressMatcher,
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_xcm::PalletXcmPrecompile;
52
use pallet_evm_precompile_xcm_transactor::{
53
	v1::XcmTransactorPrecompileV1, v2::XcmTransactorPrecompileV2, v3::XcmTransactorPrecompileV3,
54
};
55
use pallet_evm_precompile_xcm_utils::XcmUtilsPrecompile;
56
use pallet_evm_precompile_xtokens::XtokensPrecompile;
57
use pallet_evm_precompileset_assets_erc20::Erc20AssetsPrecompileSet;
58
use pallet_precompile_benchmarks::WeightInfo;
59
use precompile_utils::precompile_set::*;
60
use xcm_primitives::AsAssetType;
61

            
62
parameter_types! {
63
	pub P256VerifyWeight: frame_support::weights::Weight =
64
		moonriver_weights::pallet_precompile_benchmarks::WeightInfo::<Runtime>::p256_verify();
65
}
66

            
67
pub struct NativeErc20Metadata;
68

            
69
/// ERC20 metadata for the native token.
70
impl Erc20Metadata for NativeErc20Metadata {
71
	/// Returns the name of the token.
72
	fn name() -> &'static str {
73
		"MOVR token"
74
	}
75

            
76
	/// Returns the symbol of the token.
77
	fn symbol() -> &'static str {
78
		"MOVR"
79
	}
80

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

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

            
93
/// The asset precompile address prefix. Addresses that match against this prefix will be routed
94
/// to Erc20AssetsPrecompileSet being marked as foreign
95
pub const FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];
96

            
97
/// Const to identify ERC20_BALANCES_PRECOMPILE address
98
pub const ERC20_BALANCES_PRECOMPILE: u64 = 2050;
99

            
100
parameter_types! {
101
	pub ForeignAssetPrefix: &'static [u8] = FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX;
102
}
103

            
104
type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, CallableByPrecompile);
105

            
106
// Pallet-xcm precompile types.
107
// Type that converts AssetId into Location
108
type AssetIdToLocationManager = AsAssetType<AssetId, AssetType, AssetManager>;
109

            
110
// The pallet-balances address is identified by ERC20_BALANCES_PRECOMPILE const
111
type SingleAddressMatch = SingleAddressMatcher<AccountId, ERC20_BALANCES_PRECOMPILE, Balances>;
112

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

            
116
// Erc20XcmBridge pallet is used to match ERC20s
117
type Erc20Match = Erc20PalletMatcher<AccountId, Erc20XcmBridge>;
118

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

            
275
/// The PrecompileSet installed in the Moonriver runtime.
276
/// We include the nine Istanbul precompiles
277
/// (https://github.com/ethereum/go-ethereum/blob/3c46f557/core/vm/contracts.go#L69)
278
/// The following distribution has been decided for the precompiles
279
/// 0-1023: Ethereum Mainnet Precompiles
280
/// 1024-2047 Precompiles that are not in Ethereum Mainnet but are neither Moonbeam specific
281
/// 2048-4095 Moonbeam specific precompiles
282
pub type MoonriverPrecompiles<R> = PrecompileSetBuilder<
283
	R,
284
	(
285
		// Skip precompiles if out of range.
286
		PrecompilesInRangeInclusive<(AddressU64<1>, AddressU64<4095>), MoonriverPrecompilesAt<R>>,
287
		// Prefixed precompile sets (XC20)
288
		PrecompileSetStartingWith<
289
			ForeignAssetPrefix,
290
			Erc20AssetsPrecompileSet<R, ForeignAssetInstance>,
291
			CallableByContract,
292
		>,
293
		// Moonriver never had any local assets (No blacklist needed
294
		// https://moonriver.subscan.io/event?module=localassets&event_id=created
295
		// https://moonriver.subscan.io/event?module=localassets&event_id=forcecreated
296
	),
297
>;