-
Notifications
You must be signed in to change notification settings - Fork 126
/
ZBScraper.go
254 lines (210 loc) · 5.87 KB
/
ZBScraper.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
package scrapers
import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/diadata-org/diadata/pkg/dia"
models "github.com/diadata-org/diadata/pkg/model"
"github.com/diadata-org/diadata/pkg/utils"
ws "github.com/gorilla/websocket"
)
var ZBSocketURL string = "wss://api.zb.live/websocket"
type ZBSubscribe struct {
Event string `json:"event"`
Channel string `json:"channel"`
}
type ZBTradeResponse struct {
DataType string `json:"dataType"`
Data []struct {
Amount string `json:"amount"`
Price string `json:"price"`
Tid int `json:"tid"`
Date int `json:"date"`
Type string `json:"type"`
TradeType string `json:"trade_type"`
} `json:"data"`
Channel string `json:"channel"`
}
type ZBScraper struct {
wsClient *ws.Conn
// signaling channels for session initialization and finishing
//initDone chan nothing
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]*ZBPairScraper
exchangeName string
chanTrades chan *dia.Trade
db *models.RelDB
}
// NewZBScraper returns a new ZBScraper for the given pair
func NewZBScraper(exchange dia.Exchange, scrape bool, relDB *models.RelDB) *ZBScraper {
s := &ZBScraper{
shutdown: make(chan nothing),
shutdownDone: make(chan nothing),
pairScrapers: make(map[string]*ZBPairScraper),
exchangeName: exchange.Name,
error: nil,
chanTrades: make(chan *dia.Trade),
db: relDB,
}
ZBWsURL := utils.Getenv("ZB_WS_URL", ZBSocketURL)
var wsDialer ws.Dialer
SwConn, _, err := wsDialer.Dial(ZBWsURL, nil)
if err != nil {
println(err.Error())
}
s.wsClient = SwConn
if scrape {
go s.mainLoop()
}
return s
}
// runs in a goroutine until s is closed
func (s *ZBScraper) mainLoop() {
for {
message := &ZBTradeResponse{}
if s.error = s.wsClient.ReadJSON(&message); s.error != nil {
log.Error(s.error.Error())
break
}
for _, trade := range message.Data {
var exchangepair dia.ExchangePair
ps, ok := s.pairScrapers[strings.TrimSuffix(message.Channel, "_trades")]
if !ok {
log.Error("unknown pair: " + message.Channel)
continue
}
f64Price, err := strconv.ParseFloat(trade.Price, 64)
if err != nil {
log.Error("error parsing price: " + trade.Price)
continue
}
f64Volume, err := strconv.ParseFloat(trade.Amount, 64)
if err != nil {
log.Error("error parsing volume: " + trade.Price)
continue
}
if trade.Type == "sell" {
f64Volume = -f64Volume
}
exchangepair, err = s.db.GetExchangePairCache(s.exchangeName, strings.TrimSuffix(message.Channel, "_trades"))
if err != nil {
log.Error(err)
}
t := &dia.Trade{
Symbol: ps.Pair().Symbol,
Pair: strings.TrimSuffix(message.Channel, "_trades"),
Price: f64Price,
Volume: f64Volume,
Time: time.Unix(int64(trade.Date), 0),
ForeignTradeID: fmt.Sprint(trade.Tid),
Source: s.exchangeName,
VerifiedPair: exchangepair.Verified,
BaseToken: exchangepair.UnderlyingPair.BaseToken,
QuoteToken: exchangepair.UnderlyingPair.QuoteToken,
}
ps.parent.chanTrades <- t
if exchangepair.Verified {
log.Infoln("Got verified trade: ", t)
}
}
}
s.cleanup(s.error)
}
func (s *ZBScraper) NormalizePair(pair dia.ExchangePair) (dia.ExchangePair, error) {
return dia.ExchangePair{}, nil
}
func (s *ZBScraper) cleanup(err error) {
s.errorLock.Lock()
defer s.errorLock.Unlock()
if err != nil {
s.error = err
}
s.closed = true
close(s.shutdownDone)
}
// FillSymbolData is not used by DEX scrapers.
func (s *ZBScraper) FillSymbolData(symbol string) (dia.Asset, error) {
return dia.Asset{Symbol: symbol}, nil
}
// Close closes any existing API connections, as well as channels of
// PairScrapers from calls to ScrapePair
func (s *ZBScraper) Close() error {
if s.closed {
return errors.New("ZBScraper: Already closed")
}
close(s.shutdown)
err := s.wsClient.Close()
if err != nil {
return err
}
<-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 *ZBScraper) 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("ZBScraper: Call ScrapePair on closed scraper")
}
ps := &ZBPairScraper{
parent: s,
pair: pair,
}
s.pairScrapers[pair.ForeignName] = ps
a := &ZBSubscribe{
Event: "addChannel",
Channel: pair.ForeignName + "_trades",
}
if err := s.wsClient.WriteJSON(a); err != nil {
fmt.Println(err.Error())
}
return ps, nil
}
// FetchAvailablePairs returns a list with all available trade pairs
func (s *ZBScraper) FetchAvailablePairs() (pairs []dia.ExchangePair, err error) {
return []dia.ExchangePair{}, errors.New("FetchAvailablePairs() not implemented")
}
// ZBPairScraper implements PairScraper for ZB
type ZBPairScraper struct {
parent *ZBScraper
pair dia.ExchangePair
closed bool
}
// Close stops listening for trades of the pair associated with s
func (ps *ZBPairScraper) Close() error {
ps.closed = true
return nil
}
// Channel returns a channel that can be used to receive trades
func (ps *ZBScraper) Channel() chan *dia.Trade {
return ps.chanTrades
}
// Error returns an error when the channel Channel() is closed
// and nil otherwise
func (ps *ZBPairScraper) Error() error {
s := ps.parent
s.errorLock.RLock()
defer s.errorLock.RUnlock()
return s.error
}
// Pair returns the pair this scraper is subscribed to
func (ps *ZBPairScraper) Pair() dia.ExchangePair {
return ps.pair
}