Skip to main content

binance/futures/
model.rs

1use serde::{Deserialize, Serialize};
2use crate::model::{string_or_float, string_or_float_opt, string_or_bool};
3
4pub use crate::model::{
5    Asks, Bids, BookTickers, Filters, KlineSummaries, KlineSummary, RateLimit, ServerTime,
6    SymbolPrice, Tickers,
7};
8
9#[derive(Debug, Serialize, Deserialize, Clone)]
10#[serde(rename_all = "camelCase")]
11pub struct ExchangeInformation {
12    pub timezone: String,
13    pub server_time: u64,
14    pub rate_limits: Vec<RateLimit>,
15    pub exchange_filters: Vec<String>,
16    pub symbols: Vec<Symbol>,
17}
18
19#[derive(Debug, Serialize, Deserialize, Clone)]
20#[serde(rename_all = "camelCase")]
21pub struct Symbol {
22    pub symbol: String,
23    pub status: String,
24    pub maint_margin_percent: String,
25    pub required_margin_percent: String,
26    pub base_asset: String,
27    pub quote_asset: String,
28    pub onboard_date: u128,
29    pub price_precision: u16,
30    pub quantity_precision: u16,
31    pub base_asset_precision: u64,
32    pub quote_precision: u64,
33    pub filters: Vec<Filters>,
34    pub order_types: Vec<String>,
35    pub time_in_force: Vec<String>,
36}
37
38#[derive(Debug, Serialize, Deserialize, Clone)]
39#[serde(rename_all = "camelCase")]
40pub struct OrderBook {
41    pub last_update_id: u64,
42    // Undocumented
43    #[serde(rename = "E")]
44    pub event_time: u64,
45    // Undocumented
46    #[serde(rename = "T")]
47    pub trade_order_time: u64,
48    pub bids: Vec<Bids>,
49    pub asks: Vec<Asks>,
50}
51
52#[derive(Debug, Serialize, Deserialize, Clone)]
53#[serde(rename_all = "camelCase")]
54pub struct PriceStats {
55    pub symbol: String,
56    pub price_change: String,
57    pub price_change_percent: String,
58    pub weighted_avg_price: String,
59    #[serde(with = "string_or_float")]
60    pub last_price: f64,
61    #[serde(with = "string_or_float")]
62    pub open_price: f64,
63    #[serde(with = "string_or_float")]
64    pub high_price: f64,
65    #[serde(with = "string_or_float")]
66    pub low_price: f64,
67    #[serde(with = "string_or_float")]
68    pub volume: f64,
69    #[serde(with = "string_or_float")]
70    pub quote_volume: f64,
71    #[serde(with = "string_or_float")]
72    pub last_qty: f64,
73    pub open_time: u64,
74    pub close_time: u64,
75    pub first_id: u64,
76    pub last_id: u64,
77    pub count: u64,
78}
79
80#[derive(Debug, Serialize, Deserialize, Clone)]
81#[serde(rename_all = "camelCase")]
82pub struct TradeHistory {
83    pub buyer: bool,
84    #[serde(with = "string_or_float")]
85    pub commission: f64,
86    pub commission_asset: String,
87    pub id: u64,
88    pub maker: bool,
89    pub order_id: u64,
90    #[serde(with = "string_or_float")]
91    pub price: f64,
92    #[serde(with = "string_or_float")]
93    pub qty: f64,
94    #[serde(with = "string_or_float")]
95    pub quote_qty: f64,
96    #[serde(with = "string_or_float")]
97    pub realized_pnl: f64,
98    pub side: String,
99    pub position_side: String,
100    pub symbol: String,
101    pub time: u64,
102}
103
104#[derive(Debug, Serialize, Deserialize, Clone)]
105#[serde(untagged)]
106pub enum Trades {
107    AllTrades(Vec<Trade>),
108}
109
110#[derive(Debug, Serialize, Deserialize, Clone)]
111#[serde(rename_all = "camelCase")]
112pub struct Trade {
113    pub id: u64,
114    pub is_buyer_maker: bool,
115    #[serde(with = "string_or_float")]
116    pub price: f64,
117    #[serde(with = "string_or_float")]
118    pub qty: f64,
119    #[serde(with = "string_or_float")]
120    pub quote_qty: f64,
121    pub time: u64,
122}
123
124#[derive(Debug, Serialize, Deserialize, Clone)]
125#[serde(untagged)]
126pub enum AggTrades {
127    AllAggTrades(Vec<AggTrade>),
128}
129
130#[derive(Debug, Serialize, Deserialize, Clone)]
131#[serde(rename_all = "camelCase")]
132pub struct AggTrade {
133    #[serde(rename = "T")]
134    pub time: u64,
135    #[serde(rename = "a")]
136    pub agg_id: u64,
137    #[serde(rename = "f")]
138    pub first_id: u64,
139    #[serde(rename = "l")]
140    pub last_id: u64,
141    #[serde(rename = "m")]
142    pub maker: bool,
143    #[serde(rename = "p", with = "string_or_float")]
144    pub price: f64,
145    #[serde(rename = "q", with = "string_or_float")]
146    pub qty: f64,
147}
148
149#[derive(Debug, Serialize, Deserialize, Clone)]
150#[serde(untagged)]
151pub enum MarkPrices {
152    AllMarkPrices(Vec<MarkPrice>),
153}
154
155#[derive(Debug, Serialize, Deserialize, Clone)]
156#[serde(rename_all = "camelCase")]
157pub struct MarkPrice {
158    pub symbol: String,
159    #[serde(with = "string_or_float")]
160    pub mark_price: f64,
161    #[serde(with = "string_or_float")]
162    pub last_funding_rate: f64,
163    pub next_funding_time: u64,
164    pub time: u64,
165}
166
167#[derive(Debug, Serialize, Deserialize, Clone)]
168#[serde(untagged)]
169pub enum LiquidationOrders {
170    AllLiquidationOrders(Vec<LiquidationOrder>),
171}
172
173#[derive(Debug, Serialize, Deserialize, Clone)]
174#[serde(rename_all = "camelCase")]
175pub struct LiquidationOrder {
176    #[serde(with = "string_or_float")]
177    pub average_price: f64,
178    #[serde(with = "string_or_float")]
179    pub executed_qty: f64,
180    #[serde(with = "string_or_float")]
181    pub orig_qty: f64,
182    #[serde(with = "string_or_float")]
183    pub price: f64,
184    pub side: String,
185    pub status: String,
186    pub symbol: String,
187    pub time: u64,
188    pub time_in_force: String,
189    pub r#type: String,
190}
191
192#[derive(Debug, Serialize, Deserialize, Clone)]
193#[serde(rename_all = "camelCase")]
194pub struct OpenInterest {
195    #[serde(with = "string_or_float")]
196    pub open_interest: f64,
197    pub symbol: String,
198}
199
200#[derive(Debug, Deserialize, PartialEq, Eq)]
201#[serde(rename_all = "camelCase")]
202pub struct OpenInterestHist {
203    pub symbol: String,
204    pub sum_open_interest: String,
205    pub sum_open_interest_value: String,
206    pub timestamp: u64,
207}
208
209#[derive(Debug, Deserialize, Clone)]
210#[serde(rename_all = "camelCase")]
211pub struct Order {
212    pub client_order_id: String,
213    #[serde(with = "string_or_float", default = "default_stop_price")]
214    pub cum_qty: f64,
215    #[serde(with = "string_or_float")]
216    pub cum_quote: f64,
217    #[serde(with = "string_or_float")]
218    pub executed_qty: f64,
219    pub order_id: u64,
220    #[serde(with = "string_or_float")]
221    pub avg_price: f64,
222    #[serde(with = "string_or_float")]
223    pub orig_qty: f64,
224    #[serde(with = "string_or_float")]
225    pub price: f64,
226    pub side: String,
227    pub reduce_only: bool,
228    pub position_side: String,
229    pub status: String,
230    #[serde(with = "string_or_float", default = "default_stop_price")]
231    pub stop_price: f64,
232    pub close_position: bool,
233    pub symbol: String,
234    pub time_in_force: String,
235    #[serde(rename = "type")]
236    pub order_type: String,
237    pub orig_type: String,
238    #[serde(with = "string_or_float", default = "default_activation_price")]
239    pub activation_price: f64,
240    #[serde(with = "string_or_float", default = "default_price_rate")]
241    pub price_rate: f64,
242    pub update_time: u64,
243    pub working_type: String,
244    pub price_protect: bool,
245}
246
247#[derive(Debug, Serialize, Deserialize, Clone)]
248#[serde(rename_all = "camelCase")]
249pub struct Transaction {
250    pub client_order_id: String,
251    #[serde(with = "string_or_float")]
252    pub cum_qty: f64,
253    #[serde(with = "string_or_float")]
254    pub cum_quote: f64,
255    #[serde(with = "string_or_float")]
256    pub executed_qty: f64,
257    pub order_id: u64,
258    #[serde(with = "string_or_float")]
259    pub avg_price: f64,
260    #[serde(with = "string_or_float")]
261    pub orig_qty: f64,
262    pub reduce_only: bool,
263    pub side: String,
264    pub position_side: String,
265    pub status: String,
266    #[serde(with = "string_or_float")]
267    pub stop_price: f64,
268    pub close_position: bool,
269    pub symbol: String,
270    pub time_in_force: String,
271    #[serde(rename = "type")]
272    pub type_name: String,
273    pub orig_type: String,
274    #[serde(default)]
275    #[serde(with = "string_or_float_opt")]
276    pub activate_price: Option<f64>,
277    #[serde(default)]
278    #[serde(with = "string_or_float_opt")]
279    pub price_rate: Option<f64>,
280    pub update_time: u64,
281    pub working_type: String,
282    price_protect: bool,
283}
284
285#[derive(Debug, Serialize, Deserialize, Clone)]
286#[serde(rename_all = "camelCase")]
287pub struct CanceledOrder {
288    pub client_order_id: String,
289    #[serde(with = "string_or_float")]
290    pub cum_qty: f64,
291    #[serde(with = "string_or_float")]
292    pub cum_quote: f64,
293    #[serde(with = "string_or_float")]
294    pub executed_qty: f64,
295    pub order_id: u64,
296    #[serde(with = "string_or_float")]
297    pub orig_qty: f64,
298    pub orig_type: String,
299    #[serde(with = "string_or_float")]
300    pub price: f64,
301    pub reduce_only: bool,
302    pub side: String,
303    pub position_side: String,
304    pub status: String,
305    #[serde(with = "string_or_float")]
306    pub stop_price: f64,
307    pub close_position: bool,
308    pub symbol: String,
309    pub time_in_force: String,
310    #[serde(rename = "type")]
311    pub type_name: String,
312    #[serde(default)]
313    #[serde(with = "string_or_float_opt")]
314    pub activate_price: Option<f64>,
315    #[serde(default)]
316    #[serde(with = "string_or_float_opt")]
317    pub price_rate: Option<f64>,
318    pub update_time: u64,
319    pub working_type: String,
320    price_protect: bool,
321}
322
323#[derive(Debug, Serialize, Deserialize, Clone)]
324#[serde(rename_all = "camelCase")]
325pub struct PositionRisk {
326    #[serde(with = "string_or_float")]
327    pub entry_price: f64,
328    pub margin_type: String,
329    #[serde(with = "string_or_bool")]
330    pub is_auto_add_margin: bool,
331    #[serde(with = "string_or_float")]
332    pub isolated_margin: f64,
333    pub leverage: String,
334    #[serde(with = "string_or_float")]
335    pub liquidation_price: f64,
336    #[serde(with = "string_or_float")]
337    pub mark_price: f64,
338    #[serde(with = "string_or_float")]
339    pub max_notional_value: f64,
340    #[serde(with = "string_or_float", rename = "positionAmt")]
341    pub position_amount: f64,
342    pub symbol: String,
343    #[serde(with = "string_or_float", rename = "unRealizedProfit")]
344    pub unrealized_profit: f64,
345    pub position_side: String,
346    #[serde(with = "string_or_float")]
347    pub notional: f64,
348    #[serde(with = "string_or_float")]
349    pub isolated_wallet: f64,
350    pub update_time: u64,
351}
352
353#[derive(Debug, Serialize, Deserialize, Clone)]
354#[serde(rename_all = "camelCase")]
355pub struct FuturesAsset {
356    pub asset: String,
357    #[serde(with = "string_or_float")]
358    pub wallet_balance: f64,
359    #[serde(with = "string_or_float")]
360    pub unrealized_profit: f64,
361    #[serde(with = "string_or_float")]
362    pub margin_balance: f64,
363    #[serde(with = "string_or_float")]
364    pub maint_margin: f64,
365    #[serde(with = "string_or_float")]
366    pub initial_margin: f64,
367    #[serde(with = "string_or_float")]
368    pub position_initial_margin: f64,
369    #[serde(with = "string_or_float")]
370    pub open_order_initial_margin: f64,
371    #[serde(with = "string_or_float")]
372    pub max_withdraw_amount: f64,
373    #[serde(with = "string_or_float")]
374    pub cross_wallet_balance: f64,
375    #[serde(with = "string_or_float")]
376    pub cross_un_pnl: f64,
377    #[serde(with = "string_or_float")]
378    pub available_balance: f64,
379    #[serde(with = "string_or_bool")]
380    pub margin_available: bool,
381    pub update_time: u64,
382}
383
384#[derive(Debug, Serialize, Deserialize, Clone)]
385#[serde(rename_all = "camelCase")]
386pub struct FuturesPosition {
387    pub symbol: String,
388    #[serde(with = "string_or_float")]
389    pub initial_margin: f64,
390    #[serde(with = "string_or_float")]
391    pub maint_margin: f64,
392    #[serde(with = "string_or_float")]
393    pub unrealized_profit: f64,
394    #[serde(with = "string_or_float")]
395    pub position_initial_margin: f64,
396    #[serde(with = "string_or_float")]
397    pub open_order_initial_margin: f64,
398    pub leverage: String,
399    #[serde(with = "string_or_bool")]
400    pub isolated: bool,
401    #[serde(with = "string_or_float")]
402    pub entry_price: f64,
403    #[serde(with = "string_or_float")]
404    pub max_notional: f64,
405    pub position_side: String,
406    #[serde(with = "string_or_float", rename = "positionAmt")]
407    pub position_amount: f64,
408    #[serde(with = "string_or_float")]
409    pub notional: f64,
410    #[serde(with = "string_or_float")]
411    pub isolated_wallet: f64,
412    pub update_time: u64,
413    #[serde(with = "string_or_float")]
414    pub bid_notional: f64,
415    #[serde(with = "string_or_float")]
416    pub ask_notional: f64,
417}
418
419#[derive(Debug, Serialize, Deserialize, Clone)]
420#[serde(rename_all = "camelCase")]
421pub struct AccountInformation {
422    #[serde(with = "string_or_float")]
423    pub fee_tier: f64,
424    #[serde(with = "string_or_bool")]
425    pub can_trade: bool,
426    #[serde(with = "string_or_bool")]
427    pub can_deposit: bool,
428    #[serde(with = "string_or_bool")]
429    pub can_withdraw: bool,
430    #[serde(with = "string_or_float")]
431    pub update_time: f64,
432    #[serde(with = "string_or_float")]
433    pub total_initial_margin: f64,
434    #[serde(with = "string_or_float")]
435    pub total_maint_margin: f64,
436    #[serde(with = "string_or_float")]
437    pub total_wallet_balance: f64,
438    #[serde(with = "string_or_float")]
439    pub total_unrealized_profit: f64,
440    #[serde(with = "string_or_float")]
441    pub total_margin_balance: f64,
442    #[serde(with = "string_or_float")]
443    pub total_position_initial_margin: f64,
444    #[serde(with = "string_or_float")]
445    pub total_open_order_initial_margin: f64,
446    #[serde(with = "string_or_float")]
447    pub total_cross_wallet_balance: f64,
448    #[serde(with = "string_or_float")]
449    pub total_cross_un_pnl: f64,
450    #[serde(with = "string_or_float")]
451    pub available_balance: f64,
452    #[serde(with = "string_or_float")]
453    pub max_withdraw_amount: f64,
454    pub assets: Vec<FuturesAsset>,
455    pub positions: Vec<FuturesPosition>,
456}
457
458#[derive(Debug, Serialize, Deserialize, Clone)]
459#[serde(rename_all = "camelCase")]
460pub struct AccountBalance {
461    pub account_alias: String,
462    pub asset: String,
463    #[serde(with = "string_or_float")]
464    pub balance: f64,
465    #[serde(with = "string_or_float")]
466    pub cross_wallet_balance: f64,
467    #[serde(with = "string_or_float", rename = "crossUnPnl")]
468    pub cross_unrealized_pnl: f64,
469    #[serde(with = "string_or_float")]
470    pub available_balance: f64,
471    #[serde(with = "string_or_float")]
472    pub max_withdraw_amount: f64,
473    pub margin_available: bool,
474    pub update_time: u64,
475}
476
477#[derive(Debug, Serialize, Deserialize, Clone)]
478#[serde(rename_all = "camelCase")]
479pub struct ChangeLeverageResponse {
480    pub leverage: u8,
481    #[serde(with = "string_or_float")]
482    pub max_notional_value: f64,
483    pub symbol: String,
484}
485
486fn default_stop_price() -> f64 {
487    0.0
488}
489fn default_activation_price() -> f64 {
490    0.0
491}
492fn default_price_rate() -> f64 {
493    0.0
494}
495
496#[derive(Debug, Serialize, Deserialize, Clone)]
497#[serde(rename_all = "camelCase")]
498pub struct OrderUpdate {
499    #[serde(rename = "s")]
500    pub symbol: String,
501
502    #[serde(rename = "c")]
503    pub new_client_order_id: String,
504
505    #[serde(rename = "S")]
506    pub side: String,
507
508    #[serde(rename = "o")]
509    pub order_type: String,
510
511    #[serde(rename = "f")]
512    pub time_in_force: String,
513
514    #[serde(rename = "q")]
515    pub qty: String,
516
517    #[serde(rename = "p")]
518    pub price: String,
519
520    #[serde(rename = "ap")]
521    pub average_price: String,
522
523    #[serde(rename = "sp")]
524    pub stop_price: String,
525
526    #[serde(rename = "x")]
527    pub execution_type: String,
528
529    #[serde(rename = "X")]
530    pub order_status: String,
531
532    #[serde(rename = "i")]
533    pub order_id: u64,
534
535    #[serde(rename = "l")]
536    pub qty_last_filled_trade: String,
537
538    #[serde(rename = "z")]
539    pub accumulated_qty_filled_trades: String,
540
541    #[serde(rename = "L")]
542    pub price_last_filled_trade: String,
543
544    #[serde(skip, rename = "N")]
545    pub asset_commisioned: Option<String>,
546
547    #[serde(rename = "n")]
548    pub commission: Option<String>,
549
550    #[serde(rename = "T")]
551    pub trade_order_time: u64,
552
553    #[serde(rename = "t")]
554    pub trade_id: i64,
555
556    #[serde(rename = "b")]
557    pub bids_notional: String,
558
559    #[serde(rename = "a")]
560    pub ask_notional: String,
561
562    #[serde(rename = "m")]
563    pub is_buyer_maker: bool,
564
565    #[serde(rename = "R")]
566    pub is_reduce_only: bool,
567
568    #[serde(rename = "wt")]
569    pub stop_price_working_type: String,
570
571    #[serde(rename = "ot")]
572    pub original_order_type: String,
573
574    #[serde(rename = "ps")]
575    pub position_side: String,
576
577    #[serde(rename = "cp")]
578    pub close_all: Option<bool>,
579
580    #[serde(rename = "AP")]
581    pub activation_price: Option<String>,
582
583    #[serde(rename = "cr")]
584    pub callback_rate: Option<String>,
585
586    #[serde(rename = "pP")]
587    pub pp_ignore: bool,
588
589    #[serde(rename = "si")]
590    pub si_ignore: i32,
591
592    #[serde(rename = "ss")]
593    pub ss_ignore: i32,
594
595    #[serde(rename = "rp")]
596    pub realized_profit: String,
597}
598
599#[derive(Debug, Serialize, Deserialize, Clone)]
600#[serde(rename_all = "camelCase")]
601pub struct OrderTradeEvent {
602    #[serde(rename = "e")]
603    pub event_type: String,
604
605    #[serde(rename = "E")]
606    pub event_time: u64,
607
608    #[serde(rename = "T")]
609    pub transaction_time: u64,
610
611    #[serde(rename = "o")]
612    pub order: OrderUpdate,
613}
614
615#[derive(Debug, Serialize, Deserialize, Clone)]
616#[serde(rename_all = "camelCase")]
617pub struct Income {
618    pub symbol: String,
619    pub income_type: String,
620    #[serde(with = "string_or_float")]
621    pub income: f64,
622    pub asset: String,
623    pub info: String,
624    pub time: u64,
625    pub tran_id: u64,
626    pub trade_id: String,
627}