-
Notifications
You must be signed in to change notification settings - Fork 126
/
BitBayScraper.go
392 lines (333 loc) · 9.04 KB
/
BitBayScraper.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package scrapers
import (
"encoding/json"
"errors"
"strconv"
"strings"
"sync"
"time"
ws "github.com/gorilla/websocket"
"github.com/diadata-org/diadata/pkg/dia"
models "github.com/diadata-org/diadata/pkg/model"
"github.com/diadata-org/diadata/pkg/utils"
)
var BitBaySocketURL string = "wss://api.zonda.exchange/websocket/"
type BitBaySubscribe struct {
Action string `json:"action"`
Module string `json:"module"`
Path string `json:"path"`
}
//{
//"action": "subscribe-public",
//"module": "trading",
//"path": "ticker/{market_code}"
//}
//TradeInfo as received from API response
type BitBayTrade struct {
Date int64 `json:"date"`
Price float64 `json:"price"`
TransactionType string `json:"type"`
Amount float64 `json:"amount"`
Tid string `json:"tid"`
}
// BitBayScraper provides methods needed to get Trade information from BitBay
type BitBayScraper struct {
// control flag for main loop
run bool
wsClient *ws.Conn
// signaling channels for session initialization and finishing
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
// used to keep track of trading pairs that we subscribed to
pairScrapers map[string]*BitBayPairScraper
// exchange name
exchangeName string
// channel to send trades
chanTrades chan *dia.Trade
db *models.RelDB
}
//NewBitBayScraper get a scrapper for BitBay exchange
func NewBitBayScraper(exchange dia.Exchange, scrape bool, relDB *models.RelDB) *BitBayScraper {
s := &BitBayScraper{
shutdown: make(chan nothing),
shutdownDone: make(chan nothing),
pairScrapers: make(map[string]*BitBayPairScraper),
exchangeName: exchange.Name,
error: nil,
chanTrades: make(chan *dia.Trade),
closed: false,
db: relDB,
}
var wsDialer ws.Dialer
SwConn, _, err := wsDialer.Dial(BitBaySocketURL, nil)
if err != nil {
println(err.Error())
}
s.wsClient = SwConn
if scrape {
go s.mainLoop()
}
return s
}
func (s *BitBayScraper) getMarkets() (markets []string) {
var bbm BitBayMarkets
b, _, err := utils.GetRequest("https://api.zonda.exchange/rest/trading/ticker")
if err != nil {
log.Errorln("Error Getting markets", err)
}
err = json.Unmarshal(b, &bbm)
if err != nil {
log.Error("getting markets: ", err)
}
for key := range bbm.Items {
markets = append(markets, key)
}
return
}
func (s *BitBayScraper) ping() {
a := &BitBaySubscribe{
Action: "ping",
}
log.Infoln("Ping: ", a.Action)
if err := s.wsClient.WriteJSON(a); err != nil {
log.Println(err.Error())
}
}
func (s *BitBayScraper) subscribe() {
markets := s.getMarkets()
for _, market := range markets {
a := &BitBaySubscribe{
Action: "subscribe-public",
Module: "trading",
Path: "transactions/" + market,
}
log.Println("subscribing", a)
if err := s.wsClient.WriteJSON(a); err != nil {
log.Println(err.Error())
}
}
}
// runs in a goroutine until s is closed
func (s *BitBayScraper) mainLoop() {
s.subscribe()
pingTimer := time.NewTicker(10 * time.Second)
go func() {
for range pingTimer.C {
go s.ping()
}
}()
for {
var response BitBayWSResponse
if s.error = s.wsClient.ReadJSON(&response); s.error != nil {
log.Error("ws connection error: ", s.error.Error())
s.subscribe()
}
//b,_ := json.Marshal(message)
//
//log.Infoln("Message",string(b[:]))
if len(response.Message.Transactions) == 0 {
log.Warn("empty message - continue")
continue
}
timestamp, err := strconv.ParseInt(response.Timestamp, 10, 64)
if err != nil {
log.Error("Error Parsing time", err)
}
pair := strings.TrimPrefix(response.Topic, "trading/transactions/")
if response.Topic == "" {
log.Warn("empty response - continue.")
continue
}
pair = strings.Replace(pair, "-", "", -1)
pair = strings.ToUpper(pair)
ps, ok := s.pairScrapers[pair]
if !ok {
log.Error("unknown pair: " + pair)
continue
}
for _, trade := range response.Message.Transactions {
var exchangepair dia.ExchangePair
f64Price, err := strconv.ParseFloat(trade.R, 64)
if err != nil {
log.Error("error parsing price: " + trade.R)
continue
}
f64Volume, err := strconv.ParseFloat(trade.A, 64)
if err != nil {
log.Error("error parsing volume: " + trade.A)
continue
}
if trade.Ty == "Sell" {
f64Volume = -f64Volume
}
exchangepair, err = s.db.GetExchangePairCache(s.exchangeName, pair)
if err != nil {
log.Error(err)
}
t := &dia.Trade{
Symbol: ps.Pair().Symbol,
Pair: pair,
Price: f64Price,
Volume: f64Volume,
Time: time.Unix(timestamp/1e3, 0),
ForeignTradeID: trade.ID,
Source: s.exchangeName,
VerifiedPair: exchangepair.Verified,
BaseToken: exchangepair.UnderlyingPair.BaseToken,
QuoteToken: exchangepair.UnderlyingPair.QuoteToken,
}
if exchangepair.Verified {
log.Infoln("Got verified trade", t)
}
ps.parent.chanTrades <- t
}
}
if s.error == nil {
s.error = errors.New(s.exchangeName + "Scraper: terminated by Close()")
}
s.cleanup(s.error)
}
// Close channels for shutdown
func (s *BitBayScraper) cleanup(err error) {
s.errorLock.Lock()
defer s.errorLock.Unlock()
if err != nil {
s.error = err
}
s.closed = true
close(s.chanTrades)
close(s.shutdownDone)
}
// Close any existing API connections, as well as channels, and terminates main loop
func (s *BitBayScraper) Close() error {
if s.closed {
return errors.New(s.exchangeName + "Scraper: Already closed")
}
s.run = false
<-s.shutdownDone
s.errorLock.RLock()
defer s.errorLock.RUnlock()
return s.error
}
// ScrapePair returns a PairScraper that can be used to get trades for a single pair from
// this APIScraper
func (s *BitBayScraper) ScrapePair(pair dia.ExchangePair) (PairScraper, error) {
if s.closed {
return nil, errors.New("s.exchangeName+Scraper: Call ScrapePair on closed scraper")
}
ps := &BitBayPairScraper{
parent: s,
pair: pair,
apiEndPoint: pair.ForeignName,
latestTrade: 0,
}
s.pairScrapers[pair.ForeignName] = ps
return ps, nil
}
func (s *BitBayScraper) NormalizePair(pair dia.ExchangePair) (dia.ExchangePair, error) {
return dia.ExchangePair{}, nil
}
//Channel returns the channel to get trades
func (s *BitBayScraper) Channel() chan *dia.Trade {
return s.chanTrades
}
func (s *BitBayScraper) FillSymbolData(symbol string) (dia.Asset, error) {
// TO DO
return dia.Asset{Symbol: symbol}, nil
}
//FetchAvailablePairs returns a list with all available trade pairs
func (s *BitBayScraper) FetchAvailablePairs() (pairs []dia.ExchangePair, err error) {
type items struct {
Status string `json:"status"`
Markets map[string]interface{} `json:"items"`
}
var bitbayResponse items
data, _, err := utils.GetRequest("https://api.zonda.exchange/rest/trading/ticker")
if err != nil {
return
}
err = json.Unmarshal(data, &bitbayResponse)
if err != nil {
return
}
pairmap := bitbayResponse.Markets
for key := range pairmap {
pairslice := strings.Split(key, "-")
pairs = append(pairs, dia.ExchangePair{
Symbol: pairslice[0],
ForeignName: pairslice[0] + pairslice[1],
Exchange: s.exchangeName,
})
}
return pairs, err
}
// Error returns an error when the channel Channel() is closed
// and nil otherwise
func (s *BitBayScraper) Error() error {
s.errorLock.RLock()
defer s.errorLock.RUnlock()
return s.error
}
// BitBayPairScraper implements PairScraper for BitBay
type BitBayPairScraper struct {
apiEndPoint string
parent *BitBayScraper
pair dia.ExchangePair
closed bool
latestTrade int
}
// Close stops listening for trades of the pair associated
func (ps *BitBayPairScraper) Close() error {
ps.closed = true
return ps.Error()
}
// Error returns an error when the channel Channel() is closed
// and nil otherwise
func (ps *BitBayPairScraper) Error() error {
ps.parent.errorLock.RLock()
defer ps.parent.errorLock.RUnlock()
return ps.parent.error
}
// Pair returns the pair this scraper is subscribed to
func (ps *BitBayPairScraper) Pair() dia.ExchangePair {
return ps.pair
}
type BitBayMarkets struct {
Status string `json:"status"`
Items map[string]BitBayMarket `json:"items"`
}
type BitBayMarket struct {
Code string `json:"code"`
First struct {
Currency string `json:"currency"`
MinOffer string `json:"minOffer"`
Scale int `json:"scale"`
} `json:"first"`
Second struct {
Currency string `json:"currency"`
MinOffer string `json:"minOffer"`
Scale int `json:"scale"`
} `json:"second"`
}
type BitBayWSResponse struct {
Action string `json:"action"`
Message Message `json:"message"`
SeqNo int `json:"seqNo"`
Timestamp string `json:"timestamp"`
Topic string `json:"topic"`
}
type BitBayTransaction struct {
A string `json:"a"`
ID string `json:"id"`
R string `json:"r"`
T string `json:"t"`
Ty string `json:"ty"`
}
type Message struct {
Transactions []BitBayTransaction `json:"transactions"`
}