-
Notifications
You must be signed in to change notification settings - Fork 129
/
pools.go
502 lines (462 loc) · 13.7 KB
/
pools.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package models
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/diadata-org/diadata/pkg/dia"
clientInfluxdb "github.com/influxdata/influxdb1-client/v2"
"github.com/jackc/pgx/v4"
)
// SavePoolInflux stores a DEX pool in influx.
func (datastore *DB) SavePoolInflux(p dia.Pool) error {
assetvolumesEncoded, err := json.Marshal(p.Assetvolumes)
if err != nil {
log.Error("marshal volumes: ", err)
}
// Create a point and add to batch
tags := map[string]string{
"exchange": p.Exchange.Name,
"blockchain": p.Blockchain.Name,
"address": p.Address,
}
fields := map[string]interface{}{
"volumes": string(assetvolumesEncoded),
}
pt, err := clientInfluxdb.NewPoint(influxDbDEXPoolTable, tags, fields, p.Time)
if err != nil {
log.Errorln("NewTradeInflux:", err)
} else {
datastore.addPoint(pt)
}
err = datastore.WriteBatchInflux()
if err != nil {
log.Errorln("Write influx batch: ", err)
}
return err
}
// GetPoolInflux returns all info/liquidities of pool with @poolAddress in the time-range [starttime, endtime).
func (datastore *DB) GetPoolInflux(poolAddress string, starttime time.Time, endtime time.Time) ([]dia.Pool, error) {
pools := []dia.Pool{}
queryString := "SELECT \"exchange\",\"blockchain\",volumes FROM %s WHERE address='%s' AND time >= %d AND time < %d ORDER BY DESC"
q := fmt.Sprintf(queryString, influxDbDEXPoolTable, poolAddress, starttime.UnixNano(), endtime.UnixNano())
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
return pools, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
for i := 0; i < len(res[0].Series[0].Values); i++ {
var pool dia.Pool
pool.Time, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string))
if err != nil {
return pools, err
}
pool.Exchange.Name = res[0].Series[0].Values[i][1].(string)
if err != nil {
return pools, err
}
pool.Blockchain.Name = res[0].Series[0].Values[i][2].(string)
stat := res[0].Series[0].Values[i][3].(string)
if err := json.Unmarshal([]byte(stat), &pool.Assetvolumes); err != nil {
log.Error("unmarshal: ", err)
}
pool.Address = poolAddress
pools = append(pools, pool)
}
} else {
return pools, errors.New("parsing pool from database")
}
return pools, nil
}
// SetPool writes pool data into pool table and the underlying asset and liquidity data into the poolasset table.
func (rdb *RelDB) SetPool(pool dia.Pool) error {
if len(pool.Assetvolumes) < 2 {
return errors.New("not enough asset data on pool")
}
query0 := fmt.Sprintf(
`INSERT INTO %s (exchange,blockchain,address) VALUES ($1,$2,$3) ON CONFLICT (blockchain,address) DO NOTHING`,
poolTable,
)
_, err := rdb.postgresClient.Exec(
context.Background(),
query0,
pool.Exchange.Name,
pool.Blockchain.Name,
pool.Address,
)
if err != nil {
if !strings.Contains(err.Error(), "duplicate") {
return err
} else {
log.Warn("pool already exists, update liquidity")
}
}
// Add assets and liquidity to the underlying poolasset table.
var query1 string
for i := 0; i < len(pool.Assetvolumes); i++ {
query1 = fmt.Sprintf(
`INSERT INTO %s (pool_id,asset_id,liquidity,liquidity_usd,time_stamp,token_index)
VALUES ((SELECT pool_id from %s where address=$1 and blockchain=$2),(SELECT asset_id from %s where address=$3 and blockchain=$4),$5,$6,$7,$8)
ON CONFLICT (pool_id,asset_id)
DO UPDATE SET liquidity=EXCLUDED.liquidity, liquidity_usd=EXCLUDED.liquidity_usd, time_stamp=EXCLUDED.time_stamp, token_index=EXCLUDED.token_index`,
poolassetTable,
poolTable,
assetTable,
)
_, err := rdb.postgresClient.Exec(
context.Background(),
query1,
pool.Address,
pool.Blockchain.Name,
pool.Assetvolumes[i].Asset.Address,
pool.Assetvolumes[i].Asset.Blockchain,
pool.Assetvolumes[i].Volume,
pool.Assetvolumes[i].VolumeUSD,
pool.Time,
pool.Assetvolumes[i].Index,
)
if err != nil {
return err
}
}
return nil
}
// GetAllDEXPoolsCount returns a map which maps a DEX onto the number of pools on the DEX.
func (rdb *RelDB) GetAllDEXPoolsCount() (map[string]int, error) {
poolsCount := make(map[string]int)
query := fmt.Sprintf("SELECT exchange,COUNT(address) FROM %s GROUP BY exchange", poolTable)
rows, err := rdb.postgresClient.Query(context.Background(), query)
if err != nil {
return poolsCount, err
}
defer rows.Close()
for rows.Next() {
var exchange string
var numPools int
err = rows.Scan(
&exchange,
&numPools,
)
if err != nil {
return poolsCount, err
}
poolsCount[exchange] = numPools
}
return poolsCount, nil
}
// GetPoolByAddress returns the most recent pool data, i.e. liquidity.
func (rdb *RelDB) GetPoolByAddress(blockchain string, address string) (pool dia.Pool, err error) {
var rows pgx.Rows
query := fmt.Sprintf(`
SELECT pa.liquidity,pa.liquidity_usd,a.symbol,a.name,a.address,a.decimals,p.exchange,pa.time_stamp,pa.token_index
FROM %s pa
INNER JOIN %s p
ON p.pool_id=pa.pool_id
INNER JOIN %s a
ON pa.asset_id=a.asset_id
WHERE p.blockchain=$1
AND p.address=$2`,
poolassetTable,
poolTable,
assetTable,
)
rows, err = rdb.postgresClient.Query(context.Background(), query, blockchain, address)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var (
decimals sql.NullInt64
index sql.NullInt64
timestamp sql.NullTime
liquidity sql.NullFloat64
liquidityUSD sql.NullFloat64
assetvolume dia.AssetVolume
)
err = rows.Scan(
&liquidity,
&liquidityUSD,
&assetvolume.Asset.Symbol,
&assetvolume.Asset.Name,
&assetvolume.Asset.Address,
&decimals,
&pool.Exchange.Name,
×tamp,
&index,
)
if err != nil {
return
}
if decimals.Valid {
assetvolume.Asset.Decimals = uint8(decimals.Int64)
}
if index.Valid {
assetvolume.Index = uint8(index.Int64)
}
if timestamp.Valid {
pool.Time = timestamp.Time
}
if liquidity.Valid {
assetvolume.Volume = liquidity.Float64
}
if liquidityUSD.Valid {
assetvolume.VolumeUSD = liquidityUSD.Float64
}
assetvolume.Asset.Blockchain = blockchain
pool.Assetvolumes = append(pool.Assetvolumes, assetvolume)
}
pool.Blockchain.Name = blockchain
pool.Address = address
return
}
// GetAllPoolAddrsExchange returns all pool addresses available for @exchange.
func (rdb *RelDB) GetAllPoolAddrsExchange(exchange string, liquiThreshold float64) (addresses []string, err error) {
var (
rows pgx.Rows
query string
)
if liquiThreshold == float64(0) {
query = fmt.Sprintf("SELECT address FROM %s WHERE exchange=$1", poolTable)
rows, err = rdb.postgresClient.Query(context.Background(), query, exchange)
} else {
query = fmt.Sprintf(`
SELECT DISTINCT p.address
FROM %s p
INNER JOIN %s pa
ON p.pool_id=pa.pool_id
WHERE p.exchange=$1
AND pa.liquidity>=$2
`, poolTable, poolassetTable)
rows, err = rdb.postgresClient.Query(context.Background(), query, exchange, liquiThreshold)
}
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var poolAddr string
err := rows.Scan(&poolAddr)
if err != nil {
log.Error(err)
}
addresses = append(addresses, poolAddr)
}
return
}
// GetAllPoolsExchange returns all pool addresses available for @exchange.
// Remark that it returns each pool n times where n is the number of assets in the pool.
func (rdb *RelDB) GetAllPoolsExchange(exchange string, liquiThreshold float64) (pools []dia.Pool, err error) {
var (
rows pgx.Rows
query string
)
query = fmt.Sprintf(`
SELECT exch_pools.address,a.address,a.blockchain,a.decimals,a.symbol,a.name,pa.token_index,pa.liquidity,pa.liquidity_usd
FROM (
SELECT p.pool_id,p.address, SUM(CASE WHEN pa.liquidity<$1 THEN 1 ELSE 0 END) AS no_liqui
FROM %s p
INNER JOIN %s pa
ON p.pool_id=pa.pool_id
WHERE p.exchange=$2
GROUP BY p.pool_id,p.address
) exch_pools
INNER JOIN %s pa
ON exch_pools.pool_id=pa.pool_id
INNER JOIN %s a
ON pa.asset_id=a.asset_id
WHERE exch_pools.no_liqui=0;
`,
poolTable,
poolassetTable,
poolassetTable,
assetTable,
)
rows, err = rdb.postgresClient.Query(context.Background(), query, liquiThreshold, exchange)
if err != nil {
return
}
defer rows.Close()
poolIndexMap := make(map[string]int)
for rows.Next() {
var (
poolAddress string
av dia.AssetVolume
decimals sql.NullInt64
index sql.NullInt64
liquidity sql.NullFloat64
liquidityUSD sql.NullFloat64
)
err := rows.Scan(
&poolAddress,
&av.Asset.Address,
&av.Asset.Blockchain,
&decimals,
&av.Asset.Symbol,
&av.Asset.Name,
&index,
&liquidity,
&liquidityUSD,
)
if err != nil {
log.Error(err)
}
if decimals.Valid {
av.Asset.Decimals = uint8(decimals.Int64)
}
if index.Valid {
av.Index = uint8(index.Int64)
}
if liquidity.Valid {
av.Volume = liquidity.Float64
}
if liquidityUSD.Valid {
av.VolumeUSD = liquidityUSD.Float64
}
// map poolasset to pool if pool address already exists.
if _, ok := poolIndexMap[poolAddress]; !ok {
// Pool does not exist yet, so initialize.
pool := dia.Pool{Exchange: dia.Exchange{Name: exchange}, Address: poolAddress, Blockchain: dia.BlockChain{Name: av.Asset.Blockchain}}
pool.Assetvolumes = append(pool.Assetvolumes, av)
pools = append(pools, pool)
poolIndexMap[poolAddress] = len(pools) - 1
} else {
// Pool already exists, just add pool asset.
pools[poolIndexMap[poolAddress]].Assetvolumes = append(pools[poolIndexMap[poolAddress]].Assetvolumes, av)
}
}
return
}
// GetPoolsByAsset returns all pools with @asset as a pool asset and both assets have liquidity above @liquiThreshold.
// If @liquidityThresholdUSD>0 AND @liquiThreshold=0, only pools where total liquidity is available
// AND above @liquidityThresholdUSD are returned.
func (rdb *RelDB) GetPoolsByAsset(asset dia.Asset, liquidityThreshold float64, liquidityThresholdUSD float64) ([]dia.Pool, error) {
var (
query string
pools []dia.Pool
)
query = fmt.Sprintf(`
SELECT exch_pools.exchange,exch_pools.address,a.address,a.blockchain,a.decimals,a.symbol,a.name,pa.token_index,pa.liquidity,pa.liquidity_usd,pa.time_stamp
FROM (
SELECT p.exchange,p.pool_id,p.address, SUM(CASE WHEN pa.liquidity>=$1 THEN 0 ELSE 1 END) AS no_liqui, SUM(CASE WHEN a.address=$2 THEN 1 ELSE 0 END) AS correct_asset
FROM %s p
INNER JOIN %s pa
ON p.pool_id=pa.pool_id
INNER JOIN %s a
ON pa.asset_id=a.asset_id
WHERE p.blockchain=$3
GROUP BY p.exchange,p.pool_id,p.address
) exch_pools
INNER JOIN %s pa
ON exch_pools.pool_id=pa.pool_id
INNER JOIN %s a ON pa.asset_id=a.asset_id
WHERE exch_pools.no_liqui=0
AND exch_pools.correct_asset=1
AND pa.time_stamp IS NOT NULL;
`,
poolTable,
poolassetTable,
assetTable,
poolassetTable,
assetTable,
)
rows, err := rdb.postgresClient.Query(context.Background(), query, liquidityThreshold, asset.Address, asset.Blockchain)
if err != nil {
return pools, err
}
defer rows.Close()
poolIndexMap := make(map[string]int)
for rows.Next() {
var (
exchange string
poolAddress string
av dia.AssetVolume
decimals sql.NullInt64
index sql.NullInt64
liquidity sql.NullFloat64
liquidityUSD sql.NullFloat64
timestamp sql.NullTime
)
err := rows.Scan(
&exchange,
&poolAddress,
&av.Asset.Address,
&av.Asset.Blockchain,
&decimals,
&av.Asset.Symbol,
&av.Asset.Name,
&index,
&liquidity,
&liquidityUSD,
×tamp,
)
if err != nil {
log.Error(err)
}
if decimals.Valid {
av.Asset.Decimals = uint8(decimals.Int64)
}
if index.Valid {
av.Index = uint8(index.Int64)
}
if liquidity.Valid {
av.Volume = liquidity.Float64
}
if liquidityUSD.Valid {
av.VolumeUSD = liquidityUSD.Float64
}
// map poolasset to pool if pool address already exists.
if _, ok := poolIndexMap[poolAddress]; !ok {
// Pool does not exist yet, so initialize.
pool := dia.Pool{Exchange: dia.Exchange{Name: exchange}, Address: poolAddress, Blockchain: dia.BlockChain{Name: av.Asset.Blockchain}}
if timestamp.Valid {
pool.Time = timestamp.Time
}
pool.Assetvolumes = append(pool.Assetvolumes, av)
pools = append(pools, pool)
poolIndexMap[poolAddress] = len(pools) - 1
} else {
// Pool already exists, just add pool asset.
pools[poolIndexMap[poolAddress]].Assetvolumes = append(pools[poolIndexMap[poolAddress]].Assetvolumes, av)
}
}
if liquidityThresholdUSD > 0 {
var filteredPools []dia.Pool
for _, pool := range pools {
totalLiquidity, lowerBound := pool.GetPoolLiquidityUSD()
if totalLiquidity > liquidityThresholdUSD && !lowerBound {
filteredPools = append(filteredPools, pool)
}
}
return filteredPools, nil
}
return pools, nil
}
// GetPoolLiquiditiesUSD attempts to fill the field @VolumeUSD by fetching the price
// of the corresponding asset.
// @priceCache acts as a poor man's cache for repeated requests.
func (datastore *DB) GetPoolLiquiditiesUSD(p *dia.Pool, priceCache map[string]float64) {
for i, av := range p.Assetvolumes {
var price float64
// For some pools, for instance on BalancerV2 type contracts, the pool contains itself as an asset.
if av.Asset.Address == p.Address {
log.Warnf("%s: Pool token %s has the same address as pool itself.", p.Exchange.Name, p.Address)
continue
}
if _, ok := priceCache[av.Asset.Identifier()]; !ok {
assetQuotation, err := datastore.GetAssetQuotationLatest(av.Asset, time.Now().Add(-time.Duration(assetQuotationLookbackHours)*time.Hour))
if err != nil {
log.Errorf("GetAssetQuotationLatest on %s with address %s: %v", av.Asset.Blockchain, av.Asset.Address, err)
continue
}
price = assetQuotation.Price
priceCache[av.Asset.Identifier()] = price
} else {
price = priceCache[av.Asset.Identifier()]
}
p.Assetvolumes[i].VolumeUSD = price * p.Assetvolumes[i].Volume
}
}