alloy_zksync/network/
tx_type.rs

1use std::fmt;
2
3use alloy::network::eip2718::Eip2718Error;
4
5/// Transaction types supported by the Era network.
6#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[doc(alias = "TransactionType")]
8pub enum TxType {
9    /// Legacy transaction type.
10    Legacy = 0,
11    /// EIP-2930 transaction type.
12    Eip2930 = 1,
13    /// EIP-1559 transaction type.
14    Eip1559 = 2,
15    /// EIP-4844 transaction type.
16    Eip4844 = 3,
17    /// EIP-7702 transaction type.
18    Eip7702 = 4,
19    /// ZKsync-specific EIP712-based transaction type.
20    Eip712 = 0x71,
21    // TODO: L1-based transaction type
22}
23
24impl TxType {
25    /// Tries to represent transaction as an Ethereum transaction type.
26    /// Will return `None` for ZKsync-specific transactions.
27    pub fn as_eth_type(self) -> Option<alloy::consensus::TxType> {
28        Some(match self {
29            TxType::Legacy => alloy::consensus::TxType::Legacy,
30            TxType::Eip2930 => alloy::consensus::TxType::Eip2930,
31            TxType::Eip1559 => alloy::consensus::TxType::Eip1559,
32            TxType::Eip4844 => alloy::consensus::TxType::Eip4844,
33            TxType::Eip7702 => alloy::consensus::TxType::Eip7702,
34            TxType::Eip712 => return None,
35        })
36    }
37}
38
39impl alloy::consensus::Typed2718 for TxType {
40    fn ty(&self) -> u8 {
41        u8::from(*self)
42    }
43}
44
45impl From<alloy::consensus::TxType> for TxType {
46    fn from(value: alloy::consensus::TxType) -> Self {
47        let raw_value = value as u8;
48        Self::try_from(raw_value).expect("Era supports all Eth tx types")
49    }
50}
51
52impl From<TxType> for u8 {
53    fn from(value: TxType) -> Self {
54        value as Self
55    }
56}
57
58impl fmt::Display for TxType {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        match self {
61            Self::Legacy => write!(f, "Legacy"),
62            Self::Eip2930 => write!(f, "EIP-2930"),
63            Self::Eip1559 => write!(f, "EIP-1559"),
64            Self::Eip4844 => write!(f, "EIP-4844"),
65            Self::Eip7702 => write!(f, "EIP-7702"),
66            Self::Eip712 => write!(f, "Era EIP-712"),
67        }
68    }
69}
70
71impl TryFrom<u8> for TxType {
72    type Error = Eip2718Error;
73
74    fn try_from(value: u8) -> Result<Self, Self::Error> {
75        Ok(match value {
76            0 => Self::Legacy,
77            1 => Self::Eip2930,
78            2 => Self::Eip1559,
79            3 => Self::Eip4844,
80            4 => Self::Eip7702,
81            0x71 => Self::Eip712,
82            _ => return Err(Eip2718Error::UnexpectedType(value)),
83        })
84    }
85}