bpx_api_client/routes/
capital.rs

1use crate::error::Result;
2use std::collections::HashMap;
3
4use bpx_api_types::{
5    Blockchain,
6    capital::{Balance, Collateral, Deposit, DepositAddress, RequestWithdrawalPayload, Withdrawal},
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(
32        &self,
33        limit: Option<i64>,
34        offset: Option<i64>,
35    ) -> Result<Vec<Deposit>> {
36        let mut url = format!("{}{}", self.base_url, API_DEPOSITS);
37        for (k, v) in [("limit", limit), ("offset", offset)] {
38            if let Some(v) = v {
39                url.push_str(&format!("&{k}={v}"));
40            }
41        }
42        let res = self.get(url).await?;
43        res.json().await.map_err(Into::into)
44    }
45
46    /// Fetches the deposit address for a specified blockchain.
47    pub async fn get_deposit_address(&self, blockchain: Blockchain) -> Result<DepositAddress> {
48        let url = format!(
49            "{}{}?blockchain={}",
50            self.base_url, API_DEPOSIT_ADDRESS, blockchain
51        );
52        let res = self.get(url).await?;
53        res.json().await.map_err(Into::into)
54    }
55
56    /// Retrieves a list of withdrawals with optional pagination.
57    pub async fn get_withdrawals(
58        &self,
59        limit: Option<i64>,
60        offset: Option<i64>,
61    ) -> Result<Vec<Withdrawal>> {
62        let mut url = format!("{}{}", self.base_url, API_WITHDRAWALS);
63        for (k, v) in [("limit", limit), ("offset", offset)] {
64            if let Some(v) = v {
65                url.push_str(&format!("{k}={v}&"));
66            }
67        }
68        let res = self.get(url).await?;
69        res.json().await.map_err(Into::into)
70    }
71
72    /// Submits a withdrawal request for the specified payload.
73    pub async fn request_withdrawal(
74        &self,
75        payload: RequestWithdrawalPayload,
76    ) -> Result<Withdrawal> {
77        let endpoint = format!("{}{}", self.base_url, API_WITHDRAWALS);
78        let res = self.post(endpoint, payload).await?;
79        res.json().await.map_err(Into::into)
80    }
81
82    /// Fetches the subaccount's collateral information.
83    pub async fn get_collateral(&self) -> Result<Collateral> {
84        let url = format!("{}{}", self.base_url, API_COLLATERAL);
85        let res = self.get(url).await?;
86        res.json().await.map_err(Into::into)
87    }
88}