use core::fmt;
use crate::{Eip658Value, Receipt, ReceiptWithBloom, TxReceipt, TxType};
use alloy_eips::{
eip2718::{
Decodable2718, Eip2718Error, Eip2718Result, Encodable2718, EIP1559_TX_TYPE_ID,
EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, EIP7702_TX_TYPE_ID, LEGACY_TX_TYPE_ID,
},
Typed2718,
};
use alloy_primitives::{Bloom, Log};
use alloy_rlp::{BufMut, Decodable, Encodable};
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
#[doc(alias = "TransactionReceiptEnvelope", alias = "TxReceiptEnvelope")]
pub enum ReceiptEnvelope<T = Log> {
#[cfg_attr(feature = "serde", serde(rename = "0x0", alias = "0x00"))]
Legacy(ReceiptWithBloom<Receipt<T>>),
#[cfg_attr(feature = "serde", serde(rename = "0x1", alias = "0x01"))]
Eip2930(ReceiptWithBloom<Receipt<T>>),
#[cfg_attr(feature = "serde", serde(rename = "0x2", alias = "0x02"))]
Eip1559(ReceiptWithBloom<Receipt<T>>),
#[cfg_attr(feature = "serde", serde(rename = "0x3", alias = "0x03"))]
Eip4844(ReceiptWithBloom<Receipt<T>>),
#[cfg_attr(feature = "serde", serde(rename = "0x4", alias = "0x04"))]
Eip7702(ReceiptWithBloom<Receipt<T>>),
}
impl<T> ReceiptEnvelope<T> {
pub fn map_logs<U>(self, f: impl FnMut(T) -> U) -> ReceiptEnvelope<U> {
match self {
Self::Legacy(r) => ReceiptEnvelope::Legacy(r.map_logs(f)),
Self::Eip2930(r) => ReceiptEnvelope::Eip2930(r.map_logs(f)),
Self::Eip1559(r) => ReceiptEnvelope::Eip1559(r.map_logs(f)),
Self::Eip4844(r) => ReceiptEnvelope::Eip4844(r.map_logs(f)),
Self::Eip7702(r) => ReceiptEnvelope::Eip7702(r.map_logs(f)),
}
}
pub fn into_primitives_receipt(self) -> ReceiptEnvelope<Log>
where
T: Into<Log>,
{
self.map_logs(Into::into)
}
#[doc(alias = "transaction_type")]
pub const fn tx_type(&self) -> TxType {
match self {
Self::Legacy(_) => TxType::Legacy,
Self::Eip2930(_) => TxType::Eip2930,
Self::Eip1559(_) => TxType::Eip1559,
Self::Eip4844(_) => TxType::Eip4844,
Self::Eip7702(_) => TxType::Eip7702,
}
}
pub fn is_success(&self) -> bool {
self.status()
}
pub fn status(&self) -> bool {
self.as_receipt().unwrap().status.coerce_status()
}
pub fn cumulative_gas_used(&self) -> u64 {
self.as_receipt().unwrap().cumulative_gas_used
}
pub fn logs(&self) -> &[T] {
&self.as_receipt().unwrap().logs
}
pub fn logs_bloom(&self) -> &Bloom {
&self.as_receipt_with_bloom().unwrap().logs_bloom
}
pub const fn as_receipt_with_bloom(&self) -> Option<&ReceiptWithBloom<Receipt<T>>> {
match self {
Self::Legacy(t)
| Self::Eip2930(t)
| Self::Eip1559(t)
| Self::Eip4844(t)
| Self::Eip7702(t) => Some(t),
}
}
pub const fn as_receipt(&self) -> Option<&Receipt<T>> {
match self {
Self::Legacy(t)
| Self::Eip2930(t)
| Self::Eip1559(t)
| Self::Eip4844(t)
| Self::Eip7702(t) => Some(&t.receipt),
}
}
}
impl<T> TxReceipt for ReceiptEnvelope<T>
where
T: Clone + fmt::Debug + PartialEq + Eq + Send + Sync,
{
type Log = T;
fn status_or_post_state(&self) -> Eip658Value {
self.as_receipt().unwrap().status
}
fn status(&self) -> bool {
self.as_receipt().unwrap().status.coerce_status()
}
fn bloom(&self) -> Bloom {
self.as_receipt_with_bloom().unwrap().logs_bloom
}
fn bloom_cheap(&self) -> Option<Bloom> {
Some(self.bloom())
}
fn cumulative_gas_used(&self) -> u64 {
self.as_receipt().unwrap().cumulative_gas_used
}
fn logs(&self) -> &[T] {
&self.as_receipt().unwrap().logs
}
}
impl ReceiptEnvelope {
pub fn inner_length(&self) -> usize {
self.as_receipt_with_bloom().unwrap().length()
}
pub fn rlp_payload_length(&self) -> usize {
let length = self.as_receipt_with_bloom().unwrap().length();
match self {
Self::Legacy(_) => length,
_ => length + 1,
}
}
}
impl Encodable for ReceiptEnvelope {
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
self.network_encode(out)
}
fn length(&self) -> usize {
self.network_len()
}
}
impl Decodable for ReceiptEnvelope {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Self::network_decode(buf)
.map_or_else(|_| Err(alloy_rlp::Error::Custom("Unexpected type")), Ok)
}
}
impl Typed2718 for ReceiptEnvelope {
fn ty(&self) -> u8 {
match self {
Self::Legacy(_) => LEGACY_TX_TYPE_ID,
Self::Eip2930(_) => EIP2930_TX_TYPE_ID,
Self::Eip1559(_) => EIP1559_TX_TYPE_ID,
Self::Eip4844(_) => EIP4844_TX_TYPE_ID,
Self::Eip7702(_) => EIP7702_TX_TYPE_ID,
}
}
}
impl Encodable2718 for ReceiptEnvelope {
fn encode_2718_len(&self) -> usize {
self.inner_length() + !self.is_legacy() as usize
}
fn encode_2718(&self, out: &mut dyn BufMut) {
match self.type_flag() {
None => {}
Some(ty) => out.put_u8(ty),
}
self.as_receipt_with_bloom().unwrap().encode(out);
}
}
impl Decodable2718 for ReceiptEnvelope {
fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result<Self> {
let receipt = Decodable::decode(buf)?;
match ty.try_into().map_err(|_| alloy_rlp::Error::Custom("Unexpected type"))? {
TxType::Eip2930 => Ok(Self::Eip2930(receipt)),
TxType::Eip1559 => Ok(Self::Eip1559(receipt)),
TxType::Eip4844 => Ok(Self::Eip4844(receipt)),
TxType::Eip7702 => Ok(Self::Eip7702(receipt)),
TxType::Legacy => Err(Eip2718Error::UnexpectedType(0)),
}
}
fn fallback_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
Ok(Self::Legacy(Decodable::decode(buf)?))
}
}
#[cfg(any(test, feature = "arbitrary"))]
impl<'a, T> arbitrary::Arbitrary<'a> for ReceiptEnvelope<T>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let receipt = ReceiptWithBloom::<Receipt<T>>::arbitrary(u)?;
match u.int_in_range(0..=3)? {
0 => Ok(Self::Legacy(receipt)),
1 => Ok(Self::Eip2930(receipt)),
2 => Ok(Self::Eip1559(receipt)),
3 => Ok(Self::Eip4844(receipt)),
4 => Ok(Self::Eip7702(receipt)),
_ => unreachable!(),
}
}
}
#[cfg(test)]
mod test {
#[cfg(feature = "serde")]
#[test]
fn deser_pre658_receipt_envelope() {
use alloy_primitives::b256;
use crate::Receipt;
let receipt = super::ReceiptWithBloom::<Receipt<()>> {
receipt: super::Receipt {
status: super::Eip658Value::PostState(b256!(
"284d35bf53b82ef480ab4208527325477439c64fb90ef518450f05ee151c8e10"
)),
cumulative_gas_used: 0,
logs: Default::default(),
},
logs_bloom: Default::default(),
};
let json = serde_json::to_string(&receipt).unwrap();
println!("Serialized {}", json);
let receipt: super::ReceiptWithBloom<Receipt<()>> = serde_json::from_str(&json).unwrap();
assert_eq!(
receipt.receipt.status,
super::Eip658Value::PostState(b256!(
"284d35bf53b82ef480ab4208527325477439c64fb90ef518450f05ee151c8e10"
))
);
}
}