lighter-sdk 0.1.1

Rust SDK for interacting with the Lighter exchange over REST, WebSocket, and signer-backed transaction flows.
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use std::collections::HashMap;

use crate::models::order::Order;
use crate::models::order_book::PriceLevel;
use crate::models::ws::{
    WsAccountAllAssetsUpdate, WsAccountAllPositionsUpdate, WsAccountAllTradesUpdate,
    WsAccountAllUpdate, WsAccountSpotAvgEntryPricesUpdate, WsMarketStatsUpdate, WsOrderBook,
    WsOrderBookState, WsUserStatsUpdate,
};

/// Manages local order book state from WebSocket delta updates.
#[derive(Debug, Default)]
pub struct OrderBookHandler {
    states: HashMap<i64, WsOrderBookState>,
}

#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum OrderBookUpdateError {
    #[error("market {market_id} received an order book delta before a snapshot")]
    MissingSnapshot { market_id: i64 },
    #[error(
        "market {market_id} order book nonce gap: expected begin_nonce {expected_begin_nonce}, got {actual_begin_nonce} (previous nonce {previous_nonce})"
    )]
    NonContiguousNonce {
        market_id: i64,
        expected_begin_nonce: i64,
        actual_begin_nonce: i64,
        previous_nonce: i64,
    },
}

impl OrderBookHandler {
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
        }
    }

    /// Initialize or replace the full order book snapshot for a market.
    pub fn set_snapshot(&mut self, market_id: i64, state: WsOrderBookState) {
        self.states.insert(market_id, state);
    }

    /// Apply a delta update to the local order book state.
    /// Returns the updated state if it exists.
    ///
    /// After applying deltas, bids are sorted descending and asks ascending
    /// by price so that `.first()` always returns the best level.
    pub fn apply_update(
        &mut self,
        market_id: i64,
        update: &WsOrderBook,
    ) -> Result<&WsOrderBookState, OrderBookUpdateError> {
        let state = self
            .states
            .get_mut(&market_id)
            .ok_or(OrderBookUpdateError::MissingSnapshot { market_id })?;
        if update.begin_nonce != state.nonce {
            return Err(OrderBookUpdateError::NonContiguousNonce {
                market_id,
                expected_begin_nonce: state.nonce,
                actual_begin_nonce: update.begin_nonce,
                previous_nonce: state.nonce,
            });
        }
        apply_price_level_deltas(&mut state.asks, &update.asks);
        apply_price_level_deltas(&mut state.bids, &update.bids);
        sort_price_levels_ascending(&mut state.asks);
        sort_price_levels_descending(&mut state.bids);
        state.nonce = update.nonce;
        Ok(state)
    }

    /// Get the current order book state for a market.
    pub fn get(&self, market_id: i64) -> Option<&WsOrderBookState> {
        self.states.get(&market_id)
    }
}

/// Apply delta price levels to an existing price level list.
/// If a delta has size 0.0, the matching price level is removed.
/// If the price already exists, its size is updated.
/// If the price is new, the level is appended.
fn apply_price_level_deltas(existing: &mut Vec<PriceLevel>, deltas: &[PriceLevel]) {
    for delta in deltas {
        let delta_price = match delta.price_f64() {
            Some(p) => p,
            None => continue,
        };

        let is_removal = delta.size_f64().map(|v| v == 0.0).unwrap_or(false);

        if let Some(pos) = existing
            .iter()
            .position(|l| l.price_f64() == Some(delta_price))
        {
            if is_removal {
                existing.remove(pos);
            } else {
                existing[pos].size.clone_from(&delta.size);
            }
        } else if !is_removal {
            existing.push(delta.clone());
        }
    }
}

fn parse_price(level: &PriceLevel) -> f64 {
    level.price_f64().unwrap_or(0.0)
}

/// Sort asks ascending (best/lowest ask first).
fn sort_price_levels_ascending(levels: &mut [PriceLevel]) {
    levels.sort_by(|a, b| {
        parse_price(a)
            .partial_cmp(&parse_price(b))
            .unwrap_or(std::cmp::Ordering::Equal)
    });
}

/// Sort bids descending (best/highest bid first).
fn sort_price_levels_descending(levels: &mut [PriceLevel]) {
    levels.sort_by(|a, b| {
        parse_price(b)
            .partial_cmp(&parse_price(a))
            .unwrap_or(std::cmp::Ordering::Equal)
    });
}

/// Manages local account state from WebSocket updates.
#[derive(Debug, Default)]
pub struct AccountHandler {
    states: HashMap<i64, WsAccountAllUpdate>,
}

impl AccountHandler {
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
        }
    }

    /// Set or replace the full account state.
    pub fn set_state(&mut self, account_id: i64, state: WsAccountAllUpdate) {
        self.states.insert(account_id, state);
    }

    /// Get the current account state.
    pub fn get(&self, account_id: i64) -> Option<&WsAccountAllUpdate> {
        self.states.get(&account_id)
    }
}

/// Manages local account orders state from WebSocket updates.
#[derive(Debug, Default)]
pub struct AccountOrdersHandler {
    /// Legacy/global view: market_id -> orders.
    states_by_market: HashMap<i64, Vec<Order>>,
    /// Precise view: (account_id, market_id) -> orders.
    states_by_account_market: HashMap<(i64, i64), Vec<Order>>,
}

impl AccountOrdersHandler {
    pub fn new() -> Self {
        Self {
            states_by_market: HashMap::new(),
            states_by_account_market: HashMap::new(),
        }
    }

    /// Set or replace the orders for a market.
    pub fn set_orders(&mut self, market_id: i64, orders: Vec<Order>) {
        self.states_by_market.insert(market_id, orders);
    }

    /// Set or replace the orders for an (account, market) pair.
    pub fn set_orders_for_account(&mut self, account_id: i64, market_id: i64, orders: Vec<Order>) {
        self.states_by_account_market
            .insert((account_id, market_id), orders);
    }

    /// Get the current orders for a market.
    pub fn get(&self, market_id: i64) -> Option<&Vec<Order>> {
        self.states_by_market.get(&market_id)
    }

    /// Get the current orders for an (account, market) pair.
    pub fn get_for_account(&self, account_id: i64, market_id: i64) -> Option<&Vec<Order>> {
        self.states_by_account_market.get(&(account_id, market_id))
    }
}

/// Manages local account-all-positions state from WebSocket updates.
#[derive(Debug, Default)]
pub struct AccountAllPositionsHandler {
    /// account_id -> latest account_all_positions payload
    states: HashMap<i64, WsAccountAllPositionsUpdate>,
}

impl AccountAllPositionsHandler {
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
        }
    }

    /// Set or replace the latest account_all_positions payload.
    pub fn set_state(&mut self, account_id: i64, state: WsAccountAllPositionsUpdate) {
        self.states.insert(account_id, state);
    }

    /// Get the latest account_all_positions payload for an account.
    pub fn get(&self, account_id: i64) -> Option<&WsAccountAllPositionsUpdate> {
        self.states.get(&account_id)
    }
}

/// Manages local account-all-assets state from WebSocket updates.
#[derive(Debug, Default)]
pub struct AccountAllAssetsHandler {
    /// account_id -> latest account_all_assets payload
    states: HashMap<i64, WsAccountAllAssetsUpdate>,
}

impl AccountAllAssetsHandler {
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
        }
    }

    /// Set or replace the latest account_all_assets payload.
    pub fn set_state(&mut self, account_id: i64, state: WsAccountAllAssetsUpdate) {
        self.states.insert(account_id, state);
    }

    /// Get the latest account_all_assets payload for an account.
    pub fn get(&self, account_id: i64) -> Option<&WsAccountAllAssetsUpdate> {
        self.states.get(&account_id)
    }
}

/// Manages local account-spot-avg-entry-prices state from WebSocket updates.
#[derive(Debug, Default)]
pub struct AccountSpotAvgEntryPricesHandler {
    /// account_id -> latest account_spot_avg_entry_prices payload
    states: HashMap<i64, WsAccountSpotAvgEntryPricesUpdate>,
}

impl AccountSpotAvgEntryPricesHandler {
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
        }
    }

    /// Set or replace the latest account_spot_avg_entry_prices payload.
    pub fn set_state(&mut self, account_id: i64, state: WsAccountSpotAvgEntryPricesUpdate) {
        self.states.insert(account_id, state);
    }

    /// Get the latest account_spot_avg_entry_prices payload for an account.
    pub fn get(&self, account_id: i64) -> Option<&WsAccountSpotAvgEntryPricesUpdate> {
        self.states.get(&account_id)
    }
}

/// Manages local account-all-trades state from WebSocket updates.
#[derive(Debug, Default)]
pub struct AccountAllTradesHandler {
    /// account_id -> latest account_all_trades payload
    states: HashMap<i64, WsAccountAllTradesUpdate>,
}

impl AccountAllTradesHandler {
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
        }
    }

    /// Set or replace the latest account_all_trades payload.
    pub fn set_state(&mut self, account_id: i64, state: WsAccountAllTradesUpdate) {
        self.states.insert(account_id, state);
    }

    /// Get the latest account_all_trades payload for an account.
    pub fn get(&self, account_id: i64) -> Option<&WsAccountAllTradesUpdate> {
        self.states.get(&account_id)
    }
}

/// Manages local user stats state from WebSocket updates.
#[derive(Debug, Default)]
pub struct UserStatsHandler {
    /// account_id -> latest stats
    states: HashMap<i64, WsUserStatsUpdate>,
}

impl UserStatsHandler {
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
        }
    }

    /// Set or replace the user stats for an account.
    pub fn set_state(&mut self, account_id: i64, state: WsUserStatsUpdate) {
        self.states.insert(account_id, state);
    }

    /// Get the current user stats for an account.
    pub fn get(&self, account_id: i64) -> Option<&WsUserStatsUpdate> {
        self.states.get(&account_id)
    }
}

/// Manages local market stats state from WebSocket updates.
#[derive(Debug, Default)]
pub struct MarketStatsHandler {
    /// market_id -> latest market stats payload
    states: HashMap<i64, WsMarketStatsUpdate>,
}

impl MarketStatsHandler {
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
        }
    }

    /// Set or replace the market stats for a market.
    pub fn set_state(&mut self, market_id: i64, state: WsMarketStatsUpdate) {
        self.states.insert(market_id, state);
    }

    /// Get the current market stats for a market.
    pub fn get(&self, market_id: i64) -> Option<&WsMarketStatsUpdate> {
        self.states.get(&market_id)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::models::order::Order;
    use crate::models::order_book::PriceLevel;
    use crate::models::ws::{
        WsAccountAllAssetsUpdate, WsAccountAllTradesUpdate, WsAccountSpotAvgEntryPricesUpdate,
        WsOrderBook, WsOrderBookState,
    };

    use super::{
        AccountAllAssetsHandler, AccountAllTradesHandler, AccountOrdersHandler,
        AccountSpotAvgEntryPricesHandler, OrderBookHandler, OrderBookUpdateError,
    };

    fn level(price: &str, size: &str) -> PriceLevel {
        PriceLevel {
            price: price.to_string(),
            size: size.to_string(),
        }
    }

    fn snapshot_state() -> WsOrderBookState {
        WsOrderBookState {
            asks: vec![level("101", "2"), level("102", "3")],
            bids: vec![level("99", "4"), level("98", "5")],
            nonce: 10,
        }
    }

    fn update_book(
        begin_nonce: i64,
        nonce: i64,
        asks: Vec<PriceLevel>,
        bids: Vec<PriceLevel>,
    ) -> WsOrderBook {
        WsOrderBook {
            code: 0,
            asks,
            bids,
            offset: 0,
            nonce,
            begin_nonce,
        }
    }

    fn sample_order(order_index: i64, market_index: i64) -> Order {
        Order {
            order_index,
            client_order_index: 0,
            order_id: String::new(),
            client_order_id: String::new(),
            market_index,
            owner_account_index: 0,
            initial_base_amount: "1".to_string(),
            price: "1".to_string(),
            nonce: 0,
            remaining_base_amount: "1".to_string(),
            is_ask: true,
            base_size: 0,
            base_price: 0,
            filled_base_amount: "0".to_string(),
            filled_quote_amount: String::new(),
            side: "ask".to_string(),
            order_type: "limit".to_string(),
            time_in_force: "good-till-time".to_string(),
            reduce_only: false,
            trigger_price: String::new(),
            order_expiry: 0,
            status: "open".to_string(),
            trigger_status: String::new(),
            trigger_time: 0,
            parent_order_index: 0,
            parent_order_id: String::new(),
            to_trigger_order_id_0: String::new(),
            to_trigger_order_id_1: String::new(),
            to_cancel_order_id_0: String::new(),
            block_height: 0,
            timestamp: 0,
            created_at: 0,
            updated_at: 0,
            transaction_time: 0,
        }
    }

    #[test]
    fn account_scoped_orders_do_not_clobber_each_other() {
        let mut handler = AccountOrdersHandler::new();
        handler.set_orders_for_account(100, 89, vec![sample_order(1, 89)]);
        handler.set_orders_for_account(200, 89, vec![sample_order(2, 89)]);

        let a = handler.get_for_account(100, 89).unwrap();
        let b = handler.get_for_account(200, 89).unwrap();
        assert_eq!(a.len(), 1);
        assert_eq!(b.len(), 1);
        assert_eq!(a[0].order_index, 1);
        assert_eq!(b[0].order_index, 2);
    }

    #[test]
    fn account_all_assets_state_is_stored_by_account() {
        let mut handler = AccountAllAssetsHandler::new();
        handler.set_state(
            1234,
            WsAccountAllAssetsUpdate {
                msg_type: "update/account_all_assets".to_string(),
                channel: "account_all_assets:1234".to_string(),
                assets: vec![],
            },
        );

        assert!(handler.get(1234).is_some());
        assert!(handler.get(9999).is_none());
    }

    #[test]
    fn account_all_trades_state_is_stored_by_account() {
        let mut handler = AccountAllTradesHandler::new();
        handler.set_state(
            1234,
            WsAccountAllTradesUpdate {
                msg_type: "update/account_all_trades".to_string(),
                channel: "account_all_trades:1234".to_string(),
                trades: HashMap::new(),
                total_volume: 0.0,
                monthly_volume: 0.0,
                weekly_volume: 0.0,
                daily_volume: 0.0,
            },
        );

        assert!(handler.get(1234).is_some());
        assert!(handler.get(9999).is_none());
    }

    #[test]
    fn account_spot_avg_entry_prices_state_is_stored_by_account() {
        let mut handler = AccountSpotAvgEntryPricesHandler::new();
        handler.set_state(
            1234,
            WsAccountSpotAvgEntryPricesUpdate {
                msg_type: "update/account_spot_avg_entry_prices".to_string(),
                channel: "account_spot_avg_entry_prices/1234".to_string(),
                avg_entry_prices: HashMap::new(),
            },
        );

        assert!(handler.get(1234).is_some());
        assert!(handler.get(9999).is_none());
    }

    #[test]
    fn order_book_snapshot_initializes_state_and_nonce() {
        let mut handler = OrderBookHandler::new();
        handler.set_snapshot(42, snapshot_state());

        let state = handler.get(42).unwrap();
        assert_eq!(state.nonce, 10);
        assert_eq!(state.bids[0].price, "99");
        assert_eq!(state.asks[0].price, "101");
    }

    #[test]
    fn contiguous_order_book_delta_updates_state() {
        let mut handler = OrderBookHandler::new();
        handler.set_snapshot(42, snapshot_state());

        let updated = handler
            .apply_update(
                42,
                &update_book(
                    10,
                    11,
                    vec![level("101", "1"), level("103", "7")],
                    vec![level("99", "0"), level("100", "6")],
                ),
            )
            .unwrap();

        assert_eq!(updated.nonce, 11);
        assert_eq!(updated.asks.len(), 3);
        assert_eq!(updated.asks[0].price, "101");
        assert_eq!(updated.asks[0].size, "1");
        assert_eq!(updated.bids.len(), 2);
        assert_eq!(updated.bids[0].price, "100");
        assert_eq!(updated.bids[0].size, "6");
    }

    #[test]
    fn order_book_nonce_gap_is_rejected() {
        let mut handler = OrderBookHandler::new();
        handler.set_snapshot(42, snapshot_state());

        let err = handler
            .apply_update(42, &update_book(9, 11, vec![level("101", "1")], Vec::new()))
            .unwrap_err();

        assert_eq!(
            err,
            OrderBookUpdateError::NonContiguousNonce {
                market_id: 42,
                expected_begin_nonce: 10,
                actual_begin_nonce: 9,
                previous_nonce: 10,
            }
        );
    }
}