binance_client/http_exchange_api_v2/data/
symbol.rs

1//!
2//! The products symbol.
3//!
4
5use rust_decimal::Decimal;
6use serde::Deserialize;
7
8use crate::http_exchange_api_v2::data::status::Status;
9
10///
11/// The trading symbol data.
12///
13#[derive(Debug, Deserialize, Clone)]
14#[serde(rename_all = "camelCase")]
15pub struct Symbol {
16    /// The symbol name.
17    #[serde(rename = "s")]
18    pub symbol: String,
19    /// The symbol status on the exchange.
20    #[serde(rename = "st")]
21    pub status: Status,
22    /// The secondary token in the trading pair.
23    #[serde(rename = "b")]
24    pub base_asset: String,
25    /// The primary token in the trading pair.
26    #[serde(rename = "q")]
27    pub quote_asset: String,
28    /// The price tick size.
29    #[serde(rename = "ts")]
30    pub price_tick: Decimal,
31    /// The quantity tick size.
32    #[serde(rename = "i")]
33    pub quantity_tick: Decimal,
34    /// The market open timestamp, if the symbol is not being traded yet.
35    #[serde(rename = "planToOpenMarketTime")]
36    pub plan_to_open_market_time: Option<u64>,
37}
38
39impl Symbol {
40    ///
41    /// If the symbol is active and can be normally traded.
42    ///
43    pub fn is_trading(&self) -> bool {
44        matches!(self.status, Status::Trading)
45    }
46
47    ///
48    /// The number of fractional digits in the symbol price.
49    ///
50    /// E.g. `0.000256` has `6` fractional digits.
51    /// E.g. `0.42` has `2` fractional digits.
52    ///
53    pub fn price_precision(&self) -> u32 {
54        let mut scale = crate::r#const::PRECISION_DEFAULT;
55        let mut tick_size_check = Decimal::new(1, scale);
56        while self.price_tick > tick_size_check && scale > 0 {
57            tick_size_check *= Decimal::new(10, 0);
58            scale -= 1;
59        }
60        scale
61    }
62
63    ///
64    /// The number of fractional digits in the symbol quantity.
65    ///
66    pub fn quantity_precision(&self) -> u32 {
67        let mut scale = crate::r#const::PRECISION_DEFAULT;
68        let mut step_size_check = Decimal::new(1, scale);
69        while self.quantity_tick > step_size_check && scale > 0 {
70            step_size_check *= Decimal::new(10, 0);
71            scale -= 1;
72        }
73        scale
74    }
75}