rusty_feeder/exchange/binance/common/
types.rs

1//! Common data types for Binance API integrations
2//!
3//! This module provides optimized data structures for high-frequency trading
4//! with Binance APIs, focusing on minimal allocations and zero-copy processing.
5
6use rust_decimal::Decimal;
7use serde::{Deserialize, Serialize};
8use smallvec::SmallVec;
9use smartstring::alias::String;
10use std::fmt::Debug;
11
12/// Symbol information from Binance exchange
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct BinanceSymbolInfo {
15    /// Symbol name (e.g., "BTCUSDT")
16    pub symbol: String,
17
18    /// Base asset (e.g., "BTC")
19    #[serde(rename = "baseAsset")]
20    pub base_asset: String,
21
22    /// Quote asset (e.g., "USDT")
23    #[serde(rename = "quoteAsset")]
24    pub quote_asset: String,
25
26    /// Minimum order quantity
27    #[serde(rename = "minQty")]
28    pub min_qty: Decimal,
29
30    /// Maximum order quantity
31    #[serde(rename = "maxQty")]
32    pub max_qty: Decimal,
33
34    /// Step size for quantity
35    #[serde(rename = "stepSize")]
36    pub step_size: Decimal,
37
38    /// Minimum price
39    #[serde(rename = "minPrice")]
40    pub min_price: Decimal,
41
42    /// Maximum price
43    #[serde(rename = "maxPrice")]
44    pub max_price: Decimal,
45
46    /// Tick size for price
47    #[serde(rename = "tickSize")]
48    pub tick_size: Decimal,
49
50    /// Minimum notional value
51    #[serde(rename = "minNotional")]
52    pub min_notional: Decimal,
53
54    /// Whether iceberg orders are allowed
55    #[serde(rename = "icebergAllowed")]
56    pub iceberg_allowed: bool,
57
58    /// Whether the symbol is currently trading
59    pub status: String,
60
61    /// Base asset precision
62    #[serde(rename = "baseAssetPrecision")]
63    pub base_asset_precision: u8,
64
65    /// Quote asset precision
66    #[serde(rename = "quoteAssetPrecision")]
67    pub quote_asset_precision: u8,
68
69    /// Order types allowed for this symbol
70    #[serde(rename = "orderTypes")]
71    pub order_types: SmallVec<[String; 8]>,
72
73    /// Permissions (e.g., "SPOT", "MARGIN")
74    pub permissions: SmallVec<[String; 4]>,
75}
76
77/// Binance orderbook update structure
78/// Optimized for minimal allocations with SmallVec
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct BinanceOrderbookUpdate {
81    /// Last update ID
82    pub last_update_id: u64,
83
84    /// First update ID in event
85    #[serde(rename = "U")]
86    pub first_update_id: u64,
87
88    /// Final update ID in event
89    #[serde(rename = "u")]
90    pub final_update_id: u64,
91
92    /// Bid updates (price, quantity)
93    #[serde(default)]
94    pub bids: SmallVec<[(String, String); 32]>,
95
96    /// Ask updates (price, quantity)
97    #[serde(default)]
98    pub asks: SmallVec<[(String, String); 32]>,
99
100    /// Event timestamp
101    #[serde(default)]
102    pub event_time: Option<u64>,
103}
104
105/// Binance orderbook snapshot response
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct BinanceOrderbookSnapshot {
108    /// Last update ID
109    pub last_update_id: u64,
110
111    /// Bid entries (price, quantity)
112    pub bids: SmallVec<[[String; 2]; 32]>,
113
114    /// Ask entries (price, quantity)
115    pub asks: SmallVec<[[String; 2]; 32]>,
116}
117
118/// Binance trade update structure
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct BinanceTradeUpdate {
121    /// Event type
122    #[serde(rename = "e")]
123    pub event_type: String,
124
125    /// Event time
126    #[serde(rename = "E")]
127    pub event_time: u64,
128
129    /// Symbol
130    #[serde(rename = "s")]
131    pub symbol: String,
132
133    /// Trade ID
134    #[serde(rename = "t")]
135    pub trade_id: u64,
136
137    /// Price
138    #[serde(rename = "p")]
139    pub price: String,
140
141    /// Quantity
142    #[serde(rename = "q")]
143    pub quantity: String,
144
145    /// Buyer order ID
146    #[serde(rename = "b")]
147    pub buyer_order_id: u64,
148
149    /// Seller order ID
150    #[serde(rename = "a")]
151    pub seller_order_id: u64,
152
153    /// Trade time
154    #[serde(rename = "T")]
155    pub trade_time: u64,
156
157    /// Is buyer market maker flag
158    #[serde(rename = "m")]
159    pub is_buyer_maker: bool,
160
161    /// Is this trade the best price match
162    #[serde(rename = "M", default)]
163    pub is_best_match: bool,
164}
165
166/// Binance ticker structure
167#[derive(Debug, Clone, Serialize, Deserialize)]
168pub struct BinanceTicker {
169    /// Symbol
170    pub symbol: String,
171
172    /// Price change
173    #[serde(rename = "priceChange")]
174    pub price_change: String,
175
176    /// Price change percent
177    #[serde(rename = "priceChangePercent")]
178    pub price_change_percent: String,
179
180    /// Weighted average price
181    #[serde(rename = "weightedAvgPrice")]
182    pub weighted_avg_price: String,
183
184    /// Last price
185    #[serde(rename = "lastPrice")]
186    pub last_price: String,
187
188    /// Last quantity
189    #[serde(rename = "lastQty")]
190    pub last_qty: String,
191
192    /// Bid price
193    #[serde(rename = "bidPrice")]
194    pub bid_price: String,
195
196    /// Bid quantity
197    #[serde(rename = "bidQty")]
198    pub bid_qty: String,
199
200    /// Ask price
201    #[serde(rename = "askPrice")]
202    pub ask_price: String,
203
204    /// Ask quantity
205    #[serde(rename = "askQty")]
206    pub ask_qty: String,
207
208    /// Open price
209    #[serde(rename = "openPrice")]
210    pub open_price: String,
211
212    /// High price
213    #[serde(rename = "highPrice")]
214    pub high_price: String,
215
216    /// Low price
217    #[serde(rename = "lowPrice")]
218    pub low_price: String,
219
220    /// Volume
221    pub volume: String,
222
223    /// Quote volume
224    #[serde(rename = "quoteVolume")]
225    pub quote_volume: String,
226
227    /// Open time
228    #[serde(rename = "openTime")]
229    pub open_time: u64,
230
231    /// Close time
232    #[serde(rename = "closeTime")]
233    pub close_time: u64,
234
235    /// First trade ID
236    #[serde(rename = "firstId")]
237    pub first_id: u64,
238
239    /// Last trade ID
240    #[serde(rename = "lastId")]
241    pub last_id: u64,
242
243    /// Trade count
244    pub count: u64,
245}
246
247/// Stream subscription response
248#[derive(Debug, Clone, Serialize, Deserialize)]
249pub struct BinanceSubscriptionResponse {
250    /// Request ID
251    pub id: u64,
252
253    /// Response result
254    pub result: Option<String>,
255
256    /// Error code
257    pub code: Option<i32>,
258
259    /// Error message
260    pub msg: Option<String>,
261}
262
263/// Binance API error response
264#[derive(Debug, Clone, Serialize, Deserialize)]
265pub struct BinanceErrorResponse {
266    /// Error code
267    pub code: i32,
268
269    /// Error message
270    pub msg: String,
271}
272
273/// Binance user data listen key response
274#[derive(Debug, Clone, Serialize, Deserialize)]
275pub struct BinanceListenKeyResponse {
276    /// Listen key for user data stream
277    #[serde(rename = "listenKey")]
278    pub listen_key: String,
279}
280
281/// Binance exchange info response
282#[derive(Debug, Clone, Serialize, Deserialize)]
283pub struct BinanceExchangeInfoResponse {
284    /// Exchange timezone
285    pub timezone: String,
286
287    /// Server time
288    #[serde(rename = "serverTime")]
289    pub server_time: u64,
290
291    /// Rate limits
292    #[serde(rename = "rateLimits")]
293    pub rate_limits: Vec<BinanceRateLimit>,
294
295    /// Exchange filters
296    #[serde(default, rename = "exchangeFilters")]
297    pub exchange_filters: Vec<simd_json::OwnedValue>,
298
299    /// Symbol info
300    pub symbols: Vec<BinanceSymbolInfo>,
301}
302
303/// Binance rate limit information
304#[derive(Debug, Clone, Serialize, Deserialize)]
305pub struct BinanceRateLimit {
306    /// Rate limit type (REQUEST_WEIGHT, ORDERS, RAW_REQUESTS)
307    #[serde(rename = "rateLimitType")]
308    pub rate_limit_type: String,
309
310    /// Interval (SECOND, MINUTE, DAY)
311    pub interval: String,
312
313    /// Interval number
314    #[serde(rename = "intervalNum")]
315    pub interval_num: u32,
316
317    /// Rate limit
318    pub limit: u64,
319}
320
321/// Binance historical trades response
322#[derive(Debug, Clone, Serialize, Deserialize)]
323pub struct BinanceHistoricalTrade {
324    /// Trade ID
325    pub id: u64,
326
327    /// Price
328    pub price: String,
329
330    /// Quantity
331    pub qty: String,
332
333    /// Quote quantity
334    #[serde(rename = "quoteQty")]
335    pub quote_qty: String,
336
337    /// Timestamp
338    pub time: u64,
339
340    /// Is buyer maker flag
341    #[serde(rename = "isBuyerMaker")]
342    pub is_buyer_maker: bool,
343
344    /// Is best match flag
345    #[serde(rename = "isBestMatch")]
346    pub is_best_match: bool,
347}