trident-sdk 0.2.0

End-user SDK for Trident deposits and cross-chain spends
Documentation
use crate::evm;
use crate::evm::EvmDepositError;
use alloy_primitives::{Address, U256};
use alloy_provider::network::AnyNetwork;
use alloy_provider::{DynProvider, PendingTransactionBuilder};
use std::marker::PhantomData;

/// Chain-specific data needed to execute a deposit.
#[derive(Debug)]
pub(crate) enum DepositContext {
    Evm {
        provider: DynProvider<AnyNetwork>,
        signer_address: Address,
        deposit_contract: Address,
        token: Address,
        amount: U256,
    },
}

/// A prepared deposit ready to be submitted on-chain.
///
/// Obtained from [`TridentClient::deposit`](crate::TridentClient::deposit). Call
/// [`send`](Self::send) to submit the transaction; await `.get_receipt()` on the
/// returned builder to block until inclusion.
#[derive(Debug)]
pub struct Deposit<T> {
    inner: DepositContext,
    _chain_type: PhantomData<T>,
}

impl<T> Deposit<T> {
    pub(crate) fn new(inner: DepositContext) -> Self {
        Self {
            inner,
            _chain_type: PhantomData,
        }
    }
}

impl Deposit<Address> {
    /// Submits the deposit on-chain, approving the deposit contract first if
    /// the current ERC-20 allowance is below `amount`. Returns the deposit
    /// transaction as a [`PendingTransactionBuilder`]; the caller awaits
    /// inclusion via `.get_receipt()` or `.watch()`.
    pub async fn send(self) -> Result<PendingTransactionBuilder<AnyNetwork>, EvmDepositError> {
        let DepositContext::Evm {
            provider,
            signer_address,
            deposit_contract,
            token,
            amount,
        } = self.inner;

        evm::deposit(provider, deposit_contract, signer_address, token, amount).await
    }
}