rusty_feeder/exchange/binance/spot/
types.rs

1//! Type definitions and constants for Binance Spot markets
2//!
3//! This module contains the types and constants used for Binance Spot API interactions,
4//! including WebSocket and REST API endpoints.
5
6use crate::provider::prelude::*;
7use smartstring::alias::String;
8
9/// Base URL for Binance Spot REST API
10pub const BINANCE_SPOT_API_URL: &str = "https://api.binance.com";
11
12/// Base URL for Binance Spot WebSocket API
13pub const BINANCE_SPOT_WS_URL: &str = "wss://stream.binance.com:9443/ws";
14
15/// Base URL for Binance Spot WebSocket combined streams
16pub const BINANCE_SPOT_WS_COMBINED_URL: &str = "wss://stream.binance.com:9443/stream";
17
18/// Rate limits for Binance Spot API
19pub const BINANCE_SPOT_RATE_LIMITS: &[RateLimit] = &[
20    RateLimit {
21        limit_type: "REQUEST_WEIGHT",
22        interval: "MINUTE",
23        interval_num: 1,
24        limit: 1200,
25    },
26    RateLimit {
27        limit_type: "RAW_REQUESTS",
28        interval: "MINUTE",
29        interval_num: 5,
30        limit: 6100,
31    },
32    RateLimit {
33        limit_type: "ORDERS",
34        interval: "SECOND",
35        interval_num: 10,
36        limit: 50,
37    },
38    RateLimit {
39        limit_type: "ORDERS",
40        interval: "DAY",
41        interval_num: 1,
42        limit: 160000,
43    },
44];
45
46/// Order types supported by Binance Spot
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum BinanceOrderType {
49    /// Limit order (price required)
50    Limit,
51
52    /// Market order (quantity required)
53    Market,
54
55    /// Stop loss order (stop price required)
56    StopLoss,
57
58    /// Stop loss limit order (price and stop price required)
59    StopLossLimit,
60
61    /// Take profit order (stop price required)
62    TakeProfit,
63
64    /// Take profit limit order (price and stop price required)
65    TakeProfitLimit,
66
67    /// Limit maker order (price required, rejected if it would be a taker)
68    LimitMaker,
69}
70
71impl BinanceOrderType {
72    /// Returns the string representation of the order type
73    pub const fn as_str(&self) -> &'static str {
74        match self {
75            BinanceOrderType::Limit => "LIMIT",
76            BinanceOrderType::Market => "MARKET",
77            BinanceOrderType::StopLoss => "STOP_LOSS",
78            BinanceOrderType::StopLossLimit => "STOP_LOSS_LIMIT",
79            BinanceOrderType::TakeProfit => "TAKE_PROFIT",
80            BinanceOrderType::TakeProfitLimit => "TAKE_PROFIT_LIMIT",
81            BinanceOrderType::LimitMaker => "LIMIT_MAKER",
82        }
83    }
84
85    /// Converts order type to smartstring format
86    pub fn to_smartstring(&self) -> String {
87        self.as_str().into()
88    }
89}
90
91/// Order side (Buy or Sell)
92#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub enum BinanceOrderSide {
94    /// Buy order
95    Buy,
96
97    /// Sell order
98    Sell,
99}
100
101impl BinanceOrderSide {
102    /// Returns the string representation of the order side
103    pub const fn as_str(&self) -> &'static str {
104        match self {
105            BinanceOrderSide::Buy => "BUY",
106            BinanceOrderSide::Sell => "SELL",
107        }
108    }
109
110    /// Converts order side to smartstring format
111    pub fn to_smartstring(&self) -> String {
112        self.as_str().into()
113    }
114}
115
116/// Time in force options for orders
117#[derive(Debug, Clone, Copy, PartialEq, Eq)]
118pub enum BinanceTimeInForce {
119    /// Good Till Cancelled
120    GTC,
121
122    /// Immediate or Cancel
123    IOC,
124
125    /// Fill or Kill
126    FOK,
127}
128
129impl BinanceTimeInForce {
130    /// Returns the string representation of the time in force
131    pub const fn as_str(&self) -> &'static str {
132        match self {
133            BinanceTimeInForce::GTC => "GTC",
134            BinanceTimeInForce::IOC => "IOC",
135            BinanceTimeInForce::FOK => "FOK",
136        }
137    }
138
139    /// Converts time in force to smartstring format
140    pub fn to_smartstring(&self) -> String {
141        self.as_str().into()
142    }
143}
144
145/// Order status values
146#[derive(Debug, Clone, Copy, PartialEq, Eq)]
147pub enum BinanceOrderStatus {
148    /// Order has been accepted by the engine
149    New,
150
151    /// A part of the order has been filled
152    PartiallyFilled,
153
154    /// The order has been completely filled
155    Filled,
156
157    /// The order has been canceled
158    Canceled,
159
160    /// The order is pending cancel
161    PendingCancel,
162
163    /// The order was rejected
164    Rejected,
165
166    /// The order was expired
167    Expired,
168}
169
170impl BinanceOrderStatus {
171    /// Returns the string representation of the order status
172    pub const fn as_str(&self) -> &'static str {
173        match self {
174            BinanceOrderStatus::New => "NEW",
175            BinanceOrderStatus::PartiallyFilled => "PARTIALLY_FILLED",
176            BinanceOrderStatus::Filled => "FILLED",
177            BinanceOrderStatus::Canceled => "CANCELED",
178            BinanceOrderStatus::PendingCancel => "PENDING_CANCEL",
179            BinanceOrderStatus::Rejected => "REJECTED",
180            BinanceOrderStatus::Expired => "EXPIRED",
181        }
182    }
183
184    /// Converts order status to smartstring format
185    pub fn to_smartstring(&self) -> String {
186        self.as_str().into()
187    }
188}
189
190/// Kline/Candlestick intervals
191#[derive(Debug, Clone, Copy, PartialEq, Eq)]
192pub enum BinanceKlineInterval {
193    /// 1 minute
194    Minute1,
195
196    /// 3 minutes
197    Minute3,
198
199    /// 5 minutes
200    Minute5,
201
202    /// 15 minutes
203    Minute15,
204
205    /// 30 minutes
206    Minute30,
207
208    /// 1 hour
209    Hour1,
210
211    /// 2 hours
212    Hour2,
213
214    /// 4 hours
215    Hour4,
216
217    /// 6 hours
218    Hour6,
219
220    /// 8 hours
221    Hour8,
222
223    /// 12 hours
224    Hour12,
225
226    /// 1 day
227    Day1,
228
229    /// 3 days
230    Day3,
231
232    /// 1 week
233    Week1,
234
235    /// 1 month
236    Month1,
237}
238
239impl BinanceKlineInterval {
240    /// Returns the string representation of the kline interval
241    pub const fn as_str(&self) -> &'static str {
242        match self {
243            BinanceKlineInterval::Minute1 => "1m",
244            BinanceKlineInterval::Minute3 => "3m",
245            BinanceKlineInterval::Minute5 => "5m",
246            BinanceKlineInterval::Minute15 => "15m",
247            BinanceKlineInterval::Minute30 => "30m",
248            BinanceKlineInterval::Hour1 => "1h",
249            BinanceKlineInterval::Hour2 => "2h",
250            BinanceKlineInterval::Hour4 => "4h",
251            BinanceKlineInterval::Hour6 => "6h",
252            BinanceKlineInterval::Hour8 => "8h",
253            BinanceKlineInterval::Hour12 => "12h",
254            BinanceKlineInterval::Day1 => "1d",
255            BinanceKlineInterval::Day3 => "3d",
256            BinanceKlineInterval::Week1 => "1w",
257            BinanceKlineInterval::Month1 => "1M",
258        }
259    }
260
261    /// Converts kline interval to smartstring format
262    pub fn to_smartstring(&self) -> String {
263        self.as_str().into()
264    }
265}