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(&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(
56        &self,
57        symbol: &str,
58        limit: Option<OrderBookDepthLimit>,
59    ) -> Result<OrderBookDepth> {
60        let mut url = self.base_url.join(API_DEPTH)?;
61        url.query_pairs_mut().append_pair("symbol", symbol);
62        if let Some(limit) = limit {
63            url.query_pairs_mut().append_pair("limit", limit.as_ref());
64        }
65        let res = self.get(url).await?;
66        res.json().await.map_err(Into::into)
67    }
68
69    /// Funding interval rate history for futures.
70    pub async fn get_funding_interval_rates(&self, symbol: &str) -> Result<Vec<FundingRate>> {
71        let mut url = self.base_url.join(API_FUNDING)?;
72        url.query_pairs_mut().append_pair("symbol", symbol);
73        let res = self.get(url).await?;
74        res.json().await.map_err(Into::into)
75    }
76
77    /// Fetches historical K-line (candlestick) data for a given symbol and interval.
78    pub async fn get_k_lines(
79        &self,
80        symbol: &str,
81        kline_interval: &str,
82        start_time: i64,
83        end_time: Option<i64>,
84    ) -> Result<Vec<Kline>> {
85        let mut url = self.base_url.join(API_KLINES)?;
86        {
87            let mut query = url.query_pairs_mut();
88            query.append_pair("symbol", symbol);
89            query.append_pair("interval", kline_interval);
90            query.append_pair("startTime", &start_time.to_string());
91            if let Some(end_time) = end_time {
92                query.append_pair("endTime", &end_time.to_string());
93            }
94        }
95        let res = self.get(url).await?;
96        res.json().await.map_err(Into::into)
97    }
98}