forked from adshao/go-binance
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
591 lines (486 loc) · 18.2 KB
/
client.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
package binance
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/bitly/go-simplejson"
"github.com/spectra-fund/go-binance/common"
"github.com/spectra-fund/go-binance/futures"
)
// SideType define side type of order
type SideType string
// OrderType define order type
type OrderType string
// TimeInForceType define time in force type of order
type TimeInForceType string
// NewOrderRespType define response JSON verbosity
type NewOrderRespType string
// OrderStatusType define order status type
type OrderStatusType string
// SymbolType define symbol type
type SymbolType string
// SymbolStatusType define symbol status type
type SymbolStatusType string
// SymbolFilterType define symbol filter type
type SymbolFilterType string
// MarginTransferType define margin transfer type
type MarginTransferType int
// MarginLoanStatusType define margin loan status type
type MarginLoanStatusType string
// MarginRepayStatusType define margin repay status type
type MarginRepayStatusType string
// FuturesTransferStatusType define futures transfer status type
type FuturesTransferStatusType string
// SideEffectType define side effect type for orders
type SideEffectType string
// FuturesTransferType define futures transfer type
type FuturesTransferType int
// IsIsolatedType defines if the margin order is a isolated margin order.
type IsIsolatedType string
// Global enums
const (
SideTypeBuy SideType = "BUY"
SideTypeSell SideType = "SELL"
OrderTypeLimit OrderType = "LIMIT"
OrderTypeMarket OrderType = "MARKET"
OrderTypeLimitMaker OrderType = "LIMIT_MAKER"
OrderTypeStopLoss OrderType = "STOP_LOSS"
OrderTypeStopLossLimit OrderType = "STOP_LOSS_LIMIT"
OrderTypeTakeProfit OrderType = "TAKE_PROFIT"
OrderTypeTakeProfitLimit OrderType = "TAKE_PROFIT_LIMIT"
TimeInForceTypeGTC TimeInForceType = "GTC"
TimeInForceTypeIOC TimeInForceType = "IOC"
TimeInForceTypeFOK TimeInForceType = "FOK"
NewOrderRespTypeACK NewOrderRespType = "ACK"
NewOrderRespTypeRESULT NewOrderRespType = "RESULT"
NewOrderRespTypeFULL NewOrderRespType = "FULL"
OrderStatusTypeNew OrderStatusType = "NEW"
OrderStatusTypePartiallyFilled OrderStatusType = "PARTIALLY_FILLED"
OrderStatusTypeFilled OrderStatusType = "FILLED"
OrderStatusTypeCanceled OrderStatusType = "CANCELED"
OrderStatusTypePendingCancel OrderStatusType = "PENDING_CANCEL"
OrderStatusTypeRejected OrderStatusType = "REJECTED"
OrderStatusTypeExpired OrderStatusType = "EXPIRED"
SymbolTypeSpot SymbolType = "SPOT"
SymbolStatusTypePreTrading SymbolStatusType = "PRE_TRADING"
SymbolStatusTypeTrading SymbolStatusType = "TRADING"
SymbolStatusTypePostTrading SymbolStatusType = "POST_TRADING"
SymbolStatusTypeEndOfDay SymbolStatusType = "END_OF_DAY"
SymbolStatusTypeHalt SymbolStatusType = "HALT"
SymbolStatusTypeAuctionMatch SymbolStatusType = "AUCTION_MATCH"
SymbolStatusTypeBreak SymbolStatusType = "BREAK"
SymbolFilterTypeLotSize SymbolFilterType = "LOT_SIZE"
SymbolFilterTypePriceFilter SymbolFilterType = "PRICE_FILTER"
SymbolFilterTypePercentPrice SymbolFilterType = "PERCENT_PRICE"
SymbolFilterTypeMinNotional SymbolFilterType = "MIN_NOTIONAL"
SymbolFilterTypeIcebergParts SymbolFilterType = "ICEBERG_PARTS"
SymbolFilterTypeMarketLotSize SymbolFilterType = "MARKET_LOT_SIZE"
SymbolFilterTypeMaxNumAlgoOrders SymbolFilterType = "MAX_NUM_ALGO_ORDERS"
MarginTransferTypeToMargin MarginTransferType = 1
MarginTransferTypeToMain MarginTransferType = 2
FuturesTransferTypeToFutures FuturesTransferType = 1
FuturesTransferTypeToMain FuturesTransferType = 2
MarginLoanStatusTypePending MarginLoanStatusType = "PENDING"
MarginLoanStatusTypeConfirmed MarginLoanStatusType = "CONFIRMED"
MarginLoanStatusTypeFailed MarginLoanStatusType = "FAILED"
MarginRepayStatusTypePending MarginRepayStatusType = "PENDING"
MarginRepayStatusTypeConfirmed MarginRepayStatusType = "CONFIRMED"
MarginRepayStatusTypeFailed MarginRepayStatusType = "FAILED"
FuturesTransferStatusTypePending FuturesTransferStatusType = "PENDING"
FuturesTransferStatusTypeConfirmed FuturesTransferStatusType = "CONFIRMED"
FuturesTransferStatusTypeFailed FuturesTransferStatusType = "FAILED"
SideEffectTypeNoSideEffect SideEffectType = "NO_SIDE_EFFECT"
SideEffectTypeMarginBuy SideEffectType = "MARGIN_BUY"
SideEffectTypeAutoRepay SideEffectType = "AUTO_REPAY"
IsIsolatedTypeTrue IsIsolatedType = "TRUE"
IsIsolatedTypeFalse IsIsolatedType = "FALSE"
timestampKey = "timestamp"
signatureKey = "signature"
recvWindowKey = "recvWindow"
)
func currentTimestamp() int64 {
return FormatTimestamp(time.Now())
}
// FormatTimestamp formats a time into Unix timestamp in milliseconds, as requested by Binance.
func FormatTimestamp(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}
func newJSON(data []byte) (j *simplejson.Json, err error) {
j, err = simplejson.NewJson(data)
if err != nil {
return nil, err
}
return j, nil
}
// NewClient initialize an API client instance with API key and secret key.
// You should always call this function before using this SDK.
// Services will be created by the form client.NewXXXService().
func NewClient(apiKey, secretKey string) *Client {
return &Client{
APIKey: apiKey,
SecretKey: secretKey,
BaseURL: "https://api.binance.com",
UserAgent: "Binance/golang",
HTTPClient: http.DefaultClient,
Logger: log.New(os.Stderr, "Binance-golang ", log.LstdFlags),
}
}
// NewFuturesClient initialize client for futures API
func NewFuturesClient(apiKey, secretKey string) *futures.Client {
return futures.NewClient(apiKey, secretKey)
}
type doFunc func(req *http.Request) (*http.Response, error)
// Client define API client
type Client struct {
APIKey string
SecretKey string
BaseURL string
UserAgent string
HTTPClient *http.Client
Debug bool
Logger *log.Logger
TimeOffset int64
do doFunc
}
func (c *Client) debug(format string, v ...interface{}) {
if c.Debug {
c.Logger.Printf(format, v...)
}
}
func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
// set request options from user
for _, opt := range opts {
opt(r)
}
err = r.validate()
if err != nil {
return err
}
fullURL := fmt.Sprintf("%s%s", c.BaseURL, r.endpoint)
if r.recvWindow > 0 {
r.setParam(recvWindowKey, r.recvWindow)
}
if r.secType == secTypeSigned {
r.setParam(timestampKey, currentTimestamp()-c.TimeOffset)
}
queryString := r.query.Encode()
body := &bytes.Buffer{}
bodyString := r.form.Encode()
header := http.Header{}
if bodyString != "" {
header.Set("Content-Type", "application/x-www-form-urlencoded")
body = bytes.NewBufferString(bodyString)
}
if r.secType == secTypeAPIKey || r.secType == secTypeSigned {
header.Set("X-MBX-APIKEY", c.APIKey)
}
if r.secType == secTypeSigned {
raw := fmt.Sprintf("%s%s", queryString, bodyString)
mac := hmac.New(sha256.New, []byte(c.SecretKey))
_, err = mac.Write([]byte(raw))
if err != nil {
return err
}
v := url.Values{}
v.Set(signatureKey, fmt.Sprintf("%x", (mac.Sum(nil))))
if queryString == "" {
queryString = v.Encode()
} else {
queryString = fmt.Sprintf("%s&%s", queryString, v.Encode())
}
}
if queryString != "" {
fullURL = fmt.Sprintf("%s?%s", fullURL, queryString)
}
c.debug("full url: %s, body: %s", fullURL, bodyString)
r.fullURL = fullURL
r.header = header
r.body = body
return nil
}
func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption) (data []byte, err error) {
err = c.parseRequest(r, opts...)
if err != nil {
return []byte{}, err
}
req, err := http.NewRequest(r.method, r.fullURL, r.body)
if err != nil {
return []byte{}, err
}
req = req.WithContext(ctx)
req.Header = r.header
c.debug("request: %#v", req)
f := c.do
if f == nil {
f = c.HTTPClient.Do
}
res, err := f(req)
if err != nil {
return []byte{}, err
}
data, err = ioutil.ReadAll(res.Body)
if err != nil {
return []byte{}, err
}
defer func() {
cerr := res.Body.Close()
// Only overwrite the retured error if the original error was nil and an
// error occurred while closing the body.
if err == nil && cerr != nil {
err = cerr
}
}()
c.debug("response: %#v", res)
c.debug("response body: %s", string(data))
c.debug("response status code: %d", res.StatusCode)
if res.StatusCode >= 400 {
apiErr := new(common.APIError)
e := json.Unmarshal(data, apiErr)
if e != nil {
c.debug("failed to unmarshal json: %s", e)
}
return nil, apiErr
}
return data, nil
}
// NewPingService init ping service
func (c *Client) NewPingService() *PingService {
return &PingService{c: c}
}
// NewServerTimeService init server time service
func (c *Client) NewServerTimeService() *ServerTimeService {
return &ServerTimeService{c: c}
}
// NewSetServerTimeService init set server time service
func (c *Client) NewSetServerTimeService() *SetServerTimeService {
return &SetServerTimeService{c: c}
}
// NewDepthService init depth service
func (c *Client) NewDepthService() *DepthService {
return &DepthService{c: c}
}
// NewAggTradesService init aggregate trades service
func (c *Client) NewAggTradesService() *AggTradesService {
return &AggTradesService{c: c}
}
// NewRecentTradesService init recent trades service
func (c *Client) NewRecentTradesService() *RecentTradesService {
return &RecentTradesService{c: c}
}
// NewKlinesService init klines service
func (c *Client) NewKlinesService() *KlinesService {
return &KlinesService{c: c}
}
// NewListPriceChangeStatsService init list prices change stats service
func (c *Client) NewListPriceChangeStatsService() *ListPriceChangeStatsService {
return &ListPriceChangeStatsService{c: c}
}
// NewListPricesService init listing prices service
func (c *Client) NewListPricesService() *ListPricesService {
return &ListPricesService{c: c}
}
// NewListBookTickersService init listing booking tickers service
func (c *Client) NewListBookTickersService() *ListBookTickersService {
return &ListBookTickersService{c: c}
}
// NewCreateOrderService init creating order service
func (c *Client) NewCreateOrderService() *CreateOrderService {
return &CreateOrderService{c: c}
}
// NewCreateOCOService init creating OCO service
func (c *Client) NewCreateOCOService() *CreateOCOService {
return &CreateOCOService{c: c}
}
// NewGetOrderService init get order service
func (c *Client) NewGetOrderService() *GetOrderService {
return &GetOrderService{c: c}
}
// NewCancelOrderService init cancel order service
func (c *Client) NewCancelOrderService() *CancelOrderService {
return &CancelOrderService{c: c}
}
// NewListOpenOrdersService init list open orders service
func (c *Client) NewListOpenOrdersService() *ListOpenOrdersService {
return &ListOpenOrdersService{c: c}
}
// NewListOrdersService init listing orders service
func (c *Client) NewListOrdersService() *ListOrdersService {
return &ListOrdersService{c: c}
}
// NewGetAccountService init getting account service
func (c *Client) NewGetAccountService() *GetAccountService {
return &GetAccountService{c: c}
}
// NewGetAccountSnapshotService init getting account snapshot service
func (c *Client) NewGetAccountSnapshotService() *GetAccountSnapshotService {
return &GetAccountSnapshotService{c: c}
}
// NewListTradesService init listing trades service
func (c *Client) NewListTradesService() *ListTradesService {
return &ListTradesService{c: c}
}
// NewHistoricalTradesService init listing trades service
func (c *Client) NewHistoricalTradesService() *HistoricalTradesService {
return &HistoricalTradesService{c: c}
}
// NewListDepositsService init listing deposits service
func (c *Client) NewListDepositsService() *ListDepositsService {
return &ListDepositsService{c: c}
}
// NewGetDepositAddressService init getting deposit address service
func (c *Client) NewGetDepositAddressService() *GetDepositsAddressService {
return &GetDepositsAddressService{c: c}
}
// NewCreateWithdrawService init creating withdraw service
func (c *Client) NewCreateWithdrawService() *CreateWithdrawService {
return &CreateWithdrawService{c: c}
}
// NewListWithdrawsService init listing withdraw service
func (c *Client) NewListWithdrawsService() *ListWithdrawsService {
return &ListWithdrawsService{c: c}
}
// NewStartUserStreamService init starting user stream service
func (c *Client) NewStartUserStreamService() *StartUserStreamService {
return &StartUserStreamService{c: c}
}
// NewKeepaliveUserStreamService init keep alive user stream service
func (c *Client) NewKeepaliveUserStreamService() *KeepaliveUserStreamService {
return &KeepaliveUserStreamService{c: c}
}
// NewCloseUserStreamService init closing user stream service
func (c *Client) NewCloseUserStreamService() *CloseUserStreamService {
return &CloseUserStreamService{c: c}
}
// NewExchangeInfoService init exchange info service
func (c *Client) NewExchangeInfoService() *ExchangeInfoService {
return &ExchangeInfoService{c: c}
}
// NewGetWithdrawFeeService init get withdraw fee service
func (c *Client) NewGetWithdrawFeeService() *GetWithdrawFeeService {
return &GetWithdrawFeeService{c: c}
}
// NewAveragePriceService init average price service
func (c *Client) NewAveragePriceService() *AveragePriceService {
return &AveragePriceService{c: c}
}
// NewMarginTransferService init margin account transfer service
func (c *Client) NewMarginTransferService() *MarginTransferService {
return &MarginTransferService{c: c}
}
// NewMarginLoanService init margin account loan service
func (c *Client) NewMarginLoanService() *MarginLoanService {
return &MarginLoanService{c: c}
}
// NewMarginRepayService init margin account repay service
func (c *Client) NewMarginRepayService() *MarginRepayService {
return &MarginRepayService{c: c}
}
// NewCreateMarginOrderService init creating margin order service
func (c *Client) NewCreateMarginOrderService() *CreateMarginOrderService {
return &CreateMarginOrderService{c: c}
}
// NewCancelMarginOrderService init cancel order service
func (c *Client) NewCancelMarginOrderService() *CancelMarginOrderService {
return &CancelMarginOrderService{c: c}
}
// NewGetMarginOrderService init get order service
func (c *Client) NewGetMarginOrderService() *GetMarginOrderService {
return &GetMarginOrderService{c: c}
}
// NewListMarginLoansService init list margin loan service
func (c *Client) NewListMarginLoansService() *ListMarginLoansService {
return &ListMarginLoansService{c: c}
}
// NewListMarginRepaysService init list margin repay service
func (c *Client) NewListMarginRepaysService() *ListMarginRepaysService {
return &ListMarginRepaysService{c: c}
}
// NewGetMarginAccountService init get margin account service
func (c *Client) NewGetMarginAccountService() *GetMarginAccountService {
return &GetMarginAccountService{c: c}
}
// NewGetMarginAssetService init get margin asset service
func (c *Client) NewGetMarginAssetService() *GetMarginAssetService {
return &GetMarginAssetService{c: c}
}
// NewGetMarginPairService init get margin pair service
func (c *Client) NewGetMarginPairService() *GetMarginPairService {
return &GetMarginPairService{c: c}
}
// NewGetMarginAllPairsService init get margin all pairs service
func (c *Client) NewGetMarginAllPairsService() *GetMarginAllPairsService {
return &GetMarginAllPairsService{c: c}
}
// NewGetMarginPriceIndexService init get margin price index service
func (c *Client) NewGetMarginPriceIndexService() *GetMarginPriceIndexService {
return &GetMarginPriceIndexService{c: c}
}
// NewListMarginOpenOrdersService init list margin open orders service
func (c *Client) NewListMarginOpenOrdersService() *ListMarginOpenOrdersService {
return &ListMarginOpenOrdersService{c: c}
}
// NewListMarginOrdersService init list margin all orders service
func (c *Client) NewListMarginOrdersService() *ListMarginOrdersService {
return &ListMarginOrdersService{c: c}
}
// NewListMarginTradesService init list margin trades service
func (c *Client) NewListMarginTradesService() *ListMarginTradesService {
return &ListMarginTradesService{c: c}
}
// NewGetMaxBorrowableService init get max borrowable service
func (c *Client) NewGetMaxBorrowableService() *GetMaxBorrowableService {
return &GetMaxBorrowableService{c: c}
}
// NewGetMaxTransferableService init get max transferable service
func (c *Client) NewGetMaxTransferableService() *GetMaxTransferableService {
return &GetMaxTransferableService{c: c}
}
// NewStartMarginUserStreamService init starting margin user stream service
func (c *Client) NewStartMarginUserStreamService() *StartMarginUserStreamService {
return &StartMarginUserStreamService{c: c}
}
// NewKeepaliveMarginUserStreamService init keep alive margin user stream service
func (c *Client) NewKeepaliveMarginUserStreamService() *KeepaliveMarginUserStreamService {
return &KeepaliveMarginUserStreamService{c: c}
}
// NewCloseMarginUserStreamService init closing margin user stream service
func (c *Client) NewCloseMarginUserStreamService() *CloseMarginUserStreamService {
return &CloseMarginUserStreamService{c: c}
}
// NewFuturesTransferService init futures transfer service
func (c *Client) NewFuturesTransferService() *FuturesTransferService {
return &FuturesTransferService{c: c}
}
// NewListFuturesTransferService init list futures transfer service
func (c *Client) NewListFuturesTransferService() *ListFuturesTransferService {
return &ListFuturesTransferService{c: c}
}
// NewAssetDividendService init the asset dividend list service
func (c *Client) NewAssetDividendService() *AssetDividendService {
return &AssetDividendService{c: c}
}
// NewListDustLogService init list dust log service
func (c *Client) NewListDustLogService() *ListDustLogService {
return &ListDustLogService{c: c}
}
// NewListLendingPurchaseService init lending purchase service
func (c *Client) NewListLendingPurchaseService() *ListLendingPurchaseService {
return &ListLendingPurchaseService{c: c}
}
// NewListLendingRedemptionService init lending redemption service
func (c *Client) NewListLendingRedemptionService() *ListLendingRedemptionService {
return &ListLendingRedemptionService{c: c}
}
// NewListLendingInterestService init lending interest service
func (c *Client) NewListLendingInterestService() *ListLendingInterestService {
return &ListLendingInterestService{c: c}
}