forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
coinbasewebsocket.go
182 lines (161 loc) · 4.35 KB
/
coinbasewebsocket.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
package main
import (
"github.com/gorilla/websocket"
"log"
"net/http"
)
const (
COINBASE_WEBSOCKET_URL = "wss://ws-feed.exchange.coinbase.com"
)
type CoinbaseWebsocketSubscribe struct {
Type string `json:"type"`
ProductID string `json:"product_id"`
}
type CoinbaseWebsocketReceived struct {
Type string `json:"type"`
Time string `json:"time"`
Sequence int `json:"sequence"`
OrderID string `json:"order_id"`
Size float64 `json:"size,string"`
Price float64 `json:"price,string"`
Side string `json:"side"`
}
type CoinbaseWebsocketOpen struct {
Type string `json:"type"`
Time string `json:"time"`
Sequence int `json:"sequence"`
OrderID string `json:"order_id"`
Price float64 `json:"price,string"`
RemainingSize float64 `json:"remaining_size,string"`
Side string `json:"side"`
}
type CoinbaseWebsocketDone struct {
Type string `json:"type"`
Time string `json:"time"`
Sequence int `json:"sequence"`
Price float64 `json:"price,string"`
OrderID string `json:"order_id"`
Reason string `json:"reason"`
Side string `json:"side"`
RemainingSize float64 `json:"remaining_size,string"`
}
type CoinbaseWebsocketMatch struct {
Type string `json:"type"`
TradeID int `json:"trade_id"`
Sequence int `json:"sequence"`
MakerOrderID string `json:"maker_order_id"`
TakerOrderID string `json:"taker_order_id"`
Time string `json:"time"`
Size float64 `json:"size,string"`
Price float64 `json:"price,string"`
Side string `json:"side"`
}
type CoinbaseWebsocketChange struct {
Type string `json:"type"`
Time string `json:"time"`
Sequence int `json:"sequence"`
OrderID string `json:"order_id"`
NewSize float64 `json:"new_size,string"`
OldSize float64 `json:"old_size,string"`
Price float64 `json:"price,string"`
Side string `json:"side"`
}
func (c *Coinbase) WebsocketSubscribe(product string, conn *websocket.Conn) error {
subscribe := CoinbaseWebsocketSubscribe{"subscribe", product}
json, err := JSONEncode(subscribe)
if err != nil {
return err
}
err = conn.WriteMessage(websocket.TextMessage, json)
if err != nil {
return err
}
return nil
}
func (c *Coinbase) WebsocketClient() {
for c.Enabled && c.Websocket {
var Dialer websocket.Dialer
conn, _, err := Dialer.Dial(COINBASE_WEBSOCKET_URL, http.Header{})
if err != nil {
log.Printf("%s Unable to connect to Websocket. Error: %s\n", c.GetName(), err)
continue
}
log.Printf("%s Connected to Websocket.\n", c.GetName())
currencies := []string{}
for _, x := range c.EnabledPairs {
currency := x[0:3] + "-" + x[3:]
currencies = append(currencies, currency)
}
for _, x := range currencies {
err = c.WebsocketSubscribe(x, conn)
if err != nil {
log.Printf("%s Websocket subscription error: %s\n", c.GetName(), err)
continue
}
}
if c.Verbose {
log.Printf("%s Subscribed to product messages.", c.GetName())
}
for c.Enabled && c.Websocket {
msgType, resp, err := conn.ReadMessage()
if err != nil {
log.Println(err)
break
}
switch msgType {
case websocket.TextMessage:
type MsgType struct {
Type string `json:"type"`
}
msgType := MsgType{}
err := JSONDecode(resp, &msgType)
if err != nil {
log.Println(err)
continue
}
switch msgType.Type {
case "error":
log.Println(string(resp))
break
case "received":
received := CoinbaseWebsocketReceived{}
err := JSONDecode(resp, &received)
if err != nil {
log.Println(err)
continue
}
case "open":
open := CoinbaseWebsocketOpen{}
err := JSONDecode(resp, &open)
if err != nil {
log.Println(err)
continue
}
case "done":
done := CoinbaseWebsocketDone{}
err := JSONDecode(resp, &done)
if err != nil {
log.Println(err)
continue
}
case "match":
match := CoinbaseWebsocketMatch{}
err := JSONDecode(resp, &match)
if err != nil {
log.Println(err)
continue
}
case "change":
change := CoinbaseWebsocketChange{}
err := JSONDecode(resp, &change)
if err != nil {
log.Println(err)
continue
}
}
}
}
conn.Close()
log.Printf("%s Websocket client disconnected.", c.GetName())
}
}