/
BitstampScraper.go
321 lines (283 loc) · 8.18 KB
/
BitstampScraper.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
package scrapers
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/diadata-org/diadata/pkg/dia"
models "github.com/diadata-org/diadata/pkg/model"
ws "github.com/gorilla/websocket"
"github.com/zekroTJA/timedmap"
)
const ()
type BitstampScraper struct {
wsClient *ws.Conn
shutdown chan nothing
shutdownDone chan nothing
// error handling; to read error or closed, first acquire read lock
// only cleanup method should hold write lock
errorLock sync.RWMutex
error error
closed bool
pairScrapers map[string]*BitstampPairScraper
exchangeName string
chanTrades chan *dia.Trade
db *models.RelDB
}
type BitstampPairScraper struct {
parent *BitstampScraper
pair dia.ExchangePair
closed bool
}
type BitstampPairsInfo []struct {
Name string `json:"name"`
UrlSymbol string `json:"url_symbol"`
BaseDecimal uint8 `json:"base_decimal"`
CounterDecimals uint8 `json:"counter_decimals"`
InstantOrderCounterDecimals uint8 `json:"instant_order_counter_decimals"`
MinimumOrder string `json:"minimum_order"`
Trading string `json:"trading"`
InstantAndMarketOrders string `json:"instant_and_market_orders"`
Description string `json:"description"`
}
type BitstampWsResponse struct {
Event string `json:"event"`
Channel string `json:"channel"`
Data interface{} `json:"data"`
}
type BitstampPingData struct {
Status string `json:"status"`
}
type BitstampTradeData struct {
Id string `json:"id"`
Amount float64 `json:"amount"`
AmountStr string `json:"amount_str"`
Price float64 `json:"price"`
PriceStr string `json:"price_str"`
Type uint8 `json:"type"`
Timestamp string `json:"timestamp"`
Microtimestamp string `json:"microtimestamp"`
BuyOrderId uint64 `json:"buy_order_id"`
SellOrderId uint64 `json:"sell_order_id"`
}
func NewBitstampScraper(exchange dia.Exchange, scrape bool, relDB *models.RelDB) *BitstampScraper {
s := &BitstampScraper{
shutdown: make(chan nothing),
shutdownDone: make(chan nothing),
error: nil,
pairScrapers: make(map[string]*BitstampPairScraper),
exchangeName: exchange.Name,
chanTrades: make(chan *dia.Trade),
db: relDB,
}
var wsDialer ws.Dialer
wsConn, _, err := wsDialer.Dial("wss://ws.bitstamp.net", nil)
if err != nil {
log.Error("Websocket connect error", err)
}
s.wsClient = wsConn
if scrape {
go s.mainLoop()
}
return s
}
func extractUrlSymbolFromChannel(channel string) (after string) {
// Channel is live_trades_{pair}
// Remove the prefix live_trades_
if strings.HasPrefix(channel, "live_trades_") {
after = strings.Split(channel, "live_trades_")[1]
}
return after
}
func (s *BitstampScraper) foreignNameToUrlSymbol(foreignName string) string {
return strings.ToLower(strings.ReplaceAll(foreignName, "-", ""))
}
func (s *BitstampScraper) receive() {
tmFalseDuplicateTrades := timedmap.New(duplicateTradesScanFrequency)
tmDuplicateTrades := timedmap.New(duplicateTradesScanFrequency)
var resp BitstampWsResponse
if err := s.wsClient.ReadJSON(&resp); err != nil {
log.Error("Receive message error:", err)
return
}
foreignName := extractUrlSymbolFromChannel(resp.Channel)
switch event := resp.Event; event {
case "bts:subscription_succeeded":
log.Info("Subsription succeeded:", foreignName)
// TODO: add subscription failed...
// Probably "bts:subscription_failed"
// Just a guess
case "bts:request_reconnect":
// TODO: handle reconnect
log.Warn("Server request for a reconnect")
case "bts:heartbeat":
var data BitstampPingData
if err := json.Unmarshal([]byte(resp.Data.(string)), &data); err != nil {
log.Warn("Unmarshal ping error:", err, resp.Data)
} else if data.Status == "success" {
log.Info("Heart is beating")
} else {
log.Warning("Check Heart", data)
}
default:
if strings.HasPrefix(event, "trade") {
data := resp.Data.(map[string]interface{})
ps, ok := s.pairScrapers[foreignName]
if ok {
timestamp, _ := strconv.ParseInt(data["microtimestamp"].(string), 10, 64)
volume := data["amount"].(float64)
side := data["type"].(float64)
if side == 1 {
volume *= -1
}
pair, err := s.db.GetExchangePairCache(s.exchangeName, foreignName)
if err != nil {
log.Error("get exchange pair from cache: ", err)
}
t := &dia.Trade{
Symbol: ps.pair.Symbol,
Pair: foreignName,
Price: data["price"].(float64),
Volume: volume,
Time: time.Unix(0, 1000*timestamp),
Source: s.exchangeName,
ForeignTradeID: fmt.Sprintf("%d", int(data["id"].(float64))),
VerifiedPair: pair.Verified,
QuoteToken: pair.UnderlyingPair.QuoteToken,
BaseToken: pair.UnderlyingPair.BaseToken,
}
// Handle duplicate trades.
discardTrade := t.IdentifyDuplicateFull(tmFalseDuplicateTrades, duplicateTradesMemory)
if !discardTrade {
t.IdentifyDuplicateTagset(tmDuplicateTrades, duplicateTradesMemory)
s.chanTrades <- t
}
log.Info("Found trade:", t)
}
} else {
log.Warnf("Unidentified response event %s: -- %v", event, resp)
}
}
}
func (s *BitstampScraper) mainLoop() {
for {
select {
case <-s.shutdown:
log.Warn("Shutting down BitstampScraper")
s.cleanup(nil)
return
default:
}
s.receive()
}
}
func (s *BitstampScraper) ScrapePair(pair dia.ExchangePair) (PairScraper, error) {
s.errorLock.RLock()
defer s.errorLock.RUnlock()
if s.error != nil {
return nil, s.error
}
if s.closed {
return nil, errors.New("BitstampScraper: Call ScrapePair on closed scraper")
}
ps := &BitstampPairScraper{
parent: s,
pair: pair,
}
s.pairScrapers[pair.ForeignName] = ps
// Subscribe to pair
urlSymbol := s.foreignNameToUrlSymbol(pair.ForeignName)
message := map[string]interface{}{
"event": "bts:subscribe",
"data": map[string]interface{}{
"channel": "live_trades_" + urlSymbol,
},
}
err := s.wsClient.WriteJSON(message)
if err != nil {
log.Error("Error sending subscription for", ps.pair.ForeignName, err)
}
log.Info("Sent subscription for:", ps.pair.ForeignName)
return ps, nil
}
func (s *BitstampScraper) FetchAvailablePairs() (pairs []dia.ExchangePair, err error) {
var bitstampPairsInfo BitstampPairsInfo
resp, err := http.Get("https://www.bitstamp.net/api/v2/trading-pairs-info/")
if err != nil {
log.Error("Get Pairs:", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error("Read pair body:", err)
}
err = json.Unmarshal(body, &bitstampPairsInfo)
if err != nil {
log.Error("Unmarshal pairs:", err)
}
for _, p := range bitstampPairsInfo {
pairToNormalized := dia.ExchangePair{
Symbol: strings.Split(p.Name, "/")[0],
ForeignName: p.UrlSymbol,
Exchange: s.exchangeName,
UnderlyingPair: dia.Pair{
QuoteToken: dia.Asset{
Symbol: strings.Split(p.Name, "/")[0],
},
BaseToken: dia.Asset{
Symbol: strings.Split(p.Name, "/")[1],
},
},
}
pairs = append(pairs, pairToNormalized)
}
return
}
func (s *BitstampScraper) FillSymbolData(symbol string) (dia.Asset, error) {
return dia.Asset{Symbol: symbol}, nil
}
func (s *BitstampScraper) NormalizePair(pair dia.ExchangePair) (dia.ExchangePair, error) {
return dia.ExchangePair{}, nil
}
func (s *BitstampScraper) Channel() chan *dia.Trade {
return s.chanTrades
}
func (s *BitstampScraper) cleanup(err error) {
s.errorLock.Lock()
defer s.errorLock.Unlock()
if err != nil {
s.error = err
}
s.closed = true
close(s.shutdownDone)
}
func (s *BitstampScraper) Close() error {
if s.closed {
return errors.New("BitstampScraper: Already closed")
}
if err := s.wsClient.Close(); err != nil {
log.Error("Error closing Bitstamp.wsClient", err)
}
close(s.shutdown)
<-s.shutdownDone
defer s.errorLock.RUnlock()
return s.error
}
func (ps *BitstampPairScraper) Close() error {
ps.closed = true
return nil
}
func (ps *BitstampPairScraper) Error() error {
s := ps.parent
s.errorLock.RLock()
defer s.errorLock.RUnlock()
return s.error
}
func (ps *BitstampPairScraper) Pair() dia.ExchangePair {
return ps.pair
}