crypto_rest_client/exchanges/kraken/
kraken_spot.rs

1use super::super::utils::http_get;
2use crate::error::Result;
3use std::collections::BTreeMap;
4
5const BASE_URL: &str = "https://api.kraken.com";
6
7/// The WebSocket client for Kraken.
8///
9/// Kraken has only Spot market.
10///
11/// * REST API doc: <https://docs.kraken.com/rest/>
12/// * Trading at: <https://trade.kraken.com/>
13/// * Rate Limits: <https://docs.kraken.com/rest/#section/Rate-Limits/REST-API-Rate-Limits>
14///   * 15 requests per 45 seconds
15pub struct KrakenSpotRestClient {
16    _api_key: Option<String>,
17    _api_secret: Option<String>,
18}
19
20impl KrakenSpotRestClient {
21    pub fn new(api_key: Option<String>, api_secret: Option<String>) -> Self {
22        KrakenSpotRestClient { _api_key: api_key, _api_secret: api_secret }
23    }
24
25    /// Get most recent trades.
26    ///
27    /// If `since` is provided, return trade data since given id (exclusive).
28    ///
29    /// For example: <https://api.kraken.com/0/public/Trades?pair=XXBTZUSD&since=1609893937598797338>
30    #[allow(non_snake_case)]
31    pub fn fetch_trades(symbol: &str, since: Option<String>) -> Result<String> {
32        if symbol.contains('/') {
33            // websocket and RESTful API have different symbol format
34            // XBT/USD -> XBTUSD
35            let stripped = symbol.replace('/', "");
36            gen_api!(format!("/0/public/Trades?pair={}", &stripped), since)
37        } else {
38            gen_api!(format!("/0/public/Trades?pair={symbol}"), since)
39        }
40    }
41
42    /// Get a Level2 snapshot of orderbook.
43    ///
44    /// Top 500 bids and asks are returned.
45    ///
46    /// For example: <https://api.kraken.com/0/public/Depth?pair=XXBTZUSD&count=500>
47    pub fn fetch_l2_snapshot(symbol: &str) -> Result<String> {
48        if symbol.contains('/') {
49            // websocket and RESTful API have different symbol format
50            // XBT/USD -> XBTUSD
51            let stripped = symbol.replace('/', "");
52            gen_api!(format!("/0/public/Depth?pair={stripped}&count=500"))
53        } else {
54            gen_api!(format!("/0/public/Depth?pair={symbol}&count=500"))
55        }
56    }
57}