alloy_zksync/network/
tx_type.rs1use std::fmt;
2
3use alloy::network::eip2718::Eip2718Error;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[doc(alias = "TransactionType")]
8pub enum TxType {
9 Legacy = 0,
11 Eip2930 = 1,
13 Eip1559 = 2,
15 Eip4844 = 3,
17 Eip7702 = 4,
19 Eip712 = 0x71,
21 }
23
24impl TxType {
25 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}