bpx_api_client/routes/
capital.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::error::Result;
use std::collections::HashMap;

use bpx_api_types::{
    capital::{Balance, Deposit, DepositAddress, RequestWithdrawalPayload, Withdrawal},
    Blockchain,
};

use crate::BpxClient;

#[doc(hidden)]
pub const API_CAPITAL: &str = "/api/v1/capital";
#[doc(hidden)]
pub const API_DEPOSITS: &str = "/wapi/v1/capital/deposits";
#[doc(hidden)]
pub const API_DEPOSIT_ADDRESS: &str = "/wapi/v1/capital/deposit/address";
#[doc(hidden)]
pub const API_WITHDRAWALS: &str = "/wapi/v1/capital/withdrawals";

impl BpxClient {
    /// Fetches the account's current balances.
    pub async fn get_balances(&self) -> Result<HashMap<String, Balance>> {
        let url = format!("{}{}", self.base_url, API_CAPITAL);
        let res = self.get(url).await?;
        res.json().await.map_err(Into::into)
    }

    /// Retrieves a list of deposits with optional pagination.
    pub async fn get_deposits(&self, limit: Option<i64>, offset: Option<i64>) -> Result<Vec<Deposit>> {
        let mut url = format!("{}{}", self.base_url, API_DEPOSITS);
        for (k, v) in [("limit", limit), ("offset", offset)] {
            if let Some(v) = v {
                url.push_str(&format!("&{}={}", k, v));
            }
        }
        let res = self.get(url).await?;
        res.json().await.map_err(Into::into)
    }

    /// Fetches the deposit address for a specified blockchain.
    pub async fn get_deposit_address(&self, blockchain: Blockchain) -> Result<DepositAddress> {
        let url = format!("{}{}?blockchain={}", self.base_url, API_DEPOSIT_ADDRESS, blockchain);
        let res = self.get(url).await?;
        res.json().await.map_err(Into::into)
    }

    /// Retrieves a list of withdrawals with optional pagination.
    pub async fn get_withdrawals(&self, limit: Option<i64>, offset: Option<i64>) -> Result<Vec<Withdrawal>> {
        let mut url = format!("{}{}", self.base_url, API_WITHDRAWALS);
        for (k, v) in [("limit", limit), ("offset", offset)] {
            if let Some(v) = v {
                url.push_str(&format!("{}={}&", k, v));
            }
        }
        let res = self.get(url).await?;
        res.json().await.map_err(Into::into)
    }

    /// Submits a withdrawal request for the specified payload.
    pub async fn request_withdrawal(&self, payload: RequestWithdrawalPayload) -> Result<Withdrawal> {
        let endpoint = format!("{}{}", self.base_url, API_WITHDRAWALS);
        let res = self.post(endpoint, payload).await?;
        res.json().await.map_err(Into::into)
    }
}