-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathwebsocket_api.go
540 lines (447 loc) · 13.8 KB
/
websocket_api.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
package binance_connector
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/gorilla/websocket"
)
type WebsocketAPIClient struct {
APIKey string
APISecret string
Endpoint string
Conn *websocket.Conn
Dialer *websocket.Dialer
ReqResponseMap map[string]chan []byte
}
type WsAPIRateLimit struct {
RateLimitType string `json:"rateLimitType"`
Interval string `json:"interval"`
IntervalNum int `json:"intervalNum"`
Limit int `json:"limit"`
Count int `json:"count"`
}
type WsAPIErrorResponse struct {
Code int `json:"code"`
ID string `json:"id"`
Message string `json:"msg"`
}
var (
// WebsocketAPITimeout is an interval for sending ping/pong messages if WebsocketKeepalive is enabled
WebsocketAPITimeout = time.Second * 60
// WebsocketAPIKeepalive enables sending ping/pong messages to check the connection stability
WebsocketAPIKeepalive = true
)
func NewWebsocketAPIClient(apiKey string, apiSecret string, baseURL ...string) *WebsocketAPIClient {
// Set default base URL to production WS URL
url := "wss://ws-api.binance.com:443/ws-api/v3"
if len(baseURL) > 0 {
url = baseURL[0]
}
return &WebsocketAPIClient{
APIKey: apiKey,
APISecret: apiSecret,
Endpoint: url,
Dialer: &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
EnableCompression: false,
},
}
}
func (c *WebsocketAPIClient) Connect() error {
if c.Dialer == nil {
return fmt.Errorf("dialer not initialized")
}
headers := http.Header{}
headers.Add("User-Agent", fmt.Sprintf("%s/%s", Name, Version))
conn, _, err := c.Dialer.Dial(c.Endpoint, headers)
if err != nil {
return err
}
fmt.Println("Connected to Binance Websocket API")
c.Conn = conn
c.ReqResponseMap = make(map[string]chan []byte)
c.startReader() // start reader again
return nil
}
func (c *WebsocketAPIClient) startReader() {
go func() {
for {
_, message, err := c.Conn.ReadMessage()
if err != nil {
log.Println("Error reading:", err)
return
}
c.Handler(message)
}
}()
}
// Handler function to handle responses
func (c *WebsocketAPIClient) Handler(message []byte) {
var response WsAPIErrorResponse
err := json.Unmarshal(message, &response)
if err != nil {
log.Println("Error unmarshaling:", err)
return
}
// Send the message to the corresponding request
if channel, ok := c.ReqResponseMap[response.ID]; ok {
channel <- message
}
}
func (c *WebsocketAPIClient) WaitForCloseSignal() {
stopCh := make(chan os.Signal, 1)
signal.Notify(stopCh, os.Interrupt, syscall.SIGTERM)
<-stopCh
}
func (c *WebsocketAPIClient) Close() error {
return c.Conn.Close()
}
func (c *WebsocketAPIClient) SendMessage(msg interface{}) error {
return c.Conn.WriteJSON(msg)
}
func (c *WebsocketAPIClient) RequestHandler(req interface{}, handler WsHandler, errHandler ErrHandler) (stopCh chan struct{}, err error) {
err = c.SendMessage(req)
if err != nil {
return nil, err
}
stopCh, err = wsApiServe(c.Conn, handler, errHandler)
if err != nil {
return nil, err
}
return stopCh, nil
}
func wsApiServe(c *websocket.Conn, handler WsHandler, errHandler ErrHandler) (stopCh chan struct{}, err error) {
stopCh = make(chan struct{})
go func() {
if WebsocketAPIKeepalive {
keepAlive(c, WebsocketAPITimeout)
}
silent := false
for {
select {
case <-stopCh:
return
default:
_, message, err := c.ReadMessage()
if err != nil {
if !silent {
fmt.Println(err)
errHandler(err)
}
continue
}
handler(message)
}
}
}()
return stopCh, nil
}
func websocketAPISignature(apiKey string, apiSecret string, parameters map[string]string) (map[string]string, error) {
if apiKey == "" || apiSecret == "" {
return nil, &WebsocketClientError{
Message: "api_key and api_secret are required for websocket API signature",
}
}
parameters["timestamp"] = strconv.FormatInt(time.Now().Unix()*1000, 10)
parameters["apiKey"] = apiKey
// Sort parameters by key
keys := make([]string, 0, len(parameters))
for key := range parameters {
keys = append(keys, key)
}
sort.Strings(keys)
// Build sorted query string
var sortedParams []string
for _, key := range keys {
sortedParams = append(sortedParams, key+"="+parameters[key])
}
// Calculate signature
queryString := strings.Join(sortedParams, "&")
signature := hmacHashing(apiSecret, queryString)
parameters["signature"] = signature
return parameters, nil
}
func hmacHashing(apiSecret string, data string) string {
mac := hmac.New(sha256.New, []byte(apiSecret))
mac.Write([]byte(data))
return hex.EncodeToString(mac.Sum(nil))
}
func getUUID() string {
return fmt.Sprintf("%s-%s-%s-%s-%s", randomHex(8), randomHex(4), randomHex(4), randomHex(4), randomHex(12))
}
func randomHex(n int) string {
rand.New(rand.NewSource(time.Now().UnixNano()))
hexChars := "0123456789abcdef"
bytes := make([]byte, n)
for i := 0; i < n; i++ {
bytes[i] = hexChars[rand.Intn(len(hexChars))]
}
return string(bytes)
}
type WebsocketClientError struct {
Message string
}
func (e *WebsocketClientError) Error() string {
return e.Message
}
type TestConnectivityService struct {
websocketAPI *WebsocketAPIClient
}
func (s *TestConnectivityService) Do(ctx context.Context) (*TestConnectivityResponse, error) {
id := getUUID()
payload := map[string]interface{}{
"id": id,
"method": "ping",
}
messageCh := make(chan []byte)
s.websocketAPI.ReqResponseMap[id] = messageCh
err := s.websocketAPI.SendMessage(payload)
if err != nil {
return nil, err
}
defer delete(s.websocketAPI.ReqResponseMap, id)
select {
case response := <-messageCh:
var pingResponse TestConnectivityResponse
err = json.Unmarshal(response, &pingResponse)
if err != nil {
return nil, err
}
return &pingResponse, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
type TestConnectivityResponse struct {
ID string `json:"id"`
Status int `json:"status"`
Error *WsAPIErrorResponse `json:"error,omitempty"`
Result struct{} `json:"result,omitempty"`
RateLimits []*WsAPIRateLimit `json:"rateLimits,omitempty"`
}
type CheckServerTimeService struct {
websocketAPI *WebsocketAPIClient
}
func (s *CheckServerTimeService) Do(ctx context.Context) (*CheckServerTimeResponse, error) {
id := getUUID()
payload := map[string]interface{}{
"id": id,
"method": "time",
}
messageCh := make(chan []byte)
s.websocketAPI.ReqResponseMap[id] = messageCh
err := s.websocketAPI.SendMessage(payload)
if err != nil {
return nil, err
}
defer delete(s.websocketAPI.ReqResponseMap, id)
select {
case response := <-messageCh:
var timeResponse CheckServerTimeResponse
err = json.Unmarshal(response, &timeResponse)
if err != nil {
return nil, err
}
return &timeResponse, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
type CheckServerTimeResponse struct {
ID string `json:"id"`
Status int `json:"status"`
Error *WsAPIErrorResponse `json:"error,omitempty"`
Result struct {
ServerTime uint64 `json:"serverTime"`
} `json:"result,omitempty"`
RateLimits WsAPIRateLimit `json:"rateLimits,omitempty"`
}
type ExchangeInformationService struct {
websocketAPI *WebsocketAPIClient
symbol *string
symbols *[]string
permissions *[]string
}
func (s *ExchangeInformationService) Symbol(symbol string) *ExchangeInformationService {
s.symbol = &symbol
return s
}
func (s *ExchangeInformationService) Symbols(symbols []string) *ExchangeInformationService {
s.symbols = &symbols
return s
}
func (s *ExchangeInformationService) Permissions(permissions []string) *ExchangeInformationService {
s.permissions = &permissions
return s
}
func (s *ExchangeInformationService) Do(ctx context.Context) (*ExchangeInformationResponse, error) {
payload := map[string]interface{}{
"id": getUUID(),
"method": "exchangeInfo",
}
// Only one of the following parameters can be set: symbol, symbols, permissions
if s.symbol != nil {
payload["params"] = map[string]interface{}{
"symbol": *s.symbol,
}
} else if s.symbols != nil {
payload["params"] = map[string]interface{}{
"symbols": *s.symbols,
}
} else if s.permissions != nil {
payload["params"] = map[string]interface{}{
"permissions": *s.permissions,
}
}
responseCh := make(chan *ExchangeInformationResponse)
handler := func(message []byte) {
var response ExchangeInformationResponse
err := json.Unmarshal(message, &response)
if err != nil {
fmt.Println("Error unmarshaling:", err)
return
}
responseCh <- &response
}
errHandler := func(err error) {
fmt.Println("Error:", err)
}
doneCh, err := s.websocketAPI.RequestHandler(payload, handler, errHandler)
if err != nil {
return nil, err
}
select {
case <-doneCh:
return nil, ctx.Err()
case response := <-responseCh:
return response, nil
}
}
type ExchangeInformationResponse struct {
ID string `json:"id"`
Status int `json:"status"`
Error *WsAPIErrorResponse `json:"error,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
RateLimits []*WsAPIRateLimit `json:"rateLimits,omitempty"`
}
// General Websocket API Endpoints:
func (w *WebsocketAPIClient) NewTestConnectivityService() *TestConnectivityService {
return &TestConnectivityService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewCheckServerTimeService() *CheckServerTimeService {
return &CheckServerTimeService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewExchangeInformationService() *ExchangeInformationService {
return &ExchangeInformationService{websocketAPI: w}
}
// Account Websocket API Endpoints:
func (w *WebsocketAPIClient) NewAccountInformationService() *AccountInformationService {
return &AccountInformationService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewAccountOrderRateLimitsService() *AccountOrderRateLimitsService {
return &AccountOrderRateLimitsService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewAccountOrderHistoryService() *AccountOrderHistoryService {
return &AccountOrderHistoryService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewAccountOCOHistoryService() *AccountOCOHistoryService {
return &AccountOCOHistoryService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewAccountTradeHistoryService() *AccountTradeHistoryService {
return &AccountTradeHistoryService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewAccountPreventedMatchesService() *AccountPreventedMatchesService {
return &AccountPreventedMatchesService{websocketAPI: w}
}
// Market Websocket API Endpoints:
func (w *WebsocketAPIClient) NewDepthService() *DepthService {
return &DepthService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewRecentTradesService() *RecentTradesService {
return &RecentTradesService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewHistoricalTradesService() *HistoricalTradesService {
return &HistoricalTradesService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewAggTradesService() *AggregateTradesService {
return &AggregateTradesService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewKlinesService() *KlinesService {
return &KlinesService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewAvgPriceService() *AvgPriceService {
return &AvgPriceService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewTicker24hrService() *Ticker24hrService {
return &Ticker24hrService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewTickerService() *TickerService {
return &TickerService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewTickerPriceService() *TickerPriceService {
return &TickerPriceService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewTickerBookService() *TickerBookService {
return &TickerBookService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewUiKlinesService() *UIKlinesService {
return &UIKlinesService{websocketAPI: w}
}
// Trading Websocket API Endpoints:
func (w *WebsocketAPIClient) NewPlaceNewOrderService() *OrderPlacementService {
return &OrderPlacementService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewTestPlaceOrderService() *TestOrderPlacementService {
return &TestOrderPlacementService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewQueryOrderService() *OrderStatusService {
return &OrderStatusService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewCancelOrderService() *OrderCancelService {
return &OrderCancelService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewCancelReplaceOrderService() *OrderCancelReplaceService {
return &OrderCancelReplaceService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewCurrentOpenOrdersService() *OpenOrdersStatusService {
return &OpenOrdersStatusService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewCancelOpenOrdersService() *OpenOrdersCancelAllService {
return &OpenOrdersCancelAllService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewPlaceOCOService() *OrderListPlaceService {
return &OrderListPlaceService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewQueryOCOService() *OrderListStatusService {
return &OrderListStatusService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewCurrentOpenOCOService() *OpenOrderListsStatusService {
return &OpenOrderListsStatusService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewCancelOCOService() *OrderListCancelService {
return &OrderListCancelService{websocketAPI: w}
}
// User Data Websocket API Endpoints:
func (w *WebsocketAPIClient) NewStartUserDataStreamService() *StartUserDataStreamService {
return &StartUserDataStreamService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewPingUserDataStreamService() *PingUserDataStreamService {
return &PingUserDataStreamService{websocketAPI: w}
}
func (w *WebsocketAPIClient) NewStopUserDataStreamService() *StopUserDataStreamService {
return &StopUserDataStreamService{websocketAPI: w}
}