revm_statetest_types/
transaction.rs1use crate::{deserializer::deserialize_maybe_empty, TestAuthorization};
2use alloy_eip2930::AccessList;
3use revm::{
4 context_interface::transaction::TransactionType,
5 primitives::{Address, Bytes, B256, U256},
6};
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct TransactionParts {
13 pub data: Vec<Bytes>,
14 pub gas_limit: Vec<U256>,
15 pub gas_price: Option<U256>,
16 pub nonce: U256,
17 pub secret_key: B256,
18 #[serde(default)]
20 pub sender: Option<Address>,
21 #[serde(default, deserialize_with = "deserialize_maybe_empty")]
22 pub to: Option<Address>,
23 pub value: Vec<U256>,
24 pub max_fee_per_gas: Option<U256>,
25 pub max_priority_fee_per_gas: Option<U256>,
26
27 #[serde(default)]
28 pub access_lists: Vec<Option<AccessList>>,
29 pub authorization_list: Option<Vec<TestAuthorization>>,
30 #[serde(default)]
31 pub blob_versioned_hashes: Vec<B256>,
32 pub max_fee_per_blob_gas: Option<U256>,
33}
34
35impl TransactionParts {
36 pub fn tx_type(&self, access_list_index: usize) -> Option<TransactionType> {
44 let mut tx_type = TransactionType::Legacy;
45
46 if let Some(access_list) = self.access_lists.get(access_list_index) {
48 if access_list.is_some() {
49 tx_type = TransactionType::Eip2930;
50 }
51 }
52
53 if self.max_fee_per_gas.is_some() {
55 tx_type = TransactionType::Eip1559;
56 }
57
58 if self.max_fee_per_blob_gas.is_some() {
60 self.to?;
62 tx_type = TransactionType::Eip4844;
63 }
64
65 if self.authorization_list.is_some() {
67 self.to?;
69 tx_type = TransactionType::Eip7702;
70 }
71
72 Some(tx_type)
73 }
74}
75
76#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
78#[serde(rename_all = "camelCase", deny_unknown_fields)]
79pub struct TxPartIndices {
80 pub data: usize,
81 pub gas: usize,
82 pub value: usize,
83}
84
85#[cfg(test)]
86mod test {
87
88 use super::*;
89
90 #[test]
91 fn decode_tx_parts() {
92 let tx = r#"{
93 "nonce": "0x00",
94 "maxPriorityFeePerGas": "0x00",
95 "maxFeePerGas": "0x07",
96 "gasLimit": [
97 "0x0423ff"
98 ],
99 "to": "0x0000000000000000000000000000000000001000",
100 "value": [
101 "0x00"
102 ],
103 "data": [
104 "0x"
105 ],
106 "accessLists": [
107 [
108 {
109 "address": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71",
110 "storageKeys": [
111 "0x0000000000000000000000000000000000000000000000000000000000000000"
112 ]
113 }
114 ]
115 ],
116 "authorizationList": [
117 {
118 "chainId": "0x00",
119 "address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
120 "nonce": "0x00",
121 "v": "0x01",
122 "r": "0x5a8cac98fd240d8ef83c22db4a061ffa0facb1801245283cc05fc809d8b92837",
123 "s": "0x1c3162fe11d91bc24d4fa00fb19ca34531e0eacdf8142c804be44058d5b8244f",
124 "signer": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71"
125 }
126 ],
127 "sender": "0x8a0a19589531694250d570040a0c4b74576919b8",
128 "secretKey": "0x9e7645d0cfd9c3a04eb7a9db59a4eb7d359f2e75c9164a9d6b9a7d54e1b6a36f"
129 }"#;
130
131 let _: TransactionParts = serde_json::from_str(tx).unwrap();
132 }
133}