Skip to main content

nautilus_binance/spot/sbe/stream/
best_bid_ask.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//! BestBidAsk stream event decoder.
17//!
18//! Message layout (after 8-byte header):
19//! - eventTime: i64 (microseconds)
20//! - bookUpdateId: i64
21//! - priceExponent: i8
22//! - qtyExponent: i8
23//! - bidPrice: i64 (mantissa)
24//! - bidQty: i64 (mantissa)
25//! - askPrice: i64 (mantissa)
26//! - askQty: i64 (mantissa)
27//! - symbol: varString8
28
29use ustr::Ustr;
30
31use super::{MessageHeader, StreamDecodeError};
32use crate::spot::sbe::cursor::SbeCursor;
33
34/// Best bid/ask stream event.
35#[derive(Debug, Clone)]
36pub struct BestBidAskStreamEvent {
37    /// Event timestamp in microseconds.
38    pub event_time_us: i64,
39    /// Book update ID for sequencing.
40    pub book_update_id: i64,
41    /// Price exponent (prices = mantissa * 10^exponent).
42    pub price_exponent: i8,
43    /// Quantity exponent (quantities = mantissa * 10^exponent).
44    pub qty_exponent: i8,
45    /// Best bid price mantissa.
46    pub bid_price_mantissa: i64,
47    /// Best bid quantity mantissa.
48    pub bid_qty_mantissa: i64,
49    /// Best ask price mantissa.
50    pub ask_price_mantissa: i64,
51    /// Best ask quantity mantissa.
52    pub ask_qty_mantissa: i64,
53    /// Trading symbol.
54    pub symbol: Ustr,
55}
56
57impl BestBidAskStreamEvent {
58    /// Fixed block length (excluding header and variable-length data).
59    pub const BLOCK_LENGTH: usize = 50;
60
61    /// Minimum buffer size needed (header + block + 1-byte string length).
62    pub const MIN_BUFFER_SIZE: usize = MessageHeader::ENCODED_LENGTH + Self::BLOCK_LENGTH + 1;
63
64    /// Decode from SBE buffer (including 8-byte header).
65    ///
66    /// # Errors
67    ///
68    /// Returns error if buffer is too short or contains invalid data.
69    pub fn decode(buf: &[u8]) -> Result<Self, StreamDecodeError> {
70        let header = MessageHeader::decode(buf)?;
71        header.validate_schema()?;
72        Self::decode_validated(buf)
73    }
74
75    /// Decode from an SBE buffer whose header has already been validated.
76    pub(crate) fn decode_validated(buf: &[u8]) -> Result<Self, StreamDecodeError> {
77        let mut cursor = SbeCursor::new_at(buf, MessageHeader::ENCODED_LENGTH);
78        Self::decode_body(&mut cursor)
79    }
80
81    #[inline]
82    fn decode_body(cursor: &mut SbeCursor<'_>) -> Result<Self, StreamDecodeError> {
83        let event_time_us = cursor.read_i64_le()?;
84        let book_update_id = cursor.read_i64_le()?;
85        let price_exponent = cursor.read_i8()?;
86        let qty_exponent = cursor.read_i8()?;
87        let bid_price_mantissa = cursor.read_i64_le()?;
88        let bid_qty_mantissa = cursor.read_i64_le()?;
89        let ask_price_mantissa = cursor.read_i64_le()?;
90        let ask_qty_mantissa = cursor.read_i64_le()?;
91
92        let symbol = Ustr::from(cursor.read_var_string8_ref()?);
93
94        Ok(Self {
95            event_time_us,
96            book_update_id,
97            price_exponent,
98            qty_exponent,
99            bid_price_mantissa,
100            bid_qty_mantissa,
101            ask_price_mantissa,
102            ask_qty_mantissa,
103            symbol,
104        })
105    }
106
107    /// Get bid price as f64.
108    #[inline]
109    #[must_use]
110    pub fn bid_price(&self) -> f64 {
111        super::mantissa_to_f64(self.bid_price_mantissa, self.price_exponent)
112    }
113
114    /// Get bid quantity as f64.
115    #[inline]
116    #[must_use]
117    pub fn bid_qty(&self) -> f64 {
118        super::mantissa_to_f64(self.bid_qty_mantissa, self.qty_exponent)
119    }
120
121    /// Get ask price as f64.
122    #[inline]
123    #[must_use]
124    pub fn ask_price(&self) -> f64 {
125        super::mantissa_to_f64(self.ask_price_mantissa, self.price_exponent)
126    }
127
128    /// Get ask quantity as f64.
129    #[inline]
130    #[must_use]
131    pub fn ask_qty(&self) -> f64 {
132        super::mantissa_to_f64(self.ask_qty_mantissa, self.qty_exponent)
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use rstest::rstest;
139
140    use super::*;
141    use crate::spot::sbe::stream::{STREAM_SCHEMA_ID, template_id};
142
143    fn make_valid_buffer() -> Vec<u8> {
144        let mut buf = vec![0u8; 70];
145
146        // Header
147        buf[0..2].copy_from_slice(&50u16.to_le_bytes()); // block_length
148        buf[2..4].copy_from_slice(&template_id::BEST_BID_ASK_STREAM_EVENT.to_le_bytes());
149        buf[4..6].copy_from_slice(&STREAM_SCHEMA_ID.to_le_bytes());
150        buf[6..8].copy_from_slice(&0u16.to_le_bytes()); // version
151
152        // Body
153        let body = &mut buf[8..];
154        body[0..8].copy_from_slice(&1000000i64.to_le_bytes()); // event_time_us
155        body[8..16].copy_from_slice(&12345i64.to_le_bytes()); // book_update_id
156        body[16] = (-2i8) as u8; // price_exponent
157        body[17] = (-8i8) as u8; // qty_exponent
158        body[18..26].copy_from_slice(&4200000i64.to_le_bytes()); // bid_price
159        body[26..34].copy_from_slice(&100000000i64.to_le_bytes()); // bid_qty
160        body[34..42].copy_from_slice(&4200100i64.to_le_bytes()); // ask_price
161        body[42..50].copy_from_slice(&200000000i64.to_le_bytes()); // ask_qty
162
163        // Symbol: "BTCUSDT" (7 bytes)
164        body[50] = 7;
165        body[51..58].copy_from_slice(b"BTCUSDT");
166
167        buf
168    }
169
170    #[rstest]
171    fn test_decode_valid() {
172        let buf = make_valid_buffer();
173        let event = BestBidAskStreamEvent::decode(&buf).unwrap();
174
175        assert_eq!(event.event_time_us, 1000000);
176        assert_eq!(event.book_update_id, 12345);
177        assert_eq!(event.price_exponent, -2);
178        assert_eq!(event.qty_exponent, -8);
179        assert_eq!(event.symbol, "BTCUSDT");
180        assert!((event.bid_price() - 42000.0).abs() < 0.01);
181    }
182
183    #[rstest]
184    fn test_decode_truncated_header() {
185        let buf = [0u8; 5];
186        let err = BestBidAskStreamEvent::decode(&buf).unwrap_err();
187        assert!(matches!(err, StreamDecodeError::BufferTooShort { .. }));
188    }
189
190    #[rstest]
191    fn test_decode_truncated_body() {
192        let mut buf = make_valid_buffer();
193        buf.truncate(40); // Truncate in the middle of body
194        let err = BestBidAskStreamEvent::decode(&buf).unwrap_err();
195        assert!(matches!(err, StreamDecodeError::BufferTooShort { .. }));
196    }
197
198    #[rstest]
199    fn test_decode_wrong_schema() {
200        let mut buf = make_valid_buffer();
201        buf[4..6].copy_from_slice(&99u16.to_le_bytes()); // Wrong schema
202        let err = BestBidAskStreamEvent::decode(&buf).unwrap_err();
203        assert!(matches!(err, StreamDecodeError::SchemaMismatch { .. }));
204    }
205
206    #[rstest]
207    fn test_decode_validated_matches_decode() {
208        let buf = make_valid_buffer();
209
210        let decode_event = BestBidAskStreamEvent::decode(&buf).unwrap();
211        let validated_event = BestBidAskStreamEvent::decode_validated(&buf).unwrap();
212
213        assert_eq!(validated_event.event_time_us, decode_event.event_time_us);
214        assert_eq!(validated_event.book_update_id, decode_event.book_update_id);
215        assert_eq!(validated_event.price_exponent, decode_event.price_exponent);
216        assert_eq!(validated_event.qty_exponent, decode_event.qty_exponent);
217        assert_eq!(
218            validated_event.bid_price_mantissa,
219            decode_event.bid_price_mantissa
220        );
221        assert_eq!(validated_event.symbol, decode_event.symbol);
222    }
223}