lamarge 0.9.0

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
Documentation
//! Bridges: the external part of a move, quoted by the server, checked and signed or sent by
//! the caller ([`Session::sign_bridge`](crate::Session::sign_bridge),
//! [`Session::bridge_deposit`](crate::Session::bridge_deposit)).

use alloy_primitives::{Address, B256};
use serde::{Deserialize, Serialize};

use super::Bridges;
use crate::{
    Error, Result,
    error::{ErrorCode, Reason},
    model::{Bridge, BridgeFunding},
    transport::{Call, ops},
    types::{Amount, BridgeId, ClientId, MoveId},
};

/// `POST /v1/moves/{id}/bridges`: `amount_in` of a move through the external bridge.
///
/// Persist it before the first call: the same `client_bridge_id` and terms return the bridge.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BridgeRequest {
    client_bridge_id: ClientId,
    amount_in: Amount,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    from_wallet: Option<WalletSource>,
}

/// A wallet token a bridge starts from instead of the move's origin.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct WalletSource {
    chain_id: u64,
    token: Address,
}

impl BridgeRequest {
    /// A bridge named `id` of `amount_in`, in the origin token's base units, from the move's
    /// origin.
    pub fn new(id: ClientId, amount_in: Amount) -> Self {
        Self { client_bridge_id: id, amount_in, from_wallet: None }
    }

    /// From the payer's `token` wallet on the origin chain `chain_id` instead (after a
    /// withdrawal there).
    #[must_use]
    pub fn from_wallet(mut self, chain_id: u64, token: Address) -> Self {
        self.from_wallet = Some(WalletSource { chain_id, token });
        self
    }
}

#[derive(Serialize)]
struct Report {
    #[serde(skip_serializing_if = "Option::is_none")]
    tx_hash: Option<B256>,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    unsent: bool,
}

impl Bridges<'_> {
    /// Quotes the bridge and returns its order, unchecked: check it before signing or sending
    /// anything. The move's live bridges stay within its `amount_in`.
    pub async fn create(&self, move_id: MoveId, request: &BridgeRequest) -> Result<Bridge> {
        let call = Call::new(&ops::CREATE_BRIDGE).segment(move_id).json(request)?;
        self.inner.send(call, self.auth).await
    }

    /// A bridge.
    pub async fn get(&self, id: BridgeId) -> Result<Bridge> {
        self.inner.send(Call::new(&ops::GET_BRIDGE).segment(id), self.auth).await
    }

    /// A wallet-origin bridge's balance and allowance toward the depository, read fresh on the
    /// origin chain; refused for a `HyperCore` bridge.
    pub async fn funding(&self, id: BridgeId) -> Result<BridgeFunding> {
        self.inner.send(Call::new(&ops::GET_BRIDGE_FUNDING).segment(id), self.auth).await
    }

    /// Fences a wallet-origin bridge before the deposit prompt; sent once, never retried. After
    /// a lost answer, never prompt again.
    ///
    /// # Errors
    ///
    /// [`Error::DepositFenced`] when another client (or an earlier call) fenced it first: do not
    /// send the deposit.
    pub async fn fence(&self, id: BridgeId) -> Result<Bridge> {
        let body = Report { tx_hash: None, unsent: false };
        let call = Call::new(&ops::FENCE_BRIDGE).segment(id).json(&body)?;
        self.inner.send(call, self.auth).await.map_err(|error| {
            let fenced = error.api().is_some_and(|api| {
                *api.code() == ErrorCode::StateConflict && api.reason() == Some(&Reason::Fenced)
            });
            if fenced { Error::DepositFenced(id) } else { error }
        })
    }

    /// Reports a broadcast deposit, each replacement too (eight at most).
    pub async fn report_transaction(&self, id: BridgeId, tx_hash: B256) -> Result<Bridge> {
        self.report(id, &Report { tx_hash: Some(tx_hash), unsent: false }).await
    }

    /// States that the wallet refused before broadcasting, while no hash is reported: the
    /// bridge fails.
    pub async fn report_unsent(&self, id: BridgeId) -> Result<Bridge> {
        self.report(id, &Report { tx_hash: None, unsent: true }).await
    }

    async fn report(&self, id: BridgeId, body: &Report) -> Result<Bridge> {
        let call = Call::new(&ops::REPORT_BRIDGE_TRANSACTION).segment(id).json(body)?;
        self.inner.send(call, self.auth).await
    }
}

#[cfg(test)]
mod tests;