Skip to content

Commit

Permalink
feat: introduce Typed2718 trait (#1675)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 22, 2024
1 parent eb234fd commit e499c6d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
28 changes: 27 additions & 1 deletion crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
transaction::{
eip4844::{TxEip4844, TxEip4844Variant, TxEip4844WithSidecar},
RlpEcdsaTx,
RlpEcdsaTx, Typed2718,
},
Signed, Transaction, TxEip1559, TxEip2930, TxEip7702, TxLegacy,
};
Expand Down Expand Up @@ -125,6 +125,32 @@ impl Decodable for TxType {
}
}

impl Typed2718 for TxType {
fn is_type(&self, ty: u8) -> bool {
*self == ty
}

fn is_legacy(&self) -> bool {
matches!(self, Self::Legacy)
}

fn is_eip2930(&self) -> bool {
matches!(self, Self::Eip2930)
}

fn is_eip1559(&self) -> bool {
matches!(self, Self::Eip1559)
}

fn is_eip4844(&self) -> bool {
matches!(self, Self::Eip4844)
}

fn is_eip7702(&self) -> bool {
matches!(self, Self::Eip7702)
}
}

/// The Ethereum [EIP-2718] Transaction Envelope.
///
/// # Note:
Expand Down
29 changes: 25 additions & 4 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ impl<T: Transaction> Transaction for alloy_serde::WithOtherFields<T> {
self.inner.effective_gas_price(base_fee)
}

#[inline]
fn is_dynamic_fee(&self) -> bool {
self.inner.is_dynamic_fee()
}

#[inline]
fn kind(&self) -> TxKind {
self.inner.kind()
Expand Down Expand Up @@ -308,9 +313,25 @@ impl<T: Transaction> Transaction for alloy_serde::WithOtherFields<T> {
fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
self.inner.authorization_list()
}
}

#[inline]
fn is_dynamic_fee(&self) -> bool {
self.inner.is_dynamic_fee()
}
/// A trait that helps to determine the type of the transaction.
pub trait Typed2718 {
/// Returns true if the type matches the given type.
fn is_type(&self, ty: u8) -> bool;

/// Returns true if the type is a legacy transaction.
fn is_legacy(&self) -> bool;

/// Returns true if the type is an EIP-2930 transaction.
fn is_eip2930(&self) -> bool;

/// Returns true if the type is an EIP-1559 transaction.
fn is_eip1559(&self) -> bool;

/// Returns true if the type is an EIP-4844 transaction.
fn is_eip4844(&self) -> bool;

/// Returns true if the type is an EIP-7702 transaction.
fn is_eip7702(&self) -> bool;
}

0 comments on commit e499c6d

Please sign in to comment.