-
Notifications
You must be signed in to change notification settings - Fork 128
/
FinageForexScraper.go
325 lines (275 loc) · 8.3 KB
/
FinageForexScraper.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
package scrapers
import (
"encoding/json"
"errors"
"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"
"github.com/gorilla/websocket"
)
type FinageWSMessage struct {
Action string `json:"action"`
Symbols string `json:"symbols"`
}
type FinageTrade struct {
Symbol string `json:"s"`
PriceAsk float64 `json:"a"`
PriceBid float64 `json:"b"`
VolumeAsk string `json:"dc"`
VolumeBid string `json:"dd"`
Timestamp int64 `json:"t"`
}
// var pairs = "INR/USD,USD/INR,AED/INR, INR/AED,INR/CAD,CAD/INR"
type FinageForexScraper struct {
// signaling channels
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]*FinageForexPairScraper // dia.ExchangePair -> pairScraperSet
ticker *time.Ticker
datastore *models.RelDB
chanTrades chan *dia.Trade
wsConn *websocket.Conn
exchangeName string
apiKey string
}
// SpawnECBScraper returns a new ECBScraper initialized with default values.
// The instance is asynchronously scraping as soon as it is created.
func NewFinageForexScraper(exchange dia.Exchange, scrape bool, relDB *models.RelDB, finageAPIkey string, finageWebsocketKey string) *FinageForexScraper {
var finage = "wss://w29hxx2ndd.finage.ws:8001/?token=" + finageWebsocketKey
c, _, err := websocket.DefaultDialer.Dial(finage, nil)
if err != nil {
log.Fatal("dial:", err)
}
scraper := &FinageForexScraper{
wsConn: c,
shutdown: make(chan nothing),
exchangeName: exchange.Name,
shutdownDone: make(chan nothing),
pairScrapers: make(map[string]*FinageForexPairScraper),
error: nil,
ticker: time.NewTicker(refreshDelay),
chanTrades: make(chan *dia.Trade),
datastore: relDB,
apiKey: finageAPIkey,
}
log.Info("Scraper is built and initiated")
if scrape {
go scraper.mainLoop()
}
return scraper
}
func (scraper *FinageForexScraper) subscribe() error {
pairTosubscribe := ""
pairs, err := scraper.datastore.GetExchangePairSymbols(scraper.exchangeName)
if err != nil {
log.Errorln("Error getting pairs", err)
return err
}
log.Println("Pairs", pairs)
for _, ps := range pairs {
pairTosubscribe = pairTosubscribe + "," + ps.ForeignName
}
log.Infoln("pairTosubscribe", pairTosubscribe)
return scraper.wsConn.WriteJSON(FinageWSMessage{Action: "subscribe", Symbols: pairTosubscribe})
}
// mainLoop runs in a goroutine until channel s is closed.
func (scraper *FinageForexScraper) mainLoop() {
subscribeErr := scraper.subscribe()
if subscribeErr != nil {
log.Error("got error subscribing to scraper ", subscribeErr)
}
log.Infoln("Sunbscribed to all asset pairs")
err := scraper.Update()
if err != nil {
log.Error(err)
}
for {
select {
case <-scraper.shutdown: // user requested shutdown
log.Println("FinageScraper shutting down")
scraper.cleanup(nil)
return
}
}
}
// Update performs a HTTP Get request for the rss feed and decodes the results.
func (scraper *FinageForexScraper) Update() error {
go func() {
for {
_, message, err := scraper.wsConn.ReadMessage()
if err != nil {
//s.subscribe()
log.Println("err", err)
}
var ftrade FinageTrade
err = json.Unmarshal(message, &ftrade)
log.Info("Symbol: ", ftrade.Symbol)
log.Info("PriceAsk: ", ftrade.PriceAsk)
log.Info("PriceBid: ", ftrade.PriceBid)
log.Info("VolumeAsk: ", ftrade.VolumeAsk)
log.Info("VolumeBid: ", ftrade.VolumeBid)
log.Info("Timestamp: ", ftrade.Timestamp)
if err != nil {
log.Errorln("Not a Trade", err)
break
} else {
tradePair, _ := scraper.datastore.GetExchangePairCache(scraper.exchangeName, strings.Replace(ftrade.Symbol, "/", "-", 1))
if ftrade.Symbol != "" {
t := &dia.Trade{
Symbol: strings.Split(ftrade.Symbol, "/")[0],
Pair: strings.Replace(ftrade.Symbol, "/", "-", 1),
Price: ftrade.PriceAsk,
Volume: 1,
BaseToken: tradePair.UnderlyingPair.BaseToken,
QuoteToken: tradePair.UnderlyingPair.QuoteToken,
VerifiedPair: tradePair.Verified,
Time: time.Unix(ftrade.Timestamp/1e3, 0),
Source: scraper.exchangeName,
}
if t.VerifiedPair {
log.Info("got verified trade: ", t)
}
scraper.chanTrades <- t
}
}
}
}()
return nil
}
// closes all connected PairScrapers
// must only be called from mainLoop
func (scraper *FinageForexScraper) cleanup(err error) {
scraper.errorLock.Lock()
defer scraper.errorLock.Unlock()
scraper.ticker.Stop()
if err != nil {
scraper.error = err
}
scraper.closed = true
close(scraper.shutdownDone) // signal that shutdown is complete
}
// Close closes any existing API connections, as well as channels of
// PairScrapers from calls to ScrapePair
func (scraper *FinageForexScraper) Close() error {
if scraper.closed {
return errors.New("FinageForexScraper: Already closed")
}
close(scraper.shutdown)
<-scraper.shutdownDone
scraper.errorLock.RLock()
defer scraper.errorLock.RUnlock()
return scraper.error
}
// ECBPairScraper implements PairScraper for ECB
type FinageForexPairScraper struct {
parent *FinageForexScraper
pair dia.ExchangePair
closed bool
}
// ScrapePair returns a PairScraper that can be used to get trades for a single pair from
// this APIScraper
func (scraper *FinageForexScraper) ScrapePair(pair dia.ExchangePair) (PairScraper, error) {
scraper.errorLock.RLock()
defer scraper.errorLock.RUnlock()
if scraper.error != nil {
return nil, scraper.error
}
if scraper.closed {
return nil, errors.New("ECBScraper: Call ScrapePair on closed scraper")
}
ps := &FinageForexPairScraper{
parent: scraper,
pair: pair,
}
scraper.pairScrapers[pair.Symbol] = ps
return ps, nil
}
// Channel returns a channel that can be used to receive trades/pricing information
func (scraper *FinageForexScraper) Channel() chan *dia.Trade {
return scraper.chanTrades
}
func (pairScraper *FinageForexPairScraper) Close() error {
pairScraper.closed = true
return nil
}
// Error returns an error when the channel Channel() is closed
// and nil otherwise
func (pairScraper *FinageForexPairScraper) Error() error {
scraper := pairScraper.parent
scraper.errorLock.RLock()
defer scraper.errorLock.RUnlock()
return scraper.error
}
// Pair returns the pair this scraper is subscribed to
func (pairScraper *FinageForexPairScraper) Pair() dia.ExchangePair {
return pairScraper.pair
}
type FinageSymbolResponse struct {
Page int `json:"page"`
TotalPage int `json:"totalPage"`
Symbols []struct {
Symbol string `json:"symbol"`
Name string `json:"name"`
} `json:"symbols"`
}
func (scraper *FinageForexScraper) FetchAvailablePairs() (pairs []dia.ExchangePair, err error) {
log.Infoln("Fetching Available Pairs")
var finageurl = "https://api.finage.co.uk/symbol-list/forex?apikey=" + scraper.apiKey + "&page="
var finageSymbolResponse FinageSymbolResponse
data, _, err := utils.GetRequest(finageurl + strconv.Itoa(1))
if err != nil {
return
}
err = json.Unmarshal(data, &finageSymbolResponse)
if err != nil {
return
}
for i := 1; i <= finageSymbolResponse.TotalPage; i++ {
data, _, err := utils.GetRequest(finageurl + strconv.Itoa(i))
if err != nil {
continue
}
err = json.Unmarshal(data, &finageSymbolResponse)
if err != nil {
log.Error("unmarshal pair: ", err)
continue
}
for _, symbol := range finageSymbolResponse.Symbols {
pairToNormalize := dia.ExchangePair{
Symbol: "",
ForeignName: symbol.Symbol,
Exchange: scraper.exchangeName,
}
pair, serr := scraper.NormalizePair(pairToNormalize)
if serr == nil {
pairs = append(pairs, pair)
} else {
log.Error(serr)
}
}
}
return
}
func (scraper *FinageForexScraper) FillSymbolData(symbol string) (asset dia.Asset, err error) {
asset.Symbol = symbol
asset.Blockchain = dia.FIAT
return asset, nil
}
func (scraper *FinageForexScraper) NormalizePair(pair dia.ExchangePair) (dia.ExchangePair, error) {
runes := []rune(pair.ForeignName)
asset0 := runes[0:3]
asset1 := runes[3:6]
pair.ForeignName = string(asset0) + "/" + string(asset1)
pair.Symbol = string(asset0)
pair.Verified = true
return pair, nil
}