bpx_api_client/routes/
account.rs

1use crate::BpxClient;
2use crate::error::Result;
3use bpx_api_types::account::{
4    AccountMaxBorrow, AccountMaxWithdrawal, AccountSettings, ConvertDustPayload,
5    UpdateAccountPayload,
6};
7
8#[doc(hidden)]
9pub const API_ACCOUNT: &str = "/api/v1/account";
10#[doc(hidden)]
11pub const API_ACCOUNT_MAX_BORROW: &str = "/api/v1/account/limits/borrow";
12#[doc(hidden)]
13pub const API_ACCOUNT_MAX_WITHDRAWAL: &str = "/api/v1/account/limits/withdrawal";
14#[doc(hidden)]
15pub const API_ACCOUNT_CONVERT_DUST: &str = "/api/v1/account/convertDust";
16
17impl BpxClient {
18    /// Fetches the account's settings.
19    pub async fn get_account(&self) -> Result<AccountSettings> {
20        let url = self.base_url.join(API_ACCOUNT)?;
21        let res = self.get(url).await?;
22        res.json().await.map_err(Into::into)
23    }
24
25    /// Fetches the account's maximum borrow amount for a given symbol.
26    pub async fn get_account_max_borrow(&self, symbol: &str) -> Result<AccountMaxBorrow> {
27        let mut url = self.base_url.join(API_ACCOUNT_MAX_BORROW)?;
28        url.query_pairs_mut().append_pair("symbol", symbol);
29        let res = self.get(url).await?;
30        res.json().await.map_err(Into::into)
31    }
32
33    /// Fetches the account's maximum withdrawal amount for a given symbol.
34    pub async fn get_account_max_withdrawal(
35        &self,
36        symbol: &str,
37        auto_borrow: Option<bool>,
38        auto_lend_redeem: Option<bool>,
39    ) -> Result<AccountMaxWithdrawal> {
40        let mut url = self.base_url.join(API_ACCOUNT_MAX_WITHDRAWAL)?;
41        {
42            let mut query = url.query_pairs_mut();
43            query.append_pair("symbol", symbol);
44            if let Some(auto_borrow) = auto_borrow {
45                query.append_pair("autoBorrow", &auto_borrow.to_string());
46            }
47            if let Some(auto_lend_redeem) = auto_lend_redeem {
48                query.append_pair("autoLendRedeem", &auto_lend_redeem.to_string());
49            }
50        }
51        let res = self.get(url).await?;
52        res.json().await.map_err(Into::into)
53    }
54
55    /// Updates the account's settings.
56    pub async fn update_account(&self, payload: UpdateAccountPayload) -> Result<()> {
57        let url = self.base_url.join(API_ACCOUNT)?;
58        self.patch(url, payload).await?;
59
60        Ok(())
61    }
62
63    /// Converts a dust balance to USDC. The balance (including lend) must be less
64    /// than the minimum quantity tradable on the spot order book.
65    pub async fn convert_dust_balance(&self, payload: ConvertDustPayload) -> Result<()> {
66        let url = self.base_url.join(API_ACCOUNT_CONVERT_DUST)?;
67        self.post(url, payload).await?;
68
69        Ok(())
70    }
71}