Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dex dune #15

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion dune-acala-dex/queries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ query_ids:
- 3799539
- 3799558
- 3782346
- 3784244
- 3784244
- 4373515
- 4374073
- 3989007
- 4403783
15 changes: 15 additions & 0 deletions dune-acala-dex/queries/daily_token_prices___3989007.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- part of a query repo
-- query name: daily_token_prices
-- query link: https://dune.com/queries/3989007


SELECT
date_trunc('day', minute) as day,
symbol,
avg(price) as price
FROM prices.usd
WHERE
symbol in ('DOT', 'JITOSOL', 'USDC')
AND date_trunc('day', minute) BETWEEN TIMESTAMP '2022-02-09' AND current_date
GROUP BY 1, 2
ORDER BY 1, 2;
36 changes: 36 additions & 0 deletions dune-acala-dex/queries/dex_latest_stats___4374073.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- part of a query repo
-- query name: dex_latest_stats
-- query link: https://dune.com/queries/4374073


WITH latest_volume AS (
SELECT
date,
volume,
total_volume
FROM query_4373515
ORDER BY date DESC
LIMIT 1
),

latest_tvl AS (
SELECT
date AS tvl_date,
SUM(usd_tvl) AS usd_tvl
FROM query_3782346
WHERE date = (SELECT MAX(date) FROM query_3782346)
GROUP BY date
),

latest_stats AS (
SELECT
A.date,
A.volume,
A.total_volume,
B.usd_tvl
FROM latest_volume A
CROSS JOIN latest_tvl B
)

SELECT *
FROM latest_stats
45 changes: 36 additions & 9 deletions dune-acala-dex/queries/dex_liquidity_tx___3769045.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ liquidity_tx_extracted AS (
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token0_json, '$.erc20') IS NOT NULL THEN '{"ForeignAsset":"14"}'
WHEN JSON_EXTRACT_SCALAR(X.token0_json, '$.erc20') IS NOT NULL THEN (
CONCAT(
'{"Erc20":"',
JSON_EXTRACT_SCALAR(X.token0_json, '$.erc20'),
'"}'
)
)
ELSE '???'
END AS token0_varchar,

CASE
Expand All @@ -73,7 +80,14 @@ liquidity_tx_extracted AS (
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token1_json, '$.erc20') IS NOT NULL THEN '{"ForeignAsset":"14"}'
WHEN JSON_EXTRACT_SCALAR(X.token1_json, '$.erc20') IS NOT NULL THEN (
CONCAT(
'{"Erc20":"',
JSON_EXTRACT_SCALAR(X.token1_json, '$.erc20'),
'"}'
)
)
ELSE '???'
END AS token1_varchar
FROM liquidity_tx_raw X
),
Expand All @@ -84,23 +98,36 @@ liquidity_tx_parsed AS (
B.symbol AS token0,
B.decimals AS decimals0,
C.symbol AS token1,
C.decimals AS decimals1
C.decimals AS decimals1,
CASE
WHEN starts_with(amount0_varchar, '0x')
THEN varbinary_to_uint256(FROM_HEX(amount0_varchar))
ELSE CAST(amount0_varchar as uint256)
END AS amount0_uint256,
CASE
WHEN starts_with(amount1_varchar, '0x')
THEN varbinary_to_uint256(FROM_HEX(amount1_varchar))
ELSE CAST(amount1_varchar as uint256)
END AS amount1_uint256
FROM liquidity_tx_extracted A
JOIN query_3670410 B ON A.token0_varchar = B.asset
JOIN query_3670410 C ON A.token1_varchar = C.asset
LEFT JOIN query_4397191 B -- acala assets
ON A.token0_varchar = B.asset
LEFT JOIN query_4397191 C -- acala assets
ON A.token1_varchar = C.asset
)

SELECT
D.block_time,
D.method,
D.address,
CONCAT(token0, '/', token1) AS pool_name,
D.token0,
CAST(D.amount0_varchar AS DOUBLE) / POWER(10, D.decimals0) AS amount0,
D.token1,
CAST(D.amount1_varchar AS DOUBLE) / POWER(10, D.decimals1) AS amount1,
amount0_uint256 / POWER(10, D.decimals0) AS amount0,
amount1_uint256 / POWER(10, D.decimals1) AS amount1,
D.block_number,
D.extrinsic_hash as tx_hash
FROM liquidity_tx_parsed D
WHERE D.amount0_varchar NOT LIKE '0x%'
AND D.amount1_varchar NOT LIKE '0x%'
-- WHERE D.amount0_varchar NOT LIKE '0x%'
-- AND D.amount1_varchar NOT LIKE '0x%'
ORDER BY 1 DESC
41 changes: 31 additions & 10 deletions dune-acala-dex/queries/dex_swaps___3751506.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ dex_swap_raw_extracted AS (
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token_in_json, '$.erc20') IS NOT NULL THEN '{"ForeignAsset":"14"}'
WHEN JSON_EXTRACT_SCALAR(X.token_in_json, '$.erc20') IS NOT NULL THEN (
CONCAT(
'{"Erc20":"',
JSON_EXTRACT_SCALAR(X.token_in_json, '$.erc20'),
'"}'
)
)
END AS token_in_varchar,

CASE
Expand All @@ -71,7 +77,13 @@ dex_swap_raw_extracted AS (
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token_out_json, '$.erc20') IS NOT NULL THEN '{"ForeignAsset":"14"}'
WHEN JSON_EXTRACT_SCALAR(X.token_out_json, '$.erc20') IS NOT NULL THEN (
CONCAT(
'{"Erc20":"',
JSON_EXTRACT_SCALAR(X.token_out_json, '$.erc20'),
'"}'
)
)
END AS token_out_varchar
FROM dex_swap_raw X
),
Expand All @@ -94,8 +106,10 @@ dex_swap_parsed AS (
C.symbol AS token_out,
C.decimals AS decimals_out
FROM dex_swap_raw_extracted A
JOIN query_3670410 B ON A.token_in_varchar = B.asset
JOIN query_3670410 C ON A.token_out_varchar = C.asset
LEFT JOIN query_4397191 B -- acala assets
ON A.token_in_varchar = B.asset
LEFT JOIN query_4397191 C -- acala assets
ON A.token_out_varchar = C.asset
),

dex_swap_formatted AS (
Expand All @@ -116,20 +130,27 @@ dex_swap_formatted AS (
SELECT
E.block_time,
E.address,
E.token_in,
E.amount_in,
E.token_out,
E.token_in,
E.amount_out,
E.token_out,
CASE
WHEN E.token_in IN ('DOT', 'lcDOT') THEN E.amount_in * dot_price.price
WHEN E.token_in IN ('JITOSOL') THEN E.amount_in * jitosol_price.price
WHEN E.token_in IN ('AUSD', 'USDC') THEN E.amount_in
WHEN E.token_in = 'DOT' THEN E.amount_in * P.price
WHEN E.token_out IN ('DOT', 'lcDOT') THEN E.amount_out * dot_price.price
WHEN E.token_out IN ('JITOSOL') THEN E.amount_out * jitosol_price.price
WHEN E.token_out IN ('AUSD', 'USDC') THEN E.amount_out
WHEN E.token_out = 'DOT' THEN E.amount_out * P.price
ELSE 0
END AS usd_value,
E.block_number,
E.tx_hash
FROM dex_swap_formatted E
LEFT JOIN prices.usd_daily P
ON E.day = P.day AND P.symbol = 'DOT'
LEFT JOIN query_3989007 as dot_price
ON E.day = dot_price.day
AND dot_price.symbol = 'DOT'
LEFT JOIN query_3989007 as jitosol_price
ON E.day = jitosol_price.day
AND jitosol_price.symbol = 'JITOSOL'
WHERE E.day != DATE '2022-08-14'
ORDER BY 1 DESC
27 changes: 27 additions & 0 deletions dune-acala-dex/queries/dex_volume___4373515.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- part of a query repo
-- query name: dex_volume
-- query link: https://dune.com/queries/4373515


WITH daily_volume AS (
SELECT
date_trunc({{interval}}, block_time) as date,
SUM(usd_value) AS volume
FROM query_3751506
GROUP BY 1
),

cumulative_volume AS (
SELECT
date,
volume,
SUM(volume) OVER (ORDER BY date) AS cumulative_volume
FROM daily_volume
)

SELECT
date,
volume,
cumulative_volume as total_volume
FROM cumulative_volume
WHERE date >= date_add('month', -1 * {{show data for how many months:}}, current_date)
5 changes: 3 additions & 2 deletions dune-acala-dex/queries/pol_tvl_ausd_intr___3799562.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@


SELECT
day_timestamp,
date,
token0_tvl AS aseed_tvl,
token1_tvl AS intr_tvl
FROM query_3782346 AS pool_tvl
WHERE pool_name = 'AUSD/INTR'
WHERE pool_name = 'AUSD/INTR'
AND token0_tvl > 0
Loading