lamarge 0.7.0

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
Documentation
//! Deployment and venue registry checks: every chain the API lists is one this release pins.

use super::details::{self, deployment, unsupported};
use crate::{
    deployments,
    error::VerificationError,
    model::{EscrowChain, Settlement, Venue},
    types::{EscrowEnvironment, VenueId},
};

/// Checks `chain`, as the API lists it for `venue`, against the pins: chain id, token, decimals,
/// permit domain, wallet token and escrow (any environment's). Returns the environment of an
/// escrow venue's escrow; `None` for a venue settled inside itself, whose listed escrow is its
/// signing domain's contract.
pub fn check_registry(
    venue: &VenueId,
    chain: &EscrowChain,
) -> Result<Option<EscrowEnvironment>, VerificationError> {
    let pinned = deployments::chain(venue).ok_or(unsupported(details::VENUE))?;
    let permit = match (&chain.permit, pinned.permit) {
        (None, None) => true,
        (Some(listed), Some(permit)) => {
            listed.name == permit.name && listed.version == permit.version
        }
        _ => false,
    };
    let wallet = chain.wallet.as_ref().is_none_or(|wallet| {
        Some(wallet.token) == pinned.wallet_token && wallet.decimals == pinned.decimals
    });
    if chain.id != pinned.id
        || chain.token != pinned.token
        || chain.decimals != pinned.decimals
        || !permit
        || !wallet
    {
        return Err(deployment(details::REGISTRY));
    }
    match deployments::venue_contract(venue) {
        Some(contract) if chain.escrow == contract => Ok(None),
        Some(_) => Err(deployment(details::ESCROW)),
        None => deployments::escrow_environment(chain.id, chain.escrow)
            .map(Some)
            .ok_or(deployment(details::ESCROW)),
    }
}

/// Checks the venues the API lists (`GET /v1/venues`) and returns the environment their escrows
/// belong to. Each pinned venue lists a pinned chain, settled as pinned, unless it is disabled
/// and lists none; a venue this release does not know is skipped (nothing here signs for it).
/// Every escrow belongs to one environment, `named` when the client names one, and at least one
/// escrow venue is enabled.
pub(crate) fn check_deployment(
    venues: &[Venue],
    named: Option<EscrowEnvironment>,
) -> Result<EscrowEnvironment, VerificationError> {
    let mut environment = None;
    let mut enabled_escrow = false;
    for venue in venues {
        if deployments::chain(&venue.id).is_none() {
            continue;
        }
        let Some(chain) = &venue.chain else {
            if venue.enabled {
                return Err(deployment(details::REGISTRY));
            }
            continue;
        };
        let escrow = check_registry(&venue.id, chain)?;
        let settlement = if escrow.is_some() { Settlement::Escrow } else { Settlement::Venue };
        if venue.settlement.as_ref() != Some(&settlement) {
            return Err(deployment(details::SETTLEMENT));
        }
        if let Some(escrow) = escrow {
            if environment.is_some_and(|known| known != escrow) {
                return Err(deployment(details::ENVIRONMENT));
            }
            environment = Some(escrow);
            enabled_escrow |= venue.enabled;
        }
    }
    let environment =
        environment.filter(|_| enabled_escrow).ok_or(deployment(details::NO_ESCROW_VENUE))?;
    if named.is_some_and(|named| named != environment) {
        return Err(deployment(details::ENVIRONMENT));
    }
    Ok(environment)
}

#[cfg(test)]
mod tests;