binance-api-client 0.1.0

Async Rust client for Binance Spot REST and WebSocket APIs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Common types used across the Binance API.
//!
//! This module contains enums and types that are shared between
//! different API endpoints.

use serde::{Deserialize, Serialize};

/// Order side (buy or sell).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderSide {
    /// Buy order
    #[default]
    Buy,
    /// Sell order
    Sell,
}

/// Order type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderType {
    /// Limit order - specify price and quantity
    Limit,
    /// Market order - execute at current market price
    #[default]
    Market,
    /// Stop loss order - triggers market order when stop price is reached
    StopLoss,
    /// Stop loss limit order - triggers limit order when stop price is reached
    StopLossLimit,
    /// Take profit order - triggers market order when target price is reached
    TakeProfit,
    /// Take profit limit order - triggers limit order when target price is reached
    TakeProfitLimit,
    /// Limit maker order - rejected if it would immediately match
    LimitMaker,
    /// Unknown order type
    #[serde(other)]
    Other,
}

/// Time in force - how long an order remains active.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum TimeInForce {
    /// Good Till Canceled - order remains until filled or canceled
    #[default]
    GTC,
    /// Immediate Or Cancel - fill as much as possible, cancel the rest
    IOC,
    /// Fill Or Kill - fill completely or cancel entirely
    FOK,
    /// Good Till Crossing - only for Post Only orders
    GTX,
    /// Unknown time in force
    #[serde(other)]
    Other,
}

/// Order status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderStatus {
    /// The order has been accepted by the engine
    New,
    /// A part of the order has been filled
    PartiallyFilled,
    /// The order has been completely filled
    Filled,
    /// The order has been canceled by the user
    Canceled,
    /// Currently unused
    PendingCancel,
    /// The order was not accepted by the engine and not processed
    Rejected,
    /// The order was canceled according to the order type's rules
    Expired,
    /// The order was canceled by the exchange due to STP trigger
    ExpiredInMatch,
}

/// Execution type for order updates.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ExecutionType {
    /// The order has been accepted into the engine
    New,
    /// The order has been canceled by the user
    Canceled,
    /// Currently unused
    Replaced,
    /// The order has been rejected
    Rejected,
    /// Part of the order or all of the order's quantity has filled
    Trade,
    /// The order was canceled according to the order type's rules
    Expired,
    /// The order has expired due to STP trigger
    TradePrevention,
    /// Order modified
    Amendment,
}

/// Kline/candlestick interval.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum KlineInterval {
    /// 1 second
    #[serde(rename = "1s")]
    Seconds1,
    /// 1 minute
    #[serde(rename = "1m")]
    Minutes1,
    /// 3 minutes
    #[serde(rename = "3m")]
    Minutes3,
    /// 5 minutes
    #[serde(rename = "5m")]
    Minutes5,
    /// 15 minutes
    #[serde(rename = "15m")]
    Minutes15,
    /// 30 minutes
    #[serde(rename = "30m")]
    Minutes30,
    /// 1 hour
    #[serde(rename = "1h")]
    Hours1,
    /// 2 hours
    #[serde(rename = "2h")]
    Hours2,
    /// 4 hours
    #[serde(rename = "4h")]
    Hours4,
    /// 6 hours
    #[serde(rename = "6h")]
    Hours6,
    /// 8 hours
    #[serde(rename = "8h")]
    Hours8,
    /// 12 hours
    #[serde(rename = "12h")]
    Hours12,
    /// 1 day
    #[serde(rename = "1d")]
    Days1,
    /// 3 days
    #[serde(rename = "3d")]
    Days3,
    /// 1 week
    #[serde(rename = "1w")]
    Weeks1,
    /// 1 month
    #[serde(rename = "1M")]
    Months1,
}

impl std::fmt::Display for KlineInterval {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Seconds1 => "1s",
            Self::Minutes1 => "1m",
            Self::Minutes3 => "3m",
            Self::Minutes5 => "5m",
            Self::Minutes15 => "15m",
            Self::Minutes30 => "30m",
            Self::Hours1 => "1h",
            Self::Hours2 => "2h",
            Self::Hours4 => "4h",
            Self::Hours6 => "6h",
            Self::Hours8 => "8h",
            Self::Hours12 => "12h",
            Self::Days1 => "1d",
            Self::Days3 => "3d",
            Self::Weeks1 => "1w",
            Self::Months1 => "1M",
        };
        write!(f, "{}", s)
    }
}

/// Ticker response type for market data endpoints.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum TickerType {
    /// Full response payload.
    Full,
    /// Mini response payload.
    Mini,
}

impl std::fmt::Display for TickerType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Full => "FULL",
            Self::Mini => "MINI",
        };
        write!(f, "{}", s)
    }
}

/// Symbol status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum SymbolStatus {
    /// Pre-trading period
    PreTrading,
    /// Currently trading
    Trading,
    /// Post-trading period
    PostTrading,
    /// End of day
    EndOfDay,
    /// Trading halted
    Halt,
    /// Auction match
    AuctionMatch,
    /// Trading break
    Break,
    /// Pending trading
    PendingTrading,
    /// Unknown status
    #[serde(other)]
    Other,
}

impl std::fmt::Display for SymbolStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::PreTrading => "PRE_TRADING",
            Self::Trading => "TRADING",
            Self::PostTrading => "POST_TRADING",
            Self::EndOfDay => "END_OF_DAY",
            Self::Halt => "HALT",
            Self::AuctionMatch => "AUCTION_MATCH",
            Self::Break => "BREAK",
            Self::PendingTrading => "PENDING_TRADING",
            Self::Other => "OTHER",
        };
        write!(f, "{}", s)
    }
}

/// Symbol permission type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum SymbolPermission {
    /// Spot trading
    Spot,
    /// Margin trading
    Margin,
    /// Unknown permission
    #[serde(other)]
    Other,
}

/// Account type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum AccountType {
    /// Spot account
    Spot,
    /// USDT futures account
    UsdtFuture,
    /// Coin futures account
    CoinFuture,
    /// Leveraged account
    Leveraged,
    /// Unknown account type
    #[serde(other)]
    Other,
}

/// Rate limit type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RateLimitType {
    /// Request weight limit
    RequestWeight,
    /// Orders limit
    Orders,
    /// Raw requests limit
    RawRequests,
    /// Unknown limit type
    #[serde(other)]
    Other,
}

/// Rate limit interval.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RateLimitInterval {
    /// Per second
    Second,
    /// Per minute
    Minute,
    /// Per day
    Day,
}

/// Order response type for new orders.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderResponseType {
    /// Acknowledgement only
    Ack,
    /// Result with order details
    Result,
    /// Full response with fills
    Full,
    /// Unknown response type
    #[serde(other)]
    Other,
}

/// OCO order status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OcoStatus {
    /// Response received
    Response,
    /// Execution started
    ExecStarted,
    /// All done
    AllDone,
}

/// OCO order status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OcoOrderStatus {
    /// Executing
    Executing,
    /// All done
    AllDone,
    /// Rejected
    Reject,
}

/// Contingency type for OCO orders.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ContingencyType {
    /// One Cancels Other
    Oco,
    /// One-Triggers-the-Other
    Oto,
    /// One-Triggers-One-Cancels-the-Other
    Otoco,
    /// One-Places-the-Other
    Opo,
    /// One-Places-One-Cancels-the-Other
    Opoco,
    /// Unknown contingency type
    #[serde(other)]
    Other,
}

/// Cancel-replace mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CancelReplaceMode {
    /// Stop if the cancel fails.
    StopOnFailure,
    /// Allow new order placement even if cancel fails.
    AllowFailure,
}

impl std::fmt::Display for CancelReplaceMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::StopOnFailure => "STOP_ON_FAILURE",
            Self::AllowFailure => "ALLOW_FAILURE",
        };
        write!(f, "{}", s)
    }
}

/// Cancel-replace result status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CancelReplaceResult {
    /// Operation succeeded.
    Success,
    /// Operation failed.
    Failure,
    /// Operation was not attempted.
    NotAttempted,
}

/// Cancel restrictions for cancel-replace.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CancelRestrictions {
    /// Cancel only if the order is NEW.
    OnlyNew,
    /// Cancel only if the order is PARTIALLY_FILLED.
    OnlyPartiallyFilled,
}

impl std::fmt::Display for CancelRestrictions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::OnlyNew => "ONLY_NEW",
            Self::OnlyPartiallyFilled => "ONLY_PARTIALLY_FILLED",
        };
        write!(f, "{}", s)
    }
}

/// Order rate limit exceeded mode for cancel-replace.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderRateLimitExceededMode {
    /// Do not attempt cancel when exceeded.
    DoNothing,
    /// Cancel only even if exceeded.
    CancelOnly,
}

impl std::fmt::Display for OrderRateLimitExceededMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::DoNothing => "DO_NOTHING",
            Self::CancelOnly => "CANCEL_ONLY",
        };
        write!(f, "{}", s)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_order_side_serde() {
        let buy: OrderSide = serde_json::from_str("\"BUY\"").unwrap();
        assert_eq!(buy, OrderSide::Buy);

        let sell: OrderSide = serde_json::from_str("\"SELL\"").unwrap();
        assert_eq!(sell, OrderSide::Sell);

        let serialized = serde_json::to_string(&OrderSide::Buy).unwrap();
        assert_eq!(serialized, "\"BUY\"");
    }

    #[test]
    fn test_order_type_serde() {
        let limit: OrderType = serde_json::from_str("\"LIMIT\"").unwrap();
        assert_eq!(limit, OrderType::Limit);

        let market: OrderType = serde_json::from_str("\"MARKET\"").unwrap();
        assert_eq!(market, OrderType::Market);

        let stop_loss: OrderType = serde_json::from_str("\"STOP_LOSS\"").unwrap();
        assert_eq!(stop_loss, OrderType::StopLoss);

        // Unknown type should deserialize to Other
        let other: OrderType = serde_json::from_str("\"UNKNOWN_TYPE\"").unwrap();
        assert_eq!(other, OrderType::Other);
    }

    #[test]
    fn test_time_in_force_serde() {
        let gtc: TimeInForce = serde_json::from_str("\"GTC\"").unwrap();
        assert_eq!(gtc, TimeInForce::GTC);

        let ioc: TimeInForce = serde_json::from_str("\"IOC\"").unwrap();
        assert_eq!(ioc, TimeInForce::IOC);

        let fok: TimeInForce = serde_json::from_str("\"FOK\"").unwrap();
        assert_eq!(fok, TimeInForce::FOK);
    }

    #[test]
    fn test_order_status_serde() {
        let new: OrderStatus = serde_json::from_str("\"NEW\"").unwrap();
        assert_eq!(new, OrderStatus::New);

        let filled: OrderStatus = serde_json::from_str("\"FILLED\"").unwrap();
        assert_eq!(filled, OrderStatus::Filled);

        let canceled: OrderStatus = serde_json::from_str("\"CANCELED\"").unwrap();
        assert_eq!(canceled, OrderStatus::Canceled);
    }

    #[test]
    fn test_kline_interval_display() {
        assert_eq!(KlineInterval::Minutes1.to_string(), "1m");
        assert_eq!(KlineInterval::Hours1.to_string(), "1h");
        assert_eq!(KlineInterval::Days1.to_string(), "1d");
        assert_eq!(KlineInterval::Months1.to_string(), "1M");
    }

    #[test]
    fn test_kline_interval_serde() {
        let interval: KlineInterval = serde_json::from_str("\"1h\"").unwrap();
        assert_eq!(interval, KlineInterval::Hours1);

        let serialized = serde_json::to_string(&KlineInterval::Minutes15).unwrap();
        assert_eq!(serialized, "\"15m\"");
    }
}