bpx_api_client/routes/
capital.rs

1use crate::error::Result;
2use std::collections::HashMap;
3
4use bpx_api_types::{
5    capital::{Balance, 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
20impl BpxClient {
21    /// Fetches the account's current balances.
22    pub async fn get_balances(&self) -> Result<HashMap<String, Balance>> {
23        let url = format!("{}{}", self.base_url, API_CAPITAL);
24        let res = self.get(url).await?;
25        res.json().await.map_err(Into::into)
26    }
27
28    /// Retrieves a list of deposits with optional pagination.
29    pub async fn get_deposits(&self, limit: Option<i64>, offset: Option<i64>) -> Result<Vec<Deposit>> {
30        let mut url = format!("{}{}", self.base_url, API_DEPOSITS);
31        for (k, v) in [("limit", limit), ("offset", offset)] {
32            if let Some(v) = v {
33                url.push_str(&format!("&{}={}", k, v));
34            }
35        }
36        let res = self.get(url).await?;
37        res.json().await.map_err(Into::into)
38    }
39
40    /// Fetches the deposit address for a specified blockchain.
41    pub async fn get_deposit_address(&self, blockchain: Blockchain) -> Result<DepositAddress> {
42        let url = format!("{}{}?blockchain={}", self.base_url, API_DEPOSIT_ADDRESS, blockchain);
43        let res = self.get(url).await?;
44        res.json().await.map_err(Into::into)
45    }
46
47    /// Retrieves a list of withdrawals with optional pagination.
48    pub async fn get_withdrawals(&self, limit: Option<i64>, offset: Option<i64>) -> Result<Vec<Withdrawal>> {
49        let mut url = format!("{}{}", self.base_url, API_WITHDRAWALS);
50        for (k, v) in [("limit", limit), ("offset", offset)] {
51            if let Some(v) = v {
52                url.push_str(&format!("{}={}&", k, v));
53            }
54        }
55        let res = self.get(url).await?;
56        res.json().await.map_err(Into::into)
57    }
58
59    /// Submits a withdrawal request for the specified payload.
60    pub async fn request_withdrawal(&self, payload: RequestWithdrawalPayload) -> Result<Withdrawal> {
61        let endpoint = format!("{}{}", self.base_url, API_WITHDRAWALS);
62        let res = self.post(endpoint, payload).await?;
63        res.json().await.map_err(Into::into)
64    }
65}