alloy_network_primitives/
traits.rs

1use crate::BlockTransactions;
2use alloy_consensus::{BlockHeader, Transaction};
3use alloy_primitives::{Address, BlockHash, TxHash, B256};
4use alloy_serde::WithOtherFields;
5
6/// Receipt JSON-RPC response.
7pub trait ReceiptResponse {
8    /// Address of the created contract, or `None` if the transaction was not a deployment.
9    fn contract_address(&self) -> Option<Address>;
10
11    /// Status of the transaction.
12    ///
13    /// ## Note
14    ///
15    /// Caution must be taken when using this method for deep-historical
16    /// receipts, as it may not accurately reflect the status of the
17    /// transaction. The transaction status is not knowable from the receipt
18    /// for transactions before [EIP-658].
19    fn status(&self) -> bool;
20
21    /// Hash of the block this transaction was included within.
22    fn block_hash(&self) -> Option<BlockHash>;
23
24    /// Number of the block this transaction was included within.
25    fn block_number(&self) -> Option<u64>;
26
27    /// Transaction Hash.
28    fn transaction_hash(&self) -> TxHash;
29
30    /// Index within the block.
31    fn transaction_index(&self) -> Option<u64>;
32
33    /// Gas used by this transaction alone.
34    fn gas_used(&self) -> u64;
35
36    /// Effective gas price.
37    fn effective_gas_price(&self) -> u128;
38
39    /// Total cost of this transaction = gas_used * effective_gas_price.
40    fn cost(&self) -> u128 {
41        self.gas_used() as u128 * self.effective_gas_price()
42    }
43
44    /// Blob gas used by the eip-4844 transaction.
45    fn blob_gas_used(&self) -> Option<u64>;
46
47    /// Blob gas price paid by the eip-4844 transaction.
48    fn blob_gas_price(&self) -> Option<u128>;
49
50    /// Address of the sender.
51    fn from(&self) -> Address;
52
53    /// Address of the receiver.
54    fn to(&self) -> Option<Address>;
55
56    /// Returns the cumulative gas used at this receipt.
57    fn cumulative_gas_used(&self) -> u64;
58
59    /// The post-transaction state root (pre Byzantium)
60    ///
61    /// EIP98 makes this field optional.
62    fn state_root(&self) -> Option<B256>;
63}
64
65/// Transaction JSON-RPC response. Aggregates transaction data with its block and signer context.
66pub trait TransactionResponse: Transaction {
67    /// Hash of the transaction
68    #[doc(alias = "transaction_hash")]
69    fn tx_hash(&self) -> TxHash;
70
71    /// Block hash
72    fn block_hash(&self) -> Option<BlockHash>;
73
74    /// Block number
75    fn block_number(&self) -> Option<u64>;
76
77    /// Transaction Index
78    fn transaction_index(&self) -> Option<u64>;
79
80    /// Sender of the transaction
81    fn from(&self) -> Address;
82
83    /// Gas Price, this is the RPC format for `max_fee_per_gas`, pre-eip-1559.
84    fn gas_price(&self) -> Option<u128> {
85        if self.ty() < 2 {
86            return Some(Transaction::max_fee_per_gas(self));
87        }
88        None
89    }
90
91    /// Max BaseFeePerGas the user is willing to pay. For pre-eip-1559 transactions, the field
92    /// label `gas_price` is used instead.
93    fn max_fee_per_gas(&self) -> Option<u128> {
94        if self.ty() < 2 {
95            return None;
96        }
97        Some(Transaction::max_fee_per_gas(self))
98    }
99
100    /// Transaction type format for RPC. This field is included since eip-2930.
101    fn transaction_type(&self) -> Option<u8> {
102        match self.ty() {
103            0 => None,
104            ty => Some(ty),
105        }
106    }
107}
108
109/// Header JSON-RPC response.
110pub trait HeaderResponse: BlockHeader {
111    /// Block hash
112    fn hash(&self) -> BlockHash;
113}
114
115/// Block JSON-RPC response.
116pub trait BlockResponse {
117    /// Header type
118    type Header;
119    /// Transaction type
120    type Transaction: TransactionResponse;
121
122    /// Block header
123    fn header(&self) -> &Self::Header;
124
125    /// Block transactions
126    fn transactions(&self) -> &BlockTransactions<Self::Transaction>;
127
128    /// Mutable reference to block transactions
129    fn transactions_mut(&mut self) -> &mut BlockTransactions<Self::Transaction>;
130
131    /// Returns the `other` field from `WithOtherFields` type.
132    fn other_fields(&self) -> Option<&alloy_serde::OtherFields> {
133        None
134    }
135}
136
137impl<T: TransactionResponse> TransactionResponse for WithOtherFields<T> {
138    fn tx_hash(&self) -> TxHash {
139        self.inner.tx_hash()
140    }
141
142    fn block_hash(&self) -> Option<BlockHash> {
143        self.inner.block_hash()
144    }
145
146    fn block_number(&self) -> Option<u64> {
147        self.inner.block_number()
148    }
149
150    fn transaction_index(&self) -> Option<u64> {
151        self.inner.transaction_index()
152    }
153
154    fn from(&self) -> Address {
155        self.inner.from()
156    }
157}
158
159impl<T: ReceiptResponse> ReceiptResponse for WithOtherFields<T> {
160    fn contract_address(&self) -> Option<Address> {
161        self.inner.contract_address()
162    }
163
164    fn status(&self) -> bool {
165        self.inner.status()
166    }
167
168    fn block_hash(&self) -> Option<BlockHash> {
169        self.inner.block_hash()
170    }
171
172    fn block_number(&self) -> Option<u64> {
173        self.inner.block_number()
174    }
175
176    fn transaction_hash(&self) -> TxHash {
177        self.inner.transaction_hash()
178    }
179
180    fn transaction_index(&self) -> Option<u64> {
181        self.inner.transaction_index()
182    }
183
184    fn gas_used(&self) -> u64 {
185        self.inner.gas_used()
186    }
187
188    fn effective_gas_price(&self) -> u128 {
189        self.inner.effective_gas_price()
190    }
191
192    fn blob_gas_used(&self) -> Option<u64> {
193        self.inner.blob_gas_used()
194    }
195
196    fn blob_gas_price(&self) -> Option<u128> {
197        self.inner.blob_gas_price()
198    }
199
200    fn from(&self) -> Address {
201        self.inner.from()
202    }
203
204    fn to(&self) -> Option<Address> {
205        self.inner.to()
206    }
207
208    fn cumulative_gas_used(&self) -> u64 {
209        self.inner.cumulative_gas_used()
210    }
211
212    fn state_root(&self) -> Option<B256> {
213        self.inner.state_root()
214    }
215}
216
217impl<T: BlockResponse> BlockResponse for WithOtherFields<T> {
218    type Header = T::Header;
219    type Transaction = T::Transaction;
220
221    fn header(&self) -> &Self::Header {
222        self.inner.header()
223    }
224
225    fn transactions(&self) -> &BlockTransactions<Self::Transaction> {
226        self.inner.transactions()
227    }
228
229    fn transactions_mut(&mut self) -> &mut BlockTransactions<Self::Transaction> {
230        self.inner.transactions_mut()
231    }
232
233    fn other_fields(&self) -> Option<&alloy_serde::OtherFields> {
234        Some(&self.other)
235    }
236}
237
238impl<T: HeaderResponse> HeaderResponse for WithOtherFields<T> {
239    fn hash(&self) -> BlockHash {
240        self.inner.hash()
241    }
242}