-
Notifications
You must be signed in to change notification settings - Fork 126
/
uniswap.go
358 lines (324 loc) · 11.2 KB
/
uniswap.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
package source
import (
"math/big"
"strconv"
"strings"
"time"
"github.com/diadata-org/diadata/pkg/dia/scraper/exchange-scrapers/uniswap"
models "github.com/diadata-org/diadata/pkg/model"
"github.com/diadata-org/diadata/pkg/dia"
"github.com/diadata-org/diadata/pkg/utils"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
type UniswapPair struct {
Token0 dia.Asset
Token1 dia.Asset
ForeignName string
Address common.Address
}
const (
restDial = ""
restDialBSC = ""
restDialPolygon = ""
restDialCelo = ""
restDialFantom = ""
restDialMoonriver = ""
restDialAurora = ""
restDialArbitrum = ""
restDialMetis = ""
restDialAvalanche = ""
restDialTelos = ""
restDialEvmos = "https://evmos-evm.publicnode.com"
restDialAstar = ""
restDialMoonbeam = ""
restDialWanchain = ""
restDialUnrealTestnet = ""
restDialUnreal = ""
uniswapWaitMilliseconds = "25"
sushiswapWaitMilliseconds = "100"
pancakeswapWaitMilliseconds = "100"
dfynWaitMilliseconds = "100"
ubeswapWaitMilliseconds = "200"
spookyswapWaitMilliseconds = "200"
spiritswapWaitMilliseconds = "200"
solarbeamWaitMilliseconds = "200"
trisolarisWaitMilliseconds = "200"
metisWaitMilliseconds = "200"
moonriverWaitMilliseconds = "500"
avalancheWaitMilliseconds = "200"
telosWaitMilliseconds = "400"
evmosWaitMilliseconds = "400"
astarWaitMilliseconds = "1000"
moonbeamWaitMilliseconds = "1000"
wanchainWaitMilliseconds = "1000"
)
type UniswapAssetSource struct {
RestClient *ethclient.Client
relDB *models.RelDB
assetChannel chan dia.Asset
doneChannel chan bool
exchange dia.Exchange
waitTime int
}
var exchangeFactoryContractAddress string
func NewUniswapAssetSource(exchange dia.Exchange, relDB *models.RelDB) (uas *UniswapAssetSource) {
switch exchange.Name {
case dia.UniswapExchange:
uas = makeUniswapAssetSource(exchange, restDial, relDB, uniswapWaitMilliseconds)
case dia.SushiSwapExchange:
uas = makeUniswapAssetSource(exchange, restDial, relDB, sushiswapWaitMilliseconds)
case dia.SushiSwapExchangeArbitrum:
uas = makeUniswapAssetSource(exchange, restDialArbitrum, relDB, sushiswapWaitMilliseconds)
case dia.SushiSwapExchangeFantom:
uas = makeUniswapAssetSource(exchange, restDialFantom, relDB, sushiswapWaitMilliseconds)
case dia.ApeswapExchange:
uas = makeUniswapAssetSource(exchange, restDialBSC, relDB, sushiswapWaitMilliseconds)
case dia.CamelotExchange:
uas = makeUniswapAssetSource(exchange, restDialArbitrum, relDB, sushiswapWaitMilliseconds)
case dia.PanCakeSwap:
uas = makeUniswapAssetSource(exchange, restDialBSC, relDB, pancakeswapWaitMilliseconds)
case dia.DfynNetwork:
uas = makeUniswapAssetSource(exchange, restDialPolygon, relDB, dfynWaitMilliseconds)
case dia.QuickswapExchange:
uas = makeUniswapAssetSource(exchange, restDialPolygon, relDB, dfynWaitMilliseconds)
case dia.UbeswapExchange:
uas = makeUniswapAssetSource(exchange, restDialCelo, relDB, ubeswapWaitMilliseconds)
case dia.SpookyswapExchange:
uas = makeUniswapAssetSource(exchange, restDialFantom, relDB, spookyswapWaitMilliseconds)
case dia.SpiritswapExchange:
uas = makeUniswapAssetSource(exchange, restDialFantom, relDB, spiritswapWaitMilliseconds)
case dia.SolarbeamExchange:
uas = makeUniswapAssetSource(exchange, restDialMoonriver, relDB, solarbeamWaitMilliseconds)
case dia.TrisolarisExchange:
uas = makeUniswapAssetSource(exchange, restDialAurora, relDB, trisolarisWaitMilliseconds)
case dia.NetswapExchange:
uas = makeUniswapAssetSource(exchange, restDialMetis, relDB, metisWaitMilliseconds)
case dia.HuckleberryExchange:
uas = makeUniswapAssetSource(exchange, restDialMoonriver, relDB, moonriverWaitMilliseconds)
case dia.TraderJoeExchange:
uas = makeUniswapAssetSource(exchange, restDialAvalanche, relDB, avalancheWaitMilliseconds)
case dia.PangolinExchange:
uas = makeUniswapAssetSource(exchange, restDialAvalanche, relDB, avalancheWaitMilliseconds)
case dia.TethysExchange:
uas = makeUniswapAssetSource(exchange, restDialMetis, relDB, metisWaitMilliseconds)
case dia.HermesExchange:
uas = makeUniswapAssetSource(exchange, restDialMetis, relDB, metisWaitMilliseconds)
case dia.OmniDexExchange:
uas = makeUniswapAssetSource(exchange, restDialTelos, relDB, telosWaitMilliseconds)
case dia.DiffusionExchange:
uas = makeUniswapAssetSource(exchange, restDialEvmos, relDB, evmosWaitMilliseconds)
case dia.ArthswapExchange:
uas = makeUniswapAssetSource(exchange, restDialAstar, relDB, astarWaitMilliseconds)
case dia.StellaswapExchange:
uas = makeUniswapAssetSource(exchange, restDialMoonbeam, relDB, moonbeamWaitMilliseconds)
case dia.WanswapExchange:
uas = makeUniswapAssetSource(exchange, restDialWanchain, relDB, wanchainWaitMilliseconds)
case dia.PearlfiExchangeTestnet:
uas = makeUniswapAssetSource(exchange, restDialUnrealTestnet, relDB, wanchainWaitMilliseconds)
case dia.PearlfiExchange:
uas = makeUniswapAssetSource(exchange, restDialUnreal, relDB, wanchainWaitMilliseconds)
}
exchangeFactoryContractAddress = exchange.Contract
go func() {
uas.fetchAssets()
}()
return uas
}
// makeUniswapAssetSource returns an asset source as used in NewUniswapAssetSource.
func makeUniswapAssetSource(exchange dia.Exchange, restDial string, relDB *models.RelDB, waitMilliseconds string) *UniswapAssetSource {
var (
restClient *ethclient.Client
err error
assetChannel = make(chan dia.Asset)
doneChannel = make(chan bool)
uas *UniswapAssetSource
)
log.Infof("Init rest client for %s.", exchange.BlockChain.Name)
restClient, err = ethclient.Dial(utils.Getenv(strings.ToUpper(exchange.BlockChain.Name)+"_URI_REST", restDial))
if err != nil {
log.Fatal("init rest client: ", err)
}
var waitTime int
waitTimeString := utils.Getenv(strings.ToUpper(exchange.BlockChain.Name)+"_WAIT_TIME", waitMilliseconds)
waitTime, err = strconv.Atoi(waitTimeString)
if err != nil {
log.Error("could not parse wait time: ", err)
waitTime = 500
}
uas = &UniswapAssetSource{
RestClient: restClient,
relDB: relDB,
assetChannel: assetChannel,
doneChannel: doneChannel,
exchange: exchange,
waitTime: waitTime,
}
return uas
}
func (uas *UniswapAssetSource) Asset() chan dia.Asset {
return uas.assetChannel
}
func (uas *UniswapAssetSource) Done() chan bool {
return uas.doneChannel
}
func (uas *UniswapAssetSource) getNumPairs() (int, error) {
var contract *uniswap.IUniswapV2FactoryCaller
contract, err := uniswap.NewIUniswapV2FactoryCaller(common.HexToAddress(exchangeFactoryContractAddress), uas.RestClient)
if err != nil {
log.Error(err)
}
numPairs, err := contract.AllPairsLength(&bind.CallOpts{})
return int(numPairs.Int64()), err
}
func (uas *UniswapAssetSource) fetchAssets() {
numPairs, err := uas.getNumPairs()
if err != nil {
log.Fatal(err)
}
log.Info("Found ", numPairs, " pairs")
checkMap := make(map[string]struct{})
_, index, err := uas.relDB.GetScraperIndex(uas.exchange.Name, dia.SCRAPER_TYPE_ASSETCOLLECTOR)
if err != nil {
log.Error("GetScraperIndex: ", err)
}
log.Info("Start at index ", index)
for index < int64(numPairs) {
log.Info("index: ", index)
time.Sleep(time.Duration(uas.waitTime) * time.Millisecond)
pair, err := uas.GetPairByID(index)
if err != nil {
log.Errorln("Error getting pair with ID ", index)
}
asset0 := pair.Token0
asset1 := pair.Token1
asset0.Blockchain = uas.exchange.BlockChain.Name
asset1.Blockchain = uas.exchange.BlockChain.Name
// Don't repeat sending already sent assets
if _, ok := checkMap[asset0.Address]; !ok {
if asset0.Symbol != "" {
checkMap[asset0.Address] = struct{}{}
uas.assetChannel <- asset0
}
}
if _, ok := checkMap[asset1.Address]; !ok {
if asset1.Symbol != "" {
checkMap[asset1.Address] = struct{}{}
uas.assetChannel <- asset1
}
}
index++
}
err = uas.relDB.SetScraperIndex(uas.exchange.Name, dia.SCRAPER_TYPE_ASSETCOLLECTOR, dia.INDEX_TYPE_INDEX, index)
if err != nil {
log.Error("SetScraperIndex: ", err)
}
uas.doneChannel <- true
}
// GetPairByID returns the UniswapPair with the integer id @num
func (uas *UniswapAssetSource) GetPairByID(num int64) (UniswapPair, error) {
var contract *uniswap.IUniswapV2FactoryCaller
contract, err := uniswap.NewIUniswapV2FactoryCaller(common.HexToAddress(exchangeFactoryContractAddress), uas.RestClient)
if err != nil {
log.Error(err)
return UniswapPair{}, err
}
numToken := big.NewInt(num)
pairAddress, err := contract.AllPairs(&bind.CallOpts{}, numToken)
if err != nil {
log.Error(err)
return UniswapPair{}, err
}
pair, err := uas.GetPairByAddress(pairAddress)
if err != nil {
log.Error(err)
return UniswapPair{}, err
}
// log.Infof("Get pair with ID %v: %v", num, pair)
return pair, err
}
func (uas *UniswapAssetSource) GetPairByAddress(pairAddress common.Address) (pair UniswapPair, err error) {
connection := uas.RestClient
var pairContract *uniswap.IUniswapV2PairCaller
pairContract, err = uniswap.NewIUniswapV2PairCaller(pairAddress, connection)
if err != nil {
log.Error(err)
return UniswapPair{}, err
}
// Getting tokens from pair ---------------------
address0, _ := pairContract.Token0(&bind.CallOpts{})
address1, _ := pairContract.Token1(&bind.CallOpts{})
var token0Contract *uniswap.IERC20Caller
var token1Contract *uniswap.IERC20Caller
token0Contract, err = uniswap.NewIERC20Caller(address0, connection)
if err != nil {
log.Error(err)
}
token1Contract, err = uniswap.NewIERC20Caller(address1, connection)
if err != nil {
log.Error(err)
}
symbol0, err := token0Contract.Symbol(&bind.CallOpts{})
if err != nil {
log.Error(err)
}
symbol1, err := token1Contract.Symbol(&bind.CallOpts{})
if err != nil {
log.Error(err)
}
decimals0, err := token0Contract.Decimals(&bind.CallOpts{})
if err != nil {
log.Error(err)
}
decimals1, err := token1Contract.Decimals(&bind.CallOpts{})
if err != nil {
log.Error(err)
}
name0, err := uas.GetName(address0)
if err != nil {
log.Error(err)
return UniswapPair{}, err
}
name1, err := uas.GetName(address1)
if err != nil {
log.Error(err)
return UniswapPair{}, err
}
token0 := dia.Asset{
Address: address0.Hex(),
Symbol: symbol0,
Name: name0,
Decimals: decimals0,
}
token1 := dia.Asset{
Address: address1.Hex(),
Symbol: symbol1,
Name: name1,
Decimals: decimals1,
}
pair.Token0 = token0
pair.Token1 = token1
return pair, nil
}
// GetDecimals returns the decimals of the token with address @tokenAddress
func (uas *UniswapAssetSource) GetDecimals(tokenAddress common.Address) (decimals uint8, err error) {
var contract *uniswap.IERC20Caller
contract, err = uniswap.NewIERC20Caller(tokenAddress, uas.RestClient)
if err != nil {
log.Error(err)
return
}
decimals, err = contract.Decimals(&bind.CallOpts{})
return
}
func (uas *UniswapAssetSource) GetName(tokenAddress common.Address) (name string, err error) {
var contract *uniswap.IERC20Caller
contract, err = uniswap.NewIERC20Caller(tokenAddress, uas.RestClient)
if err != nil {
log.Error(err)
return
}
name, err = contract.Name(&bind.CallOpts{})
return
}