patisson-binance-sdk 0.1.8

Unofficial Rust SDK for the Binance exchange API
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
//! Local L2 order book for Binance Spot.
//!
//! [`OrderBookState`] encapsulates the full synchronization protocol from the
//! Binance Spot docs (snapshot + `<symbol>@depth` diff stream) behind a state
//! machine that accepts a REST snapshot and live diff events in any order.
//! Callers feed events as they arrive and inspect the returned
//! [`ApplyOutcome`] — on [`ApplyOutcome::ResyncRequired`], fetch a new
//! snapshot.
//!
//! **Spot vs. futures sync rules.** The futures stream carries a `pu` field
//! ("previous final update id") that lets the chain be verified directly.
//! Spot has no such field, so the chain is verified arithmetically:
//!
//! - Drop buffered events where `u <= lastUpdateId` (note: `<=`, not `<`).
//! - First processed event: `U <= lastUpdateId+1 AND u >= lastUpdateId+1`.
//! - Subsequent events must satisfy `next.U == prev.u + 1`.

use rust_decimal::Decimal;
use std::collections::{BTreeMap, VecDeque};

use crate::spot::{http::OrderBook, ws::DepthUpdateMsg};

/// Outcome of [`OrderBookState::apply_snapshot`] / [`OrderBookState::apply_diff`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ApplyOutcome {
    /// Data was stored but the book is not yet synchronized; more input is
    /// needed (e.g., snapshot received but no bridging diff has arrived yet,
    /// or diffs received before the snapshot).
    Buffered,
    /// State has just transitioned to fully synchronized: snapshot + bridging
    /// diffs have been applied and the local book is now live.
    Synced,
    /// Diff was applied to an already-synchronized live book.
    Applied,
    /// Data was stale relative to current state and was dropped.
    Ignored,
    /// State is unrecoverable from current data — the snapshot is too old to
    /// bridge the diff stream, or the live diff chain has a gap. The caller
    /// must fetch a fresh snapshot and feed it back via [`OrderBookState::apply_snapshot`].
    /// Buffered diffs are preserved so the new snapshot has a chance to bridge.
    ResyncRequired,
}

/// Local L2 order book for a Spot symbol.
///
/// Built from a REST depth snapshot plus the `<symbol>@depth` diff stream.
/// Snapshots and diffs may be fed in any order; the state machine handles
/// buffering, draining stale events, bridging, and live-chain verification
/// internally.
#[derive(Debug, Default)]
pub struct OrderBookState {
    inner: Inner,
}

#[derive(Debug)]
enum Inner {
    /// No snapshot yet. Diffs accumulate in the buffer.
    NoSnapshot { buffered: VecDeque<DepthUpdateMsg> },
    /// Snapshot received but not yet bridged. Diffs continue to accumulate.
    Pending {
        snapshot_last_id: i64,
        bids: BTreeMap<Decimal, Decimal>,
        asks: BTreeMap<Decimal, Decimal>,
        buffered: VecDeque<DepthUpdateMsg>,
    },
    /// Fully synchronized live book.
    Synced {
        last_update_id: i64,
        bids: BTreeMap<Decimal, Decimal>,
        asks: BTreeMap<Decimal, Decimal>,
    },
}

impl Default for Inner {
    fn default() -> Self {
        Self::NoSnapshot {
            buffered: VecDeque::new(),
        }
    }
}

impl OrderBookState {
    pub fn new() -> Self {
        Self::default()
    }

    /// Feed a REST depth snapshot into the state machine.
    pub fn apply_snapshot(&mut self, snapshot: OrderBook) -> ApplyOutcome {
        let new_id = snapshot.last_update_id;

        match &self.inner {
            Inner::Synced { last_update_id, .. } => {
                if new_id <= *last_update_id {
                    return ApplyOutcome::Ignored;
                }
                let (bids, asks) = sides_from_snapshot(snapshot);
                self.inner = Inner::Synced {
                    last_update_id: new_id,
                    bids,
                    asks,
                };
                return ApplyOutcome::Applied;
            }
            Inner::Pending {
                snapshot_last_id, ..
            } if new_id <= *snapshot_last_id => {
                return ApplyOutcome::Ignored;
            }
            _ => {}
        }

        let buffered = match std::mem::take(&mut self.inner) {
            Inner::NoSnapshot { buffered } | Inner::Pending { buffered, .. } => buffered,
            Inner::Synced { .. } => unreachable!("handled above"),
        };
        let (bids, asks) = sides_from_snapshot(snapshot);
        self.inner = Inner::Pending {
            snapshot_last_id: new_id,
            bids,
            asks,
            buffered,
        };
        self.try_bridge()
    }

    /// Feed a live diff event into the state machine.
    pub fn apply_diff(&mut self, diff: DepthUpdateMsg) -> ApplyOutcome {
        match &mut self.inner {
            Inner::NoSnapshot { buffered } => {
                buffered.push_back(diff);
                ApplyOutcome::Buffered
            }
            Inner::Pending { buffered, .. } => {
                buffered.push_back(diff);
                self.try_bridge()
            }
            Inner::Synced {
                last_update_id,
                bids,
                asks,
            } => {
                if diff.final_update_id <= *last_update_id {
                    return ApplyOutcome::Ignored;
                }
                if diff.first_update_id != *last_update_id + 1 {
                    let mut buffered = VecDeque::new();
                    buffered.push_back(diff);
                    self.inner = Inner::NoSnapshot { buffered };
                    return ApplyOutcome::ResyncRequired;
                }
                apply_diff_to_sides(bids, asks, &diff);
                *last_update_id = diff.final_update_id;
                ApplyOutcome::Applied
            }
        }
    }

    pub fn is_synced(&self) -> bool {
        matches!(self.inner, Inner::Synced { .. })
    }

    pub fn last_update_id(&self) -> Option<i64> {
        match &self.inner {
            Inner::Synced { last_update_id, .. } => Some(*last_update_id),
            _ => None,
        }
    }

    pub fn bids(&self) -> Option<&BTreeMap<Decimal, Decimal>> {
        match &self.inner {
            Inner::Synced { bids, .. } => Some(bids),
            _ => None,
        }
    }

    pub fn asks(&self) -> Option<&BTreeMap<Decimal, Decimal>> {
        match &self.inner {
            Inner::Synced { asks, .. } => Some(asks),
            _ => None,
        }
    }

    pub fn best_bid(&self) -> Option<(Decimal, Decimal)> {
        self.bids()
            .and_then(|b| b.iter().next_back().map(|(p, q)| (*p, *q)))
    }

    pub fn best_ask(&self) -> Option<(Decimal, Decimal)> {
        self.asks()
            .and_then(|a| a.iter().next().map(|(p, q)| (*p, *q)))
    }

    /// Attempt to bridge the held snapshot with buffered diffs.
    ///
    /// Per Binance Spot docs:
    /// - Drop buffered events with `u <= lastUpdateId` (stale).
    /// - First processed event must satisfy `U <= lastUpdateId+1 AND u >= lastUpdateId+1`.
    /// - Subsequent events must chain: `next.U == prev.u + 1`.
    fn try_bridge(&mut self) -> ApplyOutcome {
        if !matches!(self.inner, Inner::Pending { .. }) {
            return ApplyOutcome::Buffered;
        }

        let Inner::Pending {
            snapshot_last_id,
            bids,
            asks,
            mut buffered,
        } = std::mem::take(&mut self.inner)
        else {
            unreachable!("checked above");
        };

        while let Some(front) = buffered.front() {
            if front.final_update_id <= snapshot_last_id {
                buffered.pop_front();
            } else {
                break;
            }
        }

        let Some(first) = buffered.front() else {
            self.inner = Inner::Pending {
                snapshot_last_id,
                bids,
                asks,
                buffered,
            };
            return ApplyOutcome::Buffered;
        };

        if first.first_update_id > snapshot_last_id + 1 {
            // Snapshot is older than the diff stream — gap. Drop the
            // snapshot, keep buffered diffs for the next snapshot attempt.
            self.inner = Inner::NoSnapshot { buffered };
            return ApplyOutcome::ResyncRequired;
        }

        let mut bids = bids;
        let mut asks = asks;
        let mut last_id = snapshot_last_id;
        let mut prev_u: Option<i64> = None;
        while let Some(diff) = buffered.pop_front() {
            if let Some(p) = prev_u
                && diff.first_update_id != p + 1
            {
                let mut remaining = VecDeque::with_capacity(buffered.len() + 1);
                remaining.push_back(diff);
                remaining.extend(buffered);
                self.inner = Inner::NoSnapshot {
                    buffered: remaining,
                };
                return ApplyOutcome::ResyncRequired;
            }
            apply_diff_to_sides(&mut bids, &mut asks, &diff);
            last_id = diff.final_update_id;
            prev_u = Some(diff.final_update_id);
        }

        self.inner = Inner::Synced {
            last_update_id: last_id,
            bids,
            asks,
        };
        ApplyOutcome::Synced
    }
}

fn sides_from_snapshot(
    snapshot: OrderBook,
) -> (BTreeMap<Decimal, Decimal>, BTreeMap<Decimal, Decimal>) {
    let bids = snapshot
        .bids
        .into_iter()
        .map(|l| (l.price(), l.qty()))
        .collect();
    let asks = snapshot
        .asks
        .into_iter()
        .map(|l| (l.price(), l.qty()))
        .collect();
    (bids, asks)
}

fn apply_diff_to_sides(
    bids: &mut BTreeMap<Decimal, Decimal>,
    asks: &mut BTreeMap<Decimal, Decimal>,
    diff: &DepthUpdateMsg,
) {
    for level in &diff.bids {
        apply_side(bids, level.price(), level.qty());
    }
    for level in &diff.asks {
        apply_side(asks, level.price(), level.qty());
    }
}

fn apply_side(side: &mut BTreeMap<Decimal, Decimal>, price: Decimal, qty: Decimal) {
    if qty.is_zero() {
        side.remove(&price);
    } else {
        side.insert(price, qty);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::spot::http::OrderLevel;

    fn snapshot(last_id: i64) -> OrderBook {
        OrderBook {
            last_update_id: last_id,
            bids: vec![level("100", "1")],
            asks: vec![level("101", "2")],
        }
    }

    fn level(price: &str, qty: &str) -> OrderLevel {
        let json = format!("[\"{price}\", \"{qty}\"]");
        serde_json::from_str(&json).unwrap()
    }

    fn diff(u_first: i64, u_last: i64) -> DepthUpdateMsg {
        DepthUpdateMsg {
            event_time: 0,
            symbol: "BTCUSDT".into(),
            first_update_id: u_first,
            final_update_id: u_last,
            bids: vec![],
            asks: vec![],
        }
    }

    #[test]
    fn snapshot_then_bridging_diff() {
        let mut book = OrderBookState::new();
        assert_eq!(book.apply_snapshot(snapshot(100)), ApplyOutcome::Buffered);
        // Spot bridge: U <= L+1=101 AND u >= L+1=101.
        assert_eq!(book.apply_diff(diff(95, 105)), ApplyOutcome::Synced);
        assert_eq!(book.last_update_id(), Some(105));
    }

    #[test]
    fn diffs_then_snapshot() {
        let mut book = OrderBookState::new();
        // First diff covers 90..=95, will be dropped (u=95 <= L=100).
        assert_eq!(book.apply_diff(diff(90, 95)), ApplyOutcome::Buffered);
        // Second diff covers 96..=105, satisfies U<=101 AND u>=101.
        assert_eq!(book.apply_diff(diff(96, 105)), ApplyOutcome::Buffered);
        assert_eq!(book.apply_snapshot(snapshot(100)), ApplyOutcome::Synced);
        assert_eq!(book.last_update_id(), Some(105));
    }

    #[test]
    fn snapshot_older_than_buffered_stream_triggers_resync() {
        let mut book = OrderBookState::new();
        // First buffered diff U=200 > L+1=101 → snapshot too old.
        assert_eq!(book.apply_diff(diff(200, 210)), ApplyOutcome::Buffered);
        assert_eq!(
            book.apply_snapshot(snapshot(100)),
            ApplyOutcome::ResyncRequired
        );
        assert!(!book.is_synced());
        // Fresher snapshot with L=199 (so L+1=200 == diff.U) bridges.
        assert_eq!(book.apply_snapshot(snapshot(199)), ApplyOutcome::Synced);
        assert_eq!(book.last_update_id(), Some(210));
    }

    #[test]
    fn live_chain_break_triggers_resync() {
        let mut book = OrderBookState::new();
        book.apply_snapshot(snapshot(100));
        book.apply_diff(diff(95, 105));
        assert!(book.is_synced());
        // Next diff U=107 but expected = last_u + 1 = 106 → gap.
        assert_eq!(
            book.apply_diff(diff(107, 115)),
            ApplyOutcome::ResyncRequired
        );
        assert!(!book.is_synced());
    }

    #[test]
    fn stale_diff_after_sync_is_ignored() {
        let mut book = OrderBookState::new();
        book.apply_snapshot(snapshot(100));
        book.apply_diff(diff(95, 105));
        // Diff u=95 <= last_u=105 → stale.
        assert_eq!(book.apply_diff(diff(80, 95)), ApplyOutcome::Ignored);
        assert_eq!(book.last_update_id(), Some(105));
    }

    #[test]
    fn duplicate_snapshot_is_ignored() {
        let mut book = OrderBookState::new();
        book.apply_snapshot(snapshot(100));
        assert_eq!(book.apply_snapshot(snapshot(100)), ApplyOutcome::Ignored);
        assert_eq!(book.apply_snapshot(snapshot(50)), ApplyOutcome::Ignored);
    }

    #[test]
    fn newer_snapshot_replaces_synced_book() {
        let mut book = OrderBookState::new();
        book.apply_snapshot(snapshot(100));
        book.apply_diff(diff(95, 105));
        assert!(book.is_synced());
        assert_eq!(book.apply_snapshot(snapshot(200)), ApplyOutcome::Applied);
        assert_eq!(book.last_update_id(), Some(200));
    }

    #[test]
    fn live_continuous_chain() {
        let mut book = OrderBookState::new();
        book.apply_snapshot(snapshot(100));
        book.apply_diff(diff(95, 105));
        // 106 == 105 + 1 → applied.
        assert_eq!(book.apply_diff(diff(106, 110)), ApplyOutcome::Applied);
        // 111 == 110 + 1 → applied.
        assert_eq!(book.apply_diff(diff(111, 115)), ApplyOutcome::Applied);
        assert_eq!(book.last_update_id(), Some(115));
    }
}