bpx_api_client/routes/
markets.rs

1use bpx_api_types::markets::{
2    Asset, FundingRate, Kline, MarkPrice, Market, OrderBookDepth, OrderBookDepthLimit, 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(
27        &self,
28        market_types: Option<impl IntoIterator<Item = impl AsRef<str>>>,
29    ) -> Result<Vec<Market>> {
30        let mut url = self.base_url.join(API_MARKETS)?;
31        if let Some(market_types) = market_types {
32            let mut query = url.query_pairs_mut();
33            for market_type in market_types {
34                query.append_pair("marketType", market_type.as_ref());
35            }
36        }
37        let res = self.get(url).await?;
38        res.json().await.map_err(Into::into)
39    }
40
41    /// Retrieves mark price, index price and the funding rate for the current interval for all symbols, or the symbol specified.
42    pub async fn get_all_mark_prices(&self) -> Result<Vec<MarkPrice>> {
43        let url = self.base_url.join(API_MARK_PRICES)?;
44        let res = self.get(url).await?;
45        res.json().await.map_err(Into::into)
46    }
47
48    /// Fetches the ticker information for a given symbol.
49    pub async fn get_ticker(&self, symbol: &str) -> Result<Ticker> {
50        let mut url = self.base_url.join(API_TICKER)?;
51        url.query_pairs_mut().append_pair("symbol", symbol);
52        let res = self.get(url).await?;
53        res.json().await.map_err(Into::into)
54    }
55
56    /// Fetches the ticker information for all symbols.
57    pub async fn get_tickers(&self) -> Result<Vec<Ticker>> {
58        let url = self.base_url.join(API_TICKERS)?;
59        let res = self.get(url).await?;
60        res.json().await.map_err(Into::into)
61    }
62
63    /// Retrieves the order book depth for a given symbol.
64    pub async fn get_order_book_depth(
65        &self,
66        symbol: &str,
67        limit: Option<OrderBookDepthLimit>,
68    ) -> Result<OrderBookDepth> {
69        let mut url = self.base_url.join(API_DEPTH)?;
70        url.query_pairs_mut().append_pair("symbol", symbol);
71        if let Some(limit) = limit {
72            url.query_pairs_mut().append_pair("limit", limit.as_ref());
73        }
74        let res = self.get(url).await?;
75        res.json().await.map_err(Into::into)
76    }
77
78    /// Funding interval rate history for futures.
79    pub async fn get_funding_interval_rates(&self, symbol: &str) -> Result<Vec<FundingRate>> {
80        let mut url = self.base_url.join(API_FUNDING)?;
81        url.query_pairs_mut().append_pair("symbol", symbol);
82        let res = self.get(url).await?;
83        res.json().await.map_err(Into::into)
84    }
85
86    /// Fetches historical K-line (candlestick) data for a given symbol and interval.
87    pub async fn get_k_lines(
88        &self,
89        symbol: &str,
90        kline_interval: &str,
91        start_time: i64,
92        end_time: Option<i64>,
93    ) -> Result<Vec<Kline>> {
94        let mut url = self.base_url.join(API_KLINES)?;
95        {
96            let mut query = url.query_pairs_mut();
97            query.append_pair("symbol", symbol);
98            query.append_pair("interval", kline_interval);
99            query.append_pair("startTime", &start_time.to_string());
100            if let Some(end_time) = end_time {
101                query.append_pair("endTime", &end_time.to_string());
102            }
103        }
104        let res = self.get(url).await?;
105        res.json().await.map_err(Into::into)
106    }
107}