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 = self.base_url.join(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 = self.base_url.join(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 = self.base_url.join(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 mut url = self.base_url.join(API_TICKER)?;
42        url.query_pairs_mut().append_pair("symbol", symbol);
43        let res = self.get(url).await?;
44        res.json().await.map_err(Into::into)
45    }
46
47    /// Fetches the ticker information for all symbols.
48    pub async fn get_tickers(&self) -> Result<Vec<Ticker>> {
49        let url = self.base_url.join(API_TICKERS)?;
50        let res = self.get(url).await?;
51        res.json().await.map_err(Into::into)
52    }
53
54    /// Retrieves the order book depth for a given symbol.
55    pub async fn get_order_book_depth(&self, symbol: &str) -> Result<OrderBookDepth> {
56        let mut url = self.base_url.join(API_DEPTH)?;
57        url.query_pairs_mut().append_pair("symbol", symbol);
58        let res = self.get(url).await?;
59        res.json().await.map_err(Into::into)
60    }
61
62    /// Funding interval rate history for futures.
63    pub async fn get_funding_interval_rates(&self, symbol: &str) -> Result<Vec<FundingRate>> {
64        let mut url = self.base_url.join(API_FUNDING)?;
65        url.query_pairs_mut().append_pair("symbol", symbol);
66        let res = self.get(url).await?;
67        res.json().await.map_err(Into::into)
68    }
69
70    /// Fetches historical K-line (candlestick) data for a given symbol and interval.
71    pub async fn get_k_lines(
72        &self,
73        symbol: &str,
74        kline_interval: &str,
75        start_time: i64,
76        end_time: Option<i64>,
77    ) -> Result<Vec<Kline>> {
78        let mut url = self.base_url.join(API_KLINES)?;
79        {
80            let mut query = url.query_pairs_mut();
81            query.append_pair("symbol", symbol);
82            query.append_pair("interval", kline_interval);
83            query.append_pair("startTime", &start_time.to_string());
84            if let Some(end_time) = end_time {
85                query.append_pair("endTime", &end_time.to_string());
86            }
87        }
88        let res = self.get(url).await?;
89        res.json().await.map_err(Into::into)
90    }
91}