Skip to main content

foundry_primitives/transaction/
optimism.rs

1//! OP-stack-specific impls for [`FoundryTxEnvelope`] and [`FoundryTransactionRequest`].
2
3use super::{FoundryTransactionRequest, FoundryTxEnvelope};
4use alloy_consensus::Sealed;
5use op_alloy_consensus::{
6    OpTransaction as OpTransactionTrait, OpTxEnvelope, TxDeposit, TxPostExec,
7};
8
9impl OpTransactionTrait for FoundryTxEnvelope {
10    fn is_deposit(&self) -> bool {
11        Self::is_deposit(self)
12    }
13
14    fn as_deposit(&self) -> Option<&Sealed<TxDeposit>> {
15        match self {
16            Self::Deposit(tx) => Some(tx),
17            _ => None,
18        }
19    }
20
21    fn as_post_exec(&self) -> Option<&Sealed<TxPostExec>> {
22        if let Self::PostExec(tx) = self { Some(tx) } else { None }
23    }
24}
25
26impl From<OpTxEnvelope> for FoundryTxEnvelope {
27    fn from(tx: OpTxEnvelope) -> Self {
28        match tx {
29            OpTxEnvelope::Legacy(tx) => Self::Legacy(tx),
30            OpTxEnvelope::Eip2930(tx) => Self::Eip2930(tx),
31            OpTxEnvelope::Eip1559(tx) => Self::Eip1559(tx),
32            OpTxEnvelope::Eip7702(tx) => Self::Eip7702(tx),
33            OpTxEnvelope::Deposit(tx) => Self::Deposit(tx),
34            OpTxEnvelope::PostExec(tx) => Self::PostExec(tx),
35        }
36    }
37}
38
39impl From<op_alloy_rpc_types::Transaction<FoundryTxEnvelope>> for FoundryTransactionRequest {
40    fn from(tx: op_alloy_rpc_types::Transaction<FoundryTxEnvelope>) -> Self {
41        tx.inner.into_inner().into()
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use alloy_network::eip2718::Encodable2718;
49    use alloy_primitives::{TxHash, b256};
50    use alloy_rlp::Decodable;
51
52    #[test]
53    fn test_decode_encode_deposit_tx() {
54        // https://sepolia-optimism.etherscan.io/tx/0xbf8b5f08c43e4b860715cd64fc0849bbce0d0ea20a76b269e7bc8886d112fca7
55        let tx_hash: TxHash =
56            b256!("0xbf8b5f08c43e4b860715cd64fc0849bbce0d0ea20a76b269e7bc8886d112fca7");
57
58        // https://sepolia-optimism.etherscan.io/getRawTx?tx=0xbf8b5f08c43e4b860715cd64fc0849bbce0d0ea20a76b269e7bc8886d112fca7
59        let raw_tx = alloy_primitives::hex::decode(
60            "7ef861a0dfd7ae78bf3c414cfaa77f13c0205c82eb9365e217b2daa3448c3156b69b27ac94778f2146f48179643473b82931c4cd7b8f153efd94778f2146f48179643473b82931c4cd7b8f153efd872386f26fc10000872386f26fc10000830186a08080",
61        )
62        .unwrap();
63        let dep_tx = FoundryTxEnvelope::decode(&mut raw_tx.as_slice()).unwrap();
64
65        let mut encoded = Vec::new();
66        dep_tx.encode_2718(&mut encoded);
67
68        assert_eq!(raw_tx, encoded);
69
70        assert_eq!(tx_hash, dep_tx.hash());
71    }
72}