eest_types/state/
transaction.rs1use crate::{
2 deserializer::deserialize_maybe_empty, TestAuthorization, TransactionType,
3};
4use cfx_rpc_primitives::Bytes;
5use cfx_types::{Address, H256, U256};
6use primitives::transaction::AccessList;
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: H256,
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<H256>,
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> {
45 let mut tx_type = TransactionType::Legacy;
46
47 if let Some(access_list) = self.access_lists.get(access_list_index) {
49 if access_list.is_some() {
50 tx_type = TransactionType::Eip2930;
51 }
52 }
53
54 if self.max_fee_per_gas.is_some() {
56 tx_type = TransactionType::Eip1559;
57 }
58
59 if self.max_fee_per_blob_gas.is_some() {
61 self.to?;
63 tx_type = TransactionType::Eip4844;
64 }
65
66 if self.authorization_list.is_some() {
68 self.to?;
70 tx_type = TransactionType::Eip7702;
71 }
72
73 Some(tx_type)
74 }
75}
76
77#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
79#[serde(rename_all = "camelCase", deny_unknown_fields)]
80pub struct TxPartIndices {
81 pub data: usize,
82 pub gas: usize,
83 pub value: usize,
84}
85
86#[cfg(test)]
87mod test {
88
89 use super::*;
90
91 #[test]
92 fn decode_tx_parts() {
93 let tx = r#"{
94 "nonce": "0x00",
95 "maxPriorityFeePerGas": "0x00",
96 "maxFeePerGas": "0x07",
97 "gasLimit": [
98 "0x0423ff"
99 ],
100 "to": "0x0000000000000000000000000000000000001000",
101 "value": [
102 "0x00"
103 ],
104 "data": [
105 "0x"
106 ],
107 "accessLists": [
108 [
109 {
110 "address": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71",
111 "storageKeys": [
112 "0x0000000000000000000000000000000000000000000000000000000000000000"
113 ]
114 }
115 ]
116 ],
117 "authorizationList": [
118 {
119 "chainId": "0x00",
120 "address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
121 "nonce": "0x00",
122 "v": "0x01",
123 "r": "0x5a8cac98fd240d8ef83c22db4a061ffa0facb1801245283cc05fc809d8b92837",
124 "s": "0x1c3162fe11d91bc24d4fa00fb19ca34531e0eacdf8142c804be44058d5b8244f",
125 "signer": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71"
126 }
127 ],
128 "sender": "0x8a0a19589531694250d570040a0c4b74576919b8",
129 "secretKey": "0x9e7645d0cfd9c3a04eb7a9db59a4eb7d359f2e75c9164a9d6b9a7d54e1b6a36f"
130 }"#;
131
132 let tx: TransactionParts = serde_json::from_str(tx).unwrap();
133 println!("{:?}", tx);
134 }
135}