openzeppelin_monitor/utils/tests/builders/evm/
transaction.rs

1use crate::models::{EVMBaseTransaction, EVMTransaction};
2use alloy::{
3	primitives::{Address, Bytes, B256, U256},
4	rpc::types::Index,
5};
6
7/// A builder for creating test EVM transactions with default values.
8#[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	/// Creates a new TransactionBuilder instance.
25	pub fn new() -> Self {
26		Self::default()
27	}
28
29	/// Sets the hash of the transaction.
30	pub fn hash(mut self, hash: B256) -> Self {
31		self.hash = Some(hash);
32		self
33	}
34
35	/// Sets the sender address of the transaction.
36	pub fn from(mut self, from: Address) -> Self {
37		self.from = Some(from);
38		self
39	}
40
41	/// Sets the recipient address of the transaction.
42	pub fn to(mut self, to: Address) -> Self {
43		self.to = Some(to);
44		self
45	}
46
47	/// Sets the transaction value (amount sent).
48	pub fn value(mut self, value: U256) -> Self {
49		self.value = Some(value);
50		self
51	}
52
53	/// Sets the transaction input data.
54	pub fn input(mut self, input: Bytes) -> Self {
55		self.input = Some(input);
56		self
57	}
58
59	/// Sets the gas price for legacy transactions.
60	pub fn gas_price(mut self, gas_price: U256) -> Self {
61		self.gas_price = Some(gas_price);
62		self
63	}
64
65	/// Sets the max fee per gas for EIP-1559 transactions.
66	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	/// Sets the max priority fee per gas for EIP-1559 transactions.
72	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	/// Sets the gas limit for the transaction.
78	pub fn gas_limit(mut self, gas_limit: U256) -> Self {
79		self.gas_limit = Some(gas_limit);
80		self
81	}
82
83	/// Sets the nonce for the transaction.
84	pub fn nonce(mut self, nonce: U256) -> Self {
85		self.nonce = Some(nonce);
86		self
87	}
88
89	/// Sets the transaction index for the transaction.
90	pub fn transaction_index(mut self, transaction_index: usize) -> Self {
91		self.transaction_index = Some(Index(transaction_index));
92		self
93	}
94
95	/// Builds the Transaction instance.
96	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}