use alloc::vec::Vec;
use alloy_primitives::Address;
use alloy_rlp::{RlpDecodable, RlpEncodable};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[repr(u8)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[cfg_attr(feature = "borsh", borsh(use_discriminant = true))]
pub enum FrameStatus {
#[default]
Failure = 0x00,
Success = 0x01,
SkippedAtomicBatch = 0x02,
}
impl FrameStatus {
pub const fn is_failure(self) -> bool {
matches!(self, Self::Failure)
}
pub const fn is_success(self) -> bool {
matches!(self, Self::Success)
}
pub const fn is_skipped_atomic_batch(self) -> bool {
matches!(self, Self::SkippedAtomicBatch)
}
pub const fn try_from_u8(value: u8) -> Option<Self> {
match value {
0x00 => Some(Self::Failure),
0x01 => Some(Self::Success),
0x02 => Some(Self::SkippedAtomicBatch),
_ => None,
}
}
}
impl_u8_discriminant!(FrameStatus, InvalidStatus, "invalid EIP-8141 frame status");
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, RlpEncodable, RlpDecodable)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
pub struct FrameGasUsed {
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::quantity"))]
pub execution: u64,
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::quantity"))]
pub state: u64,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, RlpEncodable, RlpDecodable)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
pub struct FrameReceipt<Log = alloy_primitives::Log> {
pub status: FrameStatus,
pub gas_used: FrameGasUsed,
pub logs: Vec<Log>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, RlpEncodable, RlpDecodable)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
pub struct FrameReceiptPayload<Log = alloy_primitives::Log> {
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::quantity"))]
pub cumulative_gas_used: u64,
pub payer: Address,
pub frame_receipts: Vec<FrameReceipt<Log>>,
}
impl<Log> FrameReceiptPayload<Log> {
pub fn map_logs<U>(self, mut f: impl FnMut(Log) -> U) -> FrameReceiptPayload<U> {
FrameReceiptPayload {
cumulative_gas_used: self.cumulative_gas_used,
payer: self.payer,
frame_receipts: self
.frame_receipts
.into_iter()
.map(|receipt| FrameReceipt {
status: receipt.status,
gas_used: receipt.gas_used,
logs: receipt.logs.into_iter().map(&mut f).collect(),
})
.collect(),
}
}
}