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
564
565
566
567
568
569
570
//! Wallet API response models.
//!
//! Models for the Binance Wallet SAPI endpoints.

use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use super::string_or_float;

/// System status response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemStatus {
    /// Status: 0 = normal, 1 = system maintenance
    pub status: u32,
    /// Status message (e.g., "normal", "system_maintenance")
    pub msg: String,
}

impl SystemStatus {
    /// Returns true if the system is operating normally.
    pub fn is_normal(&self) -> bool {
        self.status == 0
    }
}

/// Coin network information.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoinNetwork {
    /// Address regex pattern.
    #[serde(default)]
    pub address_regex: Option<String>,
    /// Coin name.
    pub coin: String,
    /// Deposit description.
    #[serde(default)]
    pub deposit_desc: Option<String>,
    /// Whether deposits are enabled.
    pub deposit_enable: bool,
    /// Whether this is the default network.
    pub is_default: bool,
    /// Memo regex pattern.
    #[serde(default)]
    pub memo_regex: Option<String>,
    /// Minimum confirmations for deposit.
    pub min_confirm: u32,
    /// Network name.
    pub name: String,
    /// Network identifier.
    pub network: String,
    /// Whether special tips are available.
    #[serde(default)]
    pub special_tips: Option<String>,
    /// Unlock confirmations required.
    #[serde(default)]
    pub un_lock_confirm: Option<u32>,
    /// Withdraw description.
    #[serde(default)]
    pub withdraw_desc: Option<String>,
    /// Whether withdrawals are enabled.
    pub withdraw_enable: bool,
    /// Withdrawal fee.
    #[serde(with = "string_or_float")]
    pub withdraw_fee: f64,
    /// Withdrawal integer multiple.
    #[serde(default, with = "string_or_float_option")]
    pub withdraw_integer_multiple: Option<f64>,
    /// Maximum withdrawal amount.
    #[serde(with = "string_or_float")]
    pub withdraw_max: f64,
    /// Minimum withdrawal amount.
    #[serde(with = "string_or_float")]
    pub withdraw_min: f64,
    /// Whether same address is supported.
    #[serde(default)]
    pub same_address: Option<bool>,
    /// Estimated arrival time.
    #[serde(default)]
    pub estimated_arrival_time: Option<u64>,
    /// Whether the network is busy.
    #[serde(default)]
    pub busy: Option<bool>,
}

/// Coin information from wallet config.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoinInfo {
    /// Coin symbol (e.g., "BTC").
    pub coin: String,
    /// Whether deposit is available for all networks.
    pub deposit_all_enable: bool,
    /// Free balance.
    #[serde(with = "string_or_float")]
    pub free: f64,
    /// Freeze balance.
    #[serde(with = "string_or_float")]
    pub freeze: f64,
    /// IPO-able balance.
    #[serde(with = "string_or_float")]
    pub ipoable: f64,
    /// IPOING balance.
    #[serde(with = "string_or_float")]
    pub ipoing: f64,
    /// Whether legal money.
    pub is_legal_money: bool,
    /// Locked balance.
    #[serde(with = "string_or_float")]
    pub locked: f64,
    /// Full coin name.
    pub name: String,
    /// Available networks for this coin.
    pub network_list: Vec<CoinNetwork>,
    /// Storage balance.
    #[serde(with = "string_or_float")]
    pub storage: f64,
    /// Whether trading is enabled.
    pub trading: bool,
    /// Whether withdraw is available for all networks.
    pub withdraw_all_enable: bool,
    /// Withdrawing balance.
    #[serde(with = "string_or_float")]
    pub withdrawing: f64,
}

/// Deposit address information.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DepositAddress {
    /// Deposit address.
    pub address: String,
    /// Coin symbol.
    pub coin: String,
    /// Tag/memo (if applicable).
    pub tag: String,
    /// URL for address (optional).
    #[serde(default)]
    pub url: Option<String>,
}

/// Deposit record from history.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DepositRecord {
    /// Deposit amount.
    #[serde(with = "string_or_float")]
    pub amount: f64,
    /// Coin symbol.
    pub coin: String,
    /// Network used.
    pub network: String,
    /// Deposit status.
    pub status: DepositStatus,
    /// Deposit address.
    pub address: String,
    /// Address tag (if applicable).
    #[serde(default)]
    pub address_tag: Option<String>,
    /// Transaction ID.
    pub tx_id: String,
    /// Insert time (timestamp).
    pub insert_time: u64,
    /// Transfer type.
    #[serde(default)]
    pub transfer_type: Option<u32>,
    /// Confirm times.
    #[serde(default)]
    pub confirm_times: Option<String>,
    /// Unlock confirm.
    #[serde(default)]
    pub unlock_confirm: Option<u32>,
    /// Unique ID.
    #[serde(default)]
    pub id: Option<String>,
}

/// Deposit status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum DepositStatus {
    /// Pending
    Pending = 0,
    /// Success
    Success = 1,
    /// Success (credited but cannot withdraw)
    CreditedCannotWithdraw = 6,
}

/// Withdrawal record from history.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WithdrawRecord {
    /// Withdrawal address.
    pub address: String,
    /// Amount.
    #[serde(with = "string_or_float")]
    pub amount: f64,
    /// Apply time.
    pub apply_time: String,
    /// Coin symbol.
    pub coin: String,
    /// Withdrawal ID.
    pub id: String,
    /// Withdraw order ID (user-supplied).
    #[serde(default)]
    pub withdraw_order_id: Option<String>,
    /// Network used.
    pub network: String,
    /// Transfer type.
    #[serde(default)]
    pub transfer_type: Option<u32>,
    /// Status.
    pub status: WithdrawStatus,
    /// Transaction fee.
    #[serde(with = "string_or_float")]
    pub transaction_fee: f64,
    /// Confirm number.
    #[serde(default)]
    pub confirm_no: Option<u32>,
    /// Additional info.
    #[serde(default)]
    pub info: Option<String>,
    /// Transaction ID.
    #[serde(default)]
    pub tx_id: Option<String>,
}

/// Withdrawal status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum WithdrawStatus {
    /// Email sent
    EmailSent = 0,
    /// Cancelled
    Cancelled = 1,
    /// Awaiting approval
    AwaitingApproval = 2,
    /// Rejected
    Rejected = 3,
    /// Processing
    Processing = 4,
    /// Failure
    Failure = 5,
    /// Completed
    Completed = 6,
}

/// Withdrawal request response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WithdrawResponse {
    /// Withdrawal ID.
    pub id: String,
}

/// Asset detail information.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AssetDetail {
    /// Minimum withdrawal amount.
    #[serde(with = "string_or_float")]
    pub min_withdraw_amount: f64,
    /// Whether deposit is enabled.
    pub deposit_status: bool,
    /// Withdrawal fee.
    #[serde(with = "string_or_float")]
    pub withdraw_fee: f64,
    /// Whether withdrawal is enabled.
    pub withdraw_status: bool,
    /// Deposit tip (optional).
    #[serde(default)]
    pub deposit_tip: Option<String>,
}

/// Trade fee information.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TradeFee {
    /// Symbol.
    pub symbol: String,
    /// Maker commission.
    #[serde(with = "string_or_float")]
    pub maker_commission: f64,
    /// Taker commission.
    #[serde(with = "string_or_float")]
    pub taker_commission: f64,
}

/// Universal transfer type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum UniversalTransferType {
    /// Spot to USDM Futures
    MainUmfuture,
    /// Spot to COINM Futures
    MainCmfuture,
    /// Spot to Margin (cross)
    MainMargin,
    /// USDM Futures to Spot
    UmfutureMain,
    /// USDM Futures to Margin (cross)
    UmfutureMargin,
    /// COINM Futures to Spot
    CmfutureMain,
    /// COINM Futures to Margin (cross)
    CmfutureMargin,
    /// Margin (cross) to Spot
    MarginMain,
    /// Margin (cross) to USDM Futures
    MarginUmfuture,
    /// Margin (cross) to COINM Futures
    MarginCmfuture,
    /// Spot to Isolated Margin
    MainIsolatedMargin,
    /// Isolated Margin to Spot
    IsolatedMarginMain,
    /// Isolated Margin to Isolated Margin
    IsolatedMarginIsolatedMargin,
    /// Spot to Funding
    MainFunding,
    /// Funding to Spot
    FundingMain,
    /// Funding to USDM Futures
    FundingUmfuture,
    /// USDM Futures to Funding
    UmfutureFunding,
    /// Margin (cross) to Funding
    MarginFunding,
    /// Funding to Margin (cross)
    FundingMargin,
    /// Funding to COINM Futures
    FundingCmfuture,
    /// COINM Futures to Funding
    CmfutureFunding,
}

impl UniversalTransferType {
    /// Return the API wire value for this transfer type.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::MainUmfuture => "MAIN_UMFUTURE",
            Self::MainCmfuture => "MAIN_CMFUTURE",
            Self::MainMargin => "MAIN_MARGIN",
            Self::UmfutureMain => "UMFUTURE_MAIN",
            Self::UmfutureMargin => "UMFUTURE_MARGIN",
            Self::CmfutureMain => "CMFUTURE_MAIN",
            Self::CmfutureMargin => "CMFUTURE_MARGIN",
            Self::MarginMain => "MARGIN_MAIN",
            Self::MarginUmfuture => "MARGIN_UMFUTURE",
            Self::MarginCmfuture => "MARGIN_CMFUTURE",
            Self::MainIsolatedMargin => "MAIN_ISOLATED_MARGIN",
            Self::IsolatedMarginMain => "ISOLATED_MARGIN_MAIN",
            Self::IsolatedMarginIsolatedMargin => "ISOLATED_MARGIN_ISOLATED_MARGIN",
            Self::MainFunding => "MAIN_FUNDING",
            Self::FundingMain => "FUNDING_MAIN",
            Self::FundingUmfuture => "FUNDING_UMFUTURE",
            Self::UmfutureFunding => "UMFUTURE_FUNDING",
            Self::MarginFunding => "MARGIN_FUNDING",
            Self::FundingMargin => "FUNDING_MARGIN",
            Self::FundingCmfuture => "FUNDING_CMFUTURE",
            Self::CmfutureFunding => "CMFUTURE_FUNDING",
        }
    }
}

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

/// Universal transfer record.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransferRecord {
    /// Asset.
    pub asset: String,
    /// Amount.
    #[serde(with = "string_or_float")]
    pub amount: f64,
    /// Transfer type.
    #[serde(rename = "type")]
    pub transfer_type: UniversalTransferType,
    /// Status.
    pub status: String,
    /// Transaction ID.
    pub tran_id: u64,
    /// Timestamp.
    pub timestamp: u64,
}

/// Transfer history response (paginated).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransferHistory {
    /// Total count.
    pub total: u64,
    /// Transfer records.
    pub rows: Vec<TransferRecord>,
}

/// Wallet balance entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WalletBalance {
    /// Whether balance is active.
    pub activate: bool,
    /// Balance amount.
    #[serde(with = "string_or_float")]
    pub balance: f64,
    /// Wallet name.
    pub wallet_name: String,
}

/// Funding wallet asset.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FundingAsset {
    /// Asset.
    pub asset: String,
    /// Free balance.
    #[serde(with = "string_or_float")]
    pub free: f64,
    /// Locked balance.
    #[serde(with = "string_or_float")]
    pub locked: f64,
    /// Freeze balance.
    #[serde(with = "string_or_float")]
    pub freeze: f64,
    /// Withdrawing balance.
    #[serde(with = "string_or_float")]
    pub withdrawing: f64,
    /// BTC valuation (optional).
    #[serde(default, with = "string_or_float_option")]
    pub btc_valuation: Option<f64>,
}

/// Account snapshot type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum AccountSnapshotType {
    /// Spot account
    Spot,
    /// Margin account
    Margin,
    /// Futures account
    Futures,
}

/// Account snapshot response.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AccountSnapshot {
    /// Response code.
    pub code: i32,
    /// Response message.
    pub msg: String,
    /// Snapshot data.
    pub snapshot_vos: Vec<SnapshotData>,
}

/// Snapshot data entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SnapshotData {
    /// Snapshot type.
    #[serde(rename = "type")]
    pub snapshot_type: String,
    /// Update time.
    pub update_time: u64,
    /// Snapshot data (varies by type).
    pub data: serde_json::Value,
}

/// API key permissions.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiKeyPermissions {
    /// Whether IP restricted.
    pub ip_restrict: bool,
    /// Creation time.
    pub create_time: u64,
    /// Whether spot trading is enabled.
    pub enable_spot_and_margin_trading: bool,
    /// Whether withdrawals are enabled.
    pub enable_withdrawals: bool,
    /// Whether internal transfers are enabled.
    pub enable_internal_transfer: bool,
    /// Permits universal transfer.
    pub permits_universal_transfer: bool,
    /// Whether vanilla options are enabled.
    pub enable_vanilla_options: bool,
    /// Whether reading is enabled.
    pub enable_reading: bool,
    /// Whether futures trading is enabled.
    pub enable_futures: bool,
    /// Whether margin loan/borrow/repay is enabled.
    pub enable_margin: bool,
    /// Trading authority expiration time.
    #[serde(default)]
    pub trading_authority_expiration_time: Option<u64>,
}

/// Account status.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AccountStatus {
    /// Account status data.
    pub data: String,
}

/// API trading status.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiTradingStatus {
    /// Status data.
    pub data: ApiTradingStatusData,
}

/// API trading status data.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiTradingStatusData {
    /// Is locked.
    pub is_locked: bool,
    /// Planned recovery time (if locked).
    #[serde(default)]
    pub planned_recover_time: Option<u64>,
    /// Trigger condition.
    pub trigger_condition: serde_json::Value,
    /// Update time.
    pub update_time: u64,
}

/// 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),
        }
    }
}