revm_context/
tx.rs

1pub use alloy_eip2930::AccessList;
2pub use alloy_eip7702::SignedAuthorization;
3use context_interface::Transaction;
4use core::fmt::Debug;
5use primitives::{Address, Bytes, TxKind, B256, U256};
6use std::vec::Vec;
7
8/// The transaction environment
9#[derive(Clone, Debug, PartialEq, Eq)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct TxEnv {
12    pub tx_type: u8,
13    /// Caller aka Author aka transaction signer
14    pub caller: Address,
15    /// The gas limit of the transaction
16    pub gas_limit: u64,
17    /// The gas price of the transaction
18    pub gas_price: u128,
19    /// The destination of the transaction
20    pub kind: TxKind,
21    /// The value sent to `transact_to`
22    pub value: U256,
23    /// The data of the transaction
24    pub data: Bytes,
25
26    /// The nonce of the transaction
27    pub nonce: u64,
28
29    /// The chain ID of the transaction
30    ///
31    /// If set to [`None`], no checks are performed.
32    ///
33    /// Incorporated as part of the Spurious Dragon upgrade via [EIP-155].
34    ///
35    /// [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
36    pub chain_id: Option<u64>,
37
38    /// A list of addresses and storage keys that the transaction plans to access
39    ///
40    /// Added in [EIP-2930].
41    ///
42    /// [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
43    pub access_list: AccessList,
44
45    /// The priority fee per gas
46    ///
47    /// Incorporated as part of the London upgrade via [EIP-1559].
48    ///
49    /// [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559
50    pub gas_priority_fee: Option<u128>,
51
52    /// The list of blob versioned hashes
53    ///
54    /// Per EIP there should be at least one blob present if [`max_fee_per_blob_gas`][Self::max_fee_per_blob_gas] is [`Some`].
55    ///
56    /// Incorporated as part of the Cancun upgrade via [EIP-4844].
57    ///
58    /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
59    pub blob_hashes: Vec<B256>,
60
61    /// The max fee per blob gas
62    ///
63    /// Incorporated as part of the Cancun upgrade via [EIP-4844].
64    ///
65    /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
66    pub max_fee_per_blob_gas: u128,
67
68    /// List of authorizations
69    ///
70    /// `authorization_list` contains the signature that authorizes this
71    /// caller to place the code to signer account.
72    ///
73    /// Set EOA account code for one transaction via [EIP-7702].
74    ///
75    /// [EIP-7702]: https://eips.ethereum.org/EIPS/eip-7702
76    pub authorization_list: Vec<SignedAuthorization>,
77}
78
79impl Default for TxEnv {
80    fn default() -> Self {
81        Self {
82            tx_type: 0,
83            caller: Address::default(),
84            gas_limit: 30_000_000,
85            gas_price: 0,
86            kind: TxKind::Call(Address::default()),
87            value: U256::ZERO,
88            data: Bytes::default(),
89            nonce: 0,
90            chain_id: Some(1), // Mainnet chain ID is 1
91            access_list: Default::default(),
92            gas_priority_fee: Some(0),
93            blob_hashes: Vec::new(),
94            max_fee_per_blob_gas: 0,
95            authorization_list: Vec::new(),
96        }
97    }
98}
99
100impl Transaction for TxEnv {
101    type AccessList = AccessList;
102    type Authorization = SignedAuthorization;
103
104    fn tx_type(&self) -> u8 {
105        self.tx_type
106    }
107
108    fn kind(&self) -> TxKind {
109        self.kind
110    }
111
112    fn caller(&self) -> Address {
113        self.caller
114    }
115
116    fn gas_limit(&self) -> u64 {
117        self.gas_limit
118    }
119
120    fn gas_price(&self) -> u128 {
121        self.gas_price
122    }
123
124    fn value(&self) -> U256 {
125        self.value
126    }
127
128    fn nonce(&self) -> u64 {
129        self.nonce
130    }
131
132    fn chain_id(&self) -> Option<u64> {
133        self.chain_id
134    }
135
136    fn access_list(&self) -> Option<&Self::AccessList> {
137        Some(&self.access_list)
138    }
139
140    fn max_fee_per_gas(&self) -> u128 {
141        self.gas_price
142    }
143
144    fn max_fee_per_blob_gas(&self) -> u128 {
145        self.max_fee_per_blob_gas
146    }
147
148    fn authorization_list_len(&self) -> usize {
149        self.authorization_list.len()
150    }
151
152    fn authorization_list(&self) -> impl Iterator<Item = &Self::Authorization> {
153        self.authorization_list.iter()
154    }
155
156    fn input(&self) -> &Bytes {
157        &self.data
158    }
159
160    fn blob_versioned_hashes(&self) -> &[B256] {
161        &self.blob_hashes
162    }
163
164    fn max_priority_fee_per_gas(&self) -> Option<u128> {
165        self.gas_priority_fee
166    }
167}