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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Margin Trading API response models.
//!
//! Models for the Binance Margin SAPI endpoints.

use serde::{Deserialize, Serialize};

use super::string_or_float;
use crate::types::{OrderSide, OrderStatus, OrderType, TimeInForce};

/// Margin transfer type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MarginTransferType {
    /// Transfer from main account to margin account
    #[serde(rename = "1")]
    MainToMargin,
    /// Transfer from margin account to main account
    #[serde(rename = "2")]
    MarginToMain,
}

/// Isolated margin transfer type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum IsolatedMarginTransferType {
    /// Spot account
    Spot,
    /// Isolated margin account
    IsolatedMargin,
}

/// Side effect type for margin orders.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum SideEffectType {
    /// No side effect
    #[default]
    NoSideEffect,
    /// Margin buy
    MarginBuy,
    /// Auto repay
    AutoRepay,
}

/// Transaction ID response.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionId {
    /// Transaction ID
    pub tran_id: u64,
}

/// Margin account details.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginAccountDetails {
    /// Whether borrow is enabled.
    pub borrow_enabled: bool,
    /// Margin level.
    #[serde(with = "string_or_float")]
    pub margin_level: f64,
    /// Total asset in BTC.
    #[serde(with = "string_or_float")]
    pub total_asset_of_btc: f64,
    /// Total liability in BTC.
    #[serde(with = "string_or_float")]
    pub total_liability_of_btc: f64,
    /// Total net asset in BTC.
    #[serde(with = "string_or_float")]
    pub total_net_asset_of_btc: f64,
    /// Whether trading is enabled.
    pub trade_enabled: bool,
    /// Whether transfer is enabled.
    pub transfer_enabled: bool,
    /// User assets.
    pub user_assets: Vec<MarginAsset>,
}

/// Margin asset balance.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginAsset {
    /// Asset symbol.
    pub asset: String,
    /// Borrowed amount.
    #[serde(with = "string_or_float")]
    pub borrowed: f64,
    /// Free amount.
    #[serde(with = "string_or_float")]
    pub free: f64,
    /// Interest amount.
    #[serde(with = "string_or_float")]
    pub interest: f64,
    /// Locked amount.
    #[serde(with = "string_or_float")]
    pub locked: f64,
    /// Net asset amount.
    #[serde(with = "string_or_float")]
    pub net_asset: f64,
}

/// Isolated margin account details.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IsolatedMarginAccountDetails {
    /// Assets.
    #[serde(default)]
    pub assets: Vec<IsolatedMarginAccountAsset>,
    /// Total asset in BTC (optional).
    #[serde(default, with = "string_or_float_option")]
    pub total_asset_of_btc: Option<f64>,
    /// Total liability in BTC (optional).
    #[serde(default, with = "string_or_float_option")]
    pub total_liability_of_btc: Option<f64>,
    /// Total net asset in BTC (optional).
    #[serde(default, with = "string_or_float_option")]
    pub total_net_asset_of_btc: Option<f64>,
}

/// Isolated margin account asset.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IsolatedMarginAccountAsset {
    /// Base asset.
    pub base_asset: IsolatedAssetDetails,
    /// Quote asset.
    pub quote_asset: IsolatedAssetDetails,
    /// Symbol.
    pub symbol: String,
    /// Whether isolated trading is created.
    pub isolated_created: bool,
    /// Whether margin trading is enabled.
    pub enabled: bool,
    /// Margin level.
    #[serde(with = "string_or_float")]
    pub margin_level: f64,
    /// Margin ratio.
    #[serde(with = "string_or_float")]
    pub margin_ratio: f64,
    /// Index price.
    #[serde(with = "string_or_float")]
    pub index_price: f64,
    /// Liquidate price.
    #[serde(with = "string_or_float")]
    pub liquidate_price: f64,
    /// Liquidate rate.
    #[serde(with = "string_or_float")]
    pub liquidate_rate: f64,
    /// Whether trading is enabled.
    pub trade_enabled: bool,
}

/// Isolated asset details.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IsolatedAssetDetails {
    /// Asset symbol.
    pub asset: String,
    /// Whether borrow is enabled.
    pub borrow_enabled: bool,
    /// Borrowed amount.
    #[serde(with = "string_or_float")]
    pub borrowed: f64,
    /// Free amount.
    #[serde(with = "string_or_float")]
    pub free: f64,
    /// Interest amount.
    #[serde(with = "string_or_float")]
    pub interest: f64,
    /// Locked amount.
    #[serde(with = "string_or_float")]
    pub locked: f64,
    /// Net asset amount.
    #[serde(with = "string_or_float")]
    pub net_asset: f64,
    /// Net asset in BTC.
    #[serde(with = "string_or_float")]
    pub net_asset_of_btc: f64,
    /// Whether repay is enabled.
    pub repay_enabled: bool,
    /// Total asset amount.
    #[serde(with = "string_or_float")]
    pub total_asset: f64,
}

/// Max borrowable amount.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MaxBorrowableAmount {
    /// Amount.
    #[serde(with = "string_or_float")]
    pub amount: f64,
    /// Borrow limit.
    #[serde(default, with = "string_or_float_option")]
    pub borrow_limit: Option<f64>,
}

/// Max transferable amount.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MaxTransferableAmount {
    /// Amount.
    #[serde(with = "string_or_float")]
    pub amount: f64,
}

/// Margin order result.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginOrderResult {
    /// Symbol.
    pub symbol: String,
    /// Order ID.
    pub order_id: u64,
    /// Client order ID.
    pub client_order_id: String,
    /// Transaction time.
    pub transact_time: u64,
    /// Price.
    #[serde(with = "string_or_float")]
    pub price: f64,
    /// Original quantity.
    #[serde(with = "string_or_float")]
    pub orig_qty: f64,
    /// Executed quantity.
    #[serde(with = "string_or_float")]
    pub executed_qty: f64,
    /// Cumulative quote quantity.
    #[serde(with = "string_or_float")]
    pub cummulative_quote_qty: f64,
    /// Order status.
    pub status: OrderStatus,
    /// Time in force.
    pub time_in_force: TimeInForce,
    /// Order type.
    #[serde(rename = "type")]
    pub order_type: OrderType,
    /// Order side.
    pub side: OrderSide,
    /// Whether this is isolated margin.
    #[serde(default)]
    pub is_isolated: Option<bool>,
}

/// Margin order state (for query).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginOrderState {
    /// Client order ID.
    pub client_order_id: String,
    /// Cumulative quote quantity.
    #[serde(with = "string_or_float")]
    pub cummulative_quote_qty: f64,
    /// Executed quantity.
    #[serde(with = "string_or_float")]
    pub executed_qty: f64,
    /// Iceberg quantity.
    #[serde(default, with = "string_or_float_option")]
    pub iceberg_qty: Option<f64>,
    /// Whether working.
    pub is_working: bool,
    /// Order ID.
    pub order_id: u64,
    /// Original quantity.
    #[serde(with = "string_or_float")]
    pub orig_qty: f64,
    /// Price.
    #[serde(with = "string_or_float")]
    pub price: f64,
    /// Order side.
    pub side: OrderSide,
    /// Order status.
    pub status: OrderStatus,
    /// Stop price.
    #[serde(default, with = "string_or_float_option")]
    pub stop_price: Option<f64>,
    /// Symbol.
    pub symbol: String,
    /// Whether this is isolated margin.
    #[serde(default)]
    pub is_isolated: Option<bool>,
    /// Time.
    pub time: u64,
    /// Time in force.
    pub time_in_force: TimeInForce,
    /// Order type.
    #[serde(rename = "type")]
    pub order_type: OrderType,
    /// Update time.
    pub update_time: u64,
}

/// Margin order cancellation result.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginOrderCancellation {
    /// Symbol.
    pub symbol: String,
    /// Order ID.
    #[serde(default)]
    pub order_id: Option<u64>,
    /// Original client order ID.
    pub orig_client_order_id: String,
    /// Client order ID.
    pub client_order_id: String,
    /// Price.
    #[serde(with = "string_or_float")]
    pub price: f64,
    /// Original quantity.
    #[serde(with = "string_or_float")]
    pub orig_qty: f64,
    /// Executed quantity.
    #[serde(with = "string_or_float")]
    pub executed_qty: f64,
    /// Cumulative quote quantity.
    #[serde(with = "string_or_float")]
    pub cummulative_quote_qty: f64,
    /// Order status.
    pub status: OrderStatus,
    /// Time in force.
    pub time_in_force: TimeInForce,
    /// Order type.
    #[serde(rename = "type")]
    pub order_type: OrderType,
    /// Order side.
    pub side: OrderSide,
}

/// Margin trade record.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginTrade {
    /// Commission.
    #[serde(with = "string_or_float")]
    pub commission: f64,
    /// Commission asset.
    pub commission_asset: String,
    /// Trade ID.
    pub id: u64,
    /// Whether buyer.
    pub is_buyer: bool,
    /// Whether isolated margin.
    #[serde(default)]
    pub is_isolated: Option<bool>,
    /// Whether maker.
    pub is_maker: bool,
    /// Order ID.
    pub order_id: u64,
    /// Price.
    #[serde(with = "string_or_float")]
    pub price: f64,
    /// Quantity.
    #[serde(with = "string_or_float")]
    pub qty: f64,
    /// Symbol.
    pub symbol: String,
    /// Time.
    pub time: u64,
}

/// Interest history record.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InterestHistoryRecord {
    /// Asset.
    pub asset: String,
    /// Interest.
    #[serde(with = "string_or_float")]
    pub interest: f64,
    /// Interest accrue time.
    pub interest_accured_time: u64,
    /// Interest rate.
    #[serde(with = "string_or_float")]
    pub interest_rate: f64,
    /// Principal.
    #[serde(with = "string_or_float")]
    pub principal: f64,
    /// Interest type.
    #[serde(rename = "type")]
    pub interest_type: String,
    /// Isolated symbol (for isolated margin).
    #[serde(default)]
    pub isolated_symbol: Option<String>,
}

/// Interest rate history record.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InterestRateRecord {
    /// Asset.
    pub asset: String,
    /// Daily interest rate.
    #[serde(with = "string_or_float")]
    pub daily_interest_rate: f64,
    /// Timestamp.
    pub timestamp: u64,
    /// VIP level.
    #[serde(default)]
    pub vip_level: Option<u32>,
}

/// Loan record.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LoanRecord {
    /// Asset.
    pub asset: String,
    /// Principal.
    #[serde(with = "string_or_float")]
    pub principal: f64,
    /// Timestamp.
    pub timestamp: u64,
    /// Status (PENDING, CONFIRMED, FAILED).
    pub status: String,
    /// Isolated symbol (for isolated margin).
    #[serde(default)]
    pub isolated_symbol: Option<String>,
    /// Transaction ID.
    pub tx_id: u64,
}

/// Repay record.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RepayRecord {
    /// Asset.
    pub asset: String,
    /// Amount.
    #[serde(with = "string_or_float")]
    pub amount: f64,
    /// Interest.
    #[serde(with = "string_or_float")]
    pub interest: f64,
    /// Principal.
    #[serde(with = "string_or_float")]
    pub principal: f64,
    /// Timestamp.
    pub timestamp: u64,
    /// Status (PENDING, CONFIRMED, FAILED).
    pub status: String,
    /// Isolated symbol (for isolated margin).
    #[serde(default)]
    pub isolated_symbol: Option<String>,
    /// Transaction ID.
    pub tx_id: u64,
}

/// Records query result (paginated).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecordsQueryResult<T> {
    /// Total records.
    pub total: u64,
    /// Records.
    pub rows: Vec<T>,
}

/// Cross margin pair details.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginPairDetails {
    /// Symbol ID.
    pub id: u64,
    /// Symbol.
    pub symbol: String,
    /// Base asset.
    pub base: String,
    /// Quote asset.
    pub quote: String,
    /// Whether margin trading is enabled.
    pub is_margin_trade: bool,
    /// Whether buy is allowed.
    pub is_buy_allowed: bool,
    /// Whether sell is allowed.
    pub is_sell_allowed: bool,
}

/// Margin asset info.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginAssetInfo {
    /// Asset name.
    pub asset_name: String,
    /// Asset full name.
    pub asset_full_name: String,
    /// Whether borrowing is enabled.
    pub is_borrowable: bool,
    /// Whether mortgage is enabled.
    pub is_mortgageable: bool,
    /// User min borrow.
    #[serde(with = "string_or_float")]
    pub user_min_borrow: f64,
    /// User min repay.
    #[serde(with = "string_or_float")]
    pub user_min_repay: f64,
}

/// Margin price index.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarginPriceIndex {
    /// Calculation time.
    pub calc_time: u64,
    /// Price.
    #[serde(with = "string_or_float")]
    pub price: f64,
    /// Symbol.
    pub symbol: String,
}

/// Isolated margin account limit.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IsolatedAccountLimit {
    /// Enabled account count.
    pub enabled_account: u32,
    /// Max account count.
    pub max_account: u32,
}

/// BNB burn status.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BnbBurnStatus {
    /// Whether spot BNB burn is enabled.
    pub spot_bnb_burn: bool,
    /// Whether interest BNB burn is enabled.
    pub interest_bnb_burn: bool,
}

/// Helper for optional f64 fields that may be strings.
mod string_or_float_option {
    use serde::{self, Deserialize, Deserializer, Serializer};

    pub fn serialize<S>(value: &Option<f64>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match value {
            Some(v) => serializer.serialize_str(&v.to_string()),
            None => serializer.serialize_none(),
        }
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<f64>, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(untagged)]
        enum StringOrFloat {
            String(String),
            Float(f64),
        }

        let opt: Option<StringOrFloat> = Option::deserialize(deserializer)?;
        match opt {
            Some(StringOrFloat::Float(f)) => Ok(Some(f)),
            Some(StringOrFloat::String(s)) => {
                s.parse::<f64>().map(Some).map_err(serde::de::Error::custom)
            }
            None => Ok(None),
        }
    }
}