lamarge 0.2.0

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Identifiers, cursors and signatures.

use std::{fmt, str::FromStr};

use alloy_primitives::{B256, hex};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use uuid::Uuid;

const CLIENT_ID_MAX: usize = 64;

/// A caller-chosen idempotency key: 1 to 64 characters of `[A-Za-z0-9_.:-]`.
///
/// Persist it with the whole request it names before the first attempt and retry with both; the
/// server refuses its reuse with other parameters.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct ClientId(String);

impl ClientId {
    /// Validates `id`.
    pub fn new(id: impl Into<String>) -> Result<Self, InvalidClientId> {
        let id = id.into();
        let allowed = |b: u8| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'.' | b':' | b'-');
        if id.is_empty() || id.len() > CLIENT_ID_MAX || !id.bytes().all(allowed) {
            return Err(InvalidClientId);
        }
        Ok(Self(id))
    }

    /// A fresh time-ordered id (a UUID v7 as 32 hex digits).
    pub fn generate() -> Self {
        Self(Uuid::now_v7().simple().to_string())
    }

    /// The id.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for ClientId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl FromStr for ClientId {
    type Err = InvalidClientId;

    fn from_str(id: &str) -> Result<Self, Self::Err> {
        Self::new(id)
    }
}

impl<'de> Deserialize<'de> for ClientId {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Self::new(String::deserialize(deserializer)?).map_err(de::Error::custom)
    }
}

/// A text that is not 1 to 64 characters of `[A-Za-z0-9_.:-]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InvalidClientId;

impl fmt::Display for InvalidClientId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("client id must be 1 to 64 characters of [A-Za-z0-9_.:-]")
    }
}

impl std::error::Error for InvalidClientId {}

/// Declares a `u64` carried on the wire as a decimal string.
macro_rules! decimal_u64 {
    ($(#[$meta:meta])* $name:ident) => {
        $(#[$meta])*
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
        pub struct $name(u64);

        impl $name {
            /// Wraps `value`.
            pub const fn new(value: u64) -> Self {
                Self(value)
            }

            /// The value.
            pub const fn get(self) -> u64 {
                self.0
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                fmt::Display::fmt(&self.0, f)
            }
        }

        impl FromStr for $name {
            type Err = std::num::ParseIntError;

            fn from_str(text: &str) -> Result<Self, Self::Err> {
                text.parse().map(Self)
            }
        }

        impl Serialize for $name {
            fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                serializer.collect_str(&self.0)
            }
        }

        impl<'de> Deserialize<'de> for $name {
            fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                String::deserialize(deserializer)?.parse().map_err(de::Error::custom)
            }
        }
    };
}

decimal_u64! {
    /// A position in the event journal; keep it lossless (decimal string on the wire).
    Cursor
}

decimal_u64! {
    /// A resource's revision: newer state has a higher revision (decimal string on the wire).
    Revision
}

decimal_u64! {
    /// A block number (decimal string on the wire).
    BlockNumber
}

decimal_u64! {
    /// A `HyperCore` action nonce, in milliseconds (decimal string on the wire).
    HyperliquidNonce
}

/// A balance's portfolio change counter (`<count>:<microseconds>` on the wire): a later
/// observation compares greater.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PortfolioRevision {
    count: u64,
    micros: i64,
}

impl fmt::Display for PortfolioRevision {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.count, self.micros)
    }
}

impl FromStr for PortfolioRevision {
    type Err = std::num::ParseIntError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        let (count, micros) = text.split_once(':').unwrap_or((text, ""));
        Ok(Self { count: count.parse()?, micros: micros.parse()? })
    }
}

impl Serialize for PortfolioRevision {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.collect_str(self)
    }
}

impl<'de> Deserialize<'de> for PortfolioRevision {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        String::deserialize(deserializer)?.parse().map_err(de::Error::custom)
    }
}

/// An opaque history or transfer page cursor, passed back as received.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PageCursor(String);

impl PageCursor {
    /// A cursor from a saved `next_cursor`.
    pub fn new(cursor: impl Into<String>) -> Self {
        Self(cursor.into())
    }

    /// The cursor text.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for PageCursor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

/// Declares a resource id wrapping a UUID.
macro_rules! uuid_id {
    ($(#[$meta:meta])* $name:ident) => {
        $(#[$meta])*
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
        #[serde(transparent)]
        pub struct $name(Uuid);

        impl $name {
            /// Wraps `id`.
            pub const fn new(id: Uuid) -> Self {
                Self(id)
            }

            /// The UUID.
            pub const fn as_uuid(&self) -> &Uuid {
                &self.0
            }
        }

        impl From<Uuid> for $name {
            fn from(id: Uuid) -> Self {
                Self(id)
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                fmt::Display::fmt(&self.0, f)
            }
        }

        impl FromStr for $name {
            type Err = uuid::Error;

            fn from_str(text: &str) -> Result<Self, Self::Err> {
                text.parse().map(Self)
            }
        }
    };
}

uuid_id! {
    /// A transfer intent id.
    TransferId
}
uuid_id! {
    /// A take id.
    TakeId
}
uuid_id! {
    /// A fill id.
    FillId
}
uuid_id! {
    /// A sign-in session id.
    SessionId
}
uuid_id! {
    /// A maker preparation id.
    PreparationId
}
uuid_id! {
    /// A quote identity id (a maker credential's identity).
    QuoteIdentityId
}
uuid_id! {
    /// A close request id.
    CloseId
}
uuid_id! {
    /// A withdrawal id.
    WithdrawalId
}
uuid_id! {
    /// A move id.
    MoveId
}
uuid_id! {
    /// A bridge id.
    BridgeId
}

/// An order id (bytes32, `0x`-hex on the wire).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct OrderId(B256);

impl OrderId {
    /// Wraps `id`.
    pub const fn new(id: B256) -> Self {
        Self(id)
    }

    /// The 32 bytes.
    pub const fn as_b256(&self) -> &B256 {
        &self.0
    }
}

impl fmt::Display for OrderId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl FromStr for OrderId {
    type Err = hex::FromHexError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        text.parse().map(Self)
    }
}

/// A 65-byte signature `r ‖ s ‖ v` with `v` 27 or 28; `0x`-hex on the wire.
///
/// `Debug` does not show the bytes.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct SignatureBytes([u8; 65]);

impl SignatureBytes {
    /// Wraps `r ‖ s ‖ v`, normalizing `v` 0 or 1 to 27 or 28; `None` for any other `v`.
    pub const fn new(mut bytes: [u8; 65]) -> Option<Self> {
        match bytes[64] {
            0 | 1 => {
                bytes[64] += 27;
                Some(Self(bytes))
            }
            27 | 28 => Some(Self(bytes)),
            _ => None,
        }
    }

    /// The 65 bytes.
    pub const fn as_bytes(&self) -> &[u8; 65] {
        &self.0
    }
}

impl fmt::Debug for SignatureBytes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("SignatureBytes(****)")
    }
}

impl Serialize for SignatureBytes {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&hex::encode_prefixed(self.0))
    }
}

impl<'de> Deserialize<'de> for SignatureBytes {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let text = String::deserialize(deserializer)?;
        let digits = text.strip_prefix("0x").ok_or_else(|| de::Error::custom("missing 0x"))?;
        let bytes = hex::decode_to_array::<_, 65>(digits).map_err(de::Error::custom)?;
        Self::new(bytes).ok_or_else(|| de::Error::custom("signature v must be 0, 1, 27 or 28"))
    }
}

/// A fill's claim secret: whoever holds it can release the payment to the maker before `t2`.
///
/// `Debug` does not show it; [`expose_secret`](Self::expose_secret) gives the bytes.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ClaimSecret(B256);

impl ClaimSecret {
    /// The 32 bytes.
    pub const fn expose_secret(&self) -> &B256 {
        &self.0
    }
}

impl fmt::Debug for ClaimSecret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("ClaimSecret(****)")
    }
}

#[cfg(test)]
mod tests {
    use proptest::prelude::*;

    use super::*;

    #[test]
    fn client_id_charset_and_length() {
        assert!(ClientId::new("take-1:retry_2.a").is_ok());
        assert!(ClientId::new("a".repeat(64)).is_ok());
        assert_eq!(ClientId::new("a".repeat(65)), Err(InvalidClientId));
        assert_eq!(ClientId::new(""), Err(InvalidClientId));
        for bad in ["a b", "a/b", "é", "a\n", "a#b"] {
            assert_eq!(ClientId::new(bad), Err(InvalidClientId), "{bad:?}");
        }
        assert!(serde_json::from_str::<ClientId>("\"a/b\"").is_err());
        assert_eq!(serde_json::from_str::<ClientId>("\"ok\"").unwrap().as_str(), "ok");
    }

    #[test]
    fn generated_client_ids_are_valid_and_distinct() {
        let (a, b) = (ClientId::generate(), ClientId::generate());
        assert_ne!(a, b);
        assert_eq!(a.as_str().len(), 32);
        assert_eq!(ClientId::new(a.as_str()).unwrap(), a);
    }

    #[test]
    fn cursors_are_lossless_decimal_strings() {
        let cursor = Cursor::new(u64::MAX);
        let json = serde_json::to_string(&cursor).unwrap();
        assert_eq!(json, "\"18446744073709551615\"");
        assert_eq!(serde_json::from_str::<Cursor>(&json).unwrap(), cursor);
        assert!(serde_json::from_str::<Cursor>("7").is_err(), "a JSON number loses precision");
        assert!("-7".parse::<Revision>().is_err());
        assert!(Revision::new(2) > Revision::new(1));
    }

    #[test]
    fn portfolio_revisions_order_by_count_then_time() {
        let parse = |text: &str| text.parse::<PortfolioRevision>().unwrap();
        assert_eq!(parse("12:1731585600000000").to_string(), "12:1731585600000000");
        assert!(parse("12:5") > parse("12:4"));
        assert!(parse("13:0") > parse("12:9"));
        for bad in ["12", "12:", ":5", "a:1", "1:2:3"] {
            assert!(bad.parse::<PortfolioRevision>().is_err(), "{bad:?}");
        }
    }

    #[test]
    fn uuid_ids_are_transparent() {
        let text = "0192f1c2-3d4e-7f00-8a1b-2c3d4e5f6a7b";
        let id: TransferId = serde_json::from_str(&format!("\"{text}\"")).unwrap();
        assert_eq!(id.to_string(), text);
        assert_eq!(serde_json::to_string(&id).unwrap(), format!("\"{text}\""));
        assert!(serde_json::from_str::<TakeId>("\"not-a-uuid\"").is_err());
    }

    #[test]
    fn order_ids_are_prefixed_hex() {
        let text = format!("0x{}", "ab".repeat(32));
        let id: OrderId = text.parse().unwrap();
        assert_eq!(id.to_string(), text);
        assert_eq!(serde_json::to_string(&id).unwrap(), format!("\"{text}\""));
        assert!("0xabcd".parse::<OrderId>().is_err());
    }

    fn signature(v: u8) -> [u8; 65] {
        let mut bytes = [0x11; 65];
        bytes[64] = v;
        bytes
    }

    #[test]
    fn signature_v_is_normalized_and_bounded() {
        assert_eq!(SignatureBytes::new(signature(0)).unwrap().as_bytes()[64], 27);
        assert_eq!(SignatureBytes::new(signature(1)).unwrap().as_bytes()[64], 28);
        assert_eq!(SignatureBytes::new(signature(28)).unwrap().as_bytes()[64], 28);
        assert!(SignatureBytes::new(signature(2)).is_none());
        assert!(SignatureBytes::new(signature(29)).is_none());
    }

    #[test]
    fn signature_wire_form_and_redacted_debug() {
        let signature = SignatureBytes::new(signature(27)).unwrap();
        let json = serde_json::to_string(&signature).unwrap();
        assert_eq!(json, format!("\"0x{}1b\"", "11".repeat(64)));
        assert_eq!(serde_json::from_str::<SignatureBytes>(&json).unwrap(), signature);
        assert!(!format!("{signature:?}").contains("1111"));
        assert!(
            serde_json::from_str::<SignatureBytes>(&format!("\"{}1b\"", "11".repeat(64))).is_err()
        );
        assert!(
            serde_json::from_str::<SignatureBytes>(&format!("\"0x{}\"", "11".repeat(64))).is_err()
        );
    }

    proptest! {
        #[test]
        fn client_id_accepts_exactly_its_pattern(id in "[A-Za-z0-9_.:-]{1,64}") {
            prop_assert!(ClientId::new(id).is_ok());
        }

        #[test]
        fn client_id_refuses_other_characters(prefix in "[a-z]{0,10}", bad in "[^A-Za-z0-9_.:-]") {
            let id = format!("{prefix}{bad}");
            prop_assert!(ClientId::new(id).is_err());
        }

        #[test]
        fn revision_text_round_trips(value: u64) {
            prop_assert_eq!(Revision::new(value).to_string().parse::<Revision>().unwrap().get(), value);
        }
    }
}