-
Notifications
You must be signed in to change notification settings - Fork 126
/
pools.go
322 lines (290 loc) · 8.23 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
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)`,
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,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)
ON CONFLICT (pool_id,asset_id)
DO UPDATE SET liquidity=EXCLUDED.liquidity, 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.Time,
pool.Assetvolumes[i].Index,
)
if err != nil {
return err
}
}
return 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,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='%s'
AND p.address='%s'`,
poolassetTable,
poolTable,
assetTable,
blockchain,
address,
)
rows, err = rdb.postgresClient.Query(context.Background(), query)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var (
decimals sql.NullInt64
index sql.NullInt64
assetvolume dia.AssetVolume
timestamp sql.NullTime
)
err = rows.Scan(
&assetvolume.Volume,
&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
}
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='%s'", poolTable, 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='%s'
AND pa.liquidity>=%v
`, poolTable, poolassetTable, exchange, liquiThreshold)
}
rows, err = rdb.postgresClient.Query(context.Background(), query)
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
}
// GetAllPoolAddrsExchange 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
FROM (
SELECT p.pool_id,p.address, SUM(CASE WHEN pa.liquidity<%v 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='%s'
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;
`, liquiThreshold, poolTable, poolassetTable, exchange, poolassetTable, assetTable)
rows, err = rdb.postgresClient.Query(context.Background(), query)
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
)
err := rows.Scan(
&poolAddress,
&av.Asset.Address,
&av.Asset.Blockchain,
&decimals,
&av.Asset.Symbol,
&av.Asset.Name,
&index,
&liquidity,
)
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
}
// 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
}