trident-sdk 0.1.0

SDK for Trident protocol
Documentation
use alloy_primitives::{Address, U256};
use alloy_provider::network::Ethereum;
use alloy_provider::{Network, Provider};
use std::marker::PhantomData;
use tides_core_types::TidesChainId;
use trident_types::core::FixedBytes32;

pub struct DepositInEvmChainWithApprovalResponse<N: Network = Ethereum> {
    pub approval_tx_receipt: N::ReceiptResponse,
    pub deposit_tx_receipt: N::ReceiptResponse,
}

/// Represents a spend request where the balance is located in an EVM chain, and
/// the spend is done on a destination chain which is also an EVM chain.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct EvmToEvmSpendRequest<
    'a,
    S: alloy_signer::Signer + ?Sized,
    DP: Provider<DN> + ?Sized,
    DN: Network = Ethereum,
> {
    pub source_signer: &'a S,
    pub source_chain_id: TidesChainId,
    pub source_token: Address,
    pub source_token_amount: U256,
    /// Optional source fees. Otherwise, pick the one suggested by the attester.
    pub source_max_fees: Option<U256>,

    pub destination_provider: &'a DP,
    pub destination_chain_id: TidesChainId,
    pub destination_recipient: Address,

    /// Can be kept empty. It's useful only in case the source token maps to multiple
    /// destination tokens (eg: eth-USDT mapping to arbitrum-USDT & arbitrum-USDT0)
    pub destination_token: Option<Address>,

    /// Optional callback.
    pub destination_callback: Option<EvmDestinationCallback>,

    /// Optional caller, if not provided defaults to source_signer.address().
    pub destination_caller: Option<Address>,

    /// Optional spend intent max validity height, if not provided the client will infer it.
    pub max_block_height: Option<u64>,

    /// Optional salt, if not provided the client will generate one.
    pub salt: Option<FixedBytes32>,

    /// Optional salt, otherwise will be inferred.
    pub _phantoms: PhantomData<DN>,
}

impl<'a, S, DP, DN> EvmToEvmSpendRequest<'a, S, DP, DN>
where
    DP: Provider<DN>,
    S: alloy_signer::Signer + ?Sized,
    DN: Network,
{
    pub fn new(
        source_signer: &'a S,
        source_chain_id: TidesChainId,
        source_token: Address,
        source_token_amount: U256,
        destination_provider: &'a DP,
        destination_chain_id: TidesChainId,
        destination_recipient: Address,
    ) -> Self {
        Self {
            source_signer,
            source_chain_id,
            source_token,
            source_token_amount,
            source_max_fees: None,
            destination_provider,
            destination_chain_id,
            destination_recipient,
            destination_token: None,
            destination_callback: None,
            destination_caller: None,
            max_block_height: None,
            salt: None,
            _phantoms: PhantomData,
        }
    }

    pub fn with_destination_token(mut self, destination_token: Address) -> Self {
        self.destination_token = Some(destination_token);
        self
    }

    pub fn with_destination_callback(
        mut self,
        destination_callback: EvmDestinationCallback,
    ) -> Self {
        self.destination_callback = Some(destination_callback);
        self
    }

    /// Defines the chain's source fees.
    pub fn with_source_chain_max_fees(mut self, max_fees: U256) -> Self {
        self.source_max_fees = Some(max_fees);
        self
    }

    /// If provided, sets the spend intent's max validity height.
    pub fn with_max_block_height(mut self, max_block_height: u64) -> Self {
        self.max_block_height = Some(max_block_height);
        self
    }

    pub fn with_destination_caller(mut self, destination_caller: Address) -> Self {
        self.destination_caller = Some(destination_caller);
        self
    }

    pub fn with_salt(mut self, salt: impl Into<FixedBytes32>) -> Self {
        self.salt = Some(salt.into());
        self
    }
}

/// TODO: implement https://github.com/tides-protocol/tides-core/issues/72
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EvmDestinationCallback;

impl EvmDestinationCallback {
    pub(crate) fn to_vec(&self) -> Vec<u8> {
        Vec::new()
    }
}

#[derive(Debug, Clone)]
pub struct EvmToEvmSpendResponse<N: Network = Ethereum> {
    pub destination_token: Address,
    pub applied_fees: U256,
    pub max_block_height: u64,
    pub delivery_tx_receipt: N::ReceiptResponse,
}