Skip to content

Commit

Permalink
upload two missed queries for acala dex
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Nov 26, 2024
1 parent 4fd2aa6 commit ccbbe10
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dune-acala-dex/queries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ query_ids:
- 3799524
- 3799554
- 3799539
- 3799558
- 3799558
- 3782346
- 3784244
53 changes: 53 additions & 0 deletions dune-acala-dex/queries/admin_tx___3784244.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
-- part of a query repo
-- query name: admin_tx
-- query link: https://dune.com/queries/3784244



SELECT
CAST('2022-09-09 16:35:24' AS timestamp) as block_time,
1824006 as block_number,
'DOT' as token0,
'lcDOT' as token1,
CAST(66150.33536632179 as DOUBLE) as net_amount0,
CAST(-123851.06801624346 as DOUBLE) as net_amount1
UNION
SELECT
CAST('2022-09-09 16:35:24' AS timestamp) as block_time,
1824006 as block_number,
'AUSD' as token0,
'lcDOT' as token1,
CAST(-7686363.05765165 as DOUBLE) as net_amount0,
CAST(146791.8226388255 as DOUBLE) as net_amount1
UNION
SELECT
CAST('2022-09-09 16:35:24' AS timestamp) as block_time,
1824006 as block_number,
'AUSD' as token0,
'LDOT' as token1,
CAST(-3077826.0616341555 as DOUBLE) as net_amount0,
CAST(570163.9324303765 as DOUBLE) as net_amount1
UNION
SELECT
CAST('2022-09-09 16:35:24' AS timestamp) as block_time,
1824006 as block_number,
'AUSD' as token0,
'IBTC' as token1,
CAST(-13721002.917952676 as DOUBLE) as net_amount0,
CAST(2.51345675 as DOUBLE) as net_amount1
UNION
SELECT
CAST('2022-09-09 16:35:24' AS timestamp) as block_time,
1824006 as block_number,
'AUSD' as token0,
'INTR' as token1,
CAST(-1107986.6931162149 as DOUBLE) as net_amount0,
CAST(999285.1400314877 as DOUBLE) as net_amount1
UNION
SELECT
CAST('2022-09-09 16:35:24' AS timestamp) as block_time,
1824006 as block_number,
'ACA' as token0,
'AUSD' as token1,
CAST(2851872.756224419 as DOUBLE) as net_amount0,
CAST(-8138304.374647936 as DOUBLE) as net_amount1
87 changes: 87 additions & 0 deletions dune-acala-dex/queries/pool_tvl___3782346.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
-- part of a query repo
-- query name: pool_tvl
-- query link: https://dune.com/queries/3782346


WITH liquidity_txs AS (
SELECT
block_time,
block_number,
token0,
token1,
amount0 * CASE WHEN method = 'AddLiquidity' THEN 1 ELSE -1 END AS net_amount0,
amount1 * CASE WHEN method = 'AddLiquidity' THEN 1 ELSE -1 END AS net_amount1
FROM query_3769045 -- dex liquidity tx
),

provision_txs AS (
SELECT
block_time,
block_number,
token0,
token1,
amount0 AS net_amount0, -- provision only increases liquidity
amount1 AS net_amount1
FROM query_3782192 -- add provision tx
),

dex_txs AS (
SELECT
A.block_time,
A.block_number,
B.token0,
B.token1,
CASE
WHEN A.token_in = B.token0 THEN A.amount_in
WHEN A.token_out = B.token0 THEN -A.amount_out
ELSE 0
END AS net_amount0,
CASE
WHEN A.token_in = B.token1 THEN A.amount_in
WHEN A.token_out = B.token1 THEN -A.amount_out
ELSE 0
END AS net_amount1
FROM query_3787671 A -- dex_swap splitted tx
JOIN (
SELECT DISTINCT token0, token1
FROM query_3769045 -- dex liquidity tx
UNION
SELECT DISTINCT token0, token1
FROM query_3782192 -- add provision tx
) B
ON (A.token_in = B.token0 AND A.token_out = B.token1)
OR (A.token_in = B.token1 AND A.token_out = B.token0)
),

all_txs AS (
SELECT * FROM liquidity_txs
UNION ALL
SELECT * FROM provision_txs
UNION ALL
SELECT * FROM dex_txs
UNION ALL
SELECT * FROM query_3784244 AS admin_txs
),

pool_tvl AS (
SELECT
block_time,
block_number,
token0,
token1,
CONCAT(token0, '/', token1) AS pool_name,
SUM(net_amount0) OVER (PARTITION BY token0, token1 ORDER BY block_time) AS token0_tvl,
SUM(net_amount1) OVER (PARTITION BY token0, token1 ORDER BY block_time) AS token1_tvl
FROM all_txs
)
SELECT
DATE_TRUNC('day', block_time) AS day_timestamp,
pool_name,
AVG(token0_tvl) AS token0_tvl,
AVG(token1_tvl) AS token1_tvl
-- AVG(usd_value) AS usd_tvl
FROM pool_tvl
-- WHERE pool_name = {{pool_name}}
GROUP BY 1, pool_name
ORDER BY 1

0 comments on commit ccbbe10

Please sign in to comment.