bpx_api_client/routes/
markets.rs

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