crypto_rest_client/exchanges/
bitfinex.rs

1use super::utils::http_get;
2use crate::error::Result;
3use std::collections::BTreeMap;
4
5const BASE_URL: &str = "https://api-pub.bitfinex.com";
6
7/// The REST client for Bitfinex, including all markets.
8///
9/// * REST API doc: <https://docs.bitfinex.com/docs/rest-general>
10/// * Spot: <https://trading.bitfinex.com/trading>
11/// * Swap: <https://trading.bitfinex.com/t/BTCF0:USTF0>
12/// * Funding: <https://trading.bitfinex.com/funding>
13pub struct BitfinexRestClient {
14    _api_key: Option<String>,
15    _api_secret: Option<String>,
16}
17
18impl BitfinexRestClient {
19    pub fn new(api_key: Option<String>, api_secret: Option<String>) -> Self {
20        BitfinexRestClient { _api_key: api_key, _api_secret: api_secret }
21    }
22
23    /// /v2/trades/Symbol/hist
24    pub fn fetch_trades(
25        symbol: &str,
26        limit: Option<u16>,
27        start: Option<u64>,
28        end: Option<u64>,
29        sort: Option<i8>,
30    ) -> Result<String> {
31        gen_api!(format!("/v2/trades/{symbol}/hist"), limit, start, end, sort)
32    }
33
34    /// Get a Level2 snapshot of orderbook.
35    ///
36    /// Equivalent to `/v2/book/Symbol/P0` with `len=100`
37    ///
38    /// For example: <https://api-pub.bitfinex.com/v2/book/tBTCUSD/P0?len=100>
39    ///
40    /// Ratelimit: 90 req/min
41    pub fn fetch_l2_snapshot(symbol: &str) -> Result<String> {
42        let len = Some(100);
43        gen_api!(format!("/v2/book/{symbol}/P0"), len)
44    }
45
46    /// Get a Level3 snapshot of orderbook.
47    ///
48    /// Equivalent to `/v2/book/Symbol/R0` with `len=100`
49    ///
50    /// For example: <https://api-pub.bitfinex.com/v2/book/tBTCUSD/R0?len=100>
51    pub fn fetch_l3_snapshot(symbol: &str) -> Result<String> {
52        let len = Some(100);
53        gen_api!(format!("/v2/book/{symbol}/R0"), len)
54    }
55}