From 3099a061dcea607e2b851f1dbd4861c65d7b09c6 Mon Sep 17 00:00:00 2001 From: Philipp Date: Thu, 20 Jun 2024 11:42:10 +0200 Subject: [PATCH] feat: add price change percentage --- .../migration.sql | 21 +++++++++++- src/database/token/token-db.service.ts | 34 ++++++++++++++++--- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/prisma/migrations/20240619141013_add_total_reserve_function/migration.sql b/prisma/migrations/20240619141013_add_total_reserve_function/migration.sql index 5917873..9e18e00 100644 --- a/prisma/migrations/20240619141013_add_total_reserve_function/migration.sql +++ b/prisma/migrations/20240619141013_add_total_reserve_function/migration.sql @@ -1,5 +1,5 @@ CREATE -OR REPLACE FUNCTION total_reserve (integer) RETURNS numeric AS 'SELECT SUM(CASE +OR REPLACE FUNCTION total_reserve (integer, interval) RETURNS numeric AS 'SELECT SUM(CASE WHEN t.id = p.t0 THEN (latest_liquidity_info."reserve0" / POW(10, t.decimals)) ELSE (latest_liquidity_info."reserve1" / POW(10, t.decimals)) END @@ -9,6 +9,8 @@ OR REPLACE FUNCTION total_reserve (integer) RETURNS numeric AS 'SELECT SUM(CASE LEFT JOIN LATERAL (SELECT * FROM "PairLiquidityInfoHistory" WHERE p.id = "pairId" + AND "microBlockTime" <= extract(epoch from NOW() - $2) * 1000 + ORDER BY "microBlockTime" DESC, "logIndex" DESC LIMIT 1) latest_liquidity_info ON TRUE WHERE $1 = t.id' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT; @@ -40,3 +42,20 @@ OR REPLACE FUNCTION volume_usd (integer, interval) RETURNS numeric AS 'SELECT RO LEFT JOIN "PairLiquidityInfoHistory" liquidity_history ON p.id = liquidity_history."pairId" WHERE $1 = t.id' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT; + +CREATE +OR REPLACE FUNCTION historic_price (integer, interval) RETURNS numeric AS 'SELECT SUM(CASE + WHEN t.id = p.t0 THEN (latest_liquidity_info."token0AePrice") * + (latest_liquidity_info."reserve0" / POW(10, t.decimals)) + ELSE (latest_liquidity_info."token1AePrice") * + (latest_liquidity_info."reserve1" / POW(10, t.decimals)) END / + total_reserve(t.id, $2)) + FROM "Token" t + LEFT JOIN public."Pair" p on t.id = p.t0 OR t.id = p.t1 + LEFT JOIN LATERAL (SELECT * + FROM "PairLiquidityInfoHistory" + WHERE p.id = "pairId" + AND "microBlockTime" <= extract(epoch from NOW() - $2) * 1000 + ORDER BY "microBlockTime" DESC, "logIndex" DESC + LIMIT 1) latest_liquidity_info ON TRUE + WHERE $1 = t.id' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT; diff --git a/src/database/token/token-db.service.ts b/src/database/token/token-db.service.ts index 307faf4..f6a112e 100644 --- a/src/database/token/token-db.service.ts +++ b/src/database/token/token-db.service.ts @@ -37,6 +37,10 @@ export class TokenDbService { volumeUsdMonth: number; volumeUsdYear: number; volumeUsdAll: number; + priceChangeDay: number; + priceChangeWeek: number; + priceChangeMonth: number; + priceChangeYear: number; }[] >` SELECT @@ -55,7 +59,7 @@ export class TokenDbService { ELSE (latest_liquidity_info."token1AePrice") * ( latest_liquidity_info."reserve1" / POW (10, t.decimals) ) - END / total_reserve (t.id) + END / total_reserve (t.id, INTERVAL '0 DAY') ) AS "priceAe", ROUND( SUM( @@ -66,7 +70,7 @@ export class TokenDbService { ELSE (latest_liquidity_info."token1AePrice") * ( latest_liquidity_info."reserve1" / POW (10, t.decimals) ) - END * latest_liquidity_info."aeUsdPrice" / total_reserve (t.id) + END * latest_liquidity_info."aeUsdPrice" / total_reserve (t.id, INTERVAL '0 DAY') )::numeric, 4 ) AS "priceUsd", @@ -93,12 +97,32 @@ export class TokenDbService { )::numeric, 4 ) AS "fdvUsd", - total_reserve (t.id) AS "totalReserve", - count(p.id) AS "pairs", + total_reserve (t.id, INTERVAL '0 DAY') AS "totalReserve", + count(p.id)::integer AS "pairs", volume_usd (t.id, INTERVAL '1 DAY') AS "volumeUsdDay", volume_usd (t.id, INTERVAL '1 WEEK') AS "volumeUsdWeek", volume_usd (t.id, INTERVAL '1 YEAR') AS "volumeUsdYear", - volume_usd (t.id, INTERVAL '100 YEAR') AS "volumeUsdAll" + volume_usd (t.id, INTERVAL '100 YEAR') AS "volumeUsdAll", + ( + ( + historic_price (t.id, INTERVAL '0 DAY') - historic_price (t.id, INTERVAL '1 DAY') + ) / historic_price (t.id, INTERVAL '1 DAY') + ) * 100 AS "priceChangeDay", + ( + ( + historic_price (t.id, INTERVAL '0 DAY') - historic_price (t.id, INTERVAL '1 WEEK') + ) / historic_price (t.id, INTERVAL '1 WEEK') + ) * 100 AS "priceChangeWeek", + ( + ( + historic_price (t.id, INTERVAL '0 DAY') - historic_price (t.id, INTERVAL '1 MONTH') + ) / historic_price (t.id, INTERVAL '1 MONTH') + ) * 100 AS "priceChangeMonth", + ( + ( + historic_price (t.id, INTERVAL '0 DAY') - historic_price (t.id, INTERVAL '1 YEAR') + ) / historic_price (t.id, INTERVAL '1 YEAR') + ) * 100 AS "priceChangeYear" FROM "Token" t LEFT JOIN public."Pair" p ON t.id = p.t0