1use crate::test_ethereum_tx::{
4 AccountInfo, EIP1559UnsignedTransaction, EIP2930UnsignedTransaction, LegacyUnsignedTransaction,
5};
6use domain_runtime_primitives::EthereumAccountId;
7use ethereum::TransactionV2 as Transaction;
8use frame_support::pallet_prelude::DispatchClass;
9use frame_system::pallet_prelude::RuntimeCallFor;
10use hex_literal::hex;
11use pallet_evm::GasWeightMapping;
12use sp_core::{Get, H160, U256};
13use sp_domains::PermissionedActionAllowedBy;
14
15#[derive(Copy, Clone, Debug, Eq, PartialEq)]
17pub enum EvmAccountList {
18 Anyone,
19 NoOne,
20 One,
21 Multiple,
22}
23
24pub fn generate_evm_account_list(
26 account_infos: &[AccountInfo],
27 account_list_type: EvmAccountList,
28) -> PermissionedActionAllowedBy<EthereumAccountId> {
29 let evm_domain_account = hex!("e04cc55ebee1cbce552f250e85c57b70b2e2625b");
32
33 match account_list_type {
34 EvmAccountList::Anyone => PermissionedActionAllowedBy::Anyone,
35 EvmAccountList::NoOne => PermissionedActionAllowedBy::Accounts(Vec::new()),
36 EvmAccountList::One => PermissionedActionAllowedBy::Accounts(vec![
37 EthereumAccountId::from(evm_domain_account),
38 EthereumAccountId::from(account_infos[0].address),
39 ]),
40 EvmAccountList::Multiple => PermissionedActionAllowedBy::Accounts(vec![
41 EthereumAccountId::from(evm_domain_account),
42 EthereumAccountId::from(account_infos[0].address),
43 EthereumAccountId::from(account_infos[1].address),
44 EthereumAccountId::from(account_infos[2].address),
45 ]),
46 }
47}
48
49pub fn generate_evm_domain_call<TestRuntime>(
53 account_info: AccountInfo,
54 use_create: ethereum::TransactionAction,
55 recursion_depth: u8,
56 nonce: U256,
57 gas_price: U256,
58) -> RuntimeCallFor<TestRuntime>
59where
60 TestRuntime: frame_system::Config + pallet_evm::Config + pallet_utility::Config,
61 RuntimeCallFor<TestRuntime>:
62 From<pallet_utility::Call<TestRuntime>> + From<pallet_evm::Call<TestRuntime>>,
63{
64 if recursion_depth > 0 {
65 let inner_call = generate_evm_domain_call::<TestRuntime>(
66 account_info,
67 use_create,
68 recursion_depth - 1,
69 nonce,
70 gas_price,
71 );
72
73 return pallet_utility::Call::<TestRuntime>::batch {
78 calls: vec![inner_call.into()],
79 }
80 .into();
81 }
82
83 let call = match use_create {
84 ethereum::TransactionAction::Create => pallet_evm::Call::<TestRuntime>::create {
87 source: account_info.address,
88 init: vec![0; 100],
89 value: U256::zero(),
90 gas_limit: max_extrinsic_gas::<TestRuntime>(1000),
91 max_fee_per_gas: gas_price,
92 access_list: vec![],
93 max_priority_fee_per_gas: Some(U256::from(1)),
94 nonce: Some(nonce),
95 },
96 ethereum::TransactionAction::Call(contract) => pallet_evm::Call::<TestRuntime>::call {
97 source: account_info.address,
98 target: contract,
99 input: vec![0; 100],
100 value: U256::zero(),
101 gas_limit: max_extrinsic_gas::<TestRuntime>(1000),
102 max_fee_per_gas: gas_price,
103 max_priority_fee_per_gas: Some(U256::from(1)),
104 nonce: Some(nonce),
105 access_list: vec![],
106 },
107 };
108
109 call.into()
110}
111
112pub fn max_extrinsic_gas<TestRuntime: frame_system::Config + pallet_evm::Config>(
113 multiplier: u64,
114) -> u64 {
115 let limits: frame_system::limits::BlockWeights =
116 <TestRuntime as frame_system::Config>::BlockWeights::get();
117 let max_extrinsic = limits.get(DispatchClass::Normal).base_extrinsic * multiplier;
120
121 <TestRuntime as pallet_evm::Config>::GasWeightMapping::weight_to_gas(max_extrinsic)
122}
123
124pub fn generate_legacy_transfer_txn<TestRuntime: frame_system::Config + pallet_evm::Config>(
125 account_info: AccountInfo,
126 nonce: U256,
127 gas_price: U256,
128 to: H160,
129 value: U256,
130) -> Transaction {
131 LegacyUnsignedTransaction {
132 nonce,
133 gas_price,
134 gas_limit: U256::from(21000),
135 action: ethereum::TransactionAction::Call(to),
136 value,
137 input: vec![],
138 }
139 .sign(&account_info.private_key)
140}
141
142pub fn generate_legacy_tx<TestRuntime: frame_system::Config + pallet_evm::Config>(
143 account_info: AccountInfo,
144 nonce: U256,
145 action: ethereum::TransactionAction,
146 input: Vec<u8>,
147 gas_price: U256,
148) -> Transaction {
149 LegacyUnsignedTransaction {
150 nonce,
151 gas_price,
152 gas_limit: U256::from(max_extrinsic_gas::<TestRuntime>(1000)),
153 action,
154 value: U256::zero(),
155 input,
156 }
157 .sign(&account_info.private_key)
158}
159
160pub fn generate_eip2930_transfer_txn<TestRuntime: frame_system::Config + pallet_evm::Config>(
161 account_info: AccountInfo,
162 nonce: U256,
163 gas_price: U256,
164 to: H160,
165 value: U256,
166) -> Transaction {
167 EIP2930UnsignedTransaction {
168 nonce,
169 gas_price,
170 gas_limit: U256::from(21000),
171 action: ethereum::TransactionAction::Call(to),
172 value,
173 input: vec![],
174 }
175 .sign(&account_info.private_key, None)
176}
177
178pub fn generate_eip2930_tx<TestRuntime: frame_system::Config + pallet_evm::Config>(
179 account_info: AccountInfo,
180 nonce: U256,
181 action: ethereum::TransactionAction,
182 input: Vec<u8>,
183 gas_price: U256,
184) -> Transaction {
185 EIP2930UnsignedTransaction {
186 nonce,
187 gas_price,
188 gas_limit: U256::from(max_extrinsic_gas::<TestRuntime>(100)),
189 action,
190 value: U256::one(),
191 input,
192 }
193 .sign(&account_info.private_key, None)
194}
195
196pub fn generate_eip1559_transfer_txn<TestRuntime: frame_system::Config + pallet_evm::Config>(
197 account_info: AccountInfo,
198 nonce: U256,
199 gas_price: U256,
200 to: H160,
201 value: U256,
202) -> Transaction {
203 EIP1559UnsignedTransaction {
204 nonce,
205 max_priority_fee_per_gas: gas_price,
206 max_fee_per_gas: gas_price.saturating_mul(U256::from(2)),
207 gas_limit: U256::from(21000),
208 action: ethereum::TransactionAction::Call(to),
209 value,
210 input: vec![],
211 }
212 .sign(&account_info.private_key, None)
213}
214
215pub fn generate_eip1559_tx<TestRuntime: frame_system::Config + pallet_evm::Config>(
216 account_info: AccountInfo,
217 nonce: U256,
218 action: ethereum::TransactionAction,
219 input: Vec<u8>,
220 gas_price: U256,
221) -> Transaction {
222 EIP1559UnsignedTransaction {
223 nonce,
224 max_priority_fee_per_gas: U256::from(1),
225 max_fee_per_gas: gas_price,
226 gas_limit: U256::from(max_extrinsic_gas::<TestRuntime>(1000)),
227 action,
228 value: U256::zero(),
229 input,
230 }
231 .sign(&account_info.private_key, None)
232}