lamarge 0.6.0

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
Documentation
//! Transfer reads, route quotes and cancellation.

use futures_core::Stream;

use super::{
    PageRequest, Transfers,
    page::{every, history},
};
use crate::{
    Result,
    model::{Destination, Funding, Page, Route, TransferIntent},
    transport::{Call, ops},
    types::{Amount, Bps, TransferId, VenueId},
};

/// What [`Transfers::quote`] prices: paying at most `amount_in` on `from` to receive on `to`.
///
/// Reserves nothing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RouteQuery {
    from: VenueId,
    to: VenueId,
    amount_in: Amount,
    min_out: Option<Amount>,
    max_fee_bps: Option<Bps>,
    funding: Option<Funding>,
    destination: Option<Destination>,
}

impl RouteQuery {
    /// Prices paying at most `amount_in` (in `from` base units) to receive on `to`.
    pub fn new(from: VenueId, to: VenueId, amount_in: Amount) -> Self {
        Self {
            from,
            to,
            amount_in,
            min_out: None,
            max_fee_bps: None,
            funding: None,
            destination: None,
        }
    }

    /// The least received for `amount_in`, in `to` base units: the worst price taken. A route
    /// under it shrinks to the largest amount that meets it.
    #[must_use]
    pub fn min_out(mut self, amount: Amount) -> Self {
        self.min_out = Some(amount);
        self
    }

    /// The highest taker fee the takes would accept; unset, legs pay no fee.
    #[must_use]
    pub fn max_fee_bps(mut self, bps: Bps) -> Self {
        self.max_fee_bps = Some(bps);
        self
    }

    /// Where the payment comes from; the server's default is
    /// [`Funding::Venue`](crate::model::Funding::Venue).
    #[must_use]
    pub fn funding(mut self, funding: Funding) -> Self {
        self.funding = Some(funding);
        self
    }

    /// Where the route delivers; the server's default is
    /// [`Destination::Venue`](crate::model::Destination::Venue).
    #[must_use]
    pub fn destination(mut self, destination: Destination) -> Self {
        self.destination = Some(destination);
        self
    }
}

impl Transfers<'_> {
    /// The takes that would pay now on `query.to` for `query.amount_in`; reserves nothing.
    pub async fn quote(&self, query: &RouteQuery) -> Result<Route> {
        let mut call = Call::new(&ops::GET_ROUTE)
            .query("from", &query.from)
            .query("to", &query.to)
            .query("amount_in", query.amount_in);
        if let Some(min_out) = query.min_out {
            call = call.query("min_out", min_out);
        }
        if let Some(bps) = query.max_fee_bps {
            call = call.query("max_fee_bps", bps.get());
        }
        if let Some(funding) = &query.funding {
            call = call.query("funding", funding);
        }
        if let Some(destination) = &query.destination {
            call = call.query("destination", destination);
        }
        self.inner.send(call, self.auth).await
    }

    /// One page of the session's transfers, newest first, cancelled and completed included.
    pub async fn history(&self, page: &PageRequest) -> Result<Page<TransferIntent>> {
        history(self.inner, self.auth, &ops::GET_TRANSFERS, page).await
    }

    /// Every transfer, newest first, reading pages of `page_size` (clamped to `1..=500`) as
    /// consumed. The stream owns what it needs, so it outlives the handle.
    pub fn history_stream(
        &self,
        page_size: u16,
    ) -> impl Stream<Item = Result<TransferIntent>> + Send + Unpin + 'static {
        every(self.inner, self.auth, PageRequest::first(page_size), || {
            Call::new(&ops::GET_TRANSFERS)
        })
    }

    /// A transfer and its linked operations.
    pub async fn get(&self, id: TransferId) -> Result<TransferIntent> {
        self.inner.send(Call::new(&ops::GET_TRANSFER).segment(id), self.auth).await
    }

    /// A transfer with its `funding_plan` computed from fresh balances and allowances, for a
    /// wallet review; not for polling.
    pub async fn get_with_funding_plan(&self, id: TransferId) -> Result<TransferIntent> {
        let call = Call::new(&ops::GET_TRANSFER).segment(id).query("funding_plan", true);
        self.inner.send(call, self.auth).await
    }

    /// Dismisses a transfer without a take, blocking its take and prepared withdrawal. Repeats
    /// succeed; refused with a take or a withdrawal in flight.
    pub async fn cancel(&self, id: TransferId) -> Result<()> {
        self.inner.send(Call::new(&ops::CANCEL_TRANSFER).segment(id), self.auth).await
    }
}

#[cfg(test)]
mod tests;