Skip to main content

bpx_api_client/routes/
markets.rs

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