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