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;
7
8/// OP-stack deposit fields that are not part of a regular transaction request.
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
10pub struct DepositTransactionParts {
11    /// Hash that uniquely identifies the deposit's source.
12    pub source_hash: B256,
13    /// ETH minted to the sender on L2, if any.
14    pub mint: Option<u128>,
15    /// Whether the deposit is a system transaction.
16    pub is_system_transaction: bool,
17}
18
19/// Converts `OtherFields` to `DepositTransactionParts`, produces error with missing fields.
20pub fn get_deposit_tx_parts(
21    other: &OtherFields,
22) -> Result<DepositTransactionParts, Vec<&'static str>> {
23    let mut missing = Vec::new();
24    let source_hash =
25        other.get_deserialized::<B256>("sourceHash").transpose().ok().flatten().unwrap_or_else(
26            || {
27                missing.push("sourceHash");
28                Default::default()
29            },
30        );
31    let mint = other
32        .get_deserialized::<U256>("mint")
33        .transpose()
34        .unwrap_or_else(|_| {
35            missing.push("mint");
36            Default::default()
37        })
38        .map(|value| value.saturating_to::<u128>());
39    let is_system_transaction =
40        other.get_deserialized::<bool>("isSystemTx").transpose().ok().flatten().unwrap_or_else(
41            || {
42                missing.push("isSystemTx");
43                Default::default()
44            },
45        );
46    if missing.is_empty() {
47        Ok(DepositTransactionParts { source_hash, mint, is_system_transaction })
48    } else {
49        Err(missing)
50    }
51}
52
53/// Deposit accessors shared by Base and Optimism.
54impl<T> FoundryReceiptEnvelope<T> {
55    /// Return the receipt's deposit_nonce if it is a deposit receipt.
56    pub const fn deposit_nonce(&self) -> Option<u64> {
57        match self.as_deposit_receipt() {
58            Some(receipt) => receipt.deposit_nonce,
59            None => None,
60        }
61    }
62
63    /// Return the receipt's deposit version if it is a deposit receipt.
64    pub const fn deposit_receipt_version(&self) -> Option<u64> {
65        match self.as_deposit_receipt() {
66            Some(receipt) => receipt.deposit_receipt_version,
67            None => None,
68        }
69    }
70
71    /// Returns the deposit receipt if it is a deposit receipt.
72    pub const fn as_deposit_receipt(&self) -> Option<&OpDepositReceipt<T>> {
73        match self {
74            Self::Deposit(t) => Some(&t.receipt),
75            _ => None,
76        }
77    }
78}