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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Async Rust client for the Binance API.
//!
//! This library provides a type-safe, async interface to the Binance cryptocurrency
//! exchange API, supporting both REST and WebSocket endpoints.
//!
//! # Features
//!
//! - Full coverage of Binance Spot REST API
//! - WebSocket support for real-time market data streams
//! - User data stream support for account updates
//! - Automatic request signing for authenticated endpoints
//! - Production and testnet environment support
//! - Binance.US support
//!
//! # Quick Start
//!
//! ## Public API (No Authentication Required)
//!
//! ```rust,ignore
//! use binance_api_client::Binance;
//!
//! #[tokio::main]
//! async fn main() -> binance_api_client::Result<()> {
//!     // Create a client for public endpoints
//!     let client = Binance::new_unauthenticated()?;
//!
//!     // Ping the server
//!     client.market().ping().await?;
//!
//!     // Get server time
//!     let time = client.market().server_time().await?;
//!     println!("Server time: {}", time.server_time);
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Authenticated API
//!
//! ```rust,ignore
//! use binance_api_client::Binance;
//!
//! #[tokio::main]
//! async fn main() -> binance_api_client::Result<()> {
//!     // Create an authenticated client
//!     let client = Binance::new("your_api_key", "your_secret_key")?;
//!
//!     // Access account information
//!     let account = client.account().get_account().await?;
//!     println!("Account balances: {:?}", account.balances);
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Using Testnet
//!
//! ```rust,ignore
//! use binance_api_client::{Binance, Config};
//!
//! #[tokio::main]
//! async fn main() -> binance_api_client::Result<()> {
//!     let config = Config::testnet();
//!     let client = Binance::with_config(config, Some(("api_key", "secret_key")))?;
//!
//!     // Now all requests go to testnet
//!     client.market().ping().await?;
//!
//!     Ok(())
//! }
//! ```

#![deny(
    unstable_features,
    unused_must_use,
    unused_mut,
    unused_imports,
    unused_import_braces
)]

pub mod api;
pub mod client;
pub mod config;
pub mod credentials;
pub mod error;
pub mod models;
pub mod types;
pub mod websocket;

// Re-export main types at crate root
pub use client::Client;
pub use config::{Config, ConfigBuilder};
pub use credentials::{Credentials, SignatureType};
pub use error::{Error, Result};
pub use websocket::{
    ConnectionHealthMonitor, ConnectionState, DepthCache, DepthCacheConfig, DepthCacheManager,
    DepthCacheState, ReconnectConfig, ReconnectingWebSocket, UserDataStreamManager,
    WebSocketClient, WebSocketConnection, WebSocketEventStream,
};

// Re-export commonly used types
pub use types::{
    AccountType, CancelReplaceMode, CancelReplaceResult, CancelRestrictions, ContingencyType,
    ExecutionType, KlineInterval, OcoOrderStatus, OcoStatus, OrderRateLimitExceededMode,
    OrderResponseType, OrderSide, OrderStatus, OrderType, RateLimitInterval, RateLimitType,
    SymbolPermission, SymbolStatus, TickerType, TimeInForce,
};

// Re-export commonly used models
pub use models::{
    // Account models
    AccountCommission,
    AccountInfo,
    // Wallet models
    AccountSnapshot,
    AccountSnapshotType,
    AccountStatus,
    // Market models
    AggTrade,
    Allocation,
    AmendListStatus,
    AmendOrderResponse,
    AmendedOrderInfo,
    ApiKeyPermissions,
    ApiTradingStatus,
    AssetDetail,
    AveragePrice,
    Balance,
    // Margin models
    BnbBurnStatus,
    BookTicker,
    CancelOrderResponse,
    CancelReplaceErrorData,
    CancelReplaceErrorInfo,
    CancelReplaceErrorResponse,
    CancelReplaceResponse,
    CancelReplaceSideResponse,
    CoinInfo,
    CoinNetwork,
    DepositAddress,
    DepositRecord,
    DepositStatus,
    ExchangeInfo,
    Fill,
    FundingAsset,
    InterestHistoryRecord,
    InterestRateRecord,
    IsolatedAccountLimit,
    IsolatedAssetDetails,
    IsolatedMarginAccountAsset,
    IsolatedMarginAccountDetails,
    IsolatedMarginTransferType,
    Kline,
    ListenKey,
    LoanRecord,
    MarginAccountDetails,
    MarginAsset,
    MarginAssetInfo,
    MarginOrderCancellation,
    MarginOrderResult,
    MarginOrderState,
    MarginPairDetails,
    MarginPriceIndex,
    MarginTrade,
    MarginTransferType,
    MaxBorrowableAmount,
    MaxTransferableAmount,
    OcoOrder,
    OcoOrderDetail,
    OcoOrderReport,
    Order,
    OrderAck,
    OrderAmendment,
    OrderBook,
    OrderBookEntry,
    OrderFull,
    OrderResponse,
    OrderResult,
    PreventedMatch,
    RateLimit,
    RecordsQueryResult,
    RepayRecord,
    RollingWindowTicker,
    RollingWindowTickerMini,
    ServerTime,
    SideEffectType,
    SorOrderCommissionRates,
    SorOrderTestResponse,
    Symbol,
    SymbolFilter,
    SystemStatus,
    Ticker24h,
    TickerPrice,
    Trade,
    TradeFee,
    TradingDayTicker,
    TradingDayTickerMini,
    TransactionId,
    TransferHistory,
    TransferRecord,
    TransferResponse,
    UnfilledOrderCount,
    UniversalTransferType,
    UserTrade,
    WalletBalance,
    WithdrawRecord,
    WithdrawResponse,
    WithdrawStatus,
    // WebSocket models
    websocket::{
        AccountBalance, AccountPositionEvent, AggTradeEvent, BalanceUpdateEvent, BookTickerEvent,
        DepthEvent, DepthLevel, ExecutionReportEvent, KlineData, KlineEvent, ListStatusEvent,
        ListStatusOrder, MiniTickerEvent, TickerEvent, TradeEvent, WebSocketEvent,
    },
};

// Re-export order builders for convenience
pub use api::{
    CancelReplaceOrder, CancelReplaceOrderBuilder, NewOcoOrder, NewOpoOrder, NewOpocoOrder,
    NewOrder, NewOtoOrder, NewOtocoOrder, OcoOrderBuilder, OpoOrderBuilder, OpocoOrderBuilder,
    OrderBuilder, OtoOrderBuilder, OtocoOrderBuilder,
};

/// Main entry point for the Binance API client.
///
/// The `Binance` struct provides access to all API modules and handles
/// configuration and authentication.
#[derive(Clone)]
pub struct Binance {
    client: Client,
}

impl Binance {
    /// Create a new authenticated Binance client with default production configuration.
    ///
    /// # Arguments
    ///
    /// * `api_key` - Your Binance API key
    /// * `secret_key` - Your Binance secret key
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use binance_api_client::Binance;
    ///
    /// # fn run() -> binance_api_client::Result<()> {
    /// let client = Binance::new("api_key", "secret_key")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(api_key: impl Into<String>, secret_key: impl Into<String>) -> Result<Self> {
        let config = Config::default();
        let credentials = Credentials::new(api_key, secret_key);
        let client = Client::new(config, credentials)?;
        Ok(Self { client })
    }

    /// Create a new unauthenticated Binance client for public endpoints only.
    ///
    /// This client can only access public market data endpoints.
    /// For account and trading endpoints, use `Binance::new()` with credentials.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use binance_api_client::Binance;
    ///
    /// # fn run() -> binance_api_client::Result<()> {
    /// let client = Binance::new_unauthenticated()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn new_unauthenticated() -> Result<Self> {
        let config = Config::default();
        let client = Client::new_unauthenticated(config)?;
        Ok(Self { client })
    }

    /// Create a new Binance client with custom configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - Custom configuration (testnet, Binance.US, etc.)
    /// * `credentials` - Optional credentials tuple (api_key, secret_key)
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use binance_api_client::{Binance, Config};
    ///
    /// # fn run() -> binance_api_client::Result<()> {
    /// // Testnet with credentials
    /// let config = Config::testnet();
    /// let client = Binance::with_config(config, Some(("api_key", "secret_key")))?;
    ///
    /// // Production without credentials
    /// let config = Config::default();
    /// let client = Binance::with_config(config, None::<(&str, &str)>)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_config<S: Into<String>>(
        config: Config,
        credentials: Option<(S, S)>,
    ) -> Result<Self> {
        let client = match credentials {
            Some((api_key, secret_key)) => {
                let creds = Credentials::new(api_key, secret_key);
                Client::new(config, creds)?
            }
            None => Client::new_unauthenticated(config)?,
        };
        Ok(Self { client })
    }

    /// Create a new Binance client from environment variables.
    ///
    /// Expects `BINANCE_API_KEY` and `BINANCE_SECRET_KEY` environment variables.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use binance_api_client::Binance;
    ///
    /// # fn run() -> binance_api_client::Result<()> {
    /// let client = Binance::from_env()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_env() -> Result<Self> {
        let config = Config::default();
        let credentials = Credentials::from_env()?;
        let client = Client::new(config, credentials)?;
        Ok(Self { client })
    }

    /// Create a new testnet Binance client with credentials.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use binance_api_client::Binance;
    ///
    /// # fn run() -> binance_api_client::Result<()> {
    /// let client = Binance::testnet("api_key", "secret_key")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn testnet(api_key: impl Into<String>, secret_key: impl Into<String>) -> Result<Self> {
        let config = Config::testnet();
        let credentials = Credentials::new(api_key, secret_key);
        let client = Client::new(config, credentials)?;
        Ok(Self { client })
    }

    /// Create a new testnet Binance client without credentials.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use binance_api_client::Binance;
    ///
    /// # fn run() -> binance_api_client::Result<()> {
    /// let client = Binance::testnet_unauthenticated()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn testnet_unauthenticated() -> Result<Self> {
        let config = Config::testnet();
        let client = Client::new_unauthenticated(config)?;
        Ok(Self { client })
    }

    /// Create a new Binance.US client with credentials.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use binance_api_client::Binance;
    ///
    /// # fn run() -> binance_api_client::Result<()> {
    /// let client = Binance::binance_us("api_key", "secret_key")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn binance_us(api_key: impl Into<String>, secret_key: impl Into<String>) -> Result<Self> {
        let config = Config::binance_us();
        let credentials = Credentials::new(api_key, secret_key);
        let client = Client::new(config, credentials)?;
        Ok(Self { client })
    }

    /// Get the underlying HTTP client.
    ///
    /// This is useful for advanced use cases where you need direct access
    /// to the client.
    pub fn client(&self) -> &Client {
        &self.client
    }

    /// Get the current configuration.
    pub fn config(&self) -> &Config {
        self.client.config()
    }

    /// Check if this client has credentials for authenticated endpoints.
    pub fn has_credentials(&self) -> bool {
        self.client.has_credentials()
    }

    /// Access market data API endpoints.
    ///
    /// Market data endpoints are public and don't require authentication.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let client = Binance::new_unauthenticated()?;
    ///
    /// // Get current BTC price
    /// let price = client.market().price("BTCUSDT").await?;
    /// println!("BTC/USDT: {}", price.price);
    ///
    /// // Get order book
    /// let depth = client.market().depth("BTCUSDT", Some(10)).await?;
    /// ```
    pub fn market(&self) -> api::Market {
        api::Market::new(self.client.clone())
    }

    /// Access user data stream API endpoints.
    ///
    /// User data streams provide real-time updates for account balance changes,
    /// order updates, and other account events via WebSocket.
    ///
    /// **Requires authentication.**
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let client = Binance::new("api_key", "secret_key")?;
    ///
    /// // Start a user data stream
    /// let listen_key = client.user_stream().start().await?;
    ///
    /// // Keep it alive (call every 30 minutes)
    /// client.user_stream().keepalive(&listen_key).await?;
    ///
    /// // Close when done
    /// client.user_stream().close(&listen_key).await?;
    /// ```
    pub fn user_stream(&self) -> api::UserStream {
        api::UserStream::new(self.client.clone())
    }

    /// Access account and trading API endpoints.
    ///
    /// Account and trading endpoints require authentication. Use these to
    /// manage orders, query account balances, and view trade history.
    ///
    /// **Requires authentication.**
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let client = Binance::new("api_key", "secret_key")?;
    ///
    /// // Get account info
    /// let account = client.account().get_account().await?;
    ///
    /// // Place a limit buy order
    /// let order = client.account().limit_buy("BTCUSDT", "0.001", "50000.00").await?;
    ///
    /// // Or use the order builder for more control
    /// use binance_api_client::{OrderBuilder, OrderSide, OrderType, TimeInForce};
    ///
    /// let order = OrderBuilder::new("BTCUSDT", OrderSide::Buy, OrderType::Limit)
    ///     .quantity("0.001")
    ///     .price("50000.00")
    ///     .time_in_force(TimeInForce::GTC)
    ///     .build();
    ///
    /// let response = client.account().create_order(&order).await?;
    /// ```
    pub fn account(&self) -> api::Account {
        api::Account::new(self.client.clone())
    }

    /// Access wallet SAPI endpoints.
    ///
    /// Wallet endpoints provide access to deposit/withdrawal operations,
    /// asset management, universal transfers, and account status.
    ///
    /// **Requires authentication.**
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let client = Binance::new("api_key", "secret_key")?;
    ///
    /// // Check system status
    /// let status = client.wallet().system_status().await?;
    /// if status.is_normal() {
    ///     println!("System is operational");
    /// }
    ///
    /// // Get all coin information
    /// let coins = client.wallet().all_coins().await?;
    ///
    /// // Get deposit address
    /// let address = client.wallet().deposit_address("BTC", None).await?;
    /// println!("Deposit BTC to: {}", address.address);
    ///
    /// // Get trade fees
    /// let fees = client.wallet().trade_fee(Some("BTCUSDT")).await?;
    /// ```
    pub fn wallet(&self) -> api::Wallet {
        api::Wallet::new(self.client.clone())
    }

    /// Access margin trading SAPI endpoints.
    ///
    /// Margin endpoints provide access to cross-margin and isolated margin
    /// trading, loans, transfers, and account management.
    ///
    /// **Requires authentication.**
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let client = Binance::new("api_key", "secret_key")?;
    ///
    /// // Get cross-margin account details
    /// let account = client.margin().account().await?;
    /// println!("Margin level: {}", account.margin_level);
    ///
    /// // Check max borrowable
    /// let max = client.margin().max_borrowable("BTC", None).await?;
    /// println!("Max borrowable BTC: {}", max.amount);
    ///
    /// // Transfer to margin account
    /// use binance_api_client::MarginTransferType;
    /// let result = client.margin()
    ///     .transfer("USDT", "100.0", MarginTransferType::MainToMargin)
    ///     .await?;
    ///
    /// // Borrow
    /// let loan = client.margin().loan("USDT", "50.0", false, None).await?;
    /// ```
    pub fn margin(&self) -> api::Margin {
        api::Margin::new(self.client.clone())
    }

    /// Access WebSocket streaming API.
    ///
    /// The WebSocket client provides real-time market data streams including
    /// trades, klines, tickers, and order book updates. It also supports
    /// user data streams for account updates when connected with a listen key.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use binance_api_client::Binance;
    ///
    /// let client = Binance::new_unauthenticated()?;
    /// let ws = client.websocket();
    ///
    /// // Connect to aggregate trade stream
    /// let stream_name = ws.agg_trade_stream("btcusdt");
    /// let mut conn = ws.connect(&stream_name).await?;
    ///
    /// // Receive events
    /// while let Some(event) = conn.next().await {
    ///     println!("{:?}", event?);
    /// }
    /// ```
    pub fn websocket(&self) -> websocket::WebSocketClient {
        websocket::WebSocketClient::new(self.client.config().clone())
    }
}

impl std::fmt::Debug for Binance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Binance")
            .field("config", self.config())
            .field("has_credentials", &self.has_credentials())
            .finish()
    }
}

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

    #[test]
    fn test_new_unauthenticated() {
        let client = Binance::new_unauthenticated().unwrap();
        assert!(!client.has_credentials());
        assert_eq!(client.config().rest_api_endpoint, "https://api.binance.com");
    }

    #[test]
    fn test_new_authenticated() {
        let client = Binance::new("api_key", "secret_key").unwrap();
        assert!(client.has_credentials());
    }

    #[test]
    fn test_testnet() {
        let client = Binance::testnet("api_key", "secret_key").unwrap();
        assert!(client.has_credentials());
        assert_eq!(
            client.config().rest_api_endpoint,
            "https://testnet.binance.vision"
        );
    }

    #[test]
    fn test_testnet_unauthenticated() {
        let client = Binance::testnet_unauthenticated().unwrap();
        assert!(!client.has_credentials());
        assert_eq!(
            client.config().rest_api_endpoint,
            "https://testnet.binance.vision"
        );
    }

    #[test]
    fn test_binance_us() {
        let client = Binance::binance_us("api_key", "secret_key").unwrap();
        assert!(client.has_credentials());
        assert_eq!(client.config().rest_api_endpoint, "https://api.binance.us");
    }

    #[test]
    fn test_with_config() {
        let config = Config::builder()
            .rest_api_endpoint("https://custom.api.com")
            .build();
        let client = Binance::with_config(config, Some(("api_key", "secret_key"))).unwrap();
        assert!(client.has_credentials());
        assert_eq!(client.config().rest_api_endpoint, "https://custom.api.com");
    }

    #[test]
    fn test_with_config_no_credentials() {
        let config = Config::default();
        let client = Binance::with_config(config, None::<(&str, &str)>).unwrap();
        assert!(!client.has_credentials());
    }

    #[test]
    fn test_debug_output() {
        let client = Binance::new("api_key", "secret_key").unwrap();
        let debug_output = format!("{:?}", client);
        assert!(debug_output.contains("Binance"));
        assert!(debug_output.contains("has_credentials: true"));
        // Should not contain the actual secret key
        assert!(!debug_output.contains("secret_key"));
    }
}