forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btcmarkets.go
822 lines (768 loc) · 24.4 KB
/
btcmarkets.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
package btcmarkets
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
)
const (
btcMarketsAPIURL = "https://api.btcmarkets.net"
btcMarketsAPIVersion = "/v3"
// UnAuthenticated EPs
btcMarketsAllMarkets = "/markets/"
btcMarketsGetTicker = "/ticker/"
btcMarketsGetTrades = "/trades?"
btcMarketOrderBooks = "/orderbook?"
btcMarketsCandles = "/candles?"
btcMarketsTickers = "tickers?"
btcMarketsMultipleOrderbooks = "/orderbooks?"
btcMarketsGetTime = "/time"
btcMarketsWithdrawalFees = "/withdrawal-fees"
btcMarketsUnauthPath = btcMarketsAPIURL + btcMarketsAPIVersion + btcMarketsAllMarkets
// Authenticated EPs
btcMarketsAccountBalance = "/accounts/me/balances"
btcMarketsTradingFees = "/accounts/me/trading-fees"
btcMarketsTransactions = "/accounts/me/transactions"
btcMarketsOrders = "/orders"
btcMarketsTradeHistory = "/trades"
btcMarketsWithdrawals = "/withdrawals"
btcMarketsDeposits = "/deposits"
btcMarketsTransfers = "/transfers"
btcMarketsAddresses = "/addresses"
btcMarketsAssets = "/assets"
btcMarketsReports = "/reports"
btcMarketsBatchOrders = "/batchorders"
orderFailed = "Failed"
orderPartiallyCancelled = "Partially Cancelled"
orderCancelled = "Cancelled"
orderFullyMatched = "Fully Matched"
orderPartiallyMatched = "Partially Matched"
orderPlaced = "Placed"
orderAccepted = "Accepted"
ask = "ask"
limit = "Limit"
market = "Market"
stopLimit = "Stop Limit"
stop = "Stop"
takeProfit = "Take Profit"
subscribe = "subscribe"
fundChange = "fundChange"
orderChange = "orderChange"
heartbeat = "heartbeat"
tick = "tick"
wsOB = "orderbookUpdate"
tradeEndPoint = "trade"
)
// BTCMarkets is the overarching type across the BTCMarkets package
type BTCMarkets struct {
exchange.Base
}
// GetMarkets returns the BTCMarkets instruments
func (b *BTCMarkets) GetMarkets(ctx context.Context) ([]Market, error) {
var resp []Market
return resp, b.SendHTTPRequest(ctx, btcMarketsUnauthPath, &resp)
}
// GetTicker returns a ticker
// symbol - example "btc" or "ltc"
func (b *BTCMarkets) GetTicker(ctx context.Context, marketID string) (Ticker, error) {
var tick Ticker
return tick, b.SendHTTPRequest(ctx, btcMarketsUnauthPath+marketID+btcMarketsGetTicker, &tick)
}
// GetTrades returns executed trades on the exchange
func (b *BTCMarkets) GetTrades(ctx context.Context, marketID string, before, after, limit int64) ([]Trade, error) {
if (before > 0) && (after >= 0) {
return nil, errors.New("BTCMarkets only supports either before or after, not both")
}
var trades []Trade
params := url.Values{}
if before > 0 {
params.Set("before", strconv.FormatInt(before, 10))
}
if after > 0 {
params.Set("after", strconv.FormatInt(after, 10))
}
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return trades, b.SendHTTPRequest(ctx, btcMarketsUnauthPath+marketID+btcMarketsGetTrades+params.Encode(),
&trades)
}
// GetOrderbook returns current orderbook
func (b *BTCMarkets) GetOrderbook(ctx context.Context, marketID string, level int64) (Orderbook, error) {
var orderbook Orderbook
var temp tempOrderbook
params := url.Values{}
if level != 0 {
params.Set("level", strconv.FormatInt(level, 10))
}
err := b.SendHTTPRequest(ctx, btcMarketsUnauthPath+marketID+btcMarketOrderBooks+params.Encode(),
&temp)
if err != nil {
return orderbook, err
}
orderbook.MarketID = temp.MarketID
orderbook.SnapshotID = temp.SnapshotID
for x := range temp.Asks {
price, err := strconv.ParseFloat(temp.Asks[x][0], 64)
if err != nil {
return orderbook, err
}
amount, err := strconv.ParseFloat(temp.Asks[x][1], 64)
if err != nil {
return orderbook, err
}
orderbook.Asks = append(orderbook.Asks, OBData{
Price: price,
Volume: amount,
})
}
for a := range temp.Bids {
price, err := strconv.ParseFloat(temp.Bids[a][0], 64)
if err != nil {
return orderbook, err
}
amount, err := strconv.ParseFloat(temp.Bids[a][1], 64)
if err != nil {
return orderbook, err
}
orderbook.Bids = append(orderbook.Bids, OBData{
Price: price,
Volume: amount,
})
}
return orderbook, nil
}
// GetMarketCandles gets candles for specified currency pair
func (b *BTCMarkets) GetMarketCandles(ctx context.Context, marketID, timeWindow string, from, to time.Time, before, after, limit int64) (out CandleResponse, err error) {
if (before > 0) && (after >= 0) {
return out, errors.New("BTCMarkets only supports either before or after, not both")
}
params := url.Values{}
params.Set("timeWindow", timeWindow)
if from.After(to) && !to.IsZero() {
return out, errors.New("start time cannot be after end time")
}
if !from.IsZero() {
params.Set("from", from.UTC().Format(time.RFC3339))
}
if !to.IsZero() {
params.Set("to", to.UTC().Format(time.RFC3339))
}
if before > 0 {
params.Set("before", strconv.FormatInt(before, 10))
}
if after >= 0 {
params.Set("after", strconv.FormatInt(after, 10))
}
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return out, b.SendHTTPRequest(ctx, btcMarketsUnauthPath+marketID+btcMarketsCandles+params.Encode(), &out)
}
// GetTickers gets multiple tickers
func (b *BTCMarkets) GetTickers(ctx context.Context, marketIDs currency.Pairs) ([]Ticker, error) {
var tickers []Ticker
params := url.Values{}
for x := range marketIDs {
params.Add("marketId", marketIDs[x].String())
}
return tickers, b.SendHTTPRequest(ctx, btcMarketsUnauthPath+btcMarketsTickers+params.Encode(),
&tickers)
}
// GetMultipleOrderbooks gets orderbooks
func (b *BTCMarkets) GetMultipleOrderbooks(ctx context.Context, marketIDs []string) ([]Orderbook, error) {
var orderbooks []Orderbook
var temp []tempOrderbook
var tempOB Orderbook
params := url.Values{}
for x := range marketIDs {
params.Add("marketId", marketIDs[x])
}
err := b.SendHTTPRequest(ctx, btcMarketsUnauthPath+btcMarketsMultipleOrderbooks+params.Encode(),
&temp)
if err != nil {
return orderbooks, err
}
for i := range temp {
var price, volume float64
tempOB.MarketID = temp[i].MarketID
tempOB.SnapshotID = temp[i].SnapshotID
for a := range temp[i].Asks {
volume, err = strconv.ParseFloat(temp[i].Asks[a][1], 64)
if err != nil {
return orderbooks, err
}
price, err = strconv.ParseFloat(temp[i].Asks[a][0], 64)
if err != nil {
return orderbooks, err
}
tempOB.Asks = append(tempOB.Asks, OBData{Price: price, Volume: volume})
}
for y := range temp[i].Bids {
volume, err = strconv.ParseFloat(temp[i].Bids[y][1], 64)
if err != nil {
return orderbooks, err
}
price, err = strconv.ParseFloat(temp[i].Bids[y][0], 64)
if err != nil {
return orderbooks, err
}
tempOB.Bids = append(tempOB.Bids, OBData{Price: price, Volume: volume})
}
orderbooks = append(orderbooks, tempOB)
}
return orderbooks, nil
}
// GetServerTime gets time from btcmarkets
func (b *BTCMarkets) GetServerTime(ctx context.Context) (time.Time, error) {
var resp TimeResp
return resp.Time, b.SendHTTPRequest(ctx, btcMarketsAPIURL+btcMarketsAPIVersion+btcMarketsGetTime,
&resp)
}
// GetAccountBalance returns the full account balance
func (b *BTCMarkets) GetAccountBalance(ctx context.Context) ([]AccountData, error) {
var resp []AccountData
return resp,
b.SendAuthenticatedRequest(ctx, http.MethodGet,
btcMarketsAccountBalance,
nil,
&resp,
request.Auth)
}
// GetTradingFees returns trading fees for all pairs based on trading activity
func (b *BTCMarkets) GetTradingFees(ctx context.Context) (TradingFeeResponse, error) {
var resp TradingFeeResponse
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
btcMarketsTradingFees,
nil,
&resp,
request.Auth)
}
// GetTradeHistory returns trade history
func (b *BTCMarkets) GetTradeHistory(ctx context.Context, marketID, orderID string, before, after, limit int64) ([]TradeHistoryData, error) {
if (before > 0) && (after >= 0) {
return nil, errors.New("BTCMarkets only supports either before or after, not both")
}
var resp []TradeHistoryData
params := url.Values{}
if marketID != "" {
params.Set("marketId", marketID)
}
if orderID != "" {
params.Set("orderId", orderID)
}
if before > 0 {
params.Set("before", strconv.FormatInt(before, 10))
}
if after >= 0 {
params.Set("after", strconv.FormatInt(after, 10))
}
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
common.EncodeURLValues(btcMarketsTradeHistory, params),
nil,
&resp,
request.Auth)
}
// GetTradeByID returns the singular trade of the ID given
func (b *BTCMarkets) GetTradeByID(ctx context.Context, id string) (TradeHistoryData, error) {
var resp TradeHistoryData
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
btcMarketsTradeHistory+"/"+id,
nil,
&resp,
request.Auth)
}
// NewOrder requests a new order and returns an ID
func (b *BTCMarkets) NewOrder(ctx context.Context, marketID string, price, amount float64, orderType, side string, triggerPrice,
targetAmount float64, timeInForce string, postOnly bool, selfTrade, clientOrderID string) (OrderData, error) {
var resp OrderData
req := make(map[string]interface{})
req["marketId"] = marketID
req["price"] = strconv.FormatFloat(price, 'f', -1, 64)
req["amount"] = strconv.FormatFloat(amount, 'f', -1, 64)
req["type"] = orderType
req["side"] = side
if orderType == stopLimit || orderType == takeProfit || orderType == stop {
req["triggerPrice"] = strconv.FormatFloat(triggerPrice, 'f', -1, 64)
}
if targetAmount > 0 {
req["targetAmount"] = strconv.FormatFloat(targetAmount, 'f', -1, 64)
}
if timeInForce != "" {
req["timeInForce"] = timeInForce
}
req["postOnly"] = postOnly
if selfTrade != "" {
req["selfTrade"] = selfTrade
}
if clientOrderID != "" {
req["clientOrderID"] = clientOrderID
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodPost,
btcMarketsOrders,
req,
&resp,
orderFunc)
}
// GetOrders returns current order information on the exchange
func (b *BTCMarkets) GetOrders(ctx context.Context, marketID string, before, after, limit int64, openOnly bool) ([]OrderData, error) {
if (before > 0) && (after >= 0) {
return nil, errors.New("BTCMarkets only supports either before or after, not both")
}
var resp []OrderData
params := url.Values{}
if marketID != "" {
params.Set("marketId", marketID)
}
if before > 0 {
params.Set("before", strconv.FormatInt(before, 10))
}
if after >= 0 {
params.Set("after", strconv.FormatInt(after, 10))
}
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if openOnly {
params.Set("status", "open")
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
common.EncodeURLValues(btcMarketsOrders, params),
nil,
&resp,
request.Auth)
}
// CancelAllOpenOrdersByPairs cancels all open orders unless pairs are specified
func (b *BTCMarkets) CancelAllOpenOrdersByPairs(ctx context.Context, marketIDs []string) ([]CancelOrderResp, error) {
var resp []CancelOrderResp
req := make(map[string]interface{})
if len(marketIDs) > 0 {
var strTemp strings.Builder
for x := range marketIDs {
strTemp.WriteString("marketId=" + marketIDs[x] + "&")
}
req["marketId"] = strTemp.String()[:strTemp.Len()-1]
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodDelete,
btcMarketsOrders,
req,
&resp,
request.Auth)
}
// FetchOrder finds order based on the provided id
func (b *BTCMarkets) FetchOrder(ctx context.Context, id string) (OrderData, error) {
var resp OrderData
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
btcMarketsOrders+"/"+id,
nil,
&resp,
request.Auth)
}
// RemoveOrder removes a given order
func (b *BTCMarkets) RemoveOrder(ctx context.Context, id string) (CancelOrderResp, error) {
var resp CancelOrderResp
return resp, b.SendAuthenticatedRequest(ctx, http.MethodDelete,
btcMarketsOrders+"/"+id,
nil,
&resp,
request.Auth)
}
// ListWithdrawals lists the withdrawal history
func (b *BTCMarkets) ListWithdrawals(ctx context.Context, before, after, limit int64) ([]TransferData, error) {
if (before > 0) && (after >= 0) {
return nil, errors.New("BTCMarkets only supports either before or after, not both")
}
var resp []TransferData
params := url.Values{}
if before > 0 {
params.Set("before", strconv.FormatInt(before, 10))
}
if after >= 0 {
params.Set("after", strconv.FormatInt(after, 10))
}
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
common.EncodeURLValues(btcMarketsWithdrawals, params),
nil,
&resp,
request.Auth)
}
// GetWithdrawal gets withdrawawl info for a given id
func (b *BTCMarkets) GetWithdrawal(ctx context.Context, id string) (TransferData, error) {
var resp TransferData
if id == "" {
return resp, errors.New("id cannot be an empty string")
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
btcMarketsWithdrawals+"/"+id,
nil,
&resp,
request.Auth)
}
// ListDeposits lists the deposit history
func (b *BTCMarkets) ListDeposits(ctx context.Context, before, after, limit int64) ([]TransferData, error) {
if (before > 0) && (after >= 0) {
return nil, errors.New("BTCMarkets only supports either before or after, not both")
}
var resp []TransferData
params := url.Values{}
if before > 0 {
params.Set("before", strconv.FormatInt(before, 10))
}
if after >= 0 {
params.Set("after", strconv.FormatInt(after, 10))
}
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
common.EncodeURLValues(btcMarketsDeposits, params),
nil,
&resp,
request.Auth)
}
// GetDeposit gets deposit info for a given ID
func (b *BTCMarkets) GetDeposit(ctx context.Context, id string) (TransferData, error) {
var resp TransferData
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
btcMarketsDeposits+"/"+id,
nil,
&resp,
request.Auth)
}
// ListTransfers lists the past asset transfers
func (b *BTCMarkets) ListTransfers(ctx context.Context, before, after, limit int64) ([]TransferData, error) {
if (before > 0) && (after >= 0) {
return nil, errors.New("BTCMarkets only supports either before or after, not both")
}
var resp []TransferData
params := url.Values{}
if before > 0 {
params.Set("before", strconv.FormatInt(before, 10))
}
if after >= 0 {
params.Set("after", strconv.FormatInt(after, 10))
}
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
common.EncodeURLValues(btcMarketsTransfers, params),
nil,
&resp,
request.Auth)
}
// GetTransfer gets asset transfer info for a given ID
func (b *BTCMarkets) GetTransfer(ctx context.Context, id string) (TransferData, error) {
var resp TransferData
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
btcMarketsTransfers+"/"+id,
nil,
&resp,
request.Auth)
}
// FetchDepositAddress gets deposit address for the given asset
func (b *BTCMarkets) FetchDepositAddress(ctx context.Context, curr currency.Code, before, after, limit int64) (*DepositAddress, error) {
var resp DepositAddress
if (before > 0) && (after >= 0) {
return nil, errors.New("BTCMarkets only supports either before or after, not both")
}
params := url.Values{}
params.Set("assetName", curr.Upper().String())
if before > 0 {
params.Set("before", strconv.FormatInt(before, 10))
}
if after >= 0 {
params.Set("after", strconv.FormatInt(after, 10))
}
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if err := b.SendAuthenticatedRequest(ctx,
http.MethodGet,
common.EncodeURLValues(btcMarketsAddresses, params),
nil,
&resp,
request.Auth); err != nil {
return nil, err
}
if curr == currency.XRP {
splitStr := "?dt="
if !strings.Contains(resp.Address, splitStr) {
return nil, errors.New("unable to find split string for XRP")
}
splitter := strings.Split(resp.Address, splitStr)
resp.Address = splitter[0]
resp.Tag = splitter[1]
}
return &resp, nil
}
// GetWithdrawalFees gets withdrawal fees for all assets
func (b *BTCMarkets) GetWithdrawalFees(ctx context.Context) ([]WithdrawalFeeData, error) {
var resp []WithdrawalFeeData
return resp, b.SendHTTPRequest(ctx, btcMarketsAPIURL+btcMarketsAPIVersion+btcMarketsWithdrawalFees,
&resp)
}
// ListAssets lists all available assets
func (b *BTCMarkets) ListAssets(ctx context.Context) ([]AssetData, error) {
var resp []AssetData
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
btcMarketsAssets,
nil,
&resp,
request.Auth)
}
// GetTransactions gets trading fees
func (b *BTCMarkets) GetTransactions(ctx context.Context, assetName string, before, after, limit int64) ([]TransactionData, error) {
if (before > 0) && (after >= 0) {
return nil, errors.New("BTCMarkets only supports either before or after, not both")
}
var resp []TransactionData
params := url.Values{}
if assetName != "" {
params.Set("assetName", assetName)
}
if before > 0 {
params.Set("before", strconv.FormatInt(before, 10))
}
if after >= 0 {
params.Set("after", strconv.FormatInt(after, 10))
}
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
common.EncodeURLValues(btcMarketsTransactions, params),
nil,
&resp,
request.Auth)
}
// CreateNewReport creates a new report
func (b *BTCMarkets) CreateNewReport(ctx context.Context, reportType, format string) (CreateReportResp, error) {
var resp CreateReportResp
req := make(map[string]interface{})
req["type"] = reportType
req["format"] = format
return resp, b.SendAuthenticatedRequest(ctx, http.MethodPost,
btcMarketsReports,
req,
&resp,
newReportFunc)
}
// GetReport finds details bout a past report
func (b *BTCMarkets) GetReport(ctx context.Context, reportID string) (ReportData, error) {
var resp ReportData
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
btcMarketsReports+"/"+reportID,
nil,
&resp,
request.Auth)
}
// RequestWithdraw requests withdrawals
func (b *BTCMarkets) RequestWithdraw(ctx context.Context, assetName string, amount float64,
toAddress, accountName, accountNumber, bsbNumber, bankName string) (TransferData, error) {
var resp TransferData
req := make(map[string]interface{})
req["assetName"] = assetName
req["amount"] = strconv.FormatFloat(amount, 'f', -1, 64)
if assetName != "AUD" {
req["toAddress"] = toAddress
} else {
if accountName != "" {
req["accountName"] = accountName
}
if accountNumber != "" {
req["accountNumber"] = accountNumber
}
if bsbNumber != "" {
req["bsbNumber"] = bsbNumber
}
if bankName != "" {
req["bankName"] = bankName
}
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodPost,
btcMarketsWithdrawals,
req,
&resp,
withdrawFunc)
}
// BatchPlaceCancelOrders places and cancels batch orders
func (b *BTCMarkets) BatchPlaceCancelOrders(ctx context.Context, cancelOrders []CancelBatch, placeOrders []PlaceBatch) (BatchPlaceCancelResponse, error) {
var resp BatchPlaceCancelResponse
var orderRequests []interface{}
if len(cancelOrders)+len(placeOrders) > 4 {
return resp, errors.New("BTCMarkets can only handle 4 orders at a time")
}
for x := range cancelOrders {
orderRequests = append(orderRequests, CancelOrderMethod{CancelOrder: cancelOrders[x]})
}
for y := range placeOrders {
if placeOrders[y].ClientOrderID == "" {
return resp, errors.New("placeorders must have clientorderids filled")
}
orderRequests = append(orderRequests, PlaceOrderMethod{PlaceOrder: placeOrders[y]})
}
return resp, b.SendAuthenticatedRequest(ctx, http.MethodPost,
btcMarketsBatchOrders,
orderRequests,
&resp,
batchFunc)
}
// GetBatchTrades gets batch trades
func (b *BTCMarkets) GetBatchTrades(ctx context.Context, ids []string) (BatchTradeResponse, error) {
var resp BatchTradeResponse
if len(ids) > 50 {
return resp, errors.New("batchtrades can only handle 50 ids at a time")
}
marketIDs := strings.Join(ids, ",")
return resp, b.SendAuthenticatedRequest(ctx, http.MethodGet,
btcMarketsBatchOrders+"/"+marketIDs,
nil,
&resp,
request.Auth)
}
// CancelBatch cancels given ids
func (b *BTCMarkets) CancelBatch(ctx context.Context, ids []string) (BatchCancelResponse, error) {
var resp BatchCancelResponse
marketIDs := strings.Join(ids, ",")
return resp, b.SendAuthenticatedRequest(ctx, http.MethodDelete,
btcMarketsBatchOrders+"/"+marketIDs,
nil,
&resp,
batchFunc)
}
// SendHTTPRequest sends an unauthenticated HTTP request
func (b *BTCMarkets) SendHTTPRequest(ctx context.Context, path string, result interface{}) error {
item := &request.Item{
Method: http.MethodGet,
Path: path,
Result: result,
Verbose: b.Verbose,
HTTPDebugging: b.HTTPDebugging,
HTTPRecording: b.HTTPRecording,
}
return b.SendPayload(ctx, request.Unset, func() (*request.Item, error) {
return item, nil
})
}
// SendAuthenticatedRequest sends an authenticated HTTP request
func (b *BTCMarkets) SendAuthenticatedRequest(ctx context.Context, method, path string, data, result interface{}, f request.EndpointLimit) (err error) {
if !b.AllowAuthenticatedRequest() {
return fmt.Errorf("%s %w", b.Name, exchange.ErrAuthenticatedRequestWithoutCredentialsSet)
}
newRequest := func() (*request.Item, error) {
strTime := strconv.FormatInt(time.Now().UnixMilli(), 10)
var body io.Reader
var payload, hmac []byte
switch data.(type) {
case map[string]interface{}, []interface{}:
payload, err = json.Marshal(data)
if err != nil {
return nil, err
}
body = bytes.NewBuffer(payload)
strMsg := method + btcMarketsAPIVersion + path + strTime + string(payload)
hmac, err = crypto.GetHMAC(crypto.HashSHA512,
[]byte(strMsg),
[]byte(b.API.Credentials.Secret))
if err != nil {
return nil, err
}
default:
strArray := strings.Split(path, "?")
hmac, err = crypto.GetHMAC(crypto.HashSHA512,
[]byte(method+btcMarketsAPIVersion+strArray[0]+strTime),
[]byte(b.API.Credentials.Secret))
if err != nil {
return nil, err
}
}
headers := make(map[string]string)
headers["Accept"] = "application/json"
headers["Accept-Charset"] = "UTF-8"
headers["Content-Type"] = "application/json"
headers["BM-AUTH-APIKEY"] = b.API.Credentials.Key
headers["BM-AUTH-TIMESTAMP"] = strTime
headers["BM-AUTH-SIGNATURE"] = crypto.Base64Encode(hmac)
return &request.Item{
Method: method,
Path: btcMarketsAPIURL + btcMarketsAPIVersion + path,
Headers: headers,
Body: body,
Result: result,
AuthRequest: true,
Verbose: b.Verbose,
HTTPDebugging: b.HTTPDebugging,
HTTPRecording: b.HTTPRecording,
}, nil
}
return b.SendPayload(ctx, f, newRequest)
}
// GetFee returns an estimate of fee based on type of transaction
func (b *BTCMarkets) GetFee(ctx context.Context, feeBuilder *exchange.FeeBuilder) (float64, error) {
var fee float64
switch feeBuilder.FeeType {
case exchange.CryptocurrencyTradeFee:
temp, err := b.GetTradingFees(ctx)
if err != nil {
return fee, err
}
for x := range temp.FeeByMarkets {
p, err := currency.NewPairFromString(temp.FeeByMarkets[x].MarketID)
if err != nil {
return 0, err
}
if p == feeBuilder.Pair {
fee = temp.FeeByMarkets[x].MakerFeeRate
if !feeBuilder.IsMaker {
fee = temp.FeeByMarkets[x].TakerFeeRate
}
}
}
case exchange.CryptocurrencyWithdrawalFee:
temp, err := b.GetWithdrawalFees(ctx)
if err != nil {
return fee, err
}
for x := range temp {
if currency.NewCode(temp[x].AssetName) == feeBuilder.Pair.Base {
fee = temp[x].Fee * feeBuilder.PurchasePrice * feeBuilder.Amount
}
}
case exchange.InternationalBankWithdrawalFee:
return 0, errors.New("international bank withdrawals are not supported")
case exchange.OfflineTradeFee:
fee = getOfflineTradeFee(feeBuilder)
}
if fee < 0 {
fee = 0
}
return fee, nil
}
// getOfflineTradeFee calculates the worst case-scenario trading fee
func getOfflineTradeFee(feeBuilder *exchange.FeeBuilder) float64 {
switch {
case feeBuilder.Pair.IsCryptoPair():
return 0.002 * feeBuilder.PurchasePrice * feeBuilder.Amount
default:
return 0.0085 * feeBuilder.PurchasePrice * feeBuilder.Amount
}
}