Skip to main content

foundry_primitives/transaction/
deposit.rs

1//! Deposit helpers shared by Base and Optimism.
2
3use super::FoundryReceiptEnvelope;
4use alloy_primitives::{B256, U256};
5use alloy_serde::OtherFields;
6use op_alloy_consensus::OpDepositReceipt;
7use op_revm::transaction::deposit::DepositTransactionParts;
8
9/// Converts `OtherFields` to `DepositTransactionParts`, produces error with missing fields.
10pub fn get_deposit_tx_parts(
11    other: &OtherFields,
12) -> Result<DepositTransactionParts, Vec<&'static str>> {
13    let mut missing = Vec::new();
14    let source_hash =
15        other.get_deserialized::<B256>("sourceHash").transpose().ok().flatten().unwrap_or_else(
16            || {
17                missing.push("sourceHash");
18                Default::default()
19            },
20        );
21    let mint = other
22        .get_deserialized::<U256>("mint")
23        .transpose()
24        .unwrap_or_else(|_| {
25            missing.push("mint");
26            Default::default()
27        })
28        .map(|value| value.saturating_to::<u128>());
29    let is_system_transaction =
30        other.get_deserialized::<bool>("isSystemTx").transpose().ok().flatten().unwrap_or_else(
31            || {
32                missing.push("isSystemTx");
33                Default::default()
34            },
35        );
36    if missing.is_empty() {
37        Ok(DepositTransactionParts { source_hash, mint, is_system_transaction })
38    } else {
39        Err(missing)
40    }
41}
42
43/// Deposit accessors shared by Base and Optimism.
44impl<T> FoundryReceiptEnvelope<T> {
45    /// Return the receipt's deposit_nonce if it is a deposit receipt.
46    pub const fn deposit_nonce(&self) -> Option<u64> {
47        match self.as_deposit_receipt() {
48            Some(receipt) => receipt.deposit_nonce,
49            None => None,
50        }
51    }
52
53    /// Return the receipt's deposit version if it is a deposit receipt.
54    pub const fn deposit_receipt_version(&self) -> Option<u64> {
55        match self.as_deposit_receipt() {
56            Some(receipt) => receipt.deposit_receipt_version,
57            None => None,
58        }
59    }
60
61    /// Returns the deposit receipt if it is a deposit receipt.
62    pub const fn as_deposit_receipt(&self) -> Option<&OpDepositReceipt<T>> {
63        match self {
64            Self::Deposit(t) => Some(&t.receipt),
65            _ => None,
66        }
67    }
68}