-
Notifications
You must be signed in to change notification settings - Fork 69
/
margin.go
2883 lines (2607 loc) · 81.2 KB
/
margin.go
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package binance_connector
import (
"context"
"encoding/json"
"net/http"
)
// Get all margin assets API Endpoint
const (
getAllMarginAssetsEndpoint = "/sapi/v1/margin/allAssets"
)
// GetAllMarginAssetsService get all margin assets
type GetAllMarginAssetsService struct {
c *Client
}
// Do send request
func (s *GetAllMarginAssetsService) Do(ctx context.Context, opts ...RequestOption) (res []*GetAllMarginAssetsResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: getAllMarginAssetsEndpoint,
secType: secTypeSigned,
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*GetAllMarginAssetsResponse{}, err
}
res = make([]*GetAllMarginAssetsResponse, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*GetAllMarginAssetsResponse{}, err
}
return res, nil
}
// GetAllMarginAssetsResponse define get all margin assets response
type GetAllMarginAssetsResponse struct {
AssetFullName string `json:"assetFullName"`
AssetName string `json:"assetName"`
IsBorrowable bool `json:"isBorrowable"`
IsMortgageable bool `json:"isMortgageable"`
MinLoanAmt string `json:"minLoanAmt"`
MaxLoanAmt string `json:"maxLoanAmt"`
MinMortgageAmt string `json:"minMortgageAmt"`
MaxMortgageAmt string `json:"maxMortgageAmt"`
Asset string `json:"asset"`
}
// Get all margin pairs API Endpoint
const (
getAllMarginPairsEndpoint = "/sapi/v1/margin/allPairs"
)
// GetAllMarginPairsService get all margin pairs
type GetAllMarginPairsService struct {
c *Client
}
// Do send request
func (s *GetAllMarginPairsService) Do(ctx context.Context, opts ...RequestOption) (res []*GetAllMarginPairsResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: getAllMarginPairsEndpoint,
secType: secTypeAPIKey,
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*GetAllMarginPairsResponse{}, err
}
res = make([]*GetAllMarginPairsResponse, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*GetAllMarginPairsResponse{}, err
}
return res, nil
}
// GetAllMarginPairsResponse define get all margin pairs response
type GetAllMarginPairsResponse struct {
Base string `json:"base"`
Id int `json:"id"`
IsBuyAllowed bool `json:"isBuyAllowed"`
IsMarginTrade bool `json:"isMarginTrade"`
IsSellAllowed bool `json:"isSellAllowed"`
Quote string `json:"quote"`
Symbol string `json:"symbol"`
}
// Query Margin Price Index API Endpoint
const (
queryMarginPriceIndexEndpoint = "/sapi/v1/margin/priceIndex"
)
// QueryMarginPriceIndexService query margin price index
type QueryMarginPriceIndexService struct {
c *Client
symbol string
}
// Symbol set symbol
func (s *QueryMarginPriceIndexService) Symbol(symbol string) *QueryMarginPriceIndexService {
s.symbol = symbol
return s
}
// Do send request
func (s *QueryMarginPriceIndexService) Do(ctx context.Context, opts ...RequestOption) (res *QueryMarginPriceIndexResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: queryMarginPriceIndexEndpoint,
secType: secTypeAPIKey,
}
r.setParam("symbol", s.symbol)
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return &QueryMarginPriceIndexResponse{}, err
}
res = new(QueryMarginPriceIndexResponse)
err = json.Unmarshal(data, res)
if err != nil {
return &QueryMarginPriceIndexResponse{}, err
}
return res, nil
}
// QueryMarginPriceIndexResponse define query margin price index response
type QueryMarginPriceIndexResponse struct {
CalcTime int64 `json:"calcTime"`
Price string `json:"price"`
Symbol string `json:"symbol"`
}
// Margin Accouunt New Order (TRADE) API Endpoint
const (
marginAccountNewOrderEndpoint = "/sapi/v1/margin/order"
)
// MarginAccountNewOrderService margin account new order
type MarginAccountNewOrderService struct {
c *Client
symbol string
isIsolated *string
side string
orderType string
quantity *float64
quoteOrderQty *float64
price *float64
stopPrice *float64
newClientOrderId *string
icebergQty *float64
newOrderRespType *string
sideEffectType *string
timeInForce *string
}
// Symbol set symbol
func (s *MarginAccountNewOrderService) Symbol(symbol string) *MarginAccountNewOrderService {
s.symbol = symbol
return s
}
// IsIsolated set isIsolated
func (s *MarginAccountNewOrderService) IsIsolated(isIsolated string) *MarginAccountNewOrderService {
s.isIsolated = &isIsolated
return s
}
// Side set side
func (s *MarginAccountNewOrderService) Side(side string) *MarginAccountNewOrderService {
s.side = side
return s
}
// OrderType set orderType
func (s *MarginAccountNewOrderService) OrderType(orderType string) *MarginAccountNewOrderService {
s.orderType = orderType
return s
}
// Quantity set quantity
func (s *MarginAccountNewOrderService) Quantity(quantity float64) *MarginAccountNewOrderService {
s.quantity = &quantity
return s
}
// QuoteOrderQty set quoteOrderQty
func (s *MarginAccountNewOrderService) QuoteOrderQty(quoteOrderQty float64) *MarginAccountNewOrderService {
s.quoteOrderQty = "eOrderQty
return s
}
// Price set price
func (s *MarginAccountNewOrderService) Price(price float64) *MarginAccountNewOrderService {
s.price = &price
return s
}
// StopPrice set stopPrice
func (s *MarginAccountNewOrderService) StopPrice(stopPrice float64) *MarginAccountNewOrderService {
s.stopPrice = &stopPrice
return s
}
// NewClientOrderId set newClientOrderId
func (s *MarginAccountNewOrderService) NewClientOrderId(newClientOrderId string) *MarginAccountNewOrderService {
s.newClientOrderId = &newClientOrderId
return s
}
// IcebergQty set icebergQty
func (s *MarginAccountNewOrderService) IcebergQty(icebergQty float64) *MarginAccountNewOrderService {
s.icebergQty = &icebergQty
return s
}
// NewOrderRespType set newOrderRespType
func (s *MarginAccountNewOrderService) NewOrderRespType(newOrderRespType string) *MarginAccountNewOrderService {
s.newOrderRespType = &newOrderRespType
return s
}
// SideEffectType set sideEffectType
func (s *MarginAccountNewOrderService) SideEffectType(sideEffectType string) *MarginAccountNewOrderService {
s.sideEffectType = &sideEffectType
return s
}
// TimeInForce set timeInForce
func (s *MarginAccountNewOrderService) TimeInForce(timeInForce string) *MarginAccountNewOrderService {
s.timeInForce = &timeInForce
return s
}
// Do send request
func (s *MarginAccountNewOrderService) Do(ctx context.Context, opts ...RequestOption) (res interface{}, err error) {
respType := ACK
r := &request{
method: http.MethodPost,
endpoint: marginAccountNewOrderEndpoint,
secType: secTypeSigned,
}
m := params{
"symbol": s.symbol,
"side": s.side,
"type": s.orderType,
}
switch s.orderType {
case "MARKET":
respType = FULL
case "LIMIT":
respType = FULL
}
if s.isIsolated != nil {
m["isIsolated"] = *s.isIsolated
}
if s.quantity != nil {
m["quantity"] = *s.quantity
}
if s.quoteOrderQty != nil {
m["quoteOrderQty"] = *s.quoteOrderQty
}
if s.price != nil {
m["price"] = *s.price
}
if s.stopPrice != nil {
m["stopPrice"] = *s.stopPrice
}
if s.newClientOrderId != nil {
m["newClientOrderId"] = *s.newClientOrderId
}
if s.icebergQty != nil {
m["icebergQty"] = *s.icebergQty
}
if s.newOrderRespType != nil {
m["newOrderRespType"] = *s.newOrderRespType
switch *s.newOrderRespType {
case "ACK":
respType = ACK
case "RESULT":
respType = RESULT
case "FULL":
respType = FULL
}
}
if s.sideEffectType != nil {
m["sideEffectType"] = *s.sideEffectType
}
if s.timeInForce != nil {
m["timeInForce"] = *s.timeInForce
}
r.setParams(m)
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
switch respType {
case ACK:
res = new(MarginAccountNewOrderResponseACK)
case RESULT:
res = new(MarginAccountNewOrderResponseRESULT)
case FULL:
res = new(MarginAccountNewOrderResponseFULL)
}
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
// Create MarginAccountNewOrderResponseACK
type MarginAccountNewOrderResponseACK struct {
Symbol string `json:"symbol"`
OrderId int64 `json:"orderId"`
ClientOrderId int64 `json:"clientOrderId"`
IsIsolated bool `json:"isIsolated"`
TransactTime uint64 `json:"transactTime"`
}
// Create MarginAccountNewOrderResponseRESULT
type MarginAccountNewOrderResponseRESULT struct {
Symbol string `json:"symbol"`
OrderId int64 `json:"orderId"`
ClientOrderId string `json:"clientOrderId"`
TransactTime uint64 `json:"transactTime"`
Price string `json:"price"`
OrigQty string `json:"origQty"`
ExecutedQty string `json:"executedQty"`
CumulativeQuoteQty string `json:"cummulativeQuoteQty"`
Status string `json:"status"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
IsIsolated bool `json:"isIsolated"`
Side string `json:"side"`
}
// Create MarginAccountNewOrderResponseFULL
type MarginAccountNewOrderResponseFULL struct {
Symbol string `json:"symbol"`
OrderId int64 `json:"orderId"`
ClientOrderId string `json:"clientOrderId"`
TransactTime uint64 `json:"transactTime"`
Price string `json:"price"`
OrigQty string `json:"origQty"`
ExecutedQty string `json:"executedQty"`
CumulativeQuoteQty string `json:"cummulativeQuoteQty"`
Status string `json:"status"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
Side string `json:"side"`
MarginBuyBorrowAmount float64 `json:"marginBuyBorrowAmount"`
MarginBuyBorrowAsset string `json:"marginBuyBorrowAsset"`
IsIsolated bool `json:"isIsolated"`
Fills []struct {
Price string `json:"price"`
Qty string `json:"qty"`
Commission string `json:"commission"`
CommissionAsset string `json:"commissionAsset"`
} `json:"fills"`
}
// Margin Account Cancel Order (TRADE) API Endpoint
const (
marginAccountCancelOrderEndpoint = "/sapi/v1/margin/order"
)
// MarginAccountCancelOrderService margin account cancel order
type MarginAccountCancelOrderService struct {
c *Client
symbol string
isIsolated *string
orderId *int
origClientOrderId *string
newClientOrderId *string
}
// Symbol set symbol
func (s *MarginAccountCancelOrderService) Symbol(symbol string) *MarginAccountCancelOrderService {
s.symbol = symbol
return s
}
// IsIsolated set isIsolated
func (s *MarginAccountCancelOrderService) IsIsolated(isIsolated string) *MarginAccountCancelOrderService {
s.isIsolated = &isIsolated
return s
}
// OrderId set orderId
func (s *MarginAccountCancelOrderService) OrderId(orderId int) *MarginAccountCancelOrderService {
s.orderId = &orderId
return s
}
// OrigClientOrderId set origClientOrderId
func (s *MarginAccountCancelOrderService) OrigClientOrderId(origClientOrderId string) *MarginAccountCancelOrderService {
s.origClientOrderId = &origClientOrderId
return s
}
// NewClientOrderId set newClientOrderId
func (s *MarginAccountCancelOrderService) NewClientOrderId(newClientOrderId string) *MarginAccountCancelOrderService {
s.newClientOrderId = &newClientOrderId
return s
}
// Do send request
func (s *MarginAccountCancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res *MarginAccountCancelOrderResponse, err error) {
r := &request{
method: http.MethodDelete,
endpoint: marginAccountCancelOrderEndpoint,
secType: secTypeSigned,
}
m := params{
"symbol": s.symbol,
}
if s.isIsolated != nil {
m["isIsolated"] = *s.isIsolated
}
if s.orderId != nil {
m["orderId"] = *s.orderId
}
if s.origClientOrderId != nil {
m["origClientOrderId"] = *s.origClientOrderId
}
if s.newClientOrderId != nil {
m["newClientOrderId"] = *s.newClientOrderId
}
r.setParams(m)
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return &MarginAccountCancelOrderResponse{}, err
}
res = new(MarginAccountCancelOrderResponse)
err = json.Unmarshal(data, res)
if err != nil {
return &MarginAccountCancelOrderResponse{}, err
}
return res, nil
}
// MarginAccountCancelOrderResponse define margin account cancel order response
type MarginAccountCancelOrderResponse struct {
Symbol string `json:"symbol"`
IsIsolated bool `json:"isIsolated"`
OrderId int `json:"orderId"`
OrigClientOrderId string `json:"origClientOrderId"`
ClientOrderId string `json:"clientOrderId"`
Price string `json:"price"`
OrigQty string `json:"origQty"`
ExecutedQty string `json:"executedQty"`
CumulativeQuoteQty string `json:"cumulativeQuoteQty"`
Status string `json:"status"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
Side string `json:"side"`
}
// Margin Account Cancel All Orders (TRADE) API Endpoint
const (
marginAccountCancelAllOrdersEndpoint = "/sapi/v1/margin/openOrders"
)
// MarginAccountCancelAllOrdersService margin account cancel all orders
type MarginAccountCancelAllOrdersService struct {
c *Client
symbol string
isIsolated *string
}
// Symbol set symbol
func (s *MarginAccountCancelAllOrdersService) Symbol(symbol string) *MarginAccountCancelAllOrdersService {
s.symbol = symbol
return s
}
// IsIsolated set isIsolated
func (s *MarginAccountCancelAllOrdersService) IsIsolated(isIsolated string) *MarginAccountCancelAllOrdersService {
s.isIsolated = &isIsolated
return s
}
// Do send request
func (s *MarginAccountCancelAllOrdersService) Do(ctx context.Context, opts ...RequestOption) (res *MarginAccountCancelAllOrdersResponse, err error) {
r := &request{
method: http.MethodDelete,
endpoint: marginAccountCancelAllOrdersEndpoint,
secType: secTypeSigned,
}
m := params{
"symbol": s.symbol,
}
if s.isIsolated != nil {
m["isIsolated"] = *s.isIsolated
}
r.setParams(m)
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return &MarginAccountCancelAllOrdersResponse{}, err
}
res = new(MarginAccountCancelAllOrdersResponse)
err = json.Unmarshal(data, res)
if err != nil {
return &MarginAccountCancelAllOrdersResponse{}, err
}
return res, nil
}
// MarginAccountCancelAllOrdersResponse define margin account cancel all orders response
type MarginAccountCancelAllOrdersResponse struct {
Symbol string `json:"symbol"`
IsIsolated bool `json:"isIsolated"`
OrigClientOrderId string `json:"origClientOrderId"`
OrderId int `json:"orderId"`
OrderListId int `json:"orderListId"`
ClientOrderId string `json:"clientOrderId"`
Price string `json:"price"`
OrigQty string `json:"origQty"`
ExecutedQty string `json:"executedQty"`
CumulativeQuoteQty string `json:"cumulativeQuoteQty"`
Status string `json:"status"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
Side string `json:"side"`
}
// Get Cross Margin Transfer History (USER_DATA) API Endpoint
const (
crossMarginTransferHistoryEndpoint = "/sapi/v1/margin/transfer"
)
// CrossMarginTransferHistoryService get cross margin transfer history
type CrossMarginTransferHistoryService struct {
c *Client
asset *string
orderType *string
startTime *uint64
endTime *uint64
current *int
size *int
archived *string
}
// Asset set asset
func (s *CrossMarginTransferHistoryService) Asset(asset string) *CrossMarginTransferHistoryService {
s.asset = &asset
return s
}
// OrderType set orderType
func (s *CrossMarginTransferHistoryService) OrderType(orderType string) *CrossMarginTransferHistoryService {
s.orderType = &orderType
return s
}
// StartTime set startTime
func (s *CrossMarginTransferHistoryService) StartTime(startTime uint64) *CrossMarginTransferHistoryService {
s.startTime = &startTime
return s
}
// EndTime set endTime
func (s *CrossMarginTransferHistoryService) EndTime(endTime uint64) *CrossMarginTransferHistoryService {
s.endTime = &endTime
return s
}
// Current set current
func (s *CrossMarginTransferHistoryService) Current(current int) *CrossMarginTransferHistoryService {
s.current = ¤t
return s
}
// Size set size
func (s *CrossMarginTransferHistoryService) Size(size int) *CrossMarginTransferHistoryService {
s.size = &size
return s
}
// Archived set archived
func (s *CrossMarginTransferHistoryService) Archived(archived string) *CrossMarginTransferHistoryService {
s.archived = &archived
return s
}
// Do send request
func (s *CrossMarginTransferHistoryService) Do(ctx context.Context, opts ...RequestOption) (res *CrossMarginTransferHistoryResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: crossMarginTransferHistoryEndpoint,
secType: secTypeSigned,
}
if s.asset != nil {
r.setParam("asset", *s.asset)
}
if s.orderType != nil {
r.setParam("type", *s.orderType)
}
if s.startTime != nil {
r.setParam("startTime", *s.startTime)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
if s.current != nil {
r.setParam("current", *s.current)
}
if s.size != nil {
r.setParam("size", *s.size)
}
if s.archived != nil {
r.setParam("archived", *s.archived)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return &CrossMarginTransferHistoryResponse{}, err
}
res = new(CrossMarginTransferHistoryResponse)
err = json.Unmarshal(data, res)
if err != nil {
return &CrossMarginTransferHistoryResponse{}, err
}
return res, nil
}
// CrossMarginTransferHistoryResponse define cross margin transfer history response
type CrossMarginTransferHistoryResponse struct {
Rows []struct {
Amount string `json:"amount"`
Asset string `json:"asset"`
Status string `json:"status"`
Timestamp uint64 `json:"timestamp"`
TxId int64 `json:"txId"`
Type string `json:"type"`
} `json:"rows"`
Total int `json:"total"`
}
// Query Interest History (USER_DATA) API Endpoint
const (
interestHistoryEndpoint = "/sapi/v1/margin/interestHistory"
)
// InterestHistoryService query interest history
type InterestHistoryService struct {
c *Client
asset *string
isolatedSymbol *string
startTime *uint64
endTime *uint64
current *int
size *int
archived *string
}
// Asset set asset
func (s *InterestHistoryService) Asset(asset string) *InterestHistoryService {
s.asset = &asset
return s
}
// IsolatedSymbol set isolatedSymbol
func (s *InterestHistoryService) IsolatedSymbol(isolatedSymbol string) *InterestHistoryService {
s.isolatedSymbol = &isolatedSymbol
return s
}
// StartTime set startTime
func (s *InterestHistoryService) StartTime(startTime uint64) *InterestHistoryService {
s.startTime = &startTime
return s
}
// EndTime set endTime
func (s *InterestHistoryService) EndTime(endTime uint64) *InterestHistoryService {
s.endTime = &endTime
return s
}
// Current set current
func (s *InterestHistoryService) Current(current int) *InterestHistoryService {
s.current = ¤t
return s
}
// Size set size
func (s *InterestHistoryService) Size(size int) *InterestHistoryService {
s.size = &size
return s
}
// Archived set archived
func (s *InterestHistoryService) Archived(archived string) *InterestHistoryService {
s.archived = &archived
return s
}
// Do send request
func (s *InterestHistoryService) Do(ctx context.Context, opts ...RequestOption) (res *InterestHistoryResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: interestHistoryEndpoint,
secType: secTypeSigned,
}
if s.asset != nil {
r.setParam("asset", *s.asset)
}
if s.isolatedSymbol != nil {
r.setParam("isolatedSymbol", *s.isolatedSymbol)
}
if s.startTime != nil {
r.setParam("startTime", *s.startTime)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
if s.current != nil {
r.setParam("current", *s.current)
}
if s.size != nil {
r.setParam("size", *s.size)
}
if s.archived != nil {
r.setParam("archived", *s.archived)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return &InterestHistoryResponse{}, err
}
res = new(InterestHistoryResponse)
err = json.Unmarshal(data, res)
if err != nil {
return &InterestHistoryResponse{}, err
}
return res, nil
}
// InterestHistoryResponse define interest history response
type InterestHistoryResponse struct {
Rows []struct {
TxId int64 `json:"txId"`
InterestAccuredTime uint64 `json:"interestAccuredTime"`
Asset string `json:"asset"`
RawAsset string `json:"rawAsset"`
Principal string `json:"principal"`
Interest string `json:"interest"`
InterestRate string `json:"interestRate"`
Type string `json:"type"`
IsolatedSymbol string `json:"isolatedSymbol"`
} `json:"rows"`
Total int `json:"total"`
}
// Query Force Liquidation Record (USER_DATA) API Endpoint
const (
forceLiquidationRecordEndpoint = "/sapi/v1/margin/forceLiquidationRec"
)
// ForceLiquidationRecordService query force liquidation record
type ForceLiquidationRecordService struct {
c *Client
startTime *uint64
endTime *uint64
isolatedSymbol *string
current *int
size *int
}
// IsolatedSymbol set isolatedSymbol
func (s *ForceLiquidationRecordService) IsolatedSymbol(isolatedSymbol string) *ForceLiquidationRecordService {
s.isolatedSymbol = &isolatedSymbol
return s
}
// StartTime set startTime
func (s *ForceLiquidationRecordService) StartTime(startTime uint64) *ForceLiquidationRecordService {
s.startTime = &startTime
return s
}
// EndTime set endTime
func (s *ForceLiquidationRecordService) EndTime(endTime uint64) *ForceLiquidationRecordService {
s.endTime = &endTime
return s
}
// Current set current
func (s *ForceLiquidationRecordService) Current(current int) *ForceLiquidationRecordService {
s.current = ¤t
return s
}
// Size set size
func (s *ForceLiquidationRecordService) Size(size int) *ForceLiquidationRecordService {
s.size = &size
return s
}
// Do send request
func (s *ForceLiquidationRecordService) Do(ctx context.Context, opts ...RequestOption) (res *ForceLiquidationRecordResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: forceLiquidationRecordEndpoint,
secType: secTypeSigned,
}
if s.startTime != nil {
r.setParam("startTime", *s.startTime)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
if s.isolatedSymbol != nil {
r.setParam("isolatedSymbol", *s.isolatedSymbol)
}
if s.current != nil {
r.setParam("current", *s.current)
}
if s.size != nil {
r.setParam("size", *s.size)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return &ForceLiquidationRecordResponse{}, err
}
res = new(ForceLiquidationRecordResponse)
err = json.Unmarshal(data, res)
if err != nil {
return &ForceLiquidationRecordResponse{}, err
}
return res, nil
}
// ForceLiquidationRecordResponse define force liquidation record response
type ForceLiquidationRecordResponse struct {
Rows []struct {
AvgPrice string `json:"avgPrice"`
ExecutedQty string `json:"executedQty"`
OrderId int `json:"orderId"`
Price string `json:"price"`
Qty string `json:"qty"`
Side string `json:"side"`
Symbol string `json:"symbol"`
TimeInForce string `json:"timeInForce"`
IsIsolated bool `json:"isIsolated"`
UpdatedTime uint64 `json:"updatedTime"`
} `json:"rows"`
Total int `json:"total"`
}
// Query Query Cross Margin Account Details (USER_DATA) API Endpoint
const (
crossMarginAccountDetailEndpoint = "/sapi/v1/margin/account"
)
// CrossMarginAccountDetailService query cross margin account details
type CrossMarginAccountDetailService struct {
c *Client
}
// Do send request
func (s *CrossMarginAccountDetailService) Do(ctx context.Context, opts ...RequestOption) (res *CrossMarginAccountDetailResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: crossMarginAccountDetailEndpoint,
secType: secTypeSigned,
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return &CrossMarginAccountDetailResponse{}, err
}
res = new(CrossMarginAccountDetailResponse)
err = json.Unmarshal(data, res)
if err != nil {
return &CrossMarginAccountDetailResponse{}, err
}
return res, nil
}
// CrossMarginAccountDetailResponse define cross margin account detail response
type CrossMarginAccountDetailResponse struct {
BorrowEnabled bool `json:"borrowEnabled"`
MarginLevel string `json:"marginLevel"`
TotalAssetOfBtc string `json:"totalAssetOfBtc"`
TotalLiabilityOfBtc string `json:"totalLiabilityOfBtc"`
TotalNetAssetOfBtc string `json:"totalNetAssetOfBtc"`
TradeEnabled bool `json:"tradeEnabled"`
TransferEnabled bool `json:"transferEnabled"`
UserAssets []struct {
Asset string `json:"asset"`
Borrowed string `json:"borrowed"`
Free string `json:"free"`
Interest string `json:"interest"`
Locked string `json:"locked"`
NetAsset string `json:"netAsset"`
} `json:"userAssets"`
}
// Query Margin Account's Order (USER_DATA) API Endpoint
const (
marginAccountOrderEndpoint = "/sapi/v1/margin/order"
)
// MarginAccountOrderService query margin account's order
type MarginAccountOrderService struct {
c *Client
symbol string
isIsolated *string
orderId *int
origClientOrderId *string
}
// Symbol set symbol
func (s *MarginAccountOrderService) Symbol(symbol string) *MarginAccountOrderService {
s.symbol = symbol
return s
}
// IsIsolated set isIsolated
func (s *MarginAccountOrderService) IsIsolated(isIsolated string) *MarginAccountOrderService {
s.isIsolated = &isIsolated
return s
}
// OrderId set orderId
func (s *MarginAccountOrderService) OrderId(orderId int) *MarginAccountOrderService {
s.orderId = &orderId
return s
}
// OrigClientOrderId set origClientOrderId
func (s *MarginAccountOrderService) OrigClientOrderId(origClientOrderId string) *MarginAccountOrderService {
s.origClientOrderId = &origClientOrderId
return s
}
// Do send request
func (s *MarginAccountOrderService) Do(ctx context.Context, opts ...RequestOption) (res *MarginAccountOrderResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: marginAccountOrderEndpoint,
secType: secTypeSigned,
}
m := params{
"symbol": s.symbol,
}
if s.isIsolated != nil {
m["isIsolated"] = *s.isIsolated
}
if s.orderId != nil {
m["orderId"] = *s.orderId
}
if s.origClientOrderId != nil {
m["origClientOrderId"] = *s.origClientOrderId
}
r.setParams(m)
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return &MarginAccountOrderResponse{}, err
}
res = new(MarginAccountOrderResponse)
err = json.Unmarshal(data, res)
if err != nil {
return &MarginAccountOrderResponse{}, err
}
return res, nil
}
// MarginAccountOrderResponse define margin account order response
type MarginAccountOrderResponse struct {
ClientOrderId string `json:"clientOrderId"`
CumulativeQuoteQty string `json:"cumulativeQuoteQty"`
ExecutedQty string `json:"executedQty"`
IcebergQty string `json:"icebergQty"`
IsWorking bool `json:"isWorking"`
OrderId int `json:"orderId"`
OrigQty string `json:"origQty"`
Price string `json:"price"`
Side string `json:"side"`
Status string `json:"status"`
StopPrice string `json:"stopPrice"`
Symbol string `json:"symbol"`
IsIsolated bool `json:"isIsolated"`
Time uint64 `json:"time"`
TimeInForce string `json:"timeInForce"`
OrderType string `json:"type"`
UpdateTime uint64 `json:"updateTime"`
}
// Query Margin Account's Open Order (USER_DATA) API Endpoint
const (
marginAccountOpenOrderEndpoint = "/sapi/v1/margin/openOrders"
)
// MarginAccountOpenOrderService query margin account's open order
type MarginAccountOpenOrderService struct {
c *Client
symbol *string
isIsolated *string