crypto_rest_client/exchanges/gate/gate_swap.rs
1use super::super::utils::http_get;
2use crate::error::Result;
3use std::collections::BTreeMap;
4
5const BASE_URL: &str = "https://api.gateio.ws/api/v4";
6
7/// The RESTful client for Gate Swap markets.
8///
9/// * RESTful API doc: <https://www.gate.io/docs/apiv4/en/index.html#futures>
10/// * Trading at: <https://www.gateio.pro/cn/futures_trade/USDT/BTC_USDT>
11/// * Rate Limits: <https://www.gate.io/docs/apiv4/en/index.html#frequency-limit-rule>
12/// * 300 read operations per IP per second
13pub struct GateSwapRestClient {
14 _api_key: Option<String>,
15 _api_secret: Option<String>,
16}
17
18impl GateSwapRestClient {
19 pub fn new(api_key: Option<String>, api_secret: Option<String>) -> Self {
20 GateSwapRestClient { _api_key: api_key, _api_secret: api_secret }
21 }
22
23 /// Get the latest Level2 snapshot of orderbook.
24 ///
25 /// Top 200 asks and bids are returned.
26 ///
27 /// For example:
28 ///
29 /// - <https://api.gateio.ws/api/v4/futures/btc/order_book?contract=BTC_USD&limit=200>
30 /// - <https://api.gateio.ws/api/v4/futures/usdt/order_book?contract=BTC_USDT&limit=200>
31 pub fn fetch_l2_snapshot(symbol: &str) -> Result<String> {
32 let settle = if symbol.ends_with("_USD") {
33 "btc"
34 } else if symbol.ends_with("_USDT") {
35 "usdt"
36 } else {
37 panic!("Unknown symbol {symbol}");
38 };
39 gen_api!(format!("/futures/{settle}/order_book?contract={symbol}&limit=200"))
40 }
41
42 /// Get open interest.
43 ///
44 /// For example:
45 /// - <https://api.gateio.ws/api/v4/futures/btc/contract_stats?contract=BTC_USD&interval=5m>
46 /// - <https://api.gateio.ws/api/v4/futures/usdt/contract_stats?contract=BTC_USDT&interval=5m>
47 pub fn fetch_open_interest(symbol: &str) -> Result<String> {
48 let settle = if symbol.ends_with("_USD") {
49 "btc"
50 } else if symbol.ends_with("_USDT") {
51 "usdt"
52 } else {
53 panic!("Unknown symbol {symbol}");
54 };
55 gen_api!(format!("/futures/{settle}/contract_stats?contract={symbol}&interval=5m"))
56 }
57}