-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackpack_websocket.go
More file actions
180 lines (149 loc) · 6.47 KB
/
backpack_websocket.go
File metadata and controls
180 lines (149 loc) · 6.47 KB
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
package backpackgo
import (
"fmt"
"strings"
"time"
encodingjson "encoding/json"
"github.com/UnipayFI/backpack-go/models"
"github.com/UnipayFI/backpack-go/options"
ops "github.com/UnipayFI/backpack-go/websocket"
"github.com/go-json-experiment/json"
"github.com/gorilla/websocket"
)
type BackpackWebsocket struct {
BaseURL string
APIKey string
APISecret string
Windows time.Duration
conn *websocket.Conn
handlers map[string]func(encodingjson.RawMessage)
}
type Message struct {
Stream string `json:"stream"`
Data encodingjson.RawMessage `json:"data"`
}
func NewBackpackWebsocket(options ...ops.Options) (*BackpackWebsocket, error) {
opts := ops.DefaultWebSocketOptions()
dialer := websocket.DefaultDialer
for _, option := range options {
option(opts, dialer)
}
conn, _, err := dialer.Dial(opts.BaseURL, nil)
if err != nil {
return nil, err
}
conn.SetPingHandler(nil)
websocket := &BackpackWebsocket{
BaseURL: opts.BaseURL,
APIKey: opts.APIKey,
APISecret: opts.APISecret,
Windows: opts.Windows,
conn: conn,
handlers: make(map[string]func(encodingjson.RawMessage)),
}
go websocket.loop()
return websocket, nil
}
func (client *BackpackWebsocket) SubscribeOrderUpdate(handler func(*models.OrderUpdate)) error {
return SubscribePrivateStream(client, "account.orderUpdate", new(models.OrderUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeOrderUpdateWithSymbol(symbol string, handler func(*models.OrderUpdate)) error {
return SubscribePrivateStream(client, fmt.Sprintf("account.orderUpdate.%s", symbol), new(models.OrderUpdate), handler)
}
func (client *BackpackWebsocket) SubscribePositionUpdate(handler func(*models.PositionUpdate)) error {
return SubscribePrivateStream(client, "account.positionUpdate", new(models.PositionUpdate), handler)
}
func (client *BackpackWebsocket) SubscribePositionUpdateWithSymbol(symbol string, handler func(*models.PositionUpdate)) error {
return SubscribePrivateStream(client, fmt.Sprintf("account.positionUpdate.%s", symbol), new(models.PositionUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeRFQUpdate(handler func(*models.RFQUpdate)) error {
return SubscribePrivateStream(client, "account.rfqUpdate", new(models.RFQUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeRFQUpdateWithSymbol(symbol string, handler func(*models.RFQUpdate)) error {
return SubscribePrivateStream(client, fmt.Sprintf("account.rfqUpdate.%s", symbol), new(models.RFQUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeBookTicker(symbol string, handler func(*models.BookTickerUpdate)) error {
return SubscribePublicStream(client, fmt.Sprintf("bookTicker.%s", symbol), new(models.BookTickerUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeDepth(symbol string, handler func(*models.DepthUpdate)) error {
return SubscribePublicStream(client, fmt.Sprintf("depth.%s", symbol), new(models.DepthUpdate), handler)
}
func (client *BackpackWebsocket) Subscribe200msDepth(symbol string, handler func(*models.DepthUpdate)) error {
return SubscribePublicStream(client, fmt.Sprintf("depth.200ms.%s", symbol), new(models.DepthUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeKLine(interval options.KLineInterval, symbol string, handler func(*models.KLineUpdate)) error {
return SubscribePublicStream(client, fmt.Sprintf("kline.%s.%s", interval, symbol), new(models.KLineUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeLiquidation(symbol string, handler func(*models.LiquidationUpdate)) error {
return SubscribePublicStream(client, fmt.Sprintf("liquidation.%s", symbol), new(models.LiquidationUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeMarkPrice(symbol string, handler func(*models.MarkPriceUpdate)) error {
return SubscribePublicStream(client, fmt.Sprintf("markPrice.%s", symbol), new(models.MarkPriceUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeTicker(symbol string, handler func(*models.TickerUpdate)) error {
return SubscribePublicStream(client, fmt.Sprintf("ticker.%s", symbol), new(models.TickerUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeOpenInterest(symbol string, handler func(*models.OpenInterestUpdate)) error {
return SubscribePublicStream(client, fmt.Sprintf("openInterest.%s", symbol), new(models.OpenInterestUpdate), handler)
}
func (client *BackpackWebsocket) SubscribeTrade(symbol string, handler func(*models.TradeUpdate)) error {
return SubscribePublicStream(client, fmt.Sprintf("trade.%s", symbol), new(models.TradeUpdate), handler)
}
func (client *BackpackWebsocket) Unsubscribe(stream string) error {
request, err := client.sign(stream, "unsubscribe")
if err != nil {
return err
}
return client.conn.WriteJSON(request)
}
func SubscribePrivateStream[T any](client *BackpackWebsocket, stream string, newable models.Newable, handler func(T)) error {
request, err := client.sign(stream, "subscribe")
if err != nil {
return err
}
client.handlers[stream] = func(payload encodingjson.RawMessage) {
obj := newable.New()
json.Unmarshal(payload, obj)
handler(obj.(T))
}
return client.conn.WriteJSON(request)
}
func SubscribePublicStream[T any](client *BackpackWebsocket, stream string, newable models.Newable, handler func(T)) error {
request := client.payload(stream, "subscribe")
client.handlers[stream] = func(payload encodingjson.RawMessage) {
obj := newable.New()
json.Unmarshal(payload, obj)
handler(obj.(T))
}
return client.conn.WriteJSON(request)
}
func (client *BackpackWebsocket) loop() {
for {
_, message, err := client.conn.ReadMessage()
if err != nil {
return
}
client.handleMessage(message)
}
}
func (client *BackpackWebsocket) handleMessage(message []byte) {
msg := new(Message)
json.Unmarshal(message, &msg)
if client.handlers[msg.Stream] != nil {
go client.handlers[msg.Stream](msg.Data)
}
}
func (client *BackpackWebsocket) sign(stream, instruction string) (map[string]any, error) {
payload := client.payload(stream, instruction)
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
signature, err := sign(client.APISecret, nil, instruction, timestamp, client.Windows.Milliseconds())
if err == nil {
payload["signature"] = []string{client.APIKey, signature, fmt.Sprintf("%v", timestamp), fmt.Sprintf("%v", client.Windows.Milliseconds())}
return payload, nil
} else {
return nil, err
}
}
func (client *BackpackWebsocket) payload(stream, instruction string) map[string]any {
return map[string]any{"method": strings.ToUpper(instruction), "params": []string{stream}}
}