openzeppelin_monitor/utils/tests/builders/evm/
transaction.rs1use crate::models::{EVMBaseTransaction, EVMTransaction};
2use alloy::{
3 primitives::{Address, Bytes, B256, U256},
4 rpc::types::Index,
5};
6
7#[derive(Debug, Default)]
9pub struct TransactionBuilder {
10 hash: Option<B256>,
11 from: Option<Address>,
12 to: Option<Address>,
13 value: Option<U256>,
14 input: Option<Bytes>,
15 gas_price: Option<U256>,
16 max_fee_per_gas: Option<U256>,
17 max_priority_fee_per_gas: Option<U256>,
18 gas_limit: Option<U256>,
19 nonce: Option<U256>,
20 transaction_index: Option<Index>,
21}
22
23impl TransactionBuilder {
24 pub fn new() -> Self {
26 Self::default()
27 }
28
29 pub fn hash(mut self, hash: B256) -> Self {
31 self.hash = Some(hash);
32 self
33 }
34
35 pub fn from(mut self, from: Address) -> Self {
37 self.from = Some(from);
38 self
39 }
40
41 pub fn to(mut self, to: Address) -> Self {
43 self.to = Some(to);
44 self
45 }
46
47 pub fn value(mut self, value: U256) -> Self {
49 self.value = Some(value);
50 self
51 }
52
53 pub fn input(mut self, input: Bytes) -> Self {
55 self.input = Some(input);
56 self
57 }
58
59 pub fn gas_price(mut self, gas_price: U256) -> Self {
61 self.gas_price = Some(gas_price);
62 self
63 }
64
65 pub fn max_fee_per_gas(mut self, max_fee_per_gas: U256) -> Self {
67 self.max_fee_per_gas = Some(max_fee_per_gas);
68 self
69 }
70
71 pub fn max_priority_fee_per_gas(mut self, max_priority_fee_per_gas: U256) -> Self {
73 self.max_priority_fee_per_gas = Some(max_priority_fee_per_gas);
74 self
75 }
76
77 pub fn gas_limit(mut self, gas_limit: U256) -> Self {
79 self.gas_limit = Some(gas_limit);
80 self
81 }
82
83 pub fn nonce(mut self, nonce: U256) -> Self {
85 self.nonce = Some(nonce);
86 self
87 }
88
89 pub fn transaction_index(mut self, transaction_index: usize) -> Self {
91 self.transaction_index = Some(Index(transaction_index));
92 self
93 }
94
95 pub fn build(self) -> EVMTransaction {
97 let default_gas_limit = U256::from(21000);
98
99 let base_tx = EVMBaseTransaction {
100 hash: self.hash.unwrap_or_default(),
101 from: self.from,
102 to: self.to,
103 gas_price: self.gas_price,
104 max_fee_per_gas: self.max_fee_per_gas,
105 max_priority_fee_per_gas: self.max_priority_fee_per_gas,
106 gas: self.gas_limit.unwrap_or(default_gas_limit),
107 nonce: self.nonce.unwrap_or_default(),
108 value: self.value.unwrap_or_default(),
109 input: self.input.unwrap_or_default(),
110 transaction_index: self.transaction_index,
111 ..Default::default()
112 };
113
114 EVMTransaction(base_tx)
115 }
116}