forked from toorop/go-bittrex
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathws.go
230 lines (195 loc) · 4.84 KB
/
ws.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
package bittrex
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/shopspring/decimal"
"github.com/thebotguys/signalr"
)
type OrderUpdate struct {
Orderb
Type int
}
type Fill struct {
Orderb
OrderType string
Timestamp jTime
}
// ExchangeState contains fills and order book updates for a market.
type ExchangeState struct {
MarketName string
Nounce int
Buys []OrderUpdate
Sells []OrderUpdate
Fills []Fill
Initial bool
}
type SummaryState struct {
MarketName string
High decimal.Decimal
Low decimal.Decimal
Last decimal.Decimal
Volume decimal.Decimal
BaseVolume decimal.Decimal
Bid decimal.Decimal
Ask decimal.Decimal
OpenBuyOrders int
OpenSellOrders int
TimeStamp string
Created string
}
type ExchangeDelta struct {
Nounce uint64
Deltas []SummaryState
}
// doAsyncTimeout runs f in a different goroutine
// if f returns before timeout elapses, doAsyncTimeout returns the result of f().
// otherwise it returns "operation timeout" error, and calls tmFunc after f returns.
func doAsyncTimeout(f func() error, tmFunc func(error), timeout time.Duration) error {
errs := make(chan error)
go func() {
err := f()
select {
case errs <- err:
default:
if tmFunc != nil {
tmFunc(err)
}
}
}()
select {
case err := <-errs:
return err
case <-time.After(timeout):
return errors.New("operation timeout")
}
}
func subForMarkets(client *signalr.Client, markets ...string) ([]ExchangeState, error) {
var result []ExchangeState
for _, market := range markets {
_, err := client.CallHub(WS_HUB, "SubscribeToExchangeDeltas", market)
if err != nil {
return nil, err
}
msg, err := client.CallHub(WS_HUB, "QueryExchangeState", market)
if err != nil {
return nil, err
}
var st ExchangeState
if err = json.Unmarshal(msg, &st); err != nil {
return nil, err
}
st.Initial = true
st.MarketName = market
result = append(result, st)
}
return result, nil
}
func parseStates(messages []json.RawMessage, dataCh chan<- ExchangeState, markets ...string) {
all := make(map[string]struct{})
for _, market := range markets {
all[market] = struct{}{}
}
for _, msg := range messages {
var st ExchangeState
if err := json.Unmarshal(msg, &st); err != nil {
continue
}
if _, found := all[st.MarketName]; !found {
continue
}
dataCh <- st
}
}
func parseDeltas(messages []json.RawMessage, dataCh chan<- SummaryState, markets ...string) error {
all := make(map[string]struct{})
for _, market := range markets {
all[market] = struct{}{}
}
for _, msg := range messages {
var d ExchangeDelta
if err := json.Unmarshal(msg, &d); err != nil {
return err
}
for _, v := range d.Deltas {
if _, ok := all[v.MarketName]; ok {
dataCh <- v
}
}
}
return nil
}
func (b *Bittrex) SubscribeSummaryUpdate(dataCh chan<- SummaryState, stop <-chan bool, markets ...string) error {
const timeout = 5 * time.Second
client := signalr.NewWebsocketClient()
client.OnClientMethod = func(hub string, method string, messages []json.RawMessage) {
if hub != WS_HUB || method != "updateSummaryState" {
return
}
parseDeltas(messages, dataCh, markets...)
}
connect := func() error { return client.Connect("https", WS_BASE, []string{WS_HUB}) }
handleErr := func(err error) {
if err == nil {
client.Close()
} else {
//should maybe panic or something here?
fmt.Println(err.Error())
}
}
if err := doAsyncTimeout(connect, handleErr, timeout); err != nil {
return err
}
defer client.Close()
select {
case <-stop:
case <-client.DisconnectedChannel:
}
return nil
}
// SubscribeMarkets subscribes for updates of the markets.
// Updates will be sent to dataCh.
// To stop subscription, send to, or close 'stop'.
func (b *Bittrex) SubscribeMarkets(dataCh chan<- ExchangeState, stop <-chan bool, markets ...string) error {
const timeout = 5 * time.Second
client := signalr.NewWebsocketClient()
client.OnClientMethod = func(hub string, method string, messages []json.RawMessage) {
if hub != WS_HUB || method != "updateExchangeState" {
return
}
parseStates(messages, dataCh, markets...)
}
connect := func() error { return client.Connect("https", WS_BASE, []string{WS_HUB}) }
handleErr := func(err error) {
if err == nil {
client.Close()
} else {
//should maybe panic or something here?
fmt.Println(err.Error())
}
}
err := doAsyncTimeout(connect, handleErr, timeout)
if err != nil {
return err
}
defer client.Close()
var initStates []ExchangeState
subForStates := func() error {
var err error
initStates, err = subForMarkets(client, markets...)
return err
}
err = doAsyncTimeout(subForStates, nil, timeout)
if err != nil {
return err
}
for _, st := range initStates {
dataCh <- st
}
select {
case <-stop:
case <-client.DisconnectedChannel:
}
return nil
}