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