nautilus_binance/spot/http/
models.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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//! Binance Spot HTTP response models.
17//!
18//! These models represent Binance venue-specific response types decoded from SBE.
19
20/// Price/quantity level in an order book.
21#[derive(Debug, Clone, Copy, PartialEq)]
22pub struct BinancePriceLevel {
23    /// Price mantissa (multiply by 10^exponent to get actual price).
24    pub price_mantissa: i64,
25    /// Quantity mantissa (multiply by 10^exponent to get actual quantity).
26    pub qty_mantissa: i64,
27}
28
29impl BinancePriceLevel {
30    /// Converts the price mantissa to f64 using the given exponent.
31    #[must_use]
32    pub fn price_f64(&self, exponent: i8) -> f64 {
33        self.price_mantissa as f64 * 10_f64.powi(exponent as i32)
34    }
35
36    /// Converts the quantity mantissa to f64 using the given exponent.
37    #[must_use]
38    pub fn qty_f64(&self, exponent: i8) -> f64 {
39        self.qty_mantissa as f64 * 10_f64.powi(exponent as i32)
40    }
41}
42
43/// Binance order book depth response.
44#[derive(Debug, Clone, PartialEq)]
45pub struct BinanceDepth {
46    /// Last update ID for this depth snapshot.
47    pub last_update_id: i64,
48    /// Price exponent for all price levels.
49    pub price_exponent: i8,
50    /// Quantity exponent for all quantity values.
51    pub qty_exponent: i8,
52    /// Bid price levels (best bid first).
53    pub bids: Vec<BinancePriceLevel>,
54    /// Ask price levels (best ask first).
55    pub asks: Vec<BinancePriceLevel>,
56}
57
58/// A single trade from Binance.
59#[derive(Debug, Clone, PartialEq)]
60pub struct BinanceTrade {
61    /// Trade ID.
62    pub id: i64,
63    /// Price mantissa.
64    pub price_mantissa: i64,
65    /// Quantity mantissa.
66    pub qty_mantissa: i64,
67    /// Quote quantity mantissa (price * qty).
68    pub quote_qty_mantissa: i64,
69    /// Trade timestamp in milliseconds.
70    pub time: i64,
71    /// Whether the buyer is the maker.
72    pub is_buyer_maker: bool,
73    /// Whether this trade is the best price match.
74    pub is_best_match: bool,
75}
76
77impl BinanceTrade {
78    /// Converts the price mantissa to f64 using the given exponent.
79    #[must_use]
80    pub fn price_f64(&self, exponent: i8) -> f64 {
81        self.price_mantissa as f64 * 10_f64.powi(exponent as i32)
82    }
83
84    /// Converts the quantity mantissa to f64 using the given exponent.
85    #[must_use]
86    pub fn qty_f64(&self, exponent: i8) -> f64 {
87        self.qty_mantissa as f64 * 10_f64.powi(exponent as i32)
88    }
89}
90
91/// Binance trades response.
92#[derive(Debug, Clone, PartialEq)]
93pub struct BinanceTrades {
94    /// Price exponent for all trades.
95    pub price_exponent: i8,
96    /// Quantity exponent for all trades.
97    pub qty_exponent: i8,
98    /// List of trades.
99    pub trades: Vec<BinanceTrade>,
100}