lamarge 0.5.1

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
Documentation
use alloy_primitives::{b256, hex};
use serde_json::json;

use super::*;

const ARGS: [&str; 4] = [
    "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
    "0xC011a7E12a19f7B1f670d46F03B03f3342E82DFB",
    "250.5",
    "0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc",
];

/// `@msgpack/msgpack` 3.1.2's encoding, as Polymarket's docs use it
/// (`server/src/polymarket.rs`).
#[test]
fn msgpack_is_polymarkets_reference_encoding() {
    assert_eq!(
        hex::encode(msgpack(INTERNAL_TRANSFER, &ARGS).unwrap()),
        "92b0696e7465726e616c5472616e7366657294d92a307866333946643665353161616438384636463463653661423838323732373963666646623932323636d92a307843303131613745313261313966374231663637306434364630334230336633333432453832444642a53235302e35d92a307839393635353037443161353562634332363935433538626131364642333764383139423041346463"
    );
}

/// `sdk-ts/src/send.test.ts`: Polymarket's encoder and viem give this hash; the server pins it.
#[test]
fn the_operation_hash_is_polymarkets() {
    let expected = b256!("116bb5d03ed73e04edd7c0ebfd0f9c37d9cf5dd00de629f8949cbdfc63e11825");
    assert_eq!(op_hash(INTERNAL_TRANSFER, &ARGS), Some(expected));
    // The exact strings are hashed: case and decimal form matter.
    let lower = ARGS[0].to_lowercase();
    assert_ne!(op_hash(INTERNAL_TRANSFER, &[&lower, ARGS[1], ARGS[2], ARGS[3]]), Some(expected));
    assert_ne!(op_hash(INTERNAL_TRANSFER, &[ARGS[0], ARGS[1], "250.50", ARGS[3]]), Some(expected));
}

#[test]
fn msgpack_refuses_long_strings_and_many_arguments() {
    let long = "a".repeat(256);
    assert!(msgpack(INTERNAL_TRANSFER, &[long.as_str()]).is_none());
    let longest = "a".repeat(255);
    let encoded = msgpack("x", &[longest.as_str()]).unwrap();
    assert_eq!(&encoded[..5], &[0x92, 0xa1, b'x', 0x91, 0xd9]);
    assert!(msgpack("x", &["a"; 16]).is_none());
    assert_eq!(msgpack("x", &["a"; 15]).unwrap()[3], 0x9f);
}

#[test]
fn an_op_is_read_only_as_an_internal_transfer() {
    let op = json!({"type": "internalTransfer", "args": {
        "account": ARGS[0], "token": ARGS[1], "amount": ARGS[2], "to": ARGS[3]}});
    let transfer = InternalTransfer::read(&op).unwrap();
    assert_eq!(transfer.amount, "250.5");
    assert_eq!(
        transfer.hash(),
        Some(b256!("116bb5d03ed73e04edd7c0ebfd0f9c37d9cf5dd00de629f8949cbdfc63e11825"))
    );
    let mut withdraw = op.clone();
    withdraw["type"] = json!("withdraw");
    let mut extra_arg = op.clone();
    extra_arg["args"]["memo"] = json!("x");
    let mut extra_key = op.clone();
    extra_key["note"] = json!("x");
    let mut number = op.clone();
    number["args"]["amount"] = json!(250.5);
    for refused in
        [withdraw, extra_arg, extra_key, number, json!([]), json!({"type": "internalTransfer"})]
    {
        assert_eq!(InternalTransfer::read(&refused), None, "{refused}");
    }
}