-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
509 lines (404 loc) · 17 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
package binance
import (
"log"
"net/http"
"os"
"time"
"github.com/bitly/go-simplejson"
"github.com/crypto-zero/go-binance/v2/common"
"github.com/crypto-zero/go-binance/v2/delivery"
"github.com/crypto-zero/go-binance/v2/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
// Endpoints
const (
baseAPIMainURL = "https://api.binance.com"
baseAPITestnetURL = "https://testnet.binance.vision"
)
// UseTestnet switch all the API endpoints from production to the testnet
var UseTestnet = false
// 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
FuturesTransferTypeSpotToFutures FuturesTransferType = 1
FuturesTransferTypeFuturesToSpot FuturesTransferType = 2
FuturesTransferTypeSpotToFuturesM FuturesTransferType = 3
FuturesTransferTypeFuturesMToSpot FuturesTransferType = 4
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"
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
}
// getAPIEndpoint return the base endpoint of the Rest API according the testnet flag
func getAPIEndpoint(testnet bool) string {
if testnet {
return baseAPITestnetURL
}
return baseAPIMainURL
}
// 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, testnet bool) *Client {
logger := common.NewDefaultLogger(common.LogInfo, log.New(os.Stderr,
"Binance-golang ", log.LstdFlags))
return &Client{
Client: common.NewClient(apiKey, secretKey, getAPIEndpoint(testnet),
"Binance/golang", http.DefaultClient, logger),
}
}
// NewFuturesClient initialize client for futures API
func NewFuturesClient(apiKey, secretKey string, testnet bool) *futures.Client {
return futures.NewClient(apiKey, secretKey, testnet)
}
// NewDeliveryClient initialize client for coin-M futures API
func NewDeliveryClient(apiKey, secretKey string, testnet bool) *delivery.Client {
return delivery.NewClient(apiKey, secretKey, testnet)
}
type doFunc func(req *http.Request) (*http.Response, error)
// Client define API client
type Client struct {
common.Client
}
// 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}
}
// NewCancelOCOService init cancel OCO service
func (c *Client) NewCancelOCOService() *CancelOCOService {
return &CancelOCOService{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}
}
// NewCancelOpenOrdersService init cancel open orders service
func (c *Client) NewCancelOpenOrdersService() *CancelOpenOrdersService {
return &CancelOpenOrdersService{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}
}
// NewGetAssetDetailService init get asset detail service
func (c *Client) NewGetAssetDetailService() *GetAssetDetailService {
return &GetAssetDetailService{c: c}
}
// NewGetFundingAssetService init get asset detail service
func (c *Client) NewGetFundingAssetService() *GetFundingAssetService {
return &GetFundingAssetService{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}
}
// NewGetIsolatedMarginAccountService init get isolated margin asset service
func (c *Client) NewGetIsolatedMarginAccountService() *GetIsolatedMarginAccountService {
return &GetIsolatedMarginAccountService{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}
}
// NewStartIsolatedMarginUserStreamService init starting margin user stream service
func (c *Client) NewStartIsolatedMarginUserStreamService() *StartIsolatedMarginUserStreamService {
return &StartIsolatedMarginUserStreamService{c: c}
}
// NewKeepaliveIsolatedMarginUserStreamService init keep alive margin user stream service
func (c *Client) NewKeepaliveIsolatedMarginUserStreamService() *KeepaliveIsolatedMarginUserStreamService {
return &KeepaliveIsolatedMarginUserStreamService{c: c}
}
// NewCloseIsolatedMarginUserStreamService init closing margin user stream service
func (c *Client) NewCloseIsolatedMarginUserStreamService() *CloseIsolatedMarginUserStreamService {
return &CloseIsolatedMarginUserStreamService{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}
}
// NewListDustLogService init list dust log service
func (c *Client) NewListDustLogService() *ListDustLogService {
return &ListDustLogService{c: c}
}
// NewDustTransferService init dust transfer service
func (c *Client) NewDustTransferService() *DustTransferService {
return &DustTransferService{c: c}
}
// NewAPIRestrictionService init api restriction service
func (c *Client) NewAPIRestrictionService() *APIRestrictionService {
return &APIRestrictionService{c: c}
}