Skip to content

Commit

Permalink
feat: add price change percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Jun 20, 2024
1 parent 273ef4d commit 3099a06
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
34 changes: 29 additions & 5 deletions src/database/token/token-db.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export class TokenDbService {
volumeUsdMonth: number;
volumeUsdYear: number;
volumeUsdAll: number;
priceChangeDay: number;
priceChangeWeek: number;
priceChangeMonth: number;
priceChangeYear: number;
}[]
>`
SELECT
Expand All @@ -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(
Expand All @@ -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",
Expand All @@ -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
Expand Down

0 comments on commit 3099a06

Please sign in to comment.