Skip to main content

trident_sdk/
deposit.rs

1use crate::evm;
2use crate::evm::EvmDepositError;
3use alloy_primitives::{Address, U256};
4use alloy_provider::network::AnyNetwork;
5use alloy_provider::{DynProvider, PendingTransactionBuilder};
6use std::marker::PhantomData;
7
8/// Chain-specific data needed to execute a deposit.
9#[derive(Debug)]
10pub(crate) enum DepositContext {
11    Evm {
12        provider: DynProvider<AnyNetwork>,
13        signer_address: Address,
14        deposit_contract: Address,
15        token: Address,
16        amount: U256,
17    },
18}
19
20/// A prepared deposit ready to be submitted on-chain.
21///
22/// Obtained from [`TridentClient::deposit`](crate::TridentClient::deposit). Call
23/// [`send`](Self::send) to submit the transaction; await `.get_receipt()` on the
24/// returned builder to block until inclusion.
25#[derive(Debug)]
26pub struct Deposit<T> {
27    inner: DepositContext,
28    _chain_type: PhantomData<T>,
29}
30
31impl<T> Deposit<T> {
32    pub(crate) fn new(inner: DepositContext) -> Self {
33        Self {
34            inner,
35            _chain_type: PhantomData,
36        }
37    }
38}
39
40impl Deposit<Address> {
41    /// Submits the deposit on-chain, approving the deposit contract first if
42    /// the current ERC-20 allowance is below `amount`. Returns the deposit
43    /// transaction as a [`PendingTransactionBuilder`]; the caller awaits
44    /// inclusion via `.get_receipt()` or `.watch()`.
45    pub async fn send(self) -> Result<PendingTransactionBuilder<AnyNetwork>, EvmDepositError> {
46        let DepositContext::Evm {
47            provider,
48            signer_address,
49            deposit_contract,
50            token,
51            amount,
52        } = self.inner;
53
54        evm::deposit(provider, deposit_contract, signer_address, token, amount).await
55    }
56}