nautilus_binance/common/sbe/stream/
depth_diff.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Depth diff stream event decoder.
17//!
18//! Message layout (after 8-byte header):
19//! - eventTime: i64 (microseconds)
20//! - firstBookUpdateId: i64
21//! - lastBookUpdateId: i64
22//! - priceExponent: i8
23//! - qtyExponent: i8
24//! - bids group (groupSize16Encoding: u16 blockLength + u16 numInGroup):
25//!   - price: i64 (mantissa)
26//!   - qty: i64 (mantissa)
27//! - asks group (groupSize16Encoding: u16 blockLength + u16 numInGroup):
28//!   - price: i64 (mantissa)
29//!   - qty: i64 (mantissa)
30//! - symbol: varString8
31
32use ustr::Ustr;
33
34use super::{MessageHeader, PriceLevel, StreamDecodeError};
35use crate::common::sbe::cursor::SbeCursor;
36
37/// Depth diff stream event (incremental order book updates).
38#[derive(Debug, Clone)]
39pub struct DepthDiffStreamEvent {
40    /// Event timestamp in microseconds.
41    pub event_time_us: i64,
42    /// First book update ID in this diff.
43    pub first_book_update_id: i64,
44    /// Last book update ID in this diff.
45    pub last_book_update_id: i64,
46    /// Price exponent (prices = mantissa * 10^exponent).
47    pub price_exponent: i8,
48    /// Quantity exponent (quantities = mantissa * 10^exponent).
49    pub qty_exponent: i8,
50    /// Bid level updates (qty=0 means remove level).
51    pub bids: Vec<PriceLevel>,
52    /// Ask level updates (qty=0 means remove level).
53    pub asks: Vec<PriceLevel>,
54    /// Trading symbol.
55    pub symbol: Ustr,
56}
57
58impl DepthDiffStreamEvent {
59    /// Fixed block length (excluding header, groups, and variable-length data).
60    pub const BLOCK_LENGTH: usize = 26;
61
62    /// Decode from SBE buffer (including 8-byte header).
63    ///
64    /// # Errors
65    ///
66    /// Returns error if buffer is too short, group size exceeds limits,
67    /// or data is otherwise invalid.
68    pub fn decode(buf: &[u8]) -> Result<Self, StreamDecodeError> {
69        let header = MessageHeader::decode(buf)?;
70        header.validate_schema()?;
71
72        let mut cursor = SbeCursor::new_at(buf, MessageHeader::ENCODED_LENGTH);
73
74        let event_time_us = cursor.read_i64_le()?;
75        let first_book_update_id = cursor.read_i64_le()?;
76        let last_book_update_id = cursor.read_i64_le()?;
77        let price_exponent = cursor.read_i8()?;
78        let qty_exponent = cursor.read_i8()?;
79
80        let (bid_block_length, num_bids) = cursor.read_group_header_16()?;
81        let bids = cursor.read_group(bid_block_length, u32::from(num_bids), PriceLevel::decode)?;
82
83        let (ask_block_length, num_asks) = cursor.read_group_header_16()?;
84        let asks = cursor.read_group(ask_block_length, u32::from(num_asks), PriceLevel::decode)?;
85
86        let symbol_str = cursor.read_var_string8()?;
87
88        Ok(Self {
89            event_time_us,
90            first_book_update_id,
91            last_book_update_id,
92            price_exponent,
93            qty_exponent,
94            bids,
95            asks,
96            symbol: Ustr::from(&symbol_str),
97        })
98    }
99
100    /// Get price as f64 for a level.
101    #[inline]
102    #[must_use]
103    pub fn level_price(&self, level: &PriceLevel) -> f64 {
104        super::mantissa_to_f64(level.price_mantissa, self.price_exponent)
105    }
106
107    /// Get quantity as f64 for a level.
108    #[inline]
109    #[must_use]
110    pub fn level_qty(&self, level: &PriceLevel) -> f64 {
111        super::mantissa_to_f64(level.qty_mantissa, self.qty_exponent)
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use rstest::rstest;
118
119    use super::*;
120    use crate::common::sbe::stream::{STREAM_SCHEMA_ID, template_id};
121
122    fn make_valid_buffer(num_bids: usize, num_asks: usize) -> Vec<u8> {
123        let level_block_len = 16u16;
124        let body_size = 26
125            + 4
126            + (num_bids * level_block_len as usize)
127            + 4
128            + (num_asks * level_block_len as usize)
129            + 8;
130        let mut buf = vec![0u8; 8 + body_size];
131
132        // Header
133        buf[0..2].copy_from_slice(&26u16.to_le_bytes()); // block_length
134        buf[2..4].copy_from_slice(&template_id::DEPTH_DIFF_STREAM_EVENT.to_le_bytes());
135        buf[4..6].copy_from_slice(&STREAM_SCHEMA_ID.to_le_bytes());
136        buf[6..8].copy_from_slice(&0u16.to_le_bytes()); // version
137
138        // Body
139        let body = &mut buf[8..];
140        body[0..8].copy_from_slice(&1000000i64.to_le_bytes()); // event_time_us
141        body[8..16].copy_from_slice(&12345i64.to_le_bytes()); // first_book_update_id
142        body[16..24].copy_from_slice(&12350i64.to_le_bytes()); // last_book_update_id
143        body[24] = (-2i8) as u8; // price_exponent
144        body[25] = (-8i8) as u8; // qty_exponent
145
146        let mut offset = 26;
147
148        // Bids group header
149        body[offset..offset + 2].copy_from_slice(&level_block_len.to_le_bytes());
150        body[offset + 2..offset + 4].copy_from_slice(&(num_bids as u16).to_le_bytes());
151        offset += 4;
152
153        // Bids
154        for i in 0..num_bids {
155            body[offset..offset + 8].copy_from_slice(&(4200000i64 - i as i64 * 100).to_le_bytes());
156            body[offset + 8..offset + 16].copy_from_slice(&100000000i64.to_le_bytes());
157            offset += level_block_len as usize;
158        }
159
160        // Asks group header
161        body[offset..offset + 2].copy_from_slice(&level_block_len.to_le_bytes());
162        body[offset + 2..offset + 4].copy_from_slice(&(num_asks as u16).to_le_bytes());
163        offset += 4;
164
165        // Asks
166        for i in 0..num_asks {
167            body[offset..offset + 8].copy_from_slice(&(4200100i64 + i as i64 * 100).to_le_bytes());
168            body[offset + 8..offset + 16].copy_from_slice(&200000000i64.to_le_bytes());
169            offset += level_block_len as usize;
170        }
171
172        // Symbol: "BTCUSDT"
173        body[offset] = 7;
174        body[offset + 1..offset + 8].copy_from_slice(b"BTCUSDT");
175
176        buf
177    }
178
179    #[rstest]
180    fn test_decode_valid() {
181        let buf = make_valid_buffer(3, 2);
182        let event = DepthDiffStreamEvent::decode(&buf).unwrap();
183
184        assert_eq!(event.event_time_us, 1000000);
185        assert_eq!(event.first_book_update_id, 12345);
186        assert_eq!(event.last_book_update_id, 12350);
187        assert_eq!(event.bids.len(), 3);
188        assert_eq!(event.asks.len(), 2);
189        assert_eq!(event.symbol, "BTCUSDT");
190    }
191
192    #[rstest]
193    fn test_decode_empty_updates() {
194        let buf = make_valid_buffer(0, 0);
195        let event = DepthDiffStreamEvent::decode(&buf).unwrap();
196
197        assert!(event.bids.is_empty());
198        assert!(event.asks.is_empty());
199    }
200
201    #[rstest]
202    fn test_decode_truncated() {
203        let mut buf = make_valid_buffer(5, 5);
204        buf.truncate(60); // Truncate in the middle
205        let err = DepthDiffStreamEvent::decode(&buf).unwrap_err();
206        assert!(matches!(err, StreamDecodeError::BufferTooShort { .. }));
207    }
208
209    #[rstest]
210    fn test_decode_wrong_schema() {
211        let mut buf = make_valid_buffer(3, 2);
212        buf[4..6].copy_from_slice(&99u16.to_le_bytes());
213        let err = DepthDiffStreamEvent::decode(&buf).unwrap_err();
214        assert!(matches!(err, StreamDecodeError::SchemaMismatch { .. }));
215    }
216}