forked from tensam/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdaxwebsocket.go
182 lines (161 loc) · 4.29 KB
/
gdaxwebsocket.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 (
GDAX_WEBSOCKET_URL = "wss://ws-feed.exchange.gdax.com"
)
type GDAXWebsocketSubscribe struct {
Type string `json:"type"`
ProductID string `json:"product_id"`
}
type GDAXWebsocketReceived 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 GDAXWebsocketOpen 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 GDAXWebsocketDone 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 GDAXWebsocketMatch 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 GDAXWebsocketChange 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 (g *GDAX) WebsocketSubscribe(product string, conn *websocket.Conn) error {
subscribe := GDAXWebsocketSubscribe{"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 (g *GDAX) WebsocketClient() {
for g.Enabled && g.Websocket {
var Dialer websocket.Dialer
conn, _, err := Dialer.Dial(GDAX_WEBSOCKET_URL, http.Header{})
if err != nil {
log.Printf("%s Unable to connect to Websocket. Error: %s\n", g.GetName(), err)
continue
}
log.Printf("%s Connected to Websocket.\n", g.GetName())
currencies := []string{}
for _, x := range g.EnabledPairs {
currency := x[0:3] + "-" + x[3:]
currencies = append(currencies, currency)
}
for _, x := range currencies {
err = g.WebsocketSubscribe(x, conn)
if err != nil {
log.Printf("%s Websocket subscription error: %s\n", g.GetName(), err)
continue
}
}
if g.Verbose {
log.Printf("%s Subscribed to product messages.", g.GetName())
}
for g.Enabled && g.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 := GDAXWebsocketReceived{}
err := JSONDecode(resp, &received)
if err != nil {
log.Println(err)
continue
}
case "open":
open := GDAXWebsocketOpen{}
err := JSONDecode(resp, &open)
if err != nil {
log.Println(err)
continue
}
case "done":
done := GDAXWebsocketDone{}
err := JSONDecode(resp, &done)
if err != nil {
log.Println(err)
continue
}
case "match":
match := GDAXWebsocketMatch{}
err := JSONDecode(resp, &match)
if err != nil {
log.Println(err)
continue
}
case "change":
change := GDAXWebsocketChange{}
err := JSONDecode(resp, &change)
if err != nil {
log.Println(err)
continue
}
}
}
}
conn.Close()
log.Printf("%s Websocket client disconnected.", g.GetName())
}
}