revm_optimism/transaction/
abstraction.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use super::deposit::{DepositTransaction, TxDeposit};
use crate::OpTransactionError;
use revm::{
    primitives::Bytes,
    transaction::{CommonTxFields, Transaction, TransactionType},
    wiring::default::TxEnv,
};

pub trait OpTxTrait: Transaction {
    type DepositTx: DepositTransaction;

    fn deposit(&self) -> &Self::DepositTx;

    fn enveloped_tx(&self) -> Option<&Bytes>;
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum OpTransactionType {
    /// Base transaction type supported on Ethereum mainnet.
    Base(TransactionType),
    /// Optimism-specific deposit transaction type.
    Deposit,
}

impl From<OpTransactionType> for TransactionType {
    fn from(tx_type: OpTransactionType) -> Self {
        match tx_type {
            OpTransactionType::Base(tx_type) => tx_type,
            OpTransactionType::Deposit => TransactionType::Custom,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum OpTransaction<T: Transaction> {
    Base {
        tx: T,
        /// An enveloped EIP-2718 typed transaction. This is used
        /// to compute the L1 tx cost using the L1 block info, as
        /// opposed to requiring downstream apps to compute the cost
        /// externally.
        enveloped_tx: Option<Bytes>,
    },
    Deposit(TxDeposit),
}

impl Default for OpTransaction<TxEnv> {
    fn default() -> Self {
        Self::Base {
            tx: TxEnv::default(),
            enveloped_tx: None,
        }
    }
}

impl<T: Transaction> Transaction for OpTransaction<T> {
    // TODO
    type TransactionError = OpTransactionError;
    type TransactionType = OpTransactionType;

    type AccessList = T::AccessList;

    type Legacy = T::Legacy;

    type Eip2930 = T::Eip2930;

    type Eip1559 = T::Eip1559;

    type Eip4844 = T::Eip4844;

    type Eip7702 = T::Eip7702;

    fn tx_type(&self) -> Self::TransactionType {
        match self {
            Self::Base { tx, .. } => OpTransactionType::Base(tx.tx_type().into()),
            Self::Deposit(_) => OpTransactionType::Deposit,
        }
    }

    fn common_fields(&self) -> &dyn CommonTxFields {
        match self {
            Self::Base { tx, .. } => tx.common_fields(),
            Self::Deposit(deposit) => deposit,
        }
    }

    fn kind(&self) -> revm::primitives::TxKind {
        match self {
            Self::Base { tx, .. } => tx.kind(),
            Self::Deposit(deposit) => deposit.to,
        }
    }

    fn effective_gas_price(&self, base_fee: revm::primitives::U256) -> revm::primitives::U256 {
        match self {
            Self::Base { tx, .. } => tx.effective_gas_price(base_fee),
            Self::Deposit(_) => base_fee,
        }
    }

    fn max_fee(&self) -> u128 {
        match self {
            Self::Base { tx, .. } => tx.max_fee(),
            Self::Deposit(_) => 0,
        }
    }

    fn legacy(&self) -> &Self::Legacy {
        let Self::Base { tx, .. } = self else {
            panic!("Not a legacy transaction")
        };
        tx.legacy()
    }

    fn eip2930(&self) -> &Self::Eip2930 {
        let Self::Base { tx, .. } = self else {
            panic!("Not eip2930 transaction")
        };
        tx.eip2930()
    }

    fn eip1559(&self) -> &Self::Eip1559 {
        let Self::Base { tx, .. } = self else {
            panic!("Not a eip1559 transaction")
        };
        tx.eip1559()
    }

    fn eip4844(&self) -> &Self::Eip4844 {
        let Self::Base { tx, .. } = self else {
            panic!("Not a eip4844 transaction")
        };
        tx.eip4844()
    }

    fn eip7702(&self) -> &Self::Eip7702 {
        let Self::Base { tx, .. } = self else {
            panic!("Not a eip7702 transaction")
        };
        tx.eip7702()
    }
}

impl<T: Transaction> OpTxTrait for OpTransaction<T> {
    type DepositTx = TxDeposit;

    fn deposit(&self) -> &Self::DepositTx {
        match self {
            Self::Base { .. } => panic!("Not a deposit transaction"),
            Self::Deposit(deposit) => deposit,
        }
    }

    fn enveloped_tx(&self) -> Option<&Bytes> {
        match self {
            Self::Base { enveloped_tx, .. } => enveloped_tx.as_ref(),
            Self::Deposit(_) => None,
        }
    }
}