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
//! Multi-step operations run by a `Session`: takes, transfers, withdrawals, venue setup and maker
//! capital, with their request types. Each checks every server payload before signing. A signature
//! whose second copy could move funds or an allowance twice (takes, transfers, withdrawals, maker
//! capital, wallet permits) is journalled before it is submitted and reused for a matching
//! request. Venue setup steps (setup permits included), closes and quote publications are not
//! journalled and may sign again after an interruption: a second copy has no further effect.

use std::{future::Future, time::Duration};

use alloy_primitives::{Address, B256, keccak256};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::time::Instant;
use uuid::Uuid;

use crate::{
    Result, Session, deployments,
    model::{Balance, ToSignKind},
    protocol::{
        checks::details::{self, changed, unsupported},
        lighter,
        signing::Verified,
    },
    transport::Inner,
    types::{SignatureBytes, VenueId},
};

mod bridge;
mod conversion;
mod direct;
mod maker;
mod move_funds;
mod quoter;
pub(crate) mod records;
mod refund;
mod route;
mod setup;
mod take;
mod transfer;
mod withdraw;

pub use bridge::*;
pub use conversion::{
    ConversionPreview, ConversionTerms, POLYGON_FUNDING_CONVERTER, POLYGON_USDC,
    check_conversion_capability, conversion_minimum,
};
pub use direct::{
    DirectInspection, DirectOutcome, DirectStep, DirectWait, check_direct_amounts,
    inspect_direct_transaction,
};
pub use maker::*;
pub use move_funds::{MoveFundsRequest, MoveOutcome, MovePlan, MoveWait};
pub use quoter::*;
pub use refund::check_escrow_locks;
pub use route::{RouteLeg, RouteNotCommitted, RouteOutcome, RouteRequest, groupable};
pub use take::*;
pub use transfer::*;
pub use withdraw::*;

/// The wait between reads of something the server is still doing.
const POLL: Duration = if cfg!(test) { Duration::from_millis(1) } else { Duration::from_secs(2) };

/// One signature as the signature endpoints take it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct SignedItem {
    kind: ToSignKind,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    fill_id: Option<Uuid>,
    signature: SignatureBytes,
}

/// A signature endpoint's body.
#[derive(Serialize)]
struct Signatures<'a> {
    signatures: &'a [SignedItem],
}

/// Signatures saved before they are submitted, with the digests of the requests they sign and of
/// the context they were checked in: submitted again only for the same requests and context.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct SignedRecord {
    digests: Vec<B256>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    context: Option<B256>,
    items: Vec<SignedItem>,
}

impl SignedRecord {
    /// The saved signatures, if they sign `digests` in `context`; another record is a change.
    fn reuse(self, digests: &[B256], context: Option<B256>) -> Result<Vec<SignedItem>> {
        if self.digests == digests && self.context == context {
            tracing::info!("submitting the saved signatures");
            Ok(self.items)
        } else {
            Err(changed(details::DIGEST).into())
        }
    }
}

impl Session {
    /// Signs `verified` with the session's key at the client's clock.
    async fn sign(&self, verified: &Verified) -> Result<SignatureBytes> {
        self.key().sign(verified, &self.transport().clock).await
    }
}

/// What identifies each signing request: the keccak-256 of its canonical JSON, without LaMarge's
/// attestation.
fn request_digests<T: Serialize>(to_sign: &[T]) -> Vec<B256> {
    to_sign
        .iter()
        .map(|item| {
            let mut value = serde_json::to_value(item).unwrap_or(Value::Null);
            if let Value::Object(fields) = &mut value {
                fields.remove("attestation");
            }
            canonical_digest(&value)
        })
        .collect()
}

/// The keccak-256 of `value`'s canonical JSON (keys sorted).
fn canonical_digest(value: &Value) -> B256 {
    let mut bytes = Vec::new();
    canonical(value, &mut bytes);
    keccak256(bytes)
}

fn canonical(value: &Value, out: &mut Vec<u8>) {
    match value {
        Value::Object(fields) => {
            let mut names: Vec<&String> = fields.keys().collect();
            names.sort();
            out.push(b'{');
            for (n, name) in names.into_iter().enumerate() {
                if n > 0 {
                    out.push(b',');
                }
                canonical(&Value::String(name.clone()), out);
                out.push(b':');
                canonical(&fields[name], out);
            }
            out.push(b'}');
        }
        Value::Array(items) => {
            out.push(b'[');
            for (n, item) in items.iter().enumerate() {
                if n > 0 {
                    out.push(b',');
                }
                canonical(item, out);
            }
            out.push(b']');
        }
        scalar => out.extend_from_slice(scalar.to_string().as_bytes()),
    }
}

/// Reads with `read` every `interval` until it answers `Some`; `None` once `deadline` passed.
async fn poll_until<T, F>(
    deadline: Instant,
    interval: Duration,
    mut read: impl FnMut() -> F,
) -> Result<Option<T>>
where
    F: Future<Output = Result<Option<T>>>,
{
    loop {
        if let Some(value) = read().await? {
            return Ok(Some(value));
        }
        if Instant::now() >= deadline {
            return Ok(None);
        }
        tokio::time::sleep(interval).await;
    }
}

/// A venue this release pins; anything else is refused before any request.
fn supported(venue: &VenueId) -> Result<()> {
    match deployments::chain(venue) {
        Some(_) => Ok(()),
        None => Err(unsupported(details::VENUE).into()),
    }
}

/// The balance row of the account `address` has linked on `venue`; any other is a change.
fn linked<'a>(balances: &'a [Balance], venue: &VenueId, address: Address) -> Result<&'a Balance> {
    balances
        .iter()
        .find(|row| row.venue == *venue)
        .filter(|row| row.address == address)
        .ok_or_else(|| changed(details::LINKED_ACCOUNT).into())
}

/// The owner Lighter's public API reports for account `index`, if it answers for it.
pub(crate) async fn lighter_owner(transport: &Inner, index: u64) -> Result<Option<Address>> {
    let answer: Value = transport.lighter_get(&lighter::account_path(index)).await?;
    Ok(lighter::account_owner(&answer, index))
}

#[cfg(test)]
mod tests;