-
Notifications
You must be signed in to change notification settings - Fork 127
/
APIScraper.go
279 lines (254 loc) · 11 KB
/
APIScraper.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
package scrapers
import (
"io"
"github.com/diadata-org/diadata/pkg/dia"
models "github.com/diadata-org/diadata/pkg/model"
)
// The collector kills a scraper after @watchdogDelayXXX seconds of inactivity
const (
// TODO use this with test
// watchdogDelay3Mins = 3 * 60
// watchdogDelayShort = 10 * 60
// watchdogDelay = 20 * 60
// watchdogDelayLong = 120 * 60
)
// empty type used for signaling
type nothing struct{}
var (
Exchanges = make(map[string]dia.Exchange)
ExchangeDuplicates = make(map[string]dia.Exchange)
blockchains map[string]dia.BlockChain
chainConfigs map[string]dia.ChainConfig
)
var evmID map[string]string
func init() {
relDB, err := models.NewRelDataStore()
if err != nil {
log.Fatal("get rel datastore: ", err)
}
exchanges, err := relDB.GetAllExchanges()
if err != nil {
log.Fatal("get all exchanges: ", err)
}
for _, exchange := range exchanges {
Exchanges[exchange.Name] = exchange
}
evmID = make(map[string]string)
evmID["137"] = dia.POLYGON
evmID["1"] = dia.ETHEREUM
evmID["250"] = dia.FANTOM
evmID["56"] = dia.BINANCESMARTCHAIN
evmID["43114"] = dia.BINANCESMARTCHAIN
evmID["1284"] = dia.MOONBEAM
evmID["1285"] = dia.MOONRIVER
evmID["42161"] = dia.ARBITRUM
evmID["43114"] = dia.AVALANCHE
chains, err := relDB.GetAllBlockchains(false)
if err != nil {
log.Fatal("get all chains: ", err)
}
blockchains = make(map[string]dia.BlockChain)
for _, chain := range chains {
blockchains[chain.Name] = chain
}
chainconfigurations, err := relDB.GetAllChainConfig()
if err != nil {
log.Fatal("get all chains: ", err)
}
chainConfigs = make(map[string]dia.ChainConfig)
for _, chainconfig := range chainconfigurations {
chainConfigs[chainconfig.ChainID] = chainconfig
}
ExchangeDuplicates[dia.Binance2Exchange] = dia.Exchange{Name: "Binance2", Centralized: true, WatchdogDelay: 300}
}
// APIScraper provides common methods needed to get Trade information from
// exchange APIs.
type APIScraper interface {
io.Closer
// ScrapePair returns a PairScraper that continuously scrapes trades for a
// single pair from this APIScraper
ScrapePair(pair dia.ExchangePair) (PairScraper, error)
// FetchAvailablePairs returns a list with all trading pairs available on
// the exchange associated to the APIScraper. The format is such that it can
// be used by the corr. pairScraper in order to fetch trades.
FetchAvailablePairs() ([]dia.ExchangePair, error)
// FillSymbolData collects information associated to the symbol ticker of an
// asset traded on the exchange associated to the APIScraper.
// Ideally, data is returned as close to original (blockchain) notation as possible.
// This is only needed for CEX. For DEX the trade can be filled.
FillSymbolData(symbol string) (dia.Asset, error)
NormalizePair(pair dia.ExchangePair) (dia.ExchangePair, error)
// Channel returns a channel that can be used to receive trades
Channel() chan *dia.Trade
}
// PairScraper receives trades for a single pc.ExchangePair from a single exchange.
type PairScraper interface {
io.Closer
// Error returns an error when the channel Channel() is closed
// and nil otherwise
Error() error
// Pair returns the pair this scraper is subscribed to
Pair() dia.ExchangePair
}
// NewAPIScraper returns an API scraper for @exchange. If scrape==true it actually does
// scraping. Otherwise can be used for pairdiscovery.
func NewAPIScraper(exchange string, scrape bool, key string, secret string, relDB *models.RelDB) APIScraper {
switch exchange {
case dia.BinanceExchange:
return NewBinanceScraper(key, secret, Exchanges[dia.BinanceExchange], Exchanges[dia.BinanceExchange].Name, scrape, relDB)
case dia.Binance2Exchange:
return NewBinanceScraper(key, secret, Exchanges[dia.BinanceExchange], dia.Binance2Exchange, scrape, relDB)
case dia.BinanceExchangeUS:
return NewBinanceScraperUS(key, secret, Exchanges[dia.BinanceExchangeUS], scrape, relDB)
case dia.BitBayExchange:
return NewBitBayScraper(Exchanges[dia.BitBayExchange], scrape, relDB)
case dia.BitfinexExchange:
return NewBitfinexScraper(key, secret, Exchanges[dia.BitfinexExchange], scrape, relDB)
case dia.BitforexExchange:
return NewBitforexScraper(Exchanges[dia.BitforexExchange], scrape, relDB)
case dia.BittrexExchange:
return NewBittrexScraper(Exchanges[dia.BittrexExchange], scrape, relDB)
case dia.CoinBaseExchange:
return NewCoinBaseScraper(Exchanges[dia.CoinBaseExchange], scrape, relDB)
case dia.CREX24Exchange:
return NewCREX24Scraper(Exchanges[dia.CREX24Exchange], relDB)
case dia.KrakenExchange:
return NewKrakenScraper(key, secret, Exchanges[dia.KrakenExchange], scrape, relDB)
case dia.HitBTCExchange:
return NewHitBTCScraper(Exchanges[dia.HitBTCExchange], scrape, relDB)
case dia.SimexExchange:
return NewSimexScraper(Exchanges[dia.SimexExchange], scrape, relDB)
case dia.OKExExchange:
return NewOKExScraper(Exchanges[dia.OKExExchange], scrape, relDB)
case dia.CryptoDotComExchange:
return NewCryptoDotComScraper(Exchanges[dia.CryptoDotComExchange], scrape, relDB)
case dia.FTXExchange:
return NewFTXScraper(Exchanges[dia.FTXExchange], scrape, relDB)
case dia.HuobiExchange:
return NewHuobiScraper(Exchanges[dia.HuobiExchange], scrape, relDB)
case dia.LBankExchange:
return NewLBankScraper(Exchanges[dia.LBankExchange], scrape, relDB)
case dia.GateIOExchange:
return NewGateIOScraper(Exchanges[dia.GateIOExchange], scrape, relDB)
case dia.ZBExchange:
return NewZBScraper(Exchanges[dia.ZBExchange], scrape, relDB)
case dia.QuoineExchange:
return NewQuoineScraper(Exchanges[dia.QuoineExchange], scrape, relDB)
case dia.BancorExchange:
return NewBancorScraper(Exchanges[dia.BancorExchange], scrape)
case dia.UniswapExchange:
return NewUniswapScraper(Exchanges[dia.UniswapExchange], scrape)
case dia.PanCakeSwap:
return NewUniswapScraper(Exchanges[dia.PanCakeSwap], scrape)
case dia.SushiSwapExchange:
return NewUniswapScraper(Exchanges[dia.SushiSwapExchange], scrape)
case dia.LoopringExchange:
return NewLoopringScraper(Exchanges[dia.LoopringExchange], scrape, relDB)
case dia.CurveFIExchange:
return NewCurveFIScraper(Exchanges[dia.CurveFIExchange], scrape)
case dia.CurveFIExchangeFantom:
return NewCurveFIScraper(Exchanges[dia.CurveFIExchangeFantom], scrape)
case dia.CurveFIExchangeMoonbeam:
return NewCurveFIScraper(Exchanges[dia.CurveFIExchangeMoonbeam], scrape)
case dia.CurveFIExchangePolygon:
return NewCurveFIScraper(Exchanges[dia.CurveFIExchangePolygon], scrape)
case dia.BalancerExchange:
return NewBalancerScraper(Exchanges[dia.BalancerExchange], scrape)
case dia.BalancerV2Exchange:
return NewBalancerV2Scraper(Exchanges[dia.BalancerV2Exchange], scrape)
case dia.BalancerV2ExchangePolygon:
return NewBalancerV2Scraper(Exchanges[dia.BalancerV2ExchangePolygon], scrape)
case dia.BeetsExchange:
return NewBalancerV2Scraper(Exchanges[dia.BeetsExchange], scrape)
case dia.MakerExchange:
return NewMakerScraper(Exchanges[dia.MakerExchange], scrape, relDB)
case dia.KuCoinExchange:
return NewKuCoinScraper(key, secret, Exchanges[dia.KuCoinExchange], scrape, relDB)
case dia.DforceExchange:
return NewDforceScraper(Exchanges[dia.DforceExchange], scrape)
case dia.ZeroxExchange:
return NewZeroxScraper(Exchanges[dia.ZeroxExchange], scrape)
case dia.KyberExchange:
return NewKyberScraper(Exchanges[dia.KyberExchange], scrape)
case dia.BitMartExchange:
return NewBitMartScraper(Exchanges[dia.BitMartExchange], scrape, relDB)
case dia.BitMaxExchange:
return NewBitMaxScraper(Exchanges[dia.BitMaxExchange], scrape, relDB)
case dia.MEXCExchange:
return NewMEXCScraper(Exchanges[dia.MEXCExchange], scrape, relDB)
case dia.BKEXExchange:
return NewBKEXScraper(Exchanges[dia.BKEXExchange], scrape, relDB)
case dia.STEXExchange:
return NewSTEXScraper(Exchanges[dia.STEXExchange], scrape, relDB)
case dia.UniswapExchangeV3:
return NewUniswapV3Scraper(Exchanges[dia.UniswapExchangeV3], scrape)
case dia.DfynNetwork:
return NewUniswapScraper(Exchanges[dia.DfynNetwork], scrape)
case dia.UbeswapExchange:
return NewUniswapScraper(Exchanges[dia.UbeswapExchange], scrape)
case dia.SushiSwapExchangePolygon:
return NewUniswapScraper(Exchanges[dia.SushiSwapExchangePolygon], scrape)
case dia.UniswapExchangeV3Polygon:
return NewUniswapV3Scraper(Exchanges[dia.UniswapExchangeV3Polygon], scrape)
case dia.UniswapExchangeV3Arbitrum:
return NewUniswapV3Scraper(Exchanges[dia.UniswapExchangeV3Arbitrum], scrape)
case dia.HuckleberryExchange:
return NewUniswapScraper(Exchanges[dia.HuckleberryExchange], scrape)
case dia.TraderJoeExchange:
return NewUniswapScraper(Exchanges[dia.TraderJoeExchange], scrape)
case dia.PangolinExchange:
return NewUniswapScraper(Exchanges[dia.PangolinExchange], scrape)
case dia.PlatypusExchange:
return NewPlatypusScraper(Exchanges[dia.PlatypusExchange], scrape)
case dia.SpookyswapExchange:
return NewUniswapScraper(Exchanges[dia.SpookyswapExchange], scrape)
case dia.QuickswapExchange:
return NewUniswapScraper(Exchanges[dia.QuickswapExchange], scrape)
case dia.SpiritswapExchange:
return NewUniswapScraper(Exchanges[dia.SpiritswapExchange], scrape)
case dia.SolarbeamExchange:
return NewUniswapScraper(Exchanges[dia.SolarbeamExchange], scrape)
case dia.TrisolarisExchange:
return NewUniswapScraper(Exchanges[dia.TrisolarisExchange], scrape)
case dia.ByBitExchange:
return NewByBitScraper(Exchanges[dia.ByBitExchange], scrape, relDB)
case dia.SerumExchange:
return NewSerumScraper(Exchanges[dia.SerumExchange], scrape, relDB)
case dia.OrcaExchange:
return NewOrcaScraper(Exchanges[dia.OrcaExchange], scrape)
case dia.AnyswapExchange:
return NewAnyswapScraper(Exchanges[dia.AnyswapExchange], scrape, relDB)
case dia.NetswapExchange:
return NewUniswapScraper(Exchanges[dia.NetswapExchange], scrape)
case dia.BitMexExchange:
return NewBitMexScraper(Exchanges[dia.BitMexExchange], scrape, relDB)
case dia.TethysExchange:
return NewUniswapScraper(Exchanges[dia.TethysExchange], scrape)
case dia.HermesExchange:
return NewUniswapScraper(Exchanges[dia.HermesExchange], scrape)
case dia.OmniDexExchange:
return NewUniswapScraper(Exchanges[dia.OmniDexExchange], scrape)
case dia.DiffusionExchange:
return NewUniswapScraper(Exchanges[dia.DiffusionExchange], scrape)
case dia.ApeswapExchange:
return NewUniswapScraper(Exchanges[dia.ApeswapExchange], scrape)
case dia.BiswapExchange:
return NewUniswapScraper(Exchanges[dia.BiswapExchange], scrape)
case dia.ArthswapExchange:
return NewUniswapScraper(Exchanges[dia.ArthswapExchange], scrape)
case dia.StellaswapExchange:
return NewUniswapScraper(Exchanges[dia.StellaswapExchange], scrape)
case dia.WanswapExchange:
return NewUniswapScraper(Exchanges[dia.WanswapExchange], scrape)
// case dia.FinageForex:
// return NewFinageForexScraper(Exchanges[dia.FinageForex], scrape, relDB, key, secret)
case dia.MultiChain:
return NewBridgeSwapScraper(Exchanges[dia.MultiChain], scrape, relDB)
case "Influx":
return NewInfluxScraper(scrape)
case "UniswapHistory":
return NewUniswapHistoryScraper(Exchanges[dia.UniswapExchange], scrape, relDB)
default:
return nil
}
}