alloy_consensus_any/receipt/
envelope.rs1use alloy_consensus::{Eip658Value, Receipt, ReceiptWithBloom, TxReceipt};
2use alloy_eips::{
3 eip2718::{Decodable2718, Eip2718Result, Encodable2718},
4 Typed2718,
5};
6use alloy_primitives::{bytes::BufMut, Bloom, Log};
7use alloy_rlp::{Decodable, Encodable};
8use core::fmt;
9
10#[derive(Clone, Debug, PartialEq, Eq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22#[doc(alias = "AnyTransactionReceiptEnvelope", alias = "AnyTxReceiptEnvelope")]
23pub struct AnyReceiptEnvelope<T = Log> {
24 #[cfg_attr(feature = "serde", serde(flatten))]
26 pub inner: ReceiptWithBloom<Receipt<T>>,
27 #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
29 pub r#type: u8,
30}
31
32impl<T> AnyReceiptEnvelope<T> {
33 pub const fn is_legacy(&self) -> bool {
35 self.r#type == 0
36 }
37}
38
39impl<T: Encodable> AnyReceiptEnvelope<T> {
40 pub fn rlp_payload_length(&self) -> usize {
42 let length = self.inner.length();
43 if self.is_legacy() {
44 length
45 } else {
46 length + 1
47 }
48 }
49}
50
51impl<T> AnyReceiptEnvelope<T> {
52 pub const fn is_success(&self) -> bool {
61 self.status()
62 }
63
64 pub const fn status(&self) -> bool {
73 self.inner.receipt.status.coerce_status()
74 }
75
76 pub const fn bloom(&self) -> Bloom {
78 self.inner.logs_bloom
79 }
80
81 pub const fn cumulative_gas_used(&self) -> u64 {
83 self.inner.receipt.cumulative_gas_used
84 }
85
86 pub fn logs(&self) -> &[T] {
88 &self.inner.receipt.logs
89 }
90}
91
92impl<T> TxReceipt for AnyReceiptEnvelope<T>
93where
94 T: Clone + fmt::Debug + PartialEq + Eq + Send + Sync,
95{
96 type Log = T;
97
98 fn status_or_post_state(&self) -> Eip658Value {
99 self.inner.receipt.status
100 }
101
102 fn status(&self) -> bool {
103 self.status()
104 }
105
106 fn bloom(&self) -> Bloom {
107 self.bloom()
108 }
109
110 fn cumulative_gas_used(&self) -> u64 {
111 self.cumulative_gas_used()
112 }
113
114 fn logs(&self) -> &[T] {
115 self.logs()
116 }
117}
118
119impl Typed2718 for AnyReceiptEnvelope {
120 fn ty(&self) -> u8 {
121 self.r#type
122 }
123}
124
125impl Encodable2718 for AnyReceiptEnvelope {
126 fn encode_2718_len(&self) -> usize {
127 self.inner.length() + !self.is_legacy() as usize
128 }
129
130 fn encode_2718(&self, out: &mut dyn BufMut) {
131 match self.type_flag() {
132 None => {}
133 Some(ty) => out.put_u8(ty),
134 }
135 self.inner.encode(out);
136 }
137}
138
139impl Decodable2718 for AnyReceiptEnvelope {
140 fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result<Self> {
141 let receipt = Decodable::decode(buf)?;
142 Ok(Self { inner: receipt, r#type: ty })
143 }
144
145 fn fallback_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
146 Self::typed_decode(0, buf)
147 }
148}