-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathAPIScraper.go
323 lines (299 loc) · 14 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
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
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}
ExchangeDuplicates[dia.BKEX2Exchange] = dia.Exchange{Name: "BKEX2", Centralized: true, WatchdogDelay: 1200}
}
// 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.BitstampExchange:
return NewBitstampScraper(Exchanges[dia.BitstampExchange], scrape, relDB)
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.BitfinexExchange:
return NewBitfinexScraper(key, secret, Exchanges[dia.BitfinexExchange], scrape, relDB)
case dia.CoinBaseExchange:
return NewCoinBaseScraper(Exchanges[dia.CoinBaseExchange], scrape, relDB)
case dia.KrakenExchange:
return NewKrakenScraper(key, secret, Exchanges[dia.KrakenExchange], scrape, relDB)
case dia.OKExExchange:
return NewOKExScraper(Exchanges[dia.OKExExchange], scrape, relDB)
case dia.CryptoDotComExchange:
return NewCryptoDotComScraper(Exchanges[dia.CryptoDotComExchange], scrape, relDB)
case dia.HuobiExchange:
return NewHuobiScraper(Exchanges[dia.HuobiExchange], scrape, relDB)
case dia.GateIOExchange:
return NewGateIOScraper(Exchanges[dia.GateIOExchange], scrape, relDB)
case dia.BancorExchange:
return NewBancorScraper(Exchanges[dia.BancorExchange], scrape)
case dia.UniswapExchange:
return NewUniswapScraper(Exchanges[dia.UniswapExchange], scrape, relDB)
case dia.UniswapExchangeBase:
return NewUniswapScraper(Exchanges[dia.UniswapExchangeBase], scrape, relDB)
case dia.PanCakeSwap:
return NewUniswapScraper(Exchanges[dia.PanCakeSwap], scrape, relDB)
case dia.PanCakeSwapExchangeV3:
return NewUniswapV3Scraper(Exchanges[dia.PanCakeSwapExchangeV3], scrape, relDB)
case dia.SushiSwapExchange:
return NewUniswapScraper(Exchanges[dia.SushiSwapExchange], scrape, relDB)
case dia.SushiSwapExchangePolygon:
return NewUniswapScraper(Exchanges[dia.SushiSwapExchangePolygon], scrape, relDB)
case dia.SushiSwapExchangeArbitrum:
return NewUniswapScraper(Exchanges[dia.SushiSwapExchangeArbitrum], scrape, relDB)
case dia.SushiSwapExchangeFantom:
return NewUniswapScraper(Exchanges[dia.SushiSwapExchangeFantom], scrape, relDB)
case dia.CamelotExchange:
return NewUniswapScraper(Exchanges[dia.CamelotExchange], scrape, relDB)
case dia.CamelotExchangeV3:
return NewUniswapV3Scraper(Exchanges[dia.CamelotExchangeV3], scrape, relDB)
case dia.CurveFIExchange:
return NewCurveFIScraper(Exchanges[dia.CurveFIExchange], scrape, relDB)
case dia.CurveFIExchangeFantom:
return NewCurveFIScraper(Exchanges[dia.CurveFIExchangeFantom], scrape, relDB)
case dia.CurveFIExchangeMoonbeam:
return NewCurveFIScraper(Exchanges[dia.CurveFIExchangeMoonbeam], scrape, relDB)
case dia.CurveFIExchangePolygon:
return NewCurveFIScraper(Exchanges[dia.CurveFIExchangePolygon], scrape, relDB)
case dia.CurveFIExchangeArbitrum:
return NewCurveFIScraper(Exchanges[dia.CurveFIExchangeArbitrum], scrape, relDB)
case dia.BalancerV2Exchange:
return NewBalancerV2Scraper(Exchanges[dia.BalancerV2Exchange], scrape, relDB)
case dia.BalancerV2ExchangeArbitrum:
return NewBalancerV2Scraper(Exchanges[dia.BalancerV2ExchangeArbitrum], scrape, relDB)
case dia.BalancerV2ExchangePolygon:
return NewBalancerV2Scraper(Exchanges[dia.BalancerV2ExchangePolygon], scrape, relDB)
case dia.BeetsExchange:
return NewBalancerV2Scraper(Exchanges[dia.BeetsExchange], scrape, relDB)
case dia.KuCoinExchange:
return NewKuCoinScraper(key, secret, Exchanges[dia.KuCoinExchange], scrape, relDB)
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], dia.BKEXExchange, scrape, relDB)
case dia.BKEX2Exchange:
return NewBKEXScraper(Exchanges[dia.BKEXExchange], dia.BKEX2Exchange, scrape, relDB)
case dia.UniswapExchangeV3:
return NewUniswapV3Scraper(Exchanges[dia.UniswapExchangeV3], scrape, relDB)
case dia.UniswapExchangeV3Base:
return NewUniswapV3Scraper(Exchanges[dia.UniswapExchangeV3Base], scrape, relDB)
case dia.DfynNetwork:
return NewUniswapScraper(Exchanges[dia.DfynNetwork], scrape, relDB)
case dia.UbeswapExchange:
return NewUniswapScraper(Exchanges[dia.UbeswapExchange], scrape, relDB)
case dia.NileV1Exchange:
return NewUniswapScraper(Exchanges[dia.NileV1Exchange], scrape, relDB)
case dia.RamsesV1Exchange:
return NewUniswapScraper(Exchanges[dia.RamsesV1Exchange], scrape, relDB)
case dia.NileV2Exchange:
return NewUniswapV3Scraper(Exchanges[dia.NileV2Exchange], scrape, relDB)
case dia.RamsesV2Exchange:
return NewUniswapV3Scraper(Exchanges[dia.RamsesV2Exchange], scrape, relDB)
case dia.UniswapExchangeV3Polygon:
return NewUniswapV3Scraper(Exchanges[dia.UniswapExchangeV3Polygon], scrape, relDB)
case dia.UniswapExchangeV3Arbitrum:
return NewUniswapV3Scraper(Exchanges[dia.UniswapExchangeV3Arbitrum], scrape, relDB)
case dia.HuckleberryExchange:
return NewUniswapScraper(Exchanges[dia.HuckleberryExchange], scrape, relDB)
case dia.TraderJoeExchange:
return NewUniswapScraper(Exchanges[dia.TraderJoeExchange], scrape, relDB)
case dia.PangolinExchange:
return NewUniswapScraper(Exchanges[dia.PangolinExchange], scrape, relDB)
case dia.PlatypusExchange:
return NewPlatypusScraper(Exchanges[dia.PlatypusExchange], scrape, relDB)
case dia.SpookyswapExchange:
return NewUniswapScraper(Exchanges[dia.SpookyswapExchange], scrape, relDB)
case dia.QuickswapExchange:
return NewUniswapScraper(Exchanges[dia.QuickswapExchange], scrape, relDB)
case dia.SpiritswapExchange:
return NewUniswapScraper(Exchanges[dia.SpiritswapExchange], scrape, relDB)
case dia.SolarbeamExchange:
return NewUniswapScraper(Exchanges[dia.SolarbeamExchange], scrape, relDB)
case dia.TrisolarisExchange:
return NewUniswapScraper(Exchanges[dia.TrisolarisExchange], scrape, relDB)
case dia.ByBitExchange:
return NewByBitScraper(Exchanges[dia.ByBitExchange], 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, relDB)
case dia.BitMexExchange:
return NewBitMexScraper(Exchanges[dia.BitMexExchange], scrape, relDB)
case dia.TethysExchange:
return NewUniswapScraper(Exchanges[dia.TethysExchange], scrape, relDB)
case dia.HermesExchange:
return NewUniswapScraper(Exchanges[dia.HermesExchange], scrape, relDB)
case dia.OmniDexExchange:
return NewUniswapScraper(Exchanges[dia.OmniDexExchange], scrape, relDB)
case dia.DiffusionExchange:
return NewUniswapScraper(Exchanges[dia.DiffusionExchange], scrape, relDB)
case dia.ApeswapExchange:
return NewUniswapScraper(Exchanges[dia.ApeswapExchange], scrape, relDB)
case dia.BiswapExchange:
return NewUniswapScraper(Exchanges[dia.BiswapExchange], scrape, relDB)
case dia.ArthswapExchange:
return NewUniswapScraper(Exchanges[dia.ArthswapExchange], scrape, relDB)
case dia.StellaswapExchange:
return NewUniswapScraper(Exchanges[dia.StellaswapExchange], scrape, relDB)
case dia.WanswapExchange:
return NewUniswapScraper(Exchanges[dia.WanswapExchange], scrape, relDB)
case dia.TraderJoeExchangeV2_1:
return NewTraderJoeScraper(Exchanges[dia.TraderJoeExchangeV2_1], scrape, relDB)
case dia.TraderJoeExchangeV2_1Arbitrum:
return NewTraderJoeScraper(Exchanges[dia.TraderJoeExchangeV2_1Arbitrum], scrape, relDB)
case dia.TraderJoeExchangeV2_1Avalanche:
return NewTraderJoeScraper(Exchanges[dia.TraderJoeExchangeV2_1Avalanche], scrape, relDB)
case dia.TraderJoeExchangeV2_1BNB:
return NewTraderJoeScraper(Exchanges[dia.TraderJoeExchangeV2_1BNB], scrape, relDB)
case dia.TraderJoeExchangeV2_2Avalanche:
return NewTraderJoeScraper(Exchanges[dia.TraderJoeExchangeV2_2Avalanche], scrape, relDB)
case dia.OsmosisExchange:
return NewOsmosisScraper(Exchanges[dia.OsmosisExchange], scrape, relDB)
case dia.ZenlinkswapExchange:
return NewZenlinkScraper(Exchanges[dia.ZenlinkswapExchange], scrape)
case dia.ZenlinkswapExchangeBifrostPolkadot:
return NewZenlinkScraper(Exchanges[dia.ZenlinkswapExchangeBifrostPolkadot], scrape)
case dia.VelodromeExchange:
return NewVelodromeScraper(Exchanges[dia.VelodromeExchange], scrape, relDB)
case dia.AerodromeV1Exchange:
return NewVelodromeScraper(Exchanges[dia.AerodromeV1Exchange], scrape, relDB)
case dia.MaverickExchange:
return NewMaverickScraper(Exchanges[dia.MaverickExchange], scrape, relDB)
case dia.PearlfiExchangeTestnet:
return NewUniswapV3Scraper(Exchanges[dia.PearlfiExchangeTestnet], scrape, relDB)
case dia.PearlfiExchange:
return NewUniswapV3Scraper(Exchanges[dia.PearlfiExchange], scrape, relDB)
case dia.PearlfiStableswapExchange:
return NewUniswapScraper(Exchanges[dia.PearlfiStableswapExchange], scrape, relDB)
case dia.ThenaExchange:
return NewUniswapScraper(Exchanges[dia.ThenaExchange], scrape, relDB)
case dia.ThenaV3Exchange:
return NewUniswapV3Scraper(Exchanges[dia.ThenaV3Exchange], scrape, relDB)
// case dia.FinageForex:
// return NewFinageForexScraper(Exchanges[dia.FinageForex], scrape, relDB, key, secret)
case dia.BitgetExchange:
return NewBitgetScraper(Exchanges[dia.BitgetExchange], scrape, relDB)
case dia.AerodromeSlipstreamExchange:
return NewUniswapV3Scraper(Exchanges[dia.AerodromeSlipstreamExchange], scrape, relDB)
case dia.VelodromeSlipstreamExchange:
return NewUniswapV3Scraper(Exchanges[dia.VelodromeSlipstreamExchange], scrape, relDB)
// 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)
case dia.AyinExchange:
return NewAyinScraper(Exchanges[dia.AyinExchange], scrape, relDB)
case dia.BitflowExchange:
return NewBitflowScraper(Exchanges[dia.BitflowExchange], scrape, relDB)
case dia.VelarExchange:
return NewVelarScraper(Exchanges[dia.VelarExchange], scrape, relDB)
case dia.BifrostExchange:
return NewBifrostScraper(Exchanges[dia.BifrostExchange], scrape, relDB)
case dia.HydrationExchange:
return NewHydrationScraper(Exchanges[dia.HydrationExchange], scrape, relDB)
default:
return nil
}
}