openzeppelin_monitor/models/blockchain/evm/
transaction.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! EVM transaction data structures.

use std::{collections::HashMap, ops::Deref};

use serde::{Deserialize, Serialize};

use alloy::{
	consensus::Transaction as AlloyConsensusTransaction,
	primitives::{Address, Bytes, B256, U256, U64},
	rpc::types::{AccessList, Index, Transaction as AlloyTransaction},
};

/// L2-specific transaction fields
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct BaseL2Transaction {
	/// Deposit receipt version (for L2 transactions)
	#[serde(
		rename = "depositReceiptVersion",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub deposit_receipt_version: Option<U64>,

	/// Source hash (for L2 transactions)
	#[serde(
		rename = "sourceHash",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub source_hash: Option<B256>,

	/// Mint amount (for L2 transactions)
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub mint: Option<U256>,

	/// Y parity (alternative to v in some implementations)
	#[serde(rename = "yParity", default, skip_serializing_if = "Option::is_none")]
	pub y_parity: Option<U64>,
}

/// Base Transaction struct
/// Copied from web3 crate (now deprecated) and slightly modified for alloy compatibility
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct BaseTransaction {
	/// Hash
	pub hash: B256,
	/// Nonce
	pub nonce: U256,
	/// Block hash. None when pending.
	#[serde(rename = "blockHash")]
	pub block_hash: Option<B256>,
	/// Block number. None when pending.
	#[serde(rename = "blockNumber")]
	pub block_number: Option<U64>,
	/// Transaction Index. None when pending.
	#[serde(rename = "transactionIndex")]
	pub transaction_index: Option<Index>,
	/// Sender
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub from: Option<Address>,
	/// Recipient (None when contract creation)
	pub to: Option<Address>,
	/// Transferred value
	pub value: U256,
	/// Gas Price
	#[serde(rename = "gasPrice")]
	pub gas_price: Option<U256>,
	/// Gas amount
	pub gas: U256,
	/// Input data
	pub input: Bytes,
	/// ECDSA recovery id
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub v: Option<U64>,
	/// ECDSA signature r, 32 bytes
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub r: Option<U256>,
	/// ECDSA signature s, 32 bytes
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub s: Option<U256>,
	/// Raw transaction data
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub raw: Option<Bytes>,
	/// Transaction type, Some(1) for AccessList transaction, None for Legacy
	#[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
	pub transaction_type: Option<U64>,
	/// Access list
	#[serde(
		rename = "accessList",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub access_list: Option<AccessList>,
	/// Max fee per gas
	#[serde(rename = "maxFeePerGas", skip_serializing_if = "Option::is_none")]
	pub max_fee_per_gas: Option<U256>,
	/// miner bribe
	#[serde(
		rename = "maxPriorityFeePerGas",
		skip_serializing_if = "Option::is_none"
	)]
	pub max_priority_fee_per_gas: Option<U256>,

	/// L2-specific transaction fields
	#[serde(flatten)]
	pub l2: BaseL2Transaction,

	/// Catch-all for non-standard fields
	#[serde(flatten)]
	pub extra: HashMap<String, serde_json::Value>,
}

/// Wrapper around Base Transaction that implements additional functionality
///
/// This type provides a convenient interface for working with EVM transactions
/// while maintaining compatibility with the base types.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Transaction(pub BaseTransaction);

impl Transaction {
	/// Get the transaction value (amount of ETH transferred)
	pub fn value(&self) -> &U256 {
		&self.0.value
	}

	/// Get the transaction sender address
	pub fn sender(&self) -> Option<&Address> {
		self.0.from.as_ref()
	}

	/// Get the transaction recipient address (None for contract creation)
	pub fn to(&self) -> Option<&Address> {
		self.0.to.as_ref()
	}

	/// Get the gas limit for the transaction
	pub fn gas(&self) -> &U256 {
		&self.0.gas
	}

	/// Get the gas price (None for EIP-1559 transactions)
	pub fn gas_price(&self) -> Option<&U256> {
		self.0.gas_price.as_ref()
	}

	/// Get the transaction nonce
	pub fn nonce(&self) -> &U256 {
		&self.0.nonce
	}

	/// Get the transaction hash
	pub fn hash(&self) -> &B256 {
		&self.0.hash
	}
}

impl From<BaseTransaction> for Transaction {
	fn from(tx: BaseTransaction) -> Self {
		Self(tx)
	}
}

impl From<AlloyTransaction> for Transaction {
	fn from(tx: AlloyTransaction) -> Self {
		let tx = BaseTransaction {
			hash: *tx.inner.tx_hash(),
			nonce: U256::from(tx.inner.nonce()),
			block_hash: tx.block_hash,
			block_number: tx.block_number.map(U64::from),
			transaction_index: tx.transaction_index.map(|i| Index::from(i as usize)),
			from: Some(tx.inner.signer()),
			to: tx.inner.to(),
			value: tx.inner.value(),
			gas_price: tx.inner.gas_price().map(U256::from),
			gas: U256::from(tx.inner.gas_limit()),
			input: tx.inner.input().clone(),
			v: Some(U64::from(u64::from(tx.inner.signature().v()))),
			r: Some(U256::from(tx.inner.signature().r())),
			s: Some(U256::from(tx.inner.signature().s())),
			raw: None,
			transaction_type: Some(U64::from(tx.inner.tx_type() as u64)),
			access_list: tx.inner.access_list().cloned(),
			max_fee_per_gas: Some(U256::from(tx.inner.max_fee_per_gas())),
			max_priority_fee_per_gas: Some(U256::from(
				tx.inner.max_priority_fee_per_gas().unwrap_or(0),
			)),
			l2: BaseL2Transaction {
				deposit_receipt_version: None,
				source_hash: None,
				mint: None,
				y_parity: None,
			},
			extra: HashMap::new(),
		};
		Self(tx)
	}
}

impl Deref for Transaction {
	type Target = BaseTransaction;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use alloy::{
		primitives::{Address, Bytes, B256, U256, U64},
		rpc::types::Index,
	};

	fn create_test_transaction() -> BaseTransaction {
		BaseTransaction {
			hash: B256::with_last_byte(1),
			nonce: U256::from(2),
			block_hash: Some(B256::with_last_byte(3)),
			block_number: Some(U64::from(4)),
			transaction_index: Some(Index::from(0)),
			from: Some(Address::with_last_byte(5)),
			to: Some(Address::with_last_byte(6)),
			value: U256::from(100),
			gas_price: Some(U256::from(20)),
			gas: U256::from(21000),
			input: Bytes::default(),
			v: None,
			r: None,
			s: None,
			raw: None,
			transaction_type: None,
			access_list: None,
			max_priority_fee_per_gas: None,
			max_fee_per_gas: None,
			l2: BaseL2Transaction {
				deposit_receipt_version: None,
				source_hash: None,
				mint: None,
				y_parity: None,
			},
			extra: HashMap::new(),
		}
	}

	#[test]
	fn test_value() {
		let tx = Transaction(create_test_transaction());
		assert_eq!(*tx.value(), U256::from(100));
	}

	#[test]
	fn test_sender() {
		let tx = Transaction(create_test_transaction());
		assert_eq!(tx.sender(), Some(&Address::with_last_byte(5)));
	}

	#[test]
	fn test_recipient() {
		let tx = Transaction(create_test_transaction());
		assert_eq!(tx.to(), Some(&Address::with_last_byte(6)));
	}

	#[test]
	fn test_gas() {
		let tx = Transaction(create_test_transaction());
		assert_eq!(*tx.gas(), U256::from(21000));
	}

	#[test]
	fn test_gas_price() {
		let tx = Transaction(create_test_transaction());
		assert_eq!(tx.gas_price(), Some(&U256::from(20)));
	}

	#[test]
	fn test_nonce() {
		let tx = Transaction(create_test_transaction());
		assert_eq!(*tx.nonce(), U256::from(2));
	}

	#[test]
	fn test_hash() {
		let tx = Transaction(create_test_transaction());
		assert_eq!(*tx.hash(), B256::with_last_byte(1));
	}

	#[test]
	fn test_from_base_transaction() {
		let base_tx = create_test_transaction();
		let tx: Transaction = base_tx.clone().into();
		assert_eq!(tx.0, base_tx);
	}

	#[test]
	fn test_deref() {
		let base_tx = create_test_transaction();
		let tx = Transaction(base_tx.clone());
		assert_eq!(*tx, base_tx);
	}
}