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 = self.base_url.join(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 = self.base_url.join(API_DEPOSITS)?;
37        {
38            let mut query = url.query_pairs_mut();
39            if let Some(limit) = limit {
40                query.append_pair("limit", &limit.to_string());
41            }
42            if let Some(offset) = offset {
43                query.append_pair("offset", &offset.to_string());
44            }
45        }
46        let res = self.get(url).await?;
47        res.json().await.map_err(Into::into)
48    }
49
50    /// Fetches the deposit address for a specified blockchain.
51    pub async fn get_deposit_address(&self, blockchain: Blockchain) -> Result<DepositAddress> {
52        let mut url = self.base_url.join(API_DEPOSIT_ADDRESS)?;
53        url.query_pairs_mut()
54            .append_pair("blockchain", &blockchain.to_string());
55        let res = self.get(url).await?;
56        res.json().await.map_err(Into::into)
57    }
58
59    /// Retrieves a list of withdrawals with optional pagination.
60    pub async fn get_withdrawals(
61        &self,
62        limit: Option<i64>,
63        offset: Option<i64>,
64    ) -> Result<Vec<Withdrawal>> {
65        let mut url = self.base_url.join(API_WITHDRAWALS)?;
66        {
67            let mut query = url.query_pairs_mut();
68            if let Some(limit) = limit {
69                query.append_pair("limit", &limit.to_string());
70            }
71            if let Some(offset) = offset {
72                query.append_pair("offset", &offset.to_string());
73            }
74        }
75        let res = self.get(url).await?;
76        res.json().await.map_err(Into::into)
77    }
78
79    /// Submits a withdrawal request for the specified payload.
80    pub async fn request_withdrawal(
81        &self,
82        payload: RequestWithdrawalPayload,
83    ) -> Result<Withdrawal> {
84        let endpoint = self.base_url.join(API_WITHDRAWALS)?;
85        let res = self.post(endpoint, payload).await?;
86        res.json().await.map_err(Into::into)
87    }
88
89    /// Fetches the subaccount's collateral information.
90    pub async fn get_collateral(&self) -> Result<Collateral> {
91        let url = self.base_url.join(API_COLLATERAL)?;
92        let res = self.get(url).await?;
93        res.json().await.map_err(Into::into)
94    }
95}