nautilus_binance/common/sbe/stream/
best_bid_ask.rs1use ustr::Ustr;
30
31use super::{MessageHeader, StreamDecodeError};
32use crate::common::sbe::cursor::SbeCursor;
33
34#[derive(Debug, Clone)]
36pub struct BestBidAskStreamEvent {
37 pub event_time_us: i64,
39 pub book_update_id: i64,
41 pub price_exponent: i8,
43 pub qty_exponent: i8,
45 pub bid_price_mantissa: i64,
47 pub bid_qty_mantissa: i64,
49 pub ask_price_mantissa: i64,
51 pub ask_qty_mantissa: i64,
53 pub symbol: Ustr,
55}
56
57impl BestBidAskStreamEvent {
58 pub const BLOCK_LENGTH: usize = 50;
60
61 pub const MIN_BUFFER_SIZE: usize = MessageHeader::ENCODED_LENGTH + Self::BLOCK_LENGTH + 1;
63
64 pub fn decode(buf: &[u8]) -> Result<Self, StreamDecodeError> {
70 let header = MessageHeader::decode(buf)?;
71 header.validate_schema()?;
72
73 let mut cursor = SbeCursor::new_at(buf, MessageHeader::ENCODED_LENGTH);
74
75 let event_time_us = cursor.read_i64_le()?;
76 let book_update_id = cursor.read_i64_le()?;
77 let price_exponent = cursor.read_i8()?;
78 let qty_exponent = cursor.read_i8()?;
79 let bid_price_mantissa = cursor.read_i64_le()?;
80 let bid_qty_mantissa = cursor.read_i64_le()?;
81 let ask_price_mantissa = cursor.read_i64_le()?;
82 let ask_qty_mantissa = cursor.read_i64_le()?;
83
84 let symbol_str = cursor.read_var_string8()?;
85
86 Ok(Self {
87 event_time_us,
88 book_update_id,
89 price_exponent,
90 qty_exponent,
91 bid_price_mantissa,
92 bid_qty_mantissa,
93 ask_price_mantissa,
94 ask_qty_mantissa,
95 symbol: Ustr::from(&symbol_str),
96 })
97 }
98
99 #[inline]
101 #[must_use]
102 pub fn bid_price(&self) -> f64 {
103 super::mantissa_to_f64(self.bid_price_mantissa, self.price_exponent)
104 }
105
106 #[inline]
108 #[must_use]
109 pub fn bid_qty(&self) -> f64 {
110 super::mantissa_to_f64(self.bid_qty_mantissa, self.qty_exponent)
111 }
112
113 #[inline]
115 #[must_use]
116 pub fn ask_price(&self) -> f64 {
117 super::mantissa_to_f64(self.ask_price_mantissa, self.price_exponent)
118 }
119
120 #[inline]
122 #[must_use]
123 pub fn ask_qty(&self) -> f64 {
124 super::mantissa_to_f64(self.ask_qty_mantissa, self.qty_exponent)
125 }
126}
127
128#[cfg(test)]
129mod tests {
130 use rstest::rstest;
131
132 use super::*;
133 use crate::common::sbe::stream::{STREAM_SCHEMA_ID, template_id};
134
135 fn make_valid_buffer() -> Vec<u8> {
136 let mut buf = vec![0u8; 70];
137
138 buf[0..2].copy_from_slice(&50u16.to_le_bytes()); buf[2..4].copy_from_slice(&template_id::BEST_BID_ASK_STREAM_EVENT.to_le_bytes());
141 buf[4..6].copy_from_slice(&STREAM_SCHEMA_ID.to_le_bytes());
142 buf[6..8].copy_from_slice(&0u16.to_le_bytes()); let body = &mut buf[8..];
146 body[0..8].copy_from_slice(&1000000i64.to_le_bytes()); body[8..16].copy_from_slice(&12345i64.to_le_bytes()); body[16] = (-2i8) as u8; body[17] = (-8i8) as u8; body[18..26].copy_from_slice(&4200000i64.to_le_bytes()); body[26..34].copy_from_slice(&100000000i64.to_le_bytes()); body[34..42].copy_from_slice(&4200100i64.to_le_bytes()); body[42..50].copy_from_slice(&200000000i64.to_le_bytes()); body[50] = 7;
157 body[51..58].copy_from_slice(b"BTCUSDT");
158
159 buf
160 }
161
162 #[rstest]
163 fn test_decode_valid() {
164 let buf = make_valid_buffer();
165 let event = BestBidAskStreamEvent::decode(&buf).unwrap();
166
167 assert_eq!(event.event_time_us, 1000000);
168 assert_eq!(event.book_update_id, 12345);
169 assert_eq!(event.price_exponent, -2);
170 assert_eq!(event.qty_exponent, -8);
171 assert_eq!(event.symbol, "BTCUSDT");
172 assert!((event.bid_price() - 42000.0).abs() < 0.01);
173 }
174
175 #[rstest]
176 fn test_decode_truncated_header() {
177 let buf = [0u8; 5];
178 let err = BestBidAskStreamEvent::decode(&buf).unwrap_err();
179 assert!(matches!(err, StreamDecodeError::BufferTooShort { .. }));
180 }
181
182 #[rstest]
183 fn test_decode_truncated_body() {
184 let mut buf = make_valid_buffer();
185 buf.truncate(40); let err = BestBidAskStreamEvent::decode(&buf).unwrap_err();
187 assert!(matches!(err, StreamDecodeError::BufferTooShort { .. }));
188 }
189
190 #[rstest]
191 fn test_decode_wrong_schema() {
192 let mut buf = make_valid_buffer();
193 buf[4..6].copy_from_slice(&99u16.to_le_bytes()); let err = BestBidAskStreamEvent::decode(&buf).unwrap_err();
195 assert!(matches!(err, StreamDecodeError::SchemaMismatch { .. }));
196 }
197}