crypto_rest_client/exchanges/binance/
binance_inverse.rs

1use super::{super::utils::http_get, utils::*};
2use crate::error::Result;
3use std::collections::BTreeMap;
4
5const BASE_URL: &str = "https://dapi.binance.com";
6
7/// Binance Coin-margined Future and Swap market
8///
9/// * REST API doc: <https://binance-docs.github.io/apidocs/delivery/en/>
10/// * Trading at: <https://www.binance.com/en/delivery/btcusd_perpetual>
11/// Rate Limits: <https://binance-docs.github.io/apidocs/delivery/en/#limits>
12///   * 2400 request weight per minute
13pub struct BinanceInverseRestClient {
14    _api_key: Option<String>,
15    _api_secret: Option<String>,
16}
17
18impl BinanceInverseRestClient {
19    pub fn new(api_key: Option<String>, api_secret: Option<String>) -> Self {
20        BinanceInverseRestClient { _api_key: api_key, _api_secret: api_secret }
21    }
22
23    /// Get compressed, aggregate trades.
24    ///
25    /// Equivalent to `/dapi/v1/aggTrades` with `limit=1000`
26    ///
27    /// For example:
28    ///
29    /// - <https://dapi.binance.com/dapi/v1/aggTrades?symbol=BTCUSD_PERP&limit=1000>
30    /// - <https://dapi.binance.com/dapi/v1/aggTrades?symbol=BTCUSD_210625&limit=1000>
31    pub fn fetch_agg_trades(
32        symbol: &str,
33        from_id: Option<u64>,
34        start_time: Option<u64>,
35        end_time: Option<u64>,
36    ) -> Result<String> {
37        check_symbol(symbol);
38        let symbol = Some(symbol);
39        let limit = Some(1000);
40        gen_api_binance!("/dapi/v1/aggTrades", symbol, from_id, start_time, end_time, limit)
41    }
42
43    /// Get a Level2 snapshot of orderbook.
44    ///
45    /// Equivalent to `/dapi/v1/depth` with `limit=1000`
46    ///
47    /// For example:
48    ///
49    /// - <https://dapi.binance.com/dapi/v1/depth?symbol=BTCUSD_PERP&limit=1000>
50    /// - <https://dapi.binance.com/dapi/v1/depth?symbol=BTCUSD_211231&limit=1000>
51    pub fn fetch_l2_snapshot(symbol: &str) -> Result<String> {
52        check_symbol(symbol);
53        let symbol = Some(symbol);
54        let limit = Some(1000);
55        gen_api_binance!("/dapi/v1/depth", symbol, limit)
56    }
57
58    /// Get open interest.
59    ///
60    /// For example:
61    ///
62    /// - <https://dapi.binance.com/dapi/v1/openInterest?symbol=BTCUSD_PERP>
63    /// - <https://dapi.binance.com/dapi/v1/openInterest?symbol=BTCUSD_211231>
64    pub fn fetch_open_interest(symbol: &str) -> Result<String> {
65        check_symbol(symbol);
66        let symbol = Some(symbol);
67        gen_api_binance!("/dapi/v1/openInterest", symbol)
68    }
69}