rsbit 0.5.4

This is a library for the Bybit API.
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use crate::{
    v5::api::{
        BybitApi,
        get::Get,
    },
    utils::{
        deserialize_f64,
        deserialize_option_f64,
    },
};

use serde::{
    Serialize,
    Deserialize,
};
use serde_json::Value;
use anyhow::Result;

const PATH: &'static str = "/v5/account/wallet-balance";

impl BybitApi {
    pub async fn get_wallet_balance(&self, params: GetWalletBalanceParameters) -> Result<GetWalletBalanceResponse> {
        self.get(PATH, Some(params), true).await
    }
}

#[derive(Debug, Clone, Serialize)]
pub enum GetWalletBalanceAccountType {
    UNIFIED,
    CONTRACT,
    SPOT,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetWalletBalanceParameters {
    account_type: GetWalletBalanceAccountType,
    coin: Option<String>,
}

impl GetWalletBalanceParameters {
    pub fn new(account_type: GetWalletBalanceAccountType) -> Self {
        Self {
            account_type,
            coin: None
        }
    }

    pub fn with_coin(mut self, coin: String) -> Self {
        self.coin = Some(coin);
        self
    }
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetWalletBalanceResponse {
    ret_code: i32,
    ret_msg: String,
    result: WalletBalanceResult,
    ret_ext_info: Value,
    time: u64,
}
impl GetWalletBalanceResponse {
    pub fn ret_code(&self) -> i32 {
        self.ret_code
    }

    pub fn set_ret_code(&mut self, ret_code: i32) {
        self.ret_code = ret_code;
    }

    pub fn ret_msg(&self) -> &str {
        &self.ret_msg
    }

    pub fn set_ret_msg(&mut self, ret_msg: String) {
        self.ret_msg = ret_msg;
    }

    pub fn result(&self) -> &WalletBalanceResult {
        &self.result
    }

    pub fn set_result(&mut self, result: WalletBalanceResult) {
        self.result = result;
    }

    pub fn ret_ext_info(&self) -> &Value {
        &self.ret_ext_info
    }

    pub fn set_ret_ext_info(&mut self, ret_ext_info: Value) {
        self.ret_ext_info = ret_ext_info;
    }

    pub fn time(&self) -> u64 {
        self.time
    }

    pub fn set_time(&mut self, time: u64) {
        self.time = time;
    }
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WalletBalanceResult {
    list: Vec<WalletBalance>
}
impl WalletBalanceResult {
    pub fn list(&self) -> &Vec<WalletBalance> {
        &self.list
    }

    pub fn set_list(&mut self, list: Vec<WalletBalance>) {
        self.list = list;
    }
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WalletBalance {
    #[serde(deserialize_with = "deserialize_option_f64")]
    pub total_equity: Option<f64>,
    #[serde(deserialize_with = "deserialize_option_f64")]
    account_i_m_rate: Option<f64>,
    #[serde(deserialize_with = "deserialize_option_f64")]
    total_margin_balance: Option<f64>,
    #[serde(deserialize_with = "deserialize_option_f64")]
    total_initial_margin: Option<f64>,
    account_type: String,
    #[serde(deserialize_with = "deserialize_option_f64")]
    total_available_balance: Option<f64>,
    #[serde(deserialize_with = "deserialize_option_f64")]
    account_m_m_rate: Option<f64>,
    #[serde(deserialize_with = "deserialize_option_f64")]
    total_perp_u_p_l: Option<f64>,
    #[serde(deserialize_with = "deserialize_option_f64")]
    total_wallet_balance: Option<f64>,
    #[serde(deserialize_with = "deserialize_option_f64")]
    account_l_t_v: Option<f64>,
    #[serde(deserialize_with = "deserialize_option_f64")]
    total_maintenance_margin: Option<f64>,
    coin: Vec<Coin>,
}

impl WalletBalance {
    pub fn total_equity(&self) -> Option<f64> {
        self.total_equity
    }

    pub fn set_total_equity(&mut self, total_equity: f64) {
        self.total_equity = Some(total_equity);
    }
    
    pub fn account_i_m_rate(&self) -> Option<f64> {
        self.account_i_m_rate
    }

    pub fn set_account_i_m_rate(&mut self, account_i_m_rate: f64) {
        self.account_i_m_rate = Some(account_i_m_rate);
    }

    pub fn total_margin_balance(&self) -> Option<f64> {
        self.total_margin_balance
    }

    pub fn set_total_margin_balance(&mut self, total_margin_balance: f64) {
        self.total_margin_balance = Some(total_margin_balance);
    }

    pub fn total_initial_margin(&self) -> Option<f64> {
        self.total_initial_margin
    }

    pub fn set_total_initial_margin(&mut self, total_initial_margin: f64) {
        self.total_initial_margin = Some(total_initial_margin);
    }

    pub fn account_type(&self) -> &str {
        &self.account_type
    }

    pub fn set_account_type(&mut self, account_type: String) {
        self.account_type = account_type;
    }

    pub fn total_available_balance(&self) -> Option<f64> {
        self.total_available_balance
    }

    pub fn set_total_available_balance(&mut self, total_available_balance: f64) {
        self.total_available_balance = Some(total_available_balance);
    }

    pub fn account_m_m_rate(&self) -> Option<f64> {
        self.account_m_m_rate
    }

    pub fn set_account_m_m_rate(&mut self, account_m_m_rate: f64) {
        self.account_m_m_rate = Some(account_m_m_rate);
    }

    pub fn total_perp_u_p_l(&self) -> Option<f64> {
        self.total_perp_u_p_l
    }

    pub fn set_total_perp_u_p_l(&mut self, total_perp_u_p_l: f64) {
        self.total_perp_u_p_l = Some(total_perp_u_p_l);
    }

    pub fn total_wallet_balance(&self) -> Option<f64> {
        self.total_wallet_balance
    }

    pub fn set_total_wallet_balance(&mut self, total_wallet_balance: f64) {
        self.total_wallet_balance = Some(total_wallet_balance);
    }

    pub fn account_l_t_v(&self) -> Option<f64> {
        self.account_l_t_v
    }

    pub fn set_account_l_t_v(&mut self, account_l_t_v: f64) {
        self.account_l_t_v = Some(account_l_t_v);
    }

    pub fn total_maintenance_margin(&self) -> Option<f64> {
        self.total_maintenance_margin
    }

    pub fn set_total_maintenance_margin(&mut self, total_maintenance_margin: f64) {
        self.total_maintenance_margin = Some(total_maintenance_margin);
    }

    pub fn coin(&self) -> &Vec<Coin> {
        &self.coin
    }

    pub fn set_coin(&mut self, coin: Vec<Coin>) {
        self.coin = coin;
    }

}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Coin {
    #[serde(deserialize_with = "deserialize_option_f64")]
    available_to_borrow: Option<f64>,
    #[serde(deserialize_with = "deserialize_f64")]
    bonus: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    accrued_interest: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    available_to_withdraw: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    total_order_i_m: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    equity: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    total_position_m_m: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    usd_value: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    spot_hedging_qty: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    unrealised_pnl: f64,
    collateral_switch: bool,
    #[serde(deserialize_with = "deserialize_f64")]
    borrow_amount: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    total_position_i_m: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    wallet_balance: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    cum_realised_pnl: f64,
    #[serde(deserialize_with = "deserialize_f64")]
    locked: f64,
    margin_collateral: bool,
    coin: String,
}

impl Coin {
    pub fn available_to_borrow(&self) -> Option<f64> {
        self.available_to_borrow
    }

    pub fn set_available_to_borrow(&mut self, available_to_borrow: f64) {
        self.available_to_borrow = Some(available_to_borrow);
    }

    pub fn bonus(&self) -> f64 {
        self.bonus
    }

    pub fn set_bonus(&mut self, bonus: f64) {
        self.bonus = bonus;
    }

    pub fn accrued_interest(&self) -> f64 {
        self.accrued_interest
    }

    pub fn set_accrued_interest(&mut self, accrued_interest: f64) {
        self.accrued_interest = accrued_interest;
    }

    pub fn available_to_withdraw(&self) -> f64 {
        self.available_to_withdraw
    }

    pub fn set_available_to_withdraw(&mut self, available_to_withdraw: f64) {
        self.available_to_withdraw = available_to_withdraw;
    }

    pub fn total_order_i_m(&self) -> f64 {
        self.total_order_i_m
    }

    pub fn set_total_order_i_m(&mut self, total_order_i_m: f64) {
        self.total_order_i_m = total_order_i_m;
    }

    pub fn equity(&self) -> f64 {
        self.equity
    }

    pub fn set_equity(&mut self, equity: f64) {
        self.equity = equity;
    }

    pub fn total_position_m_m(&self) -> f64 {
        self.total_position_m_m
    }

    pub fn set_total_position_m_m(&mut self, total_position_m_m: f64) {
        self.total_position_m_m = total_position_m_m;
    }

    pub fn usd_value(&self) -> f64 {
        self.usd_value
    }

    pub fn set_usd_value(&mut self, usd_value: f64) {
        self.usd_value = usd_value;
    }

    pub fn spot_hedging_qty(&self) -> f64 {
        self.spot_hedging_qty
    }

    pub fn set_spot_hedging_qty(&mut self, spot_hedging_qty: f64) {
        self.spot_hedging_qty = spot_hedging_qty;
    }

    pub fn unrealised_pnl(&self) -> f64 {
        self.unrealised_pnl
    }

    pub fn set_unrealised_pnl(&mut self, unrealised_pnl: f64) {
        self.unrealised_pnl = unrealised_pnl;
    }

    pub fn collateral_switch(&self) -> bool {
        self.collateral_switch
    }

    pub fn set_collateral_switch(&mut self, collateral_switch: bool) {
        self.collateral_switch = collateral_switch;
    }

    pub fn borrow_amount(&self) -> f64 {
        self.borrow_amount
    }

    pub fn set_borrow_amount(&mut self, borrow_amount: f64) {
        self.borrow_amount = borrow_amount;
    }

    pub fn total_position_i_m(&self) -> f64 {
        self.total_position_i_m
    }

    pub fn set_total_position_i_m(&mut self, total_position_i_m: f64) {
        self.total_position_i_m = total_position_i_m;
    }

    pub fn wallet_balance(&self) -> f64 {
        self.wallet_balance
    }

    pub fn set_wallet_balance(&mut self, wallet_balance: f64) {
        self.wallet_balance = wallet_balance;
    }

    pub fn cum_realised_pnl(&self) -> f64 {
        self.cum_realised_pnl
    }

    pub fn set_cum_realised_pnl(&mut self, cum_realised_pnl: f64) {
        self.cum_realised_pnl = cum_realised_pnl;
    }

    pub fn locked(&self) -> f64 {
        self.locked
    }

    pub fn set_locked(&mut self, locked: f64) {
        self.locked = locked;
    }

    pub fn margin_collateral(&self) -> bool {
        self.margin_collateral
    }

    pub fn set_margin_collateral(&mut self, margin_collateral: bool) {
        self.margin_collateral = margin_collateral;
    }

    pub fn coin(&self) -> &str {
        &self.coin
    }

    pub fn set_coin(&mut self, coin: String) {
        self.coin = coin;
    }
}