Skip to content

Commit 083cf06

Browse files
authored
Merge pull request #466 from solidityteam/fix/dex-tvl-query
fix: dex metrics tvl history query
2 parents 1ba8634 + e107792 commit 083cf06

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

indexer/src/kadena-server/repository/infra/repository/dex-metrics-db-repository.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,26 @@ export default class DexMetricsDbRepository implements DexMetricsRepository {
4444

4545
// Get TVL history
4646
const tvlHistoryQuery = `
47-
SELECT
48-
date_trunc('day', pc.timestamp) as timestamp,
49-
SUM(CAST(pc."tvlUsd" AS DECIMAL)) as value
50-
FROM "PoolCharts" pc
51-
JOIN "Pairs" p ON p.id = pc."pairId"
52-
WHERE pc.timestamp BETWEEN $1 AND $2
53-
AND p.address = $3
54-
GROUP BY date_trunc('day', pc.timestamp)
55-
ORDER BY timestamp ASC
47+
SELECT
48+
date_trunc('day', t.timestamp) AS timestamp,
49+
SUM(CAST(t."tvlUsd" AS DECIMAL)) AS value
50+
FROM (
51+
SELECT
52+
pc."pairId",
53+
pc."tvlUsd",
54+
pc."timestamp",
55+
ROW_NUMBER() OVER (
56+
PARTITION BY pc."pairId", date_trunc('day', pc."timestamp")
57+
ORDER BY pc."timestamp" DESC
58+
) as rn
59+
FROM "PoolCharts" pc
60+
JOIN "Pairs" p ON p.id = pc."pairId"
61+
WHERE pc.timestamp BETWEEN $1 AND $2
62+
AND p.address = $3
63+
) t
64+
WHERE t.rn = 1
65+
GROUP BY date_trunc('day', t.timestamp)
66+
ORDER BY timestamp ASC;
5667
`;
5768

5869
const tvlHistory = await sequelize.query(tvlHistoryQuery, {

0 commit comments

Comments
 (0)