reth_rpc_convert/
receipt.rs

1//! Conversion traits for receipt responses to primitive receipt types.
2
3use alloy_network::Network;
4use std::convert::Infallible;
5
6/// Trait for converting network receipt responses to primitive receipt types.
7pub trait TryFromReceiptResponse<N: Network> {
8    /// The error type returned if the conversion fails.
9    type Error: core::error::Error + Send + Sync + Unpin;
10
11    /// Converts a network receipt response to a primitive receipt type.
12    ///
13    /// # Returns
14    ///
15    /// Returns `Ok(Self)` on successful conversion, or `Err(Self::Error)` if the conversion fails.
16    fn from_receipt_response(receipt_response: N::ReceiptResponse) -> Result<Self, Self::Error>
17    where
18        Self: Sized;
19}
20
21impl TryFromReceiptResponse<alloy_network::Ethereum> for reth_ethereum_primitives::Receipt {
22    type Error = Infallible;
23
24    fn from_receipt_response(
25        receipt_response: alloy_rpc_types_eth::TransactionReceipt,
26    ) -> Result<Self, Self::Error> {
27        Ok(receipt_response.into_inner().into())
28    }
29}
30
31#[cfg(feature = "op")]
32impl TryFromReceiptResponse<op_alloy_network::Optimism> for reth_optimism_primitives::OpReceipt {
33    type Error = Infallible;
34
35    fn from_receipt_response(
36        receipt_response: op_alloy_rpc_types::OpTransactionReceipt,
37    ) -> Result<Self, Self::Error> {
38        Ok(receipt_response.inner.inner.map_logs(Into::into).into())
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use alloy_consensus::ReceiptEnvelope;
46    use alloy_network::Ethereum;
47    use reth_ethereum_primitives::Receipt;
48
49    #[test]
50    fn test_try_from_receipt_response() {
51        let rpc_receipt = alloy_rpc_types_eth::TransactionReceipt {
52            inner: ReceiptEnvelope::Eip1559(Default::default()),
53            transaction_hash: Default::default(),
54            transaction_index: None,
55            block_hash: None,
56            block_number: None,
57            gas_used: 0,
58            effective_gas_price: 0,
59            blob_gas_used: None,
60            blob_gas_price: None,
61            from: Default::default(),
62            to: None,
63            contract_address: None,
64        };
65        let result =
66            <Receipt as TryFromReceiptResponse<Ethereum>>::from_receipt_response(rpc_receipt);
67        assert!(result.is_ok());
68    }
69
70    #[cfg(feature = "op")]
71    #[test]
72    fn test_try_from_receipt_response_optimism() {
73        use op_alloy_consensus::OpReceiptEnvelope;
74        use op_alloy_network::Optimism;
75        use op_alloy_rpc_types::OpTransactionReceipt;
76        use reth_optimism_primitives::OpReceipt;
77
78        let op_receipt = OpTransactionReceipt {
79            inner: alloy_rpc_types_eth::TransactionReceipt {
80                inner: OpReceiptEnvelope::Eip1559(Default::default()),
81                transaction_hash: Default::default(),
82                transaction_index: None,
83                block_hash: None,
84                block_number: None,
85                gas_used: 0,
86                effective_gas_price: 0,
87                blob_gas_used: None,
88                blob_gas_price: None,
89                from: Default::default(),
90                to: None,
91                contract_address: None,
92            },
93            l1_block_info: Default::default(),
94        };
95        let result =
96            <OpReceipt as TryFromReceiptResponse<Optimism>>::from_receipt_response(op_receipt);
97        assert!(result.is_ok());
98    }
99}