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, 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_tx<TestRuntime: frame_system::Config + pallet_evm::Config>(
125 account_info: AccountInfo,
126 nonce: U256,
127 action: ethereum::TransactionAction,
128 input: Vec<u8>,
129 gas_price: U256,
130) -> Transaction {
131 LegacyUnsignedTransaction {
132 nonce,
133 gas_price,
134 gas_limit: U256::from(max_extrinsic_gas::<TestRuntime>(1000)),
135 action,
136 value: U256::zero(),
137 input,
138 }
139 .sign(&account_info.private_key)
140}
141
142pub fn generate_eip2930_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 EIP2930UnsignedTransaction {
150 nonce,
151 gas_price,
152 gas_limit: U256::from(max_extrinsic_gas::<TestRuntime>(100)),
153 action,
154 value: U256::one(),
155 input,
156 }
157 .sign(&account_info.private_key, None)
158}
159
160pub fn generate_eip1559_tx<TestRuntime: frame_system::Config + pallet_evm::Config>(
161 account_info: AccountInfo,
162 nonce: U256,
163 action: ethereum::TransactionAction,
164 input: Vec<u8>,
165 gas_price: U256,
166) -> Transaction {
167 EIP1559UnsignedTransaction {
168 nonce,
169 max_priority_fee_per_gas: U256::from(1),
170 max_fee_per_gas: gas_price,
171 gas_limit: U256::from(max_extrinsic_gas::<TestRuntime>(1000)),
172 action,
173 value: U256::zero(),
174 input,
175 }
176 .sign(&account_info.private_key, None)
177}