Skip to main content

bybit_api/api/
account.rs

1//! Account API endpoints.
2
3use crate::client::BybitClient;
4use crate::error::Result;
5use crate::models::account::*;
6use crate::models::*;
7
8impl BybitClient {
9    /// Get wallet balance.
10    ///
11    /// # Arguments
12    /// * `account_type` - Account type (UNIFIED, CONTRACT, etc.)
13    pub async fn get_wallet_balance(&self, account_type: AccountType) -> Result<WalletBalance> {
14        let account_type_str = format!("{:?}", account_type);
15        let params = vec![("accountType", account_type_str.as_str())];
16
17        self.get("/v5/account/wallet-balance", &params).await
18    }
19
20    /// Get account info.
21    pub async fn get_account_info(&self) -> Result<AccountInfo> {
22        self.get("/v5/account/info", &[]).await
23    }
24
25    /// Get fee rate.
26    ///
27    /// # Arguments
28    /// * `category` - Product category
29    /// * `symbol` - Optional symbol filter
30    pub async fn get_fee_rate(&self, category: Category, symbol: Option<&str>) -> Result<FeeRates> {
31        let cat_str = category.to_string();
32        let mut params = vec![("category", cat_str.as_str())];
33
34        if let Some(s) = symbol {
35            params.push(("symbol", s));
36        }
37
38        self.get("/v5/account/fee-rate", &params).await
39    }
40
41    /// Get transaction log.
42    ///
43    /// # Arguments
44    /// * `category` - Optional category filter
45    /// * `limit` - Optional limit (default 20)
46    pub async fn get_transaction_log(
47        &self,
48        category: Option<Category>,
49        limit: Option<u32>,
50    ) -> Result<TransactionLogs> {
51        let limit_str = limit.unwrap_or(20).to_string();
52        let mut params = vec![("limit", limit_str.as_str())];
53
54        let cat_str;
55        if let Some(c) = category {
56            cat_str = c.to_string();
57            params.push(("category", cat_str.as_str()));
58        }
59
60        self.get("/v5/account/transaction-log", &params).await
61    }
62
63    /// Set margin mode.
64    ///
65    /// # Arguments
66    /// * `margin_mode` - Margin mode (REGULAR_MARGIN, PORTFOLIO_MARGIN)
67    pub async fn set_margin_mode(&self, margin_mode: MarginMode) -> Result<serde_json::Value> {
68        let mode_str = format!("{:?}", margin_mode);
69        let params = SetMarginModeParams {
70            set_margin_mode: mode_str,
71        };
72
73        self.post("/v5/account/set-margin-mode", &params).await
74    }
75
76    /// Get collateral info.
77    ///
78    /// # Arguments
79    /// * `currency` - Optional currency filter
80    pub async fn get_collateral_info(&self, currency: Option<&str>) -> Result<CollateralInfo> {
81        let mut params = vec![];
82
83        if let Some(c) = currency {
84            params.push(("currency", c));
85        }
86
87        self.get("/v5/account/collateral-info", &params).await
88    }
89
90    /// Get borrow history.
91    ///
92    /// # Arguments
93    /// * `currency` - Optional currency filter
94    /// * `limit` - Optional limit (default 20)
95    pub async fn get_borrow_history(
96        &self,
97        currency: Option<&str>,
98        limit: Option<u32>,
99    ) -> Result<BorrowHistory> {
100        let limit_str = limit.unwrap_or(20).to_string();
101        let mut params = vec![("limit", limit_str.as_str())];
102
103        if let Some(c) = currency {
104            params.push(("currency", c));
105        }
106
107        self.get("/v5/account/borrow-history", &params).await
108    }
109}