alloy_consensus_any/receipt/
envelope.rs1use 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#[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 #[cfg_attr(feature = "serde", serde(flatten))]
25 pub inner: ReceiptWithBloom<Receipt<T>>,
26 #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
28 pub r#type: u8,
29}
30
31impl<T> AnyReceiptEnvelope<T> {
32 pub const fn is_legacy(&self) -> bool {
34 self.r#type == 0
35 }
36}
37
38impl<T: Encodable> AnyReceiptEnvelope<T> {
39 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 pub const fn is_success(&self) -> bool {
60 self.status()
61 }
62
63 pub const fn status(&self) -> bool {
72 self.inner.receipt.status.coerce_status()
73 }
74
75 pub const fn bloom(&self) -> Bloom {
77 self.inner.logs_bloom
78 }
79
80 pub const fn bloom_ref(&self) -> &Bloom {
82 &self.inner.logs_bloom
83 }
84
85 pub const fn cumulative_gas_used(&self) -> u64 {
87 self.inner.receipt.cumulative_gas_used
88 }
89
90 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}