bpx_api_client/routes/
capital.rs

1use crate::error::Result;
2use std::collections::HashMap;
3
4use bpx_api_types::{
5    capital::{Balance, Collateral, Deposit, DepositAddress, RequestWithdrawalPayload, Withdrawal},
6    Blockchain,
7};
8
9use crate::BpxClient;
10
11#[doc(hidden)]
12pub const API_CAPITAL: &str = "/api/v1/capital";
13#[doc(hidden)]
14pub const API_DEPOSITS: &str = "/wapi/v1/capital/deposits";
15#[doc(hidden)]
16pub const API_DEPOSIT_ADDRESS: &str = "/wapi/v1/capital/deposit/address";
17#[doc(hidden)]
18pub const API_WITHDRAWALS: &str = "/wapi/v1/capital/withdrawals";
19#[doc(hidden)]
20pub const API_COLLATERAL: &str = "/api/v1/capital/collateral";
21
22impl BpxClient {
23    /// Fetches the account's current balances.
24    pub async fn get_balances(&self) -> Result<HashMap<String, Balance>> {
25        let url = format!("{}{}", self.base_url, API_CAPITAL);
26        let res = self.get(url).await?;
27        res.json().await.map_err(Into::into)
28    }
29
30    /// Retrieves a list of deposits with optional pagination.
31    pub async fn get_deposits(&self, limit: Option<i64>, offset: Option<i64>) -> Result<Vec<Deposit>> {
32        let mut url = format!("{}{}", self.base_url, API_DEPOSITS);
33        for (k, v) in [("limit", limit), ("offset", offset)] {
34            if let Some(v) = v {
35                url.push_str(&format!("&{}={}", k, v));
36            }
37        }
38        let res = self.get(url).await?;
39        res.json().await.map_err(Into::into)
40    }
41
42    /// Fetches the deposit address for a specified blockchain.
43    pub async fn get_deposit_address(&self, blockchain: Blockchain) -> Result<DepositAddress> {
44        let url = format!("{}{}?blockchain={}", self.base_url, API_DEPOSIT_ADDRESS, blockchain);
45        let res = self.get(url).await?;
46        res.json().await.map_err(Into::into)
47    }
48
49    /// Retrieves a list of withdrawals with optional pagination.
50    pub async fn get_withdrawals(&self, limit: Option<i64>, offset: Option<i64>) -> Result<Vec<Withdrawal>> {
51        let mut url = format!("{}{}", self.base_url, API_WITHDRAWALS);
52        for (k, v) in [("limit", limit), ("offset", offset)] {
53            if let Some(v) = v {
54                url.push_str(&format!("{}={}&", k, v));
55            }
56        }
57        let res = self.get(url).await?;
58        res.json().await.map_err(Into::into)
59    }
60
61    /// Submits a withdrawal request for the specified payload.
62    pub async fn request_withdrawal(&self, payload: RequestWithdrawalPayload) -> Result<Withdrawal> {
63        let endpoint = format!("{}{}", self.base_url, API_WITHDRAWALS);
64        let res = self.post(endpoint, payload).await?;
65        res.json().await.map_err(Into::into)
66    }
67
68    /// Fetches the subaccount's collateral information.
69    pub async fn get_collateral(&self) -> Result<Collateral> {
70        let url = format!("{}{}", self.base_url, API_COLLATERAL);
71        let res = self.get(url).await?;
72        res.json().await.map_err(Into::into)
73    }
74}