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
//! Lighter L1 messages an owner signs with `personal_sign`, fill memos, and account owner
//! lookups on Lighter's public API.
//!
//! The texts are Lighter's (`server/src/lighter_sig`, checked against Lighter's Go vectors).

use alloy_primitives::{Address, hex};
use serde_json::Value;

use super::eip712;
use crate::types::FillId;

/// A taker's transfer moves USDC.
pub const USDC_ASSET: u8 = 3;
/// A taker's transfer moves between collateral routes.
pub const COLLATERAL_ROUTE: u8 = 0;

/// An internal transfer between Lighter accounts, as its owner message names it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Transfer {
    /// The API key's nonce.
    pub(crate) nonce: u64,
    /// The paying account's index.
    pub(crate) from: u64,
    /// The paying account's route ([`COLLATERAL_ROUTE`]).
    pub(crate) from_route: u8,
    /// The index of the API key sending it.
    pub(crate) api_key: u8,
    /// The paid account's index.
    pub(crate) to: u64,
    /// The paid account's route ([`COLLATERAL_ROUTE`]).
    pub(crate) to_route: u8,
    /// The asset ([`USDC_ASSET`]).
    pub(crate) asset: u8,
    /// USDC base units.
    pub(crate) amount: u64,
    /// Lighter's fee on top, USDC base units.
    pub(crate) fee: u64,
    /// The memo ([`fill_memo`] for a take's payment).
    pub(crate) memo: [u8; 32],
}

/// `0x` and sixteen hex digits, as Lighter writes integers in its messages.
fn word(value: impl Into<u64>) -> String {
    format!("{:#018x}", value.into())
}

/// The text an owner signs to authorize `transfer` on Lighter's chain `chain_id`.
pub(crate) fn transfer_message(transfer: &Transfer, chain_id: u64) -> String {
    format!(
        "Transfer\n\nnonce: {}\nfrom: {} (route {})\napi key: {}\nto: {} (route {})\nasset: {}\namount: {}\n\
         fee: {}\nchainId: {}\nmemo: {}\nOnly sign this message for a trusted client!",
        word(transfer.nonce),
        word(transfer.from),
        word(transfer.from_route),
        word(transfer.api_key),
        word(transfer.to),
        word(transfer.to_route),
        word(transfer.asset),
        word(transfer.amount),
        word(transfer.fee),
        word(chain_id),
        hex::encode(transfer.memo),
    )
}

/// The text an owner signs to register API key `api_key` with `public_key` on `account`.
pub fn registration_message(
    account: u64,
    api_key: u8,
    public_key: &[u8; 40],
    nonce: u64,
) -> String {
    format!(
        "Register Lighter Account\n\npubkey: 0x{}\nnonce: {}\naccount index: {}\napi key index: {}\n\
         Only sign this message for a trusted client!",
        hex::encode(public_key),
        word(nonce),
        word(account),
        word(api_key),
    )
}

/// The memo naming a fill: its id's 16 bytes, then 16 zero bytes.
pub fn fill_memo(fill: FillId) -> [u8; 32] {
    let mut memo = [0; 32];
    memo[..16].copy_from_slice(fill.as_uuid().as_bytes());
    memo
}

/// The path and query reading account `index` on Lighter's public API.
pub(crate) fn account_path(index: u64) -> String {
    format!("/api/v1/account?by=index&value={index}")
}

/// The owner of account `index` in Lighter's answer: exactly one account, with that index.
pub(crate) fn account_owner(answer: &Value, index: u64) -> Option<Address> {
    let [account] = answer.get("accounts")?.as_array()?.as_slice() else { return None };
    if account.get("index")?.as_u64()? != index {
        return None;
    }
    eip712::address_text(account.get("l1_address")?.as_str()?)
}

#[cfg(test)]
mod tests;