1use crate::{Runtime, RuntimeCall, UnifiedAccounts};
22use astar_primitives::precompiles::DispatchFilterValidate;
23use frame_support::traits::ConstU32;
24use frame_support::{parameter_types, traits::Contains};
25use pallet_evm_precompile_assets_erc20::Erc20AssetsPrecompileSet;
26use pallet_evm_precompile_blake2::Blake2F;
27use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
28use pallet_evm_precompile_dapp_staking::DappStakingV3Precompile;
29use pallet_evm_precompile_dispatch::Dispatch;
30use pallet_evm_precompile_dispatch_lockdrop::DispatchLockdrop;
31use pallet_evm_precompile_ed25519::Ed25519Verify;
32use pallet_evm_precompile_modexp::Modexp;
33use pallet_evm_precompile_sha3fips::Sha3FIPS256;
34use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
35use pallet_evm_precompile_sr25519::Sr25519Precompile;
36use pallet_evm_precompile_substrate_ecdsa::SubstrateEcdsaPrecompile;
37use pallet_evm_precompile_unified_accounts::UnifiedAccountsPrecompile;
38use precompile_utils::precompile_set::*;
39use sp_std::fmt::Debug;
40
41pub const ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];
44parameter_types! {
45 pub AssetPrefix: &'static [u8] = ASSET_PRECOMPILE_ADDRESS_PREFIX;
46}
47
48type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, CallableByPrecompile);
51
52pub struct WhitelistedCalls;
54
55impl Contains<RuntimeCall> for WhitelistedCalls {
56 fn contains(t: &RuntimeCall) -> bool {
57 match t {
58 RuntimeCall::Utility(pallet_utility::Call::batch { calls })
59 | RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
60 calls.iter().all(|call| WhitelistedCalls::contains(call))
61 }
62 RuntimeCall::DappStaking(_) => true,
63 RuntimeCall::Assets(pallet_assets::Call::transfer { .. }) => true,
64 RuntimeCall::Democracy(_)
65 | RuntimeCall::Treasury(_)
66 | RuntimeCall::CommunityTreasury(_)
67 | RuntimeCall::Preimage(_) => true,
68 _ => false,
69 }
70 }
71}
72
73pub struct WhitelistedLockdropCalls;
75
76impl Contains<RuntimeCall> for WhitelistedLockdropCalls {
77 fn contains(t: &RuntimeCall) -> bool {
78 match t {
79 RuntimeCall::Utility(pallet_utility::Call::batch { calls })
80 | RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => calls
81 .iter()
82 .all(|call| WhitelistedLockdropCalls::contains(call)),
83 RuntimeCall::DappStaking(pallet_dapp_staking::Call::unbond_and_unstake { .. }) => true,
84 RuntimeCall::DappStaking(pallet_dapp_staking::Call::withdraw_unbonded { .. }) => true,
85 RuntimeCall::Balances(pallet_balances::Call::transfer_all { .. }) => true,
86 RuntimeCall::Balances(pallet_balances::Call::transfer_keep_alive { .. }) => true,
87 RuntimeCall::Balances(pallet_balances::Call::transfer_allow_death { .. }) => true,
88 RuntimeCall::Assets(pallet_assets::Call::transfer { .. }) => true,
89 _ => false,
90 }
91 }
92}
93
94#[precompile_utils::precompile_name_from_address]
96pub type LocalPrecompilesSetAt<R> = (
97 PrecompileAt<AddressU64<1>, ECRecover, EthereumPrecompilesChecks>,
100 PrecompileAt<AddressU64<2>, Sha256, EthereumPrecompilesChecks>,
101 PrecompileAt<AddressU64<3>, Ripemd160, EthereumPrecompilesChecks>,
102 PrecompileAt<AddressU64<4>, Identity, EthereumPrecompilesChecks>,
103 PrecompileAt<AddressU64<5>, Modexp, EthereumPrecompilesChecks>,
104 PrecompileAt<AddressU64<6>, Bn128Add, EthereumPrecompilesChecks>,
105 PrecompileAt<AddressU64<7>, Bn128Mul, EthereumPrecompilesChecks>,
106 PrecompileAt<AddressU64<8>, Bn128Pairing, EthereumPrecompilesChecks>,
107 PrecompileAt<AddressU64<9>, Blake2F, EthereumPrecompilesChecks>,
108 PrecompileAt<
110 AddressU64<1024>,
111 Sha3FIPS256<Runtime, ()>,
112 (CallableByContract, CallableByPrecompile),
113 >,
114 PrecompileAt<
115 AddressU64<1025>,
116 Dispatch<R, DispatchFilterValidate<RuntimeCall, WhitelistedCalls>>,
117 (),
119 >,
120 PrecompileAt<AddressU64<1026>, ECRecoverPublicKey, (CallableByContract, CallableByPrecompile)>,
121 PrecompileAt<AddressU64<1027>, Ed25519Verify, (CallableByContract, CallableByPrecompile)>,
122 PrecompileAt<
124 AddressU64<20481>,
125 DappStakingV3Precompile<R>,
126 (CallableByContract, CallableByPrecompile),
127 >,
128 PrecompileAt<
129 AddressU64<20482>,
130 Sr25519Precompile<R>,
131 (CallableByContract, CallableByPrecompile),
132 >,
133 PrecompileAt<
134 AddressU64<20483>,
135 SubstrateEcdsaPrecompile<R>,
136 (CallableByContract, CallableByPrecompile),
137 >,
138 PrecompileAt<
141 AddressU64<20486>,
142 UnifiedAccountsPrecompile<R, UnifiedAccounts>,
143 (CallableByContract, CallableByPrecompile),
144 >,
145 PrecompileAt<
146 AddressU64<20487>,
147 DispatchLockdrop<
148 R,
149 DispatchFilterValidate<RuntimeCall, WhitelistedLockdropCalls>,
150 ConstU32<8>,
151 >,
152 (),
154 >,
155);
156
157pub type LocalPrecompiles<R> = PrecompileSetBuilder<
158 R,
159 (
160 PrecompilesInRangeInclusive<
162 (AddressU64<1>, AddressU64<20487>),
164 LocalPrecompilesSetAt<R>,
165 >,
166 PrecompileSetStartingWith<AssetPrefix, Erc20AssetsPrecompileSet<R>, CallableByContract>,
168 ),
169>;