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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#![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::v5asset;
use super::http_manager::{HttpManager, Manager};
#[async_trait]
pub trait Asset {
fn new(http_manager: Arc<HttpManager>) -> Self;
async fn get_coin_exchange_records(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_option_delivery_record(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_usdc_contract_settlement(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_spot_asset_info(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_coins_balance(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_coin_balance(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_transferable_coin(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn create_internal_transfer(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_internal_transfer_records(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_sub_uid(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn enable_universal_transfer_for_sub_uid(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn create_universal_transfer(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_universal_transfer_records(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_allowed_deposit_coin_info(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn set_deposit_account(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_deposit_records(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_sub_deposit_records(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_internal_deposit_records(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_master_deposit_address(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_sub_deposit_address(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_coin_info(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_withdrawal_records(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn get_withdrawable_amount(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn withdraw(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn cancel_withdrawal(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>>;
}
pub struct AssetHTTP {
http_manager: Arc<HttpManager>,
}
#[async_trait]
impl Asset for AssetHTTP {
///
///
/// Initialize the AssetHTTP by passing the HttpManager
///
///
fn new(http_manager: Arc<HttpManager>) -> Self {
AssetHTTP { http_manager }
}
/// Query the coin exchange records.
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/exchange
async fn get_coin_exchange_records(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5asset::Asset::GetCoinExchangeRecords.to_string(),
true,
query,
)
.await
}
/// Query option delivery records, sorted by deliveryTime in descending order
/// Required args:
/// category (string): Product type. option
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/option-delivery
async fn get_option_delivery_record(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5asset::Asset::GetOptionDeliveryRecord.to_string(),
true,
query,
)
.await
}
/// Query session settlement records of USDC perpetual and futures
/// Required args:
/// category (string): Product type. linear
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/settlement
async fn get_usdc_contract_settlement(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5asset::Asset::GetUsdcContractSettlement.to_string(),
true,
query,
)
.await
}
/// Query asset information
/// Required args:
/// accountType (string): Account type. SPOT
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/asset-info
async fn get_spot_asset_info(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetSpotAssetInfo.to_string(),
query,
true,
)
.await
}
/// You could get all coin balance of all account types under the master account, and sub account.
/// Required args:
/// memberId (string): User Id. It is required when you use master api key to check sub account coin balance
/// accountType (string): Account type
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/all-balance
async fn get_coins_balance(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetAllCoinsBalance.to_string(),
query,
true,
)
.await
}
/// Query the balance of a specific coin in a specific account type. Supports querying sub UID's balance.
/// Required args:
/// memberId (string): UID. Required when querying sub UID balance
/// accountType (string): Account type
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/account-coin-balance
async fn get_coin_balance(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetSingleCoinBalance.to_string(),
query,
true,
)
.await
}
/// Query the transferable coin list between each account type
/// Required args:
/// fromAccountType (string): From account type
/// toAccountType (string): To account type
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/transferable-coin
async fn get_transferable_coin(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetTransferableCoin.to_string(),
query,
true,
)
.await
}
/// Create the internal transfer between different account types under the same UID.
/// Required args:
/// transferId (string): UUID. Please manually generate a UUID
/// coin (string): Coin
/// amount (string): Amount
/// fromAccountType (string): From account type
/// toAccountType (string): To account type
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/create-inter-transfer
async fn create_internal_transfer(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5asset::Asset::CreateInternalTransfer.to_string(),
true,
query,
)
.await
}
/// Query the internal transfer records between different account types under the same UID.
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/inter-transfer-list
async fn get_internal_transfer_records(
&self,
query: HashMap<String, String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetInternalTransferRecords.to_string(),
query,
true,
)
.await
}
/// Query the sub UIDs under a main UID
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/sub-uid-list
async fn get_sub_uid(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetSubUid.to_string(),
query,
true,
)
.await
}
/// Transfer between sub-sub or main-sub
/// Required args:
/// subMemberIds (array): This list has a single item. Separate multiple UIDs by comma, e.g., "uid1,uid2,uid3"
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/enable-unitransfer-subuid
async fn enable_universal_transfer_for_sub_uid(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5asset::Asset::EnableUtForSubUid.to_string(),
true,
query,
)
.await
}
/// Transfer between sub-sub or main-sub. Please make sure you have enabled universal transfer on your sub UID in advance.
/// Required args:
/// transferId (string): UUID. Please manually generate a UUID
/// coin (string): Coin
/// amount (string): Amount
/// fromMemberId (integer): From UID
/// toMemberId (integer): To UID
/// fromAccountType (string): From account type
/// toAccountType (string): To account type
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/unitransfer
async fn create_universal_transfer(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5asset::Asset::CreateUniversalTransfer.to_string(),
true,
query,
)
.await
}
/// Query universal transfer records
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/unitransfer-list
async fn get_universal_transfer_records(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetUniversalTransferRecords.to_string(),
query,
true,
)
.await
}
/// Query allowed deposit coin information. To find out paired chain of coin, please refer coin info api.
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/deposit-coin-spec
async fn get_allowed_deposit_coin_info(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetAllowedDepositCoinInfo.to_string(),
query,
true,
)
.await
}
/// Set auto transfer account after deposit. The same function as the setting for Deposit on web GUI
/// Required args:
/// accountType (string): Account type: UNIFIED,SPOT,OPTION,CONTRACT,FUND
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/set-deposit-acct
async fn set_deposit_account(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5asset::Asset::SetDepositAccount.to_string(),
true,
query,
)
.await
}
/// Query deposit records.
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/deposit-record
async fn get_deposit_records(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetDepositRecords.to_string(),
query,
true,
)
.await
}
/// Query subaccount's deposit records by MAIN UID's API key.
/// Required args:
/// subMemberId (string): Sub UID
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/sub-deposit-record
async fn get_sub_deposit_records(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetSubAccountDepositRecords.to_string(),
query,
true,
)
.await
}
/// Query deposit records within the Bybit platform. These transactions are not on the blockchain.
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/internal-deposit-record
async fn get_internal_deposit_records(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetInternalDepositRecords.to_string(),
query,
true,
)
.await
}
/// Query the deposit address information of MASTER account.
/// Required args:
/// coin (string): Coin
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/master-deposit-addr
async fn get_master_deposit_address(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetMasterDepositAddress.to_string(),
query,
true,
)
.await
}
/// Query the deposit address information of SUB account.
/// Required args:
/// coin (string): Coin
/// chainType (string): Chain, e.g.,ETH
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/sub-deposit-addr
async fn get_sub_deposit_address(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetSubDepositAddress.to_string(),
query,
true,
)
.await
}
/// Query coin information, including chain information, withdraw and deposit status.
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/coin-info
async fn get_coin_info(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetCoinInfo.to_string(),
query,
true,
)
.await
}
/// Query withdrawal records.
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/withdraw-record
async fn get_withdrawal_records(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetWithdrawalRecords.to_string(),
query,
true,
)
.await
}
/// Get withdrawable amount
/// Required args:
/// coin (string): Coin name
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/delay-amount
async fn get_withdrawable_amount(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_request(
Method::GET,
&v5asset::Asset::GetWithdrawableAmount.to_string(),
query,
true,
)
.await
}
/// Withdraw assets from your Bybit account. You can make an off-chain transfer if the target wallet address is from Bybit. This means that no blockchain fee will be charged.
/// Required args:
/// coin (string): Coin
/// chain (string): Chain
/// address (string): Wallet address
/// tag (string): Tag. Required if tag exists in the wallet address list
/// amount (string): Withdraw amount
/// timestamp (integer): Current timestamp (ms). Used for preventing from withdraw replay
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/withdraw
async fn withdraw(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5asset::Asset::Withdraw.to_string(),
true,
query,
)
.await
}
/// Cancel the withdrawal
/// Required args:
/// id (string): Withdrawal ID
/// Returns:
/// Request results as dictionary.
/// Additional information:
/// https://bybit-exchange.github.io/docs/v5/asset/cancel-withdraw
async fn cancel_withdrawal(
&self,
query: HashMap<String, String>,
) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync + 'static>> {
self.http_manager
.submit_post_request(
Method::POST,
&v5asset::Asset::CancelWithdrawal.to_string(),
true,
query,
)
.await
}
}