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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#![allow(unused)]
use async_trait::async_trait;
use std::{
collections::{BTreeMap, HashMap},
pin::Pin,
sync::Arc,
};
use futures::Future;
use reqwest::Method;
use serde_json::Value;
use crate::endpoints::v5account;
use super::http_manager::{HttpManager, Manager};
#[async_trait]
pub trait Account {
fn new(http_manager: Arc<HttpManager>) -> Self;
async fn get_wallet_balance(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn upgrade_to_unified_trading_account(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_borrow_history(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_collateral_info(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_coin_greeks(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_fee_rates(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_account_info(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_transaction_log(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn set_margin_mode(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn set_mmp(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn reset_mmp(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_mmp_state(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
}
pub struct AccountHTTP {
http_manager: Arc<HttpManager>,
}
#[async_trait]
impl Account for AccountHTTP {
///
///
/// Initialize the AccountHTTP by passing the HttpManager
///
///
fn new(http_manager: Arc<HttpManager>) -> Self {
AccountHTTP { http_manager }
}
/// Obtain wallet balance, query asset information of each currency, and account risk rate information under unified margin mode.
/// By default, currency information with assets or liabilities of 0 is not returned.
/// Required args:
/// accountType (string): Account type
/// Unified account: UNIFIED
/// Normal account: CONTRACT
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/wallet-balance
async fn get_wallet_balance(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::GetWalletBalance.to_string();
let result = self
.http_manager
.submit_request(Method::GET, &endpoint, query, true)
.await?;
Ok(result)
}
/// Upgrade Unified Account
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/upgrade-unified-account
async fn upgrade_to_unified_trading_account(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::UpgradeToUnifiedAccount.to_string();
let result = self
.http_manager
.submit_post_request(Method::POST, &endpoint, true, query)
.await?;
Ok(result)
}
/// Get interest records, sorted in reverse order of creation time.
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/borrow-history
async fn get_borrow_history(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::GetBorrowHistory.to_string();
let result = self
.http_manager
.submit_request(Method::GET, &endpoint, query, true)
.await?;
Ok(result)
}
/// Get the collateral information of the current unified margin account, including loan interest rate, loanable amount, collateral conversion rate, whether it can be mortgaged as margin, etc.
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/collateral-info
async fn get_collateral_info(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::GetCollateralInfo.to_string();
let result = self
.http_manager
.submit_request(Method::GET, &endpoint, query, true)
.await?;
Ok(result)
}
/// Get current account Greeks information
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/coin-greeks
async fn get_coin_greeks(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::GetCoinGreeks.to_string();
let result = self
.http_manager
.submit_request(Method::GET, &endpoint, query, false)
.await?;
Ok(result)
}
/// Get the trading fee rate of derivatives.
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/fee-rate
async fn get_fee_rates(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::GetFeeRate.to_string();
let result = self
.http_manager
.submit_request(Method::GET, &endpoint, query, false)
.await?;
Ok(result)
}
/// Query the margin mode configuration of the account.
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/account-info
async fn get_account_info(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::GetAccountInfo.to_string();
let result = self
.http_manager
.submit_request(Method::GET, &endpoint, query, true)
.await?;
Ok(result)
}
/// Query transaction logs in Unified account.
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/transaction-log
async fn get_transaction_log(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::GetTransactionLog.to_string();
let result = self
.http_manager
.submit_request(Method::GET, &endpoint, query, true)
.await?;
Ok(result)
}
/// Default is regular margin mode. This mode is valid for USDT Perp, USDC Perp and USDC Option.
/// Required args:
/// setMarginMode (string): REGULAR_MARGIN, PORTFOLIO_MARGIN
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/set-margin-mode
async fn set_margin_mode(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::SetMarginMode.to_string();
let result = self
.http_manager
.submit_post_request(Method::POST, &endpoint, true, query)
.await?;
Ok(result)
}
/// Market Maker Protection (MMP) is an automated mechanism designed to protect market makers (MM) against liquidity risks
/// and over-exposure in the market. It prevents simultaneous trade executions on quotes provided by the MM within a short time span.
/// The MM can automatically pull their quotes if the number of contracts traded for an underlying asset exceeds the configured
/// threshold within a certain time frame. Once MMP is triggered, any pre-existing MMP orders will be automatically canceled,
/// and new orders tagged as MMP will be rejected for a specific duration — known as the frozen period — so that MM can
/// reassess the market and modify the quotes.
/// Required args:
/// baseCoin (strin): Base coin
/// window (string): Time window (ms)
/// frozenPeriod (string): Frozen period (ms). "0" means the trade will remain frozen until manually reset
/// qtyLimit (string): Trade qty limit (positive and up to 2 decimal places)
/// deltaLimit (string): Delta limit (positive and up to 2 decimal places)
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/set-mmp
async fn set_mmp(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::SetMMP.to_string();
let result = self
.http_manager
.submit_post_request(Method::POST, &endpoint, true, query)
.await?;
Ok(result)
}
/// Once the mmp triggered, you can unfreeze the account by this endpoint
/// Required args:
/// baseCoin (string): Base coin
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/reset-mmp
async fn reset_mmp(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::ResetMMP.to_string();
let result = self
.http_manager
.submit_post_request(Method::POST, &endpoint, true, query)
.await?;
Ok(result)
}
/// Get MMP state
/// Required args:
/// baseCoin (string): Base coin
/// Returns:
/// Request results as HashMap.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/account/get-mmp-state
async fn get_mmp_state(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
let endpoint = v5account::Account::GetMMPState.to_string();
let result = self
.http_manager
.submit_request(Method::GET, &endpoint, query, true)
.await?;
Ok(result)
}
}