Skip to content

Commit

Permalink
Merge pull request #9 from AcalaNetwork/migrate-dune-queries
Browse files Browse the repository at this point in the history
migrate dune queries
  • Loading branch information
shunjizhan authored Aug 21, 2024
2 parents d682409 + 7387b5b commit 9a6ccff
Show file tree
Hide file tree
Showing 50 changed files with 1,539 additions and 3 deletions.
4 changes: 4 additions & 0 deletions dune-acala-dex/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#copy and paste this file into a .env file to run scripts locally. You will also need to add the DUNE_API_KEY into the repo settings under "Secrets and Variables"

#add a dune API key - you can create one under team settings (https://dune.com/settings/teams/manage/{team_name}/api). You must be on the premium plan.
DUNE_API_KEY=
2 changes: 2 additions & 0 deletions dune-acala-dex/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.env
6 changes: 6 additions & 0 deletions dune-acala-dex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Acala Dex Dune Dashboard

Quries for [Acala Stats](https://dune.com/euphrates/acala-dex) dune dashboard

## Commands
pull latest queries:`python3 scripts/pull_from_dune.py`
12 changes: 12 additions & 0 deletions dune-acala-dex/queries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query_ids:
- 3751506
- 3769045
- 3799555
- 3799550
- 3799556
- 3782192
- 3799562
- 3799524
- 3799554
- 3799539
- 3799558
106 changes: 106 additions & 0 deletions dune-acala-dex/queries/dex_liquidity_tx___3769045.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
-- part of a query repo
-- query name: dex_liquidity_tx
-- query link: https://dune.com/queries/3769045


WITH liquidity_tx_raw AS (
SELECT
block_time,
block_number,
method,
block_hash,
extrinsic_id,
extrinsic_hash,
event_id,
JSON_EXTRACT_SCALAR(data, '$[0]') AS address,
JSON_EXTRACT(data, '$[1]') AS token0_json,
CAST(JSON_EXTRACT(data, '$[2]') AS VARCHAR) AS amount0_varchar,
JSON_EXTRACT(data, '$[3]') AS token1_json,
CAST(JSON_EXTRACT(data, '$[4]') AS VARCHAR) AS amount1_varchar,
CAST(JSON_EXTRACT(data, '$[5]') AS VARCHAR) AS share_diff_varchar
FROM acala.events
WHERE section = 'dex'
AND (method = 'AddLiquidity' OR method = 'RemoveLiquidity')
),

liquidity_tx_extracted AS (
SELECT
*,
CASE
WHEN JSON_EXTRACT_SCALAR(X.token0_json, '$.token') IS NOT NULL THEN (
CONCAT(
'{"Token":"',
JSON_EXTRACT_SCALAR(X.token0_json, '$.token'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token0_json, '$.liquidCrowdloan') IS NOT NULL THEN (
CONCAT(
'{"LiquidCrowdloan":"',
JSON_EXTRACT_SCALAR(X.token0_json, '$.liquidCrowdloan'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token0_json, '$.foreignAsset') IS NOT NULL THEN (
CONCAT(
'{"ForeignAsset":"',
JSON_EXTRACT_SCALAR(X.token0_json, '$.foreignAsset'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token0_json, '$.erc20') IS NOT NULL THEN '{"ForeignAsset":"14"}'
END AS token0_varchar,

CASE
WHEN JSON_EXTRACT_SCALAR(X.token1_json, '$.token') IS NOT NULL THEN (
CONCAT(
'{"Token":"',
JSON_EXTRACT_SCALAR(X.token1_json, '$.token'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token1_json, '$.liquidCrowdloan') IS NOT NULL THEN (
CONCAT(
'{"LiquidCrowdloan":"',
JSON_EXTRACT_SCALAR(X.token1_json, '$.liquidCrowdloan'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token1_json, '$.foreignAsset') IS NOT NULL THEN (
CONCAT(
'{"ForeignAsset":"',
JSON_EXTRACT_SCALAR(X.token1_json, '$.foreignAsset'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token1_json, '$.erc20') IS NOT NULL THEN '{"ForeignAsset":"14"}'
END AS token1_varchar
FROM liquidity_tx_raw X
),

liquidity_tx_parsed AS (
SELECT
*,
B.symbol AS token0,
B.decimals AS decimals0,
C.symbol AS token1,
C.decimals AS decimals1
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
)

SELECT
D.block_time,
D.method,
D.address,
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,
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%'
ORDER BY 1 DESC
135 changes: 135 additions & 0 deletions dune-acala-dex/queries/dex_swaps___3751506.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
-- part of a query repo
-- query name: dex_swaps
-- query link: https://dune.com/queries/3751506


WITH dex_swap_raw AS (
SELECT
block_time,
block_number,
block_hash,
extrinsic_id,
extrinsic_hash,
event_id,
JSON_EXTRACT_SCALAR(data, '$[0]') AS address,
JSON_EXTRACT(data, '$[1][0]') AS token_in_json,
JSON_ARRAY_GET(JSON_EXTRACT(data, '$[1]'), -1) AS token_out_json,
JSON_VALUE(data, 'strict $[2][0]') AS amount_in_varchar,
JSON_VALUE(data, 'strict $[2][last]') AS amount_out_varchar
FROM acala.events
WHERE section = 'dex'
AND method = 'Swap'
),

dex_swap_raw_extracted AS (
SELECT
*,
CASE
WHEN JSON_EXTRACT_SCALAR(X.token_in_json, '$.token') IS NOT NULL THEN (
CONCAT(
'{"Token":"',
JSON_EXTRACT_SCALAR(X.token_in_json, '$.token'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token_in_json, '$.liquidCrowdloan') IS NOT NULL THEN (
CONCAT(
'{"LiquidCrowdloan":"',
JSON_EXTRACT_SCALAR(X.token_in_json, '$.liquidCrowdloan'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token_in_json, '$.foreignAsset') IS NOT NULL THEN (
CONCAT(
'{"ForeignAsset":"',
JSON_EXTRACT_SCALAR(X.token_in_json, '$.foreignAsset'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token_in_json, '$.erc20') IS NOT NULL THEN '{"ForeignAsset":"14"}'
END AS token_in_varchar,

CASE
WHEN JSON_EXTRACT_SCALAR(X.token_out_json, '$.token') IS NOT NULL THEN (
CONCAT(
'{"Token":"',
JSON_EXTRACT_SCALAR(X.token_out_json, '$.token'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token_out_json, '$.liquidCrowdloan') IS NOT NULL THEN (
CONCAT(
'{"LiquidCrowdloan":"',
JSON_EXTRACT_SCALAR(X.token_out_json, '$.liquidCrowdloan'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token_out_json, '$.foreignAsset') IS NOT NULL THEN (
CONCAT(
'{"ForeignAsset":"',
JSON_EXTRACT_SCALAR(X.token_out_json, '$.foreignAsset'),
'"}'
)
)
WHEN JSON_EXTRACT_SCALAR(X.token_out_json, '$.erc20') IS NOT NULL THEN '{"ForeignAsset":"14"}'
END AS token_out_varchar
FROM dex_swap_raw X
),

dex_swap_parsed AS (
SELECT
*,
CASE
WHEN starts_with(amount_in_varchar, '0x')
THEN varbinary_to_uint256(FROM_HEX(amount_in_varchar))
ELSE CAST(amount_in_varchar as uint256)
END AS amount_in_uint256,
CASE
WHEN starts_with(amount_out_varchar, '0x')
THEN varbinary_to_uint256(FROM_HEX(amount_out_varchar))
ELSE CAST(amount_out_varchar as uint256)
END AS amount_out_uint256 ,
B.symbol AS token_in,
B.decimals AS decimals_in,
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
),

dex_swap_formatted AS (
SELECT
D.block_time,
D.address,
D.amount_in_uint256 / POWER(10, D.decimals_in) AS amount_in,
D.token_in,
D.amount_out_uint256 / POWER(10, D.decimals_out) AS amount_out,
D.token_out,
D.block_number,
D.extrinsic_hash as tx_hash,
DATE_TRUNC('day', D.block_time) AS day
FROM dex_swap_parsed D
)


SELECT
E.block_time,
E.address,
E.token_in,
E.amount_in,
E.token_out,
E.amount_out,
CASE
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 ('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'
ORDER BY 1 DESC
11 changes: 11 additions & 0 deletions dune-acala-dex/queries/pol_tvl_ausd_intr___3799562.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- part of a query repo
-- query name: pol_tvl_ausd_intr
-- query link: https://dune.com/queries/3799562


SELECT
day_timestamp,
token0_tvl AS aseed_tvl,
token1_tvl AS intr_tvl
FROM query_3782346 AS pool_tvl
WHERE pool_name = 'AUSD/INTR'
11 changes: 11 additions & 0 deletions dune-acala-dex/queries/pool_tvl_aca_ausd___3799554.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- part of a query repo
-- query name: pool_tvl_aca_ausd
-- query link: https://dune.com/queries/3799554


SELECT
day_timestamp,
token0_tvl AS aca_tvl,
token1_tvl AS aseed_tvl
FROM query_3782346 AS pool_tvl
WHERE pool_name = 'ACA/AUSD'
12 changes: 12 additions & 0 deletions dune-acala-dex/queries/pool_tvl_aca_usdc___3799539.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- part of a query repo
-- query name: pool_tvl_aca_usdc
-- query link: https://dune.com/queries/3799539


SELECT
day_timestamp,
token0_tvl AS aca_tvl,
token1_tvl AS usdc_tvl
-- usd_value AS usd_tvl
FROM query_3782346 AS pool_tvl
WHERE pool_name = 'ACA/USDC'
11 changes: 11 additions & 0 deletions dune-acala-dex/queries/pool_tvl_ausd_ibtc___3799558.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- part of a query repo
-- query name: pool_tvl_ausd_ibtc
-- query link: https://dune.com/queries/3799558


SELECT
day_timestamp,
token0_tvl AS aseed_tvl,
token1_tvl AS ibtc_tvl
FROM query_3782346 AS pool_tvl
WHERE pool_name = 'AUSD/IBTC'
11 changes: 11 additions & 0 deletions dune-acala-dex/queries/pool_tvl_ausd_lcdot___3799550.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- part of a query repo
-- query name: pool_tvl_ausd_lcdot
-- query link: https://dune.com/queries/3799550


SELECT
day_timestamp,
token0_tvl AS aseed_tvl,
token1_tvl AS lcdot_tvl
FROM query_3782346 AS pool_tvl
WHERE pool_name = 'AUSD/lcDOT'
11 changes: 11 additions & 0 deletions dune-acala-dex/queries/pool_tvl_ausd_ldot___3799555.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- part of a query repo
-- query name: pool_tvl_ausd_ldot
-- query link: https://dune.com/queries/3799555


SELECT
day_timestamp,
token0_tvl AS aseed_tvl,
token1_tvl AS ldot_tvl
FROM query_3782346 AS pool_tvl
WHERE pool_name = 'AUSD/LDOT'
11 changes: 11 additions & 0 deletions dune-acala-dex/queries/pool_tvl_dot_lcdot___3799524.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- part of a query repo
-- query name: pool_tvl_dot_lcdot
-- query link: https://dune.com/queries/3799524


SELECT
day_timestamp,
token0_tvl AS dot_tvl,
token1_tvl AS lcdot_tvl
FROM query_3782346 AS pool_tvl
WHERE pool_name = 'DOT/lcDOT'
11 changes: 11 additions & 0 deletions dune-acala-dex/queries/pool_tvl_dot_unq___3799556.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- part of a query repo
-- query name: pool_tvl_dot_unq
-- query link: https://dune.com/queries/3799556


SELECT
day_timestamp,
token0_tvl AS dot_tvl,
token1_tvl AS unq_tvl
FROM query_3782346 AS pool_tvl
WHERE pool_name = 'DOT/UNQ'
Loading

0 comments on commit 9a6ccff

Please sign in to comment.