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 = account_type.to_string();
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    // FIXME(typed-signature): falls back to `serde_json::Value` because the
68    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
69    // not auto-resolve. Replace with a typed struct in a follow-up PR.
70    pub async fn set_margin_mode(&self, margin_mode: MarginMode) -> Result<serde_json::Value> {
71        let params = SetMarginModeParams {
72            set_margin_mode: margin_mode.to_string(),
73        };
74
75        self.post("/v5/account/set-margin-mode", &params).await
76    }
77
78    /// Get collateral info.
79    ///
80    /// # Arguments
81    /// * `currency` - Optional currency filter
82    pub async fn get_collateral_info(&self, currency: Option<&str>) -> Result<CollateralInfo> {
83        let mut params = vec![];
84
85        if let Some(c) = currency {
86            params.push(("currency", c));
87        }
88
89        self.get("/v5/account/collateral-info", &params).await
90    }
91
92    /// Get borrow history.
93    ///
94    /// # Arguments
95    /// * `currency` - Optional currency filter
96    /// * `limit` - Optional limit (default 20)
97    pub async fn get_borrow_history(
98        &self,
99        currency: Option<&str>,
100        limit: Option<u32>,
101    ) -> Result<BorrowHistory> {
102        let limit_str = limit.unwrap_or(20).to_string();
103        let mut params = vec![("limit", limit_str.as_str())];
104
105        if let Some(c) = currency {
106            params.push(("currency", c));
107        }
108
109        self.get("/v5/account/borrow-history", &params).await
110    }
111
112    pub async fn batch_set_collateral(
113        &self,
114        params: BatchSetCollateralParams,
115    ) -> Result<BatchSetCollateralResponse> {
116        self.post("/v5/account/set-collateral-switch-batch", &params)
117            .await
118    }
119
120    pub async fn get_account_instruments(
121        &self,
122        category: Category,
123        symbol: Option<&str>,
124        limit: Option<u32>,
125        cursor: Option<&str>,
126    ) -> Result<GetAccountInstrumentsResponse> {
127        let cat_str = category.to_string();
128        let limit_str;
129        let mut params = vec![("category", cat_str.as_str())];
130        if let Some(s) = symbol {
131            params.push(("symbol", s));
132        }
133        if let Some(l) = limit {
134            limit_str = l.to_string();
135            params.push(("limit", limit_str.as_str()));
136        }
137        if let Some(c) = cursor {
138            params.push(("cursor", c));
139        }
140        self.get("/v5/account/instruments-info", &params).await
141    }
142
143    pub async fn get_coin_greeks(&self, base_coin: Option<&str>) -> Result<GetCoinGreeksResponse> {
144        let mut params = vec![];
145        if let Some(b) = base_coin {
146            params.push(("baseCoin", b));
147        }
148        self.get("/v5/asset/coin-greeks", &params).await
149    }
150
151    pub async fn get_dcp_info(&self) -> Result<GetDcpInfoResponse> {
152        self.get("/v5/account/query-dcp-info", &[]).await
153    }
154
155    pub async fn get_group_fee_rate(
156        &self,
157        product_type: &str,
158        group_id: Option<&str>,
159    ) -> Result<GetGroupFeeRateResponse> {
160        let mut params = vec![("productType", product_type)];
161        if let Some(g) = group_id {
162            params.push(("groupId", g));
163        }
164        self.get_public("/v5/market/fee-group-info", &params).await
165    }
166
167    pub async fn get_mmp_state(&self, base_coin: &str) -> Result<GetMmpStateResponse> {
168        let params = vec![("baseCoin", base_coin)];
169        self.get("/v5/account/mmp-state", &params).await
170    }
171
172    pub async fn get_smp_group(&self) -> Result<GetSmpGroupResponse> {
173        self.get("/v5/account/smp-group", &[]).await
174    }
175
176    pub async fn get_transferable_amount(
177        &self,
178        coin_name: &str,
179    ) -> Result<GetTransferableAmountResponse> {
180        let params = vec![("coinName", coin_name)];
181        self.get("/v5/account/withdrawal", &params).await
182    }
183
184    pub async fn get_user_settings(&self) -> Result<GetUserSettingsResponse> {
185        self.get("/v5/account/user-setting-config", &[]).await
186    }
187
188    pub async fn manual_borrow(&self, params: ManualBorrowParams) -> Result<ManualBorrowResponse> {
189        self.post("/v5/account/borrow", &params).await
190    }
191
192    pub async fn manual_repay(&self, params: ManualRepayParams) -> Result<ManualRepayResponse> {
193        self.post("/v5/account/repay", &params).await
194    }
195
196    pub async fn no_convert_repay(
197        &self,
198        params: NoConvertRepayParams,
199    ) -> Result<NoConvertRepayResponse> {
200        self.post("/v5/account/no-convert-repay", &params).await
201    }
202
203    pub async fn one_click_repay(
204        &self,
205        params: OneClickRepayParams,
206    ) -> Result<OneClickRepayResponse> {
207        self.post("/v5/account/quick-repayment", &params).await
208    }
209
210    pub async fn reset_mmp(&self, params: ResetMmpParams) -> Result<ResetMmpResponse> {
211        self.post("/v5/account/mmp-reset", &params).await
212    }
213
214    // FIXME(typed-signature): falls back to `serde_json::Value` because the
215    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
216    // not auto-resolve. Replace with a typed struct in a follow-up PR.
217    pub async fn set_collateral_coin(
218        &self,
219        params: SetCollateralCoinParams,
220    ) -> Result<serde_json::Value> {
221        self.post("/v5/account/set-collateral-switch", &params)
222            .await
223    }
224
225    pub async fn set_mmp(&self, params: SetMmpParams) -> Result<SetMmpResponse> {
226        self.post("/v5/account/mmp-modify", &params).await
227    }
228
229    pub async fn set_price_limit(
230        &self,
231        params: SetPriceLimitParams,
232    ) -> Result<SetPriceLimitResponse> {
233        self.post("/v5/account/set-limit-px-action", &params).await
234    }
235
236    pub async fn set_spot_hedging(
237        &self,
238        params: SetSpotHedgingParams,
239    ) -> Result<SetSpotHedgingResponse> {
240        self.post("/v5/account/set-hedging-mode", &params).await
241    }
242
243    pub async fn upgrade_to_uta_pro(&self) -> Result<UpgradeToUtaProResponse> {
244        self.post("/v5/account/upgrade-to-uta", &serde_json::json!({}))
245            .await
246    }
247}