lamarge 0.8.0

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
Documentation
//! Withdrawal models: withdrawals, their history rows and states.

use alloy_primitives::Address;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::types::{Amount, MoveId, Revision, VenueId, WithdrawalId, open_enum};

open_enum! {
    /// A withdrawal's progress at its venue.
    pub enum WithdrawalState {
        /// Waiting to be submitted.
        Queued = "queued",
        /// Being submitted; becomes `unknown` after a minute.
        Submitting = "submitting",
        /// `unknown`: maybe submitted. Never resend it.
        Uncertain = "unknown",
        /// Acknowledged by the venue; arrival unconfirmed.
        Relayed = "relayed",
        /// Rejected by the venue, or never started in time.
        Refused = "refused",
    }
}

open_enum! {
    /// Whether funds leave the venue or move to another account on it.
    pub enum WithdrawalKind {
        /// Out of the venue.
        Withdrawal = "withdrawal",
        /// To another account on the same venue.
        Send = "send",
    }
}

open_enum! {
    /// A Hyperliquid balance other than the perps margin.
    pub enum Dex {
        /// A standard account's trade.xyz balance.
        Xyz = "xyz",
    }
}

/// A withdrawal or same-venue send.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Withdrawal {
    /// The withdrawal.
    pub id: WithdrawalId,
    /// The move it feeds a bridge for.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub move_id: Option<MoveId>,
    /// The venue it leaves.
    pub venue: VenueId,
    /// `Xyz` when it left a standard Hyperliquid account's trade.xyz balance.
    #[serde(deserialize_with = "super::required")]
    pub from_dex: Option<Dex>,
    /// Withdrawal or send.
    pub kind: WithdrawalKind,
    /// In `venue` base units; on Aster the venue fee included.
    pub amount: Amount,
    /// A send's recipient; a withdrawal's receiving address.
    pub destination: Address,
    /// Progress at the venue.
    pub state: WithdrawalState,
    /// The venue's withdrawal or transfer id once relayed (Polymarket, Aster).
    #[serde(deserialize_with = "super::required")]
    pub venue_ref: Option<String>,
    /// Why it failed.
    #[serde(deserialize_with = "super::required")]
    pub error: Option<String>,
    /// When it was prepared.
    pub created_at: DateTime<Utc>,
    /// When the venue acknowledged it.
    #[serde(deserialize_with = "super::required")]
    pub relayed_at: Option<DateTime<Utc>>,
}

/// A withdrawal as withdrawal history lists it, with its revision.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WithdrawalHistoryRow {
    /// The withdrawal.
    #[serde(flatten)]
    pub withdrawal: Withdrawal,
    /// Newer state has a higher revision.
    pub revision: Revision,
}

/// One chain an Aster withdrawal may go to, with its own limit (`GET /v1/withdrawals/chains`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WithdrawalChain {
    /// The chain id `POST /v1/withdrawals/prepare` takes as `chain_id`.
    pub chain_id: u64,
    /// The withdrawal's `destination Chain`: `BSC`, `Arbitrum` or `ETH`.
    pub name: String,
    /// What may leave the margin to this chain now, its fee included, in Aster base units.
    pub withdrawable: Amount,
    /// Aster's fee to this chain, taken out of the signed amount.
    pub fee: Amount,
}

/// The chains a venue's withdrawals may go to, read from the venue now.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WithdrawalChains {
    /// The venue (Aster).
    pub venue: VenueId,
    /// Each chain with its own limit and fee.
    pub chains: Vec<WithdrawalChain>,
}

#[cfg(test)]
mod tests;