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
use alloy_primitives::address;
use serde_json::json;

use super::*;

/// Lighter's Go libraries generate these (`scripts/lighter-vectors`); the server's port and
/// the TypeScript SDK check them too.
const VECTORS: &str =
    include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/vectors/lighter.json"));

fn vectors() -> Value {
    serde_json::from_str(VECTORS).unwrap()
}

fn number(case: &Value, name: &str) -> u64 {
    case[name].as_u64().unwrap()
}

fn small(case: &Value, name: &str) -> u8 {
    u8::try_from(number(case, name)).unwrap()
}

#[test]
fn transfer_messages_are_lighters() {
    let cases = vectors()["transfers"].as_array().unwrap().clone();
    assert!(!cases.is_empty());
    for case in cases {
        let transfer = Transfer {
            nonce: number(&case, "nonce"),
            from: number(&case, "from"),
            from_route: small(&case, "from_route"),
            api_key: small(&case, "api_key"),
            to: number(&case, "to"),
            to_route: small(&case, "to_route"),
            asset: small(&case, "asset"),
            amount: number(&case, "amount"),
            fee: number(&case, "fee"),
            memo: hex::decode_to_array(case["memo"].as_str().unwrap()).unwrap(),
        };
        let chain_id = number(&case, "chain_id");
        assert_eq!(transfer_message(&transfer, chain_id), case["l1_body"].as_str().unwrap());
        let more = Transfer { amount: transfer.amount + 1, ..transfer };
        assert_ne!(transfer_message(&more, chain_id), case["l1_body"].as_str().unwrap());
    }
}

#[test]
fn registration_messages_are_lighters() {
    let cases = vectors()["change_pub_keys"].as_array().unwrap().clone();
    assert!(!cases.is_empty());
    for case in cases {
        let public_key: [u8; 40] = hex::decode_to_array(case["pub_key"].as_str().unwrap()).unwrap();
        assert_eq!(
            registration_message(
                number(&case, "account"),
                small(&case, "api_key"),
                &public_key,
                number(&case, "nonce"),
            ),
            case["l1_body"].as_str().unwrap()
        );
    }
}

#[test]
fn a_fill_memo_is_its_id_then_zeros() {
    let fill: FillId = "00010203-0405-0607-0809-0a0b0c0d0e0f".parse().unwrap();
    assert_eq!(
        hex::encode(fill_memo(fill)),
        format!("000102030405060708090a0b0c0d0e0f{}", "00".repeat(16))
    );
}

#[test]
fn an_owner_is_read_for_that_index_only() {
    let owner = address!("00000000000000000000000000000000000000aa");
    let answer =
        |index: u64| json!({"code": 200, "accounts": [{"index": index, "l1_address": owner}]});
    assert_eq!(account_owner(&answer(7), 7), Some(owner));
    assert_eq!(account_owner(&answer(8), 7), None);
    assert_eq!(account_owner(&json!({"code": 21100}), 7), None);
    let two =
        json!({"accounts": [{"index": 7, "l1_address": owner}, {"index": 7, "l1_address": owner}]});
    assert_eq!(account_owner(&two, 7), None);
    let unreadable = json!({"accounts": [{"index": 7, "l1_address": "0x00aa"}]});
    assert_eq!(account_owner(&unreadable, 7), None);
    assert_eq!(account_path(7), "/api/v1/account?by=index&value=7");
}