bpx_api_client/routes/
markets.rs

1use bpx_api_types::markets::{Asset, FundingRate, Kline, MarkPrice, Market, OrderBookDepth, Ticker};
2
3use crate::error::Result;
4use crate::BpxClient;
5
6const API_ASSETS: &str = "/api/v1/assets";
7const API_MARKETS: &str = "/api/v1/markets";
8const API_TICKER: &str = "/api/v1/ticker";
9const API_TICKERS: &str = "/api/v1/tickers";
10const API_DEPTH: &str = "/api/v1/depth";
11const API_KLINES: &str = "/api/v1/klines";
12const API_FUNDING: &str = "/api/v1/fundingRates";
13const API_MARK_PRICES: &str = "/api/v1/markPrices";
14
15impl BpxClient {
16    /// Fetches available assets and their associated tokens.
17    pub async fn get_assets(&self) -> Result<Vec<Asset>> {
18        let url = format!("{}{}", self.base_url, API_ASSETS);
19        let res = self.get(url).await?;
20        res.json().await.map_err(Into::into)
21    }
22
23    /// Retrieves a list of available markets.
24    pub async fn get_markets(&self) -> Result<Vec<Market>> {
25        let url = format!("{}{}", self.base_url, API_MARKETS);
26        let res = self.get(url).await?;
27        res.json().await.map_err(Into::into)
28    }
29
30    /// Retrieves mark price, index price and the funding rate for the current interval for all symbols, or the symbol specified.
31    pub async fn get_all_mark_prices(&self) -> Result<Vec<MarkPrice>> {
32        let url = format!("{}{}", self.base_url, API_MARK_PRICES);
33        let res = self.get(url).await?;
34        res.json().await.map_err(Into::into)
35    }
36
37    /// Fetches the ticker information for a given symbol.
38    pub async fn get_ticker(&self, symbol: &str) -> Result<Ticker> {
39        let url = format!("{}{}?symbol={}", self.base_url, API_TICKER, symbol);
40        let res = self.get(url).await?;
41        res.json().await.map_err(Into::into)
42    }
43
44    /// Fetches the ticker information for all symbols.
45    pub async fn get_tickers(&self) -> Result<Vec<Ticker>> {
46        let url = format!("{}{}", self.base_url, API_TICKERS);
47        let res = self.get(url).await?;
48        res.json().await.map_err(Into::into)
49    }
50
51    /// Retrieves the order book depth for a given symbol.
52    pub async fn get_order_book_depth(&self, symbol: &str) -> Result<OrderBookDepth> {
53        let url = format!("{}{}?symbol={}", self.base_url, API_DEPTH, symbol);
54        let res = self.get(url).await?;
55        res.json().await.map_err(Into::into)
56    }
57
58    /// Funding interval rate history for futures.
59    pub async fn get_funding_interval_rates(&self, symbol: &str) -> Result<Vec<FundingRate>> {
60        let url = format!("{}{}?symbol={}", self.base_url, API_FUNDING, symbol);
61        let res = self.get(url).await?;
62        res.json().await.map_err(Into::into)
63    }
64
65    /// Fetches historical K-line (candlestick) data for a given symbol and interval.
66    pub async fn get_k_lines(
67        &self,
68        symbol: &str,
69        kline_interval: &str,
70        start_time: Option<i64>,
71        end_time: Option<i64>,
72    ) -> Result<Vec<Kline>> {
73        let mut url = format!(
74            "/{}{}?symbol={}&kline_interval={}",
75            self.base_url, API_KLINES, symbol, kline_interval
76        );
77        for (k, v) in [("start_time", start_time), ("end_time", end_time)] {
78            if let Some(v) = v {
79                url.push_str(&format!("&{k}={v}"));
80            }
81        }
82        let res = self.get(url).await?;
83        res.json().await.map_err(Into::into)
84    }
85}