forked from binance-exchange/go-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binance.go
549 lines (474 loc) · 13.5 KB
/
binance.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
package binance
import (
"fmt"
"time"
)
// Binance is wrapper for Binance API.
//
// Read web documentation for more endpoints descriptions and list of
// mandatory and optional params. Wrapper is not responsible for client-side
// validation and only sends requests further.
//
// For each API-defined enum there's a special type and list of defined
// enum values to be used.
type Binance interface {
// Ping tests connectivity.
Ping() error
// Time returns server time.
Time() (time.Time, error)
// OrderBook returns list of orders.
OrderBook(obr OrderBookRequest) (*OrderBook, error)
// AggTrades returns compressed/aggregate list of trades.
AggTrades(atr AggTradesRequest) ([]*AggTrade, error)
// Klines returns klines/candlestick data.
Klines(kr KlinesRequest) ([]*Kline, error)
// Ticker24 returns 24hr price change statistics.
Ticker24(tr TickerRequest) (*Ticker24, error)
// TickerAllPrices returns ticker data for symbols.
TickerAllPrices() ([]*PriceTicker, error)
// TickerAllBooks returns tickers for all books.
TickerAllBooks() ([]*BookTicker, error)
// NewOrder places new order and returns ProcessedOrder.
NewOrder(nor NewOrderRequest) (*ProcessedOrder, error)
// NewOrder places testing order.
NewOrderTest(nor NewOrderRequest) error
// QueryOrder returns data about existing order.
QueryOrder(qor QueryOrderRequest) (*ExecutedOrder, error)
// CancelOrder cancels order.
CancelOrder(cor CancelOrderRequest) (*CanceledOrder, error)
// OpenOrders returns list of open orders.
OpenOrders(oor OpenOrdersRequest) ([]*ExecutedOrder, error)
// AllOrders returns list of all previous orders.
AllOrders(aor AllOrdersRequest) ([]*ExecutedOrder, error)
// Account returns account data.
Account(ar AccountRequest) (*Account, error)
// MyTrades list user's trades.
MyTrades(mtr MyTradesRequest) ([]*Trade, error)
// Withdraw executes withdrawal.
Withdraw(wr WithdrawRequest) (*WithdrawResult, error)
// DepositHistory lists deposit data.
DepositHistory(hr HistoryRequest) ([]*Deposit, error)
// WithdrawHistory lists withdraw data.
WithdrawHistory(hr HistoryRequest) ([]*Withdrawal, error)
// StartUserDataStream starts stream and returns Stream with ListenKey.
StartUserDataStream() (*Stream, error)
// KeepAliveUserDataStream prolongs stream livespan.
KeepAliveUserDataStream(s *Stream) error
// CloseUserDataStream closes opened stream.
CloseUserDataStream(s *Stream) error
DepthWebsocket(dwr DepthWebsocketRequest) (chan *DepthEvent, chan struct{}, error)
KlineWebsocket(kwr KlineWebsocketRequest) (chan *KlineEvent, chan struct{}, error)
TradeWebsocket(twr TradeWebsocketRequest) (chan *AggTradeEvent, chan struct{}, error)
UserDataWebsocket(udwr UserDataWebsocketRequest) (chan *AccountEvent, chan struct{}, error)
}
type binance struct {
Service Service
}
// Error represents Binance error structure with error code and message.
type Error struct {
Code int `json:"code"`
Message string `json:"msg"`
}
// Error returns formatted error message.
func (e Error) Error() string {
return fmt.Sprintf("%d: %s", e.Code, e.Message)
}
// NewBinance returns Binance instance.
func NewBinance(service Service) Binance {
return &binance{
Service: service,
}
}
// Ping tests connectivity.
func (b *binance) Ping() error {
return b.Service.Ping()
}
// Time returns server time.
func (b *binance) Time() (time.Time, error) {
return b.Service.Time()
}
// OrderBook represents Bids and Asks.
type OrderBook struct {
LastUpdateID int `json:"lastUpdateId"`
Bids []*Order
Asks []*Order
}
type DepthEvent struct {
WSEvent
UpdateID int
OrderBook
}
// Order represents single order information.
type Order struct {
Price float64
Quantity float64
}
// OrderBookRequest represents OrderBook request data.
type OrderBookRequest struct {
Symbol string
Limit int
}
// OrderBook returns list of orders.
func (b *binance) OrderBook(obr OrderBookRequest) (*OrderBook, error) {
return b.Service.OrderBook(obr)
}
// AggTrade represents aggregated trade.
type AggTrade struct {
ID int
Price float64
Quantity float64
FirstTradeID int
LastTradeID int
Timestamp time.Time
BuyerMaker bool
BestPriceMatch bool
}
type AggTradeEvent struct {
WSEvent
AggTrade
}
// AggTradesRequest represents AggTrades request data.
type AggTradesRequest struct {
Symbol string
FromID int64
StartTime int64
EndTime int64
Limit int
}
// AggTrades returns compressed/aggregate list of trades.
func (b *binance) AggTrades(atr AggTradesRequest) ([]*AggTrade, error) {
return b.Service.AggTrades(atr)
}
// KlinesRequest represents Klines request data.
type KlinesRequest struct {
Symbol string
Interval Interval
Limit int
StartTime int64
EndTime int64
}
// Kline represents single Kline information.
type Kline struct {
OpenTime time.Time
Open float64
High float64
Low float64
Close float64
Volume float64
CloseTime time.Time
QuoteAssetVolume float64
NumberOfTrades int
TakerBuyBaseAssetVolume float64
TakerBuyQuoteAssetVolume float64
}
type KlineEvent struct {
WSEvent
Interval Interval
FirstTradeID int64
LastTradeID int64
Final bool
Kline
}
// Klines returns klines/candlestick data.
func (b *binance) Klines(kr KlinesRequest) ([]*Kline, error) {
return b.Service.Klines(kr)
}
// TickerRequest represents Ticker request data.
type TickerRequest struct {
Symbol string
}
// Ticker24 represents data for 24hr ticker.
type Ticker24 struct {
PriceChange float64
PriceChangePercent float64
WeightedAvgPrice float64
PrevClosePrice float64
LastPrice float64
BidPrice float64
AskPrice float64
OpenPrice float64
HighPrice float64
LowPrice float64
Volume float64
OpenTime time.Time
CloseTime time.Time
FirstID int
LastID int
Count int
}
// Ticker24 returns 24hr price change statistics.
func (b *binance) Ticker24(tr TickerRequest) (*Ticker24, error) {
return b.Service.Ticker24(tr)
}
// PriceTicker represents ticker data for price.
type PriceTicker struct {
Symbol string
Price float64
}
// TickerAllPrices returns ticker data for symbols.
func (b *binance) TickerAllPrices() ([]*PriceTicker, error) {
return b.Service.TickerAllPrices()
}
// BookTicker represents book ticker data.
type BookTicker struct {
Symbol string
BidPrice float64
BidQty float64
AskPrice float64
AskQty float64
}
// TickerAllBooks returns tickers for all books.
func (b *binance) TickerAllBooks() ([]*BookTicker, error) {
return b.Service.TickerAllBooks()
}
// NewOrderRequest represents NewOrder request data.
type NewOrderRequest struct {
Symbol string
Side OrderSide
Type OrderType
TimeInForce TimeInForce
Quantity float64
Price float64
NewClientOrderID string
StopPrice float64
IcebergQty float64
Timestamp time.Time
}
// ProcessedOrder represents data from processed order.
type ProcessedOrder struct {
Symbol string
OrderID int64
ClientOrderID string
TransactTime time.Time
}
// NewOrder places new order and returns ProcessedOrder.
func (b *binance) NewOrder(nor NewOrderRequest) (*ProcessedOrder, error) {
return b.Service.NewOrder(nor)
}
// NewOrder places testing order.
func (b *binance) NewOrderTest(nor NewOrderRequest) error {
return b.Service.NewOrderTest(nor)
}
// QueryOrderRequest represents QueryOrder request data.
type QueryOrderRequest struct {
Symbol string
OrderID int64
OrigClientOrderID string
RecvWindow time.Duration
Timestamp time.Time
}
// ExecutedOrder represents data about executed order.
type ExecutedOrder struct {
Symbol string
OrderID int
ClientOrderID string
Price float64
OrigQty float64
ExecutedQty float64
Status OrderStatus
TimeInForce TimeInForce
Type OrderType
Side OrderSide
StopPrice float64
IcebergQty float64
Time time.Time
}
// QueryOrder returns data about existing order.
func (b *binance) QueryOrder(qor QueryOrderRequest) (*ExecutedOrder, error) {
return b.Service.QueryOrder(qor)
}
// CancelOrderRequest represents CancelOrder request data.
type CancelOrderRequest struct {
Symbol string
OrderID int64
OrigClientOrderID string
NewClientOrderID string
RecvWindow time.Duration
Timestamp time.Time
}
// CanceledOrder represents data about canceled order.
type CanceledOrder struct {
Symbol string
OrigClientOrderID string
OrderID int64
ClientOrderID string
}
// CancelOrder cancels order.
func (b *binance) CancelOrder(cor CancelOrderRequest) (*CanceledOrder, error) {
return b.Service.CancelOrder(cor)
}
// OpenOrdersRequest represents OpenOrders request data.
type OpenOrdersRequest struct {
Symbol string
RecvWindow time.Duration
Timestamp time.Time
}
// OpenOrders returns list of open orders.
func (b *binance) OpenOrders(oor OpenOrdersRequest) ([]*ExecutedOrder, error) {
return b.Service.OpenOrders(oor)
}
// AllOrdersRequest represents AllOrders request data.
type AllOrdersRequest struct {
Symbol string
OrderID int64
Limit int
RecvWindow time.Duration
Timestamp time.Time
}
// AllOrders returns list of all previous orders.
func (b *binance) AllOrders(aor AllOrdersRequest) ([]*ExecutedOrder, error) {
return b.Service.AllOrders(aor)
}
// AccountRequest represents Account request data.
type AccountRequest struct {
RecvWindow time.Duration
Timestamp time.Time
}
// Account represents user's account information.
type Account struct {
MakerCommision int64
TakerCommision int64
BuyerCommision int64
SellerCommision int64
CanTrade bool
CanWithdraw bool
CanDeposit bool
Balances []*Balance
}
type AccountEvent struct {
WSEvent
Account
}
// Balance groups balance-related information.
type Balance struct {
Asset string
Free float64
Locked float64
}
// Account returns account data.
func (b *binance) Account(ar AccountRequest) (*Account, error) {
return b.Service.Account(ar)
}
// MyTradesRequest represents MyTrades request data.
type MyTradesRequest struct {
Symbol string
Limit int
FromID int64
RecvWindow time.Duration
Timestamp time.Time
}
// Trade represents data about trade.
type Trade struct {
ID int64
Price float64
Qty float64
Commission float64
CommissionAsset string
Time time.Time
IsBuyer bool
IsMaker bool
IsBestMatch bool
}
// MyTrades list user's trades.
func (b *binance) MyTrades(mtr MyTradesRequest) ([]*Trade, error) {
return b.Service.MyTrades(mtr)
}
// WithdrawRequest represents Withdraw request data.
type WithdrawRequest struct {
Asset string
Address string
Amount float64
Name string
RecvWindow time.Duration
Timestamp time.Time
}
// WithdrawResult represents Withdraw result.
type WithdrawResult struct {
Success bool
Msg string
}
// Withdraw executes withdrawal.
func (b *binance) Withdraw(wr WithdrawRequest) (*WithdrawResult, error) {
return b.Service.Withdraw(wr)
}
// HistoryRequest represents history-related calls request data.
type HistoryRequest struct {
Asset string
Status *int
StartTime time.Time
EndTime time.Time
RecvWindow time.Duration
Timestamp time.Time
}
// Deposit represents Deposit data.
type Deposit struct {
InsertTime time.Time
Amount float64
Asset string
Status int
}
// DepositHistory lists deposit data.
func (b *binance) DepositHistory(hr HistoryRequest) ([]*Deposit, error) {
return b.Service.DepositHistory(hr)
}
// Withdrawal represents withdrawal data.
type Withdrawal struct {
Amount float64
Address string
TxID string
Asset string
ApplyTime time.Time
Status int
}
// WithdrawHistory lists withdraw data.
func (b *binance) WithdrawHistory(hr HistoryRequest) ([]*Withdrawal, error) {
return b.Service.WithdrawHistory(hr)
}
// Stream represents stream information.
//
// Read web docs to get more information about using streams.
type Stream struct {
ListenKey string
}
// StartUserDataStream starts stream and returns Stream with ListenKey.
func (b *binance) StartUserDataStream() (*Stream, error) {
return b.Service.StartUserDataStream()
}
// KeepAliveUserDataStream prolongs stream livespan.
func (b *binance) KeepAliveUserDataStream(s *Stream) error {
return b.Service.KeepAliveUserDataStream(s)
}
// CloseUserDataStream closes opened stream.
func (b *binance) CloseUserDataStream(s *Stream) error {
return b.Service.CloseUserDataStream(s)
}
type WSEvent struct {
Type string
Time time.Time
Symbol string
}
type DepthWebsocketRequest struct {
Symbol string
}
func (b *binance) DepthWebsocket(dwr DepthWebsocketRequest) (chan *DepthEvent, chan struct{}, error) {
return b.Service.DepthWebsocket(dwr)
}
type KlineWebsocketRequest struct {
Symbol string
Interval Interval
}
func (b *binance) KlineWebsocket(kwr KlineWebsocketRequest) (chan *KlineEvent, chan struct{}, error) {
return b.Service.KlineWebsocket(kwr)
}
type TradeWebsocketRequest struct {
Symbol string
}
func (b *binance) TradeWebsocket(twr TradeWebsocketRequest) (chan *AggTradeEvent, chan struct{}, error) {
return b.Service.TradeWebsocket(twr)
}
type UserDataWebsocketRequest struct {
ListenKey string
}
func (b *binance) UserDataWebsocket(udwr UserDataWebsocketRequest) (chan *AccountEvent, chan struct{}, error) {
return b.Service.UserDataWebsocket(udwr)
}