Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
euphrates v2
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Aug 21, 2024
1 parent 177e0ee commit b4367d2
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 62 deletions.
11 changes: 5 additions & 6 deletions queries.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
query_ids:
- 3396975
- 3393781
- 3988562
- 3988572
- 3397059
- 3397041
- 3396989
- 3397026
- 3393722
- 3999276
- 3393781
- 3397026
40 changes: 40 additions & 0 deletions queries/daily_usd_flow___3999276.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- part of a query repo
-- query name: daily_usd_flow
-- query link: https://dune.com/queries/3999276


WITH daily_token_flow AS (
SELECT
DATE_TRUNC('day', "timestamp") AS day,
pool_id,
pool_name,
CASE
WHEN "pool_id" in (0, 1, 2, 3, 4, 5) THEN 'DOT'
WHEN "pool_id" = 6 THEN 'JITOSOL'
ELSE '???'
END as "token_symbol",
SUM(CASE WHEN type = 'stake' THEN token_amount_ui ELSE token_amount_ui * -1 END) AS token_amount,
SUM(CASE WHEN type = 'stake' THEN dot_amount_ui ELSE dot_amount_ui * -1 END) AS dot_amount
FROM query_3988562 /* euphrates tx v2 */
GROUP BY 1, 2, 3
ORDER BY 1, 2
),

daily_usd_flow AS (
SELECT
*,
CASE
WHEN "pool_id" in (0, 1, 2, 3, 4, 5) THEN "price" * "dot_amount"
WHEN "pool_id" = 6 THEN "price" * "token_amount"
ELSE 0
END as "token_usd"
FROM daily_token_flow A
JOIN query_3989007 as B /* daily token price */
ON A.day = B.day
AND A.token_symbol = B.symbol
ORDER BY 1, 2
)

SELECT
*
FROM daily_usd_flow
59 changes: 59 additions & 0 deletions queries/euphrates_pool_stats___3988572.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-- part of a query repo
-- query name: euphrates_pool_stats
-- query link: https://dune.com/queries/3988572


With pool_stats AS (
SELECT
from_iso8601_timestamp("timestamp") as "timestamp",
"pool_id",
"dot_amount",
"token_amount",
"dot_amount_ui",
"token_amount_ui",
CASE
WHEN "pool_id" = 0 THEN 'lcdot_ldot'
WHEN "pool_id" = 1 THEN 'lcdot_tdot'
WHEN "pool_id" = 2 THEN 'dot_ldot'
WHEN "pool_id" = 3 THEN 'dot_tdot'
WHEN "pool_id" = 4 THEN 'dot_starlay'
WHEN "pool_id" = 5 THEN 'ldot_starlay'
WHEN "pool_id" = 6 THEN 'jitosol'
ELSE '???'
END as "pool_name"
FROM dune.euphrates.dataset_euphrates_pool_stats
ORDER by 1
),

daily_pool_stats AS (
SELECT
DATE_TRUNC('day', "timestamp") as "day",
"pool_id",
"pool_name",
CASE
WHEN "pool_id" in (0, 1, 2, 3, 4, 5) THEN 'DOT'
WHEN "pool_id" = 6 THEN 'JITOSOL'
ELSE '???'
END as "token_symbol",
AVG("dot_amount") as "dot_amount",
AVG("token_amount") as "token_amount",
AVG("dot_amount_ui") as "dot_amount_ui",
AVG("token_amount_ui") as "token_amount_ui"
FROM pool_stats
GROUP BY 1, 2, 3
ORDER BY 1, 2
)

SELECT
*,
"price" * "dot_amount_ui" as "dot_usd",
CASE
WHEN "pool_id" in (0, 1, 2, 3, 4, 5) THEN "price" * "dot_amount_ui"
WHEN "pool_id" = 6 THEN "price" * "token_amount_ui"
ELSE 0
END as "token_usd"
FROM daily_pool_stats A
JOIN query_3989007 as B /* daily token price */
ON A.day = B.day
AND A.token_symbol = B.symbol
ORDER BY 1, 2
1 change: 1 addition & 0 deletions queries/euphrates_stake___3393722.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SELECT
WHEN "pool_id" = 3 THEN 'dot_tdot'
WHEN "pool_id" = 4 THEN 'dot_starlay'
WHEN "pool_id" = 5 THEN 'ldot_starlay'
WHEN "pool_id" = 7 THEN 'jitosol'
ELSE '???'
END as "pool_name",
CASE
Expand Down
30 changes: 30 additions & 0 deletions queries/euphrates_txs_v2___3988562.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- part of a query repo
-- query name: euphrates_txs_v2
-- query link: https://dune.com/queries/3988562



SELECT
from_iso8601_timestamp("timestamp") as "timestamp",
"block_number",
"type",
"pool_id",
CASE
WHEN "pool_id" = 0 THEN 'lcdot_ldot'
WHEN "pool_id" = 1 THEN 'lcdot_tdot'
WHEN "pool_id" = 2 THEN 'dot_ldot'
WHEN "pool_id" = 3 THEN 'dot_tdot'
WHEN "pool_id" = 4 THEN 'dot_starlay'
WHEN "pool_id" = 5 THEN 'ldot_starlay'
WHEN "pool_id" = 6 THEN 'jitosol'
ELSE '???'
END as "pool_name",
"share_amount",
"token_amount",
"dot_amount",
"token_amount_ui",
"dot_amount_ui",
"recipient",
"tx_hash"
FROM dune.euphrates.dataset_euphrates_txs_v2
ORDER by 1
65 changes: 41 additions & 24 deletions queries/latest_overall_stats___3397059.sql
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
-- part of a query repo
-- query name: latest_overall_stats
-- query name: latest_euphrates_stats
-- query link: https://dune.com/queries/3397059


SELECT
t1.day_timestamp,
t1.TOTAL as total_value_locked,
t2.total as total_dot_locked,
t2."lcdot_ldot",
t2."lcdot_tdot",
t2."dot_ldot",
t2."dot_tdot",
t2."cumulative_dot" + t2."cumulative_lcdot" + t2."cumulative_ldot" as total_dot_volume,
t3.total_tx_count
FROM
query_3396975 t1 -- total value locked
JOIN
query_3393781 t2 -- total dot locked
ON
t1.day_timestamp = t2.day_timestamp
JOIN
query_3397026 t3 -- transaction & users
ON
t1.day_timestamp = t3.day_timestamp
ORDER BY
t1.day_timestamp DESC
LIMIT 1;
-- somehow LIMIT is not applied before sum
-- so has to seperate the query
WITH latest_pool_stats_7 AS (
SELECT *
FROM query_3988572 AS eps -- euphrates pool stats
ORDER BY 1 DESC
LIMIT 7 -- 7 pools
),

latest_pool_stats AS (
SELECT
SUM(dot_amount_ui) AS total_dot_staked,
SUM(dot_usd) AS dot_tvl,
SUM(token_usd) AS tvl
FROM latest_pool_stats_7
),

latest_cumulative_stats_7 AS (
SELECT *
FROM query_3393781 AS tdl -- total dot locked
ORDER BY 1 DESC
LIMIT 7 -- 7 pools
),

latest_cumulative_stats AS (
SELECT SUM(cumulative_dot_staked) AS cumulative_dot_staked
FROM latest_cumulative_stats_7 -- total dot locked
),

latest_tx_stats AS (
SELECT cumulative_tx_count
FROM query_3397026 -- users and transactions
ORDER BY 1 DESC
LIMIT 1
)

SELECT *
FROM latest_pool_stats A
CROSS JOIN latest_cumulative_stats B
CROSS JOIN latest_tx_stats C
70 changes: 47 additions & 23 deletions queries/total_dot_locked___3393781.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,54 @@
-- query link: https://dune.com/queries/3393781


SELECT
dot_tvl."day_timestamp",
dot_tvl."lcdot_ldot",
dot_tvl."lcdot_tdot",
dot_tvl."dot_ldot",
dot_tvl."dot_tdot",
dot_tvl."dot_starlay",
dot_tvl."ldot_starlay",
dot_tvl."total",
dot_tvl."lcdot_ldot" + dot_tvl."lcdot_tdot" AS "lcdot",
dot_tvl."dot_ldot" + dot_tvl."dot_tdot" + dot_tvl."dot_starlay" AS "dot",
dot_tvl."ldot_starlay" AS "ldot",
dot_tvl."lcdot_ldot_volume" + dot_tvl."lcdot_tdot_volume" AS "cumulative_lcdot",
dot_tvl."dot_ldot_volume" + dot_tvl."dot_tdot_volume" + dot_tvl."dot_idot_volume" AS "cumulative_dot",
dot_tvl."ldot_ildot_volume" AS "cumulative_ldot"
from (
WITH daily_staked AS (
SELECT
DATE_TRUNC('day', "timestamp") AS day,
pool_id,
pool_name,
SUM(CASE WHEN type = 'stake' THEN dot_amount_ui ELSE dot_amount_ui * -1 END) AS dot_staked
FROM query_3988562 /* euphrates tx v2 */
GROUP BY 1, 2, 3
ORDER BY 1, 2
),

all_pools AS (
SELECT DISTINCT
day,
pool_id,
pool_name
FROM
(SELECT DISTINCT day FROM daily_staked) AS days,
(SELECT DISTINCT pool_id, pool_name FROM daily_staked) AS pools
),

/* need to fill all the blanks to make sure every pool has a record every day */
/* otherwise cumulatvie sum will entounter null for some pools and return bad result */
daily_staked_filled AS (
SELECT
eds."day_timestamp",
SUM((eds."stake_pool_0" - eds."unstake_pool_0")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "lcdot_ldot",
SUM((eds."stake_pool_1" - eds."unstake_pool_1")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "lcdot_tdot",
SUM((eds."stake_pool_2" - eds."unstake_pool_2")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "dot_ldot",
SUM((eds."stake_pool_3" - eds."unstake_pool_3")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "dot_tdot",
SUM((eds."stake_pool_4" - eds."unstake_pool_4")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "dot_starlay",
SUM((eds."stake_pool_5" - eds."unstake_pool_5")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "ldot_starlay",
ap.day,
ap.pool_id,
ap.pool_name,
COALESCE(ds.dot_staked, 0) AS dot_staked
FROM
all_pools ap
LEFT JOIN
daily_staked ds
ON ap.day = ds.day AND ap.pool_id = ds.pool_id AND ap.pool_name = ds.pool_name
ORDER BY ap.pool_id, ap.day
)

SELECT
day,
pool_id,
pool_name,
dot_staked,
SUM(CASE WHEN dot_staked > 0 THEN dot_staked ELSE 0 END)
OVER (PARTITION BY pool_id ORDER BY day ASC) AS cumulative_dot_staked
FROM daily_staked_filled
ORDER BY pool_id, day;

SUM((eds."stake_pool_5" - eds."unstake_pool_5")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "ldot_starlay",

SUM((eds."stake_pool_0")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "lcdot_ldot_volume",
SUM((eds."stake_pool_1")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "lcdot_tdot_volume",
Expand Down
37 changes: 28 additions & 9 deletions queries/users_and_transactions___3397026.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@
-- query link: https://dune.com/queries/3397026


SELECT
eds."day_timestamp",
eds."daily_tx_count",
SUM(eds."daily_tx_count") OVER (ORDER BY eds."day_timestamp" ASC) AS "total_tx_count",
eds."daily_users",
SUM(eds."daily_users") OVER (ORDER BY eds."day_timestamp" ASC) AS "cumulative_users"
FROM dune.euphrates.result_euphrates_daily_stake AS eds
ORDER BY eds."day_timestamp" ASC
LIMIT 1000
WITH daily_stats AS (
SELECT
DATE_TRUNC('day', "timestamp") as day,
COUNT(*) AS daily_tx_count,
COUNT(DISTINCT "recipient") AS daily_users
FROM query_3988562 /* euphrates tx v2 */
GROUP BY 1
),

cumulative_stats AS (
SELECT
day,
SUM(daily_tx_count) OVER (ORDER BY day) AS cumulative_tx_count,
SUM(daily_users) OVER (ORDER BY day) AS cumulative_users
FROM daily_stats
)
SELECT
ds.day,
ds.daily_tx_count,
cs.cumulative_tx_count,
ds.daily_users,
cs.cumulative_users
FROM
daily_stats ds
JOIN
cumulative_stats cs ON ds.day = cs.day
ORDER BY
ds.day ASC;

0 comments on commit b4367d2

Please sign in to comment.