Skip to main content

alloy_consensus_any/receipt/
envelope.rs

1use alloc::vec::Vec;
2use alloy_consensus::{Eip658Value, Receipt, ReceiptWithBloom, TxReceipt};
3use alloy_eips::{
4    eip2718::{Decodable2718, Eip2718Result, Encodable2718},
5    Typed2718,
6};
7use alloy_primitives::{bytes::BufMut, Bloom, Log};
8use alloy_rlp::{Decodable, Encodable};
9use core::fmt;
10
11/// Receipt envelope, as defined in [EIP-2718].
12///
13/// Represents legacy and typed EIP-2718 receipts. Type ID 0 is encoded as untagged legacy; this
14/// type does not preserve a literal `0x00` prefix.
15///
16/// Transaction receipt payloads are specified in their respective EIPs.
17///
18/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21#[doc(alias = "AnyTransactionReceiptEnvelope", alias = "AnyTxReceiptEnvelope")]
22pub struct AnyReceiptEnvelope<T = Log> {
23    /// The receipt envelope.
24    #[cfg_attr(feature = "serde", serde(flatten))]
25    pub inner: ReceiptWithBloom<Receipt<T>>,
26    /// The transaction type.
27    #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
28    pub r#type: u8,
29}
30
31impl<T> AnyReceiptEnvelope<T> {
32    /// Returns whether this is a legacy receipt (type 0)
33    pub const fn is_legacy(&self) -> bool {
34        self.r#type == 0
35    }
36}
37
38impl<T: Encodable> AnyReceiptEnvelope<T> {
39    /// Calculate the length of the rlp payload of the network encoded receipt.
40    pub fn rlp_payload_length(&self) -> usize {
41        let length = self.inner.length();
42        if self.is_legacy() {
43            length
44        } else {
45            length + 1
46        }
47    }
48}
49
50impl<T> AnyReceiptEnvelope<T> {
51    /// Return true if the transaction was successful.
52    ///
53    /// ## Note
54    ///
55    /// This method may not accurately reflect the status of the transaction
56    /// for transactions before [EIP-658].
57    ///
58    /// [EIP-658]: https://eips.ethereum.org/EIPS/eip-658
59    pub const fn is_success(&self) -> bool {
60        self.status()
61    }
62
63    /// Returns the success status of the receipt's transaction.
64    ///
65    /// ## Note
66    ///
67    /// This method may not accurately reflect the status of the transaction
68    /// for transactions before [EIP-658].
69    ///
70    /// [EIP-658]: https://eips.ethereum.org/EIPS/eip-658
71    pub const fn status(&self) -> bool {
72        self.inner.receipt.status.coerce_status()
73    }
74
75    /// Return the receipt's bloom.
76    pub const fn bloom(&self) -> Bloom {
77        self.inner.logs_bloom
78    }
79
80    /// Return a reference to the receipt's bloom.
81    pub const fn bloom_ref(&self) -> &Bloom {
82        &self.inner.logs_bloom
83    }
84
85    /// Returns the cumulative gas used at this receipt.
86    pub const fn cumulative_gas_used(&self) -> u64 {
87        self.inner.receipt.cumulative_gas_used
88    }
89
90    /// Return the receipt logs.
91    pub fn logs(&self) -> &[T] {
92        &self.inner.receipt.logs
93    }
94}
95
96impl<T> TxReceipt for AnyReceiptEnvelope<T>
97where
98    T: Clone + fmt::Debug + PartialEq + Eq + Send + Sync,
99{
100    type Log = T;
101
102    fn status_or_post_state(&self) -> Eip658Value {
103        self.inner.receipt.status
104    }
105
106    fn status(&self) -> bool {
107        self.status()
108    }
109
110    fn bloom(&self) -> Bloom {
111        self.bloom()
112    }
113
114    fn cumulative_gas_used(&self) -> u64 {
115        self.cumulative_gas_used()
116    }
117
118    fn logs(&self) -> &[T] {
119        Self::logs(self)
120    }
121
122    fn into_logs(self) -> Vec<Self::Log> {
123        self.inner.receipt.logs
124    }
125}
126
127impl Typed2718 for AnyReceiptEnvelope {
128    fn ty(&self) -> u8 {
129        self.r#type
130    }
131}
132
133impl Encodable2718 for AnyReceiptEnvelope {
134    fn encode_2718_len(&self) -> usize {
135        self.inner.length() + !self.is_legacy() as usize
136    }
137
138    fn encode_2718(&self, out: &mut dyn BufMut) {
139        match self.type_flag() {
140            None => {}
141            Some(ty) => out.put_u8(ty),
142        }
143        self.inner.encode(out);
144    }
145}
146
147impl Decodable2718 for AnyReceiptEnvelope {
148    fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result<Self> {
149        let receipt = Decodable::decode(buf)?;
150        Ok(Self { inner: receipt, r#type: ty })
151    }
152
153    fn fallback_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
154        Self::typed_decode(0, buf)
155    }
156}