alloy_consensus/transaction/
tx_type.rs

1//! Contains the Ethereum transaction type identifier.
2
3use crate::TxType;
4use core::fmt;
5
6#[allow(clippy::derivable_impls)]
7impl Default for TxType {
8    fn default() -> Self {
9        Self::Legacy
10    }
11}
12
13impl TxType {
14    /// Returns true if the transaction type is Legacy.
15    #[inline]
16    pub const fn is_legacy(&self) -> bool {
17        matches!(self, Self::Legacy)
18    }
19
20    /// Returns true if the transaction type is EIP-2930.
21    #[inline]
22    pub const fn is_eip2930(&self) -> bool {
23        matches!(self, Self::Eip2930)
24    }
25
26    /// Returns true if the transaction type is EIP-1559.
27    #[inline]
28    pub const fn is_eip1559(&self) -> bool {
29        matches!(self, Self::Eip1559)
30    }
31
32    /// Returns true if the transaction type is EIP-4844.
33    #[inline]
34    pub const fn is_eip4844(&self) -> bool {
35        matches!(self, Self::Eip4844)
36    }
37
38    /// Returns true if the transaction type is EIP-7702.
39    #[inline]
40    pub const fn is_eip7702(&self) -> bool {
41        matches!(self, Self::Eip7702)
42    }
43
44    /// Returns true if the transaction type has dynamic fee.
45    #[inline]
46    pub const fn is_dynamic_fee(&self) -> bool {
47        matches!(self, Self::Eip1559 | Self::Eip4844 | Self::Eip7702)
48    }
49}
50
51impl fmt::Display for TxType {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        match self {
54            Self::Legacy => write!(f, "Legacy"),
55            Self::Eip2930 => write!(f, "EIP-2930"),
56            Self::Eip1559 => write!(f, "EIP-1559"),
57            Self::Eip4844 => write!(f, "EIP-4844"),
58            Self::Eip7702 => write!(f, "EIP-7702"),
59        }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn check_u8_id() {
69        assert_eq!(TxType::Legacy, TxType::Legacy as u8);
70        assert_eq!(TxType::Eip2930, TxType::Eip2930 as u8);
71        assert_eq!(TxType::Eip1559, TxType::Eip1559 as u8);
72        assert_eq!(TxType::Eip7702, TxType::Eip7702 as u8);
73        assert_eq!(TxType::Eip4844, TxType::Eip4844 as u8);
74    }
75}