Skip to main content

bpx_api_client/routes/
borrow_lend.rs

1use bpx_api_types::borrow_lend::{
2    ApyRates, BorrowLendHistory, BorrowLendMarket, BorrowLendMarketHistoryParams,
3    BorrowLendPosition,
4};
5
6use crate::{BpxClient, Result};
7
8const API_BORROW_LEND_MARKETS: &str = "/api/v1/borrowLend/markets";
9const API_BORROW_LEND_MARKETS_HISTORY: &str = "/api/v1/borrowLend/markets/history";
10const API_BORROW_LEND_APY: &str = "/api/v1/borrowLend/apy";
11
12#[doc(hidden)]
13pub const API_BORROW_LEND_POSITIONS: &str = "/api/v1/borrowLend/positions";
14
15impl BpxClient {
16    /// Retrieves current state of all borrow/lend markets.
17    pub async fn get_borrow_lend_markets(&self) -> Result<Vec<BorrowLendMarket>> {
18        let url = self.base_url.join(API_BORROW_LEND_MARKETS)?;
19        let res = self.get(url).await?;
20        res.json().await.map_err(Into::into)
21    }
22
23    /// Retrieves historical snapshots of borrow/lend markets.
24    pub async fn get_borrow_lend_markets_history(
25        &self,
26        params: BorrowLendMarketHistoryParams,
27    ) -> Result<Vec<BorrowLendHistory>> {
28        let query_string = serde_qs::to_string(&params)
29            .map_err(|e| crate::error::Error::UrlParseError(e.to_string().into_boxed_str()))?;
30        let mut url = self.base_url.join(API_BORROW_LEND_MARKETS_HISTORY)?;
31        url.set_query(Some(&query_string));
32        let res = self.get(url).await?;
33        res.json().await.map_err(Into::into)
34    }
35
36    /// Retrieves borrow/lend and staking APY rates.
37    ///
38    /// When `tier_id` is provided and is 6 or higher, the USDC staking yield
39    /// reflects the corresponding VIP tier rate.
40    pub async fn get_apy_rates(&self, tier_id: Option<i32>) -> Result<ApyRates> {
41        let mut url = self.base_url.join(API_BORROW_LEND_APY)?;
42        if let Some(tier_id) = tier_id {
43            url.query_pairs_mut()
44                .append_pair("tierId", &tier_id.to_string());
45        }
46        let res = self.get(url).await?;
47        res.json().await.map_err(Into::into)
48    }
49
50    /// Retrieves all the open borrow lending positions for the account.
51    pub async fn get_borrow_lend_positions(&self) -> Result<Vec<BorrowLendPosition>> {
52        let url = self.base_url.join(API_BORROW_LEND_POSITIONS)?;
53        let res = self.get(url).await?;
54        res.json().await.map_err(Into::into)
55    }
56}