diff --git a/.env.test b/.env.test index b443d66..5f62ae7 100644 --- a/.env.test +++ b/.env.test @@ -1,7 +1,7 @@ #copy and paste this file into a .env file. 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= +DUNE_API_KEY= #generate a github token, giving the actions, workflows, and content permissions (read and write). Create this using this link (https://github.com/settings/personal-access-tokens/new). -github_token= \ No newline at end of file +GITHUB_TOKEN= \ No newline at end of file diff --git a/github_workflows/action.py b/github_workflows/action.py index cbfa8f8..e6b3633 100644 --- a/github_workflows/action.py +++ b/github_workflows/action.py @@ -1,12 +1,20 @@ -print('test action hello wrld') - +import os import yaml from dune_client.client import DuneClient +from dotenv import load_dotenv +import sys +import codecs + +# Set the default encoding to UTF-8 +sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) + +dotenv_path = os.path.join(os.path.dirname(__file__), '..', '.env') +load_dotenv(dotenv_path) -dune = DuneClient.from_env() +dune = DuneClient.from_env() # Read the queries.yml file -with open('queries.yml', 'r') as file: +with open('queries.yml', 'r', encoding='utf-8') as file: data = yaml.safe_load(file) # Extract the query_ids from the data @@ -14,4 +22,20 @@ for id in query_ids: query = dune.get_query(id) - print(query) \ No newline at end of file + print('updating query {}, {}'.format(query.base.query_id, query.base.name)) + + # Check if file exists + file_path = os.path.join(os.path.dirname(__file__), '..', 'queries', f'query_{query.base.query_id}.sql') + if os.path.exists(file_path): + # Update existing file + with open(file_path, 'r+', encoding='utf-8') as file: + content = file.read() + file.seek(0, 0) + file.write(f'-- {query.base.name}\n-- https://dune.com/queries/{query.base.query_id}\n\n\n{query.sql}') + else: + # Create new file and directories if they don't exist + os.makedirs(os.path.dirname(file_path), exist_ok=True) + with open(file_path, 'w', encoding='utf-8') as file: + file.write(f'-- {query.base.name}\n-- https://dune.com/queries/{query.base.query_id}\n\n\n{query.sql}') + + diff --git a/github_workflows/update_queries.py b/github_workflows/update_queries.py deleted file mode 100644 index eb81a23..0000000 --- a/github_workflows/update_queries.py +++ /dev/null @@ -1,14 +0,0 @@ - -import yaml - -# Read the queries.yml file -with open('queries.yml', 'r') as file: - data = yaml.safe_load(file) - -print(data) - -# Extract the query_ids from the data -query_ids = [id for id in data['query_ids']] - -# Print the query_ids -print(query_ids) diff --git a/queries/query_3237721.sql b/queries/query_3237721.sql new file mode 100644 index 0000000..f2fb92e --- /dev/null +++ b/queries/query_3237721.sql @@ -0,0 +1,85 @@ +-- DEX by volume 🏦 +-- https://dune.com/queries/3237721 + + +WITH + seven_day_volume AS ( + SELECT + project AS "Project", + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume + FROM + dex."trades" AS t + WHERE + block_time > NOW() - INTERVAL '7' day + GROUP BY + 1 + ), + one_day_volume AS ( + SELECT + project AS "Project", + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume + FROM + dex."trades" AS t + WHERE + block_time > NOW() - INTERVAL '1' day + GROUP BY + 1 + ) +SELECT + ROW_NUMBER() OVER ( + ORDER BY + SUM(seven.usd_volume) DESC NULLS FIRST + ) AS "Rank", + seven."Project", + SUM(seven.usd_volume) AS "7 Days Volume", + SUM(one.usd_volume) AS "24 Hours Volume" +FROM + seven_day_volume AS seven + LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project" +WHERE + NOT seven.usd_volume IS NULL +GROUP BY + 2 +ORDER BY + 3 DESC NULLS FIRST +-- DEX by volume 🏦 +WITH + seven_day_volume AS ( + SELECT + project AS "Project", + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume + FROM + dex."trades" AS t + WHERE + block_time > NOW() - INTERVAL '7' day + GROUP BY + 1 + ), + one_day_volume AS ( + SELECT + project AS "Project", + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume + FROM + dex."trades" AS t + WHERE + block_time > NOW() - INTERVAL '1' day + GROUP BY + 1 + ) +SELECT + ROW_NUMBER() OVER ( + ORDER BY + SUM(seven.usd_volume) DESC NULLS FIRST + ) AS "Rank", + seven."Project", + SUM(seven.usd_volume) AS "7 Days Volume", + SUM(one.usd_volume) AS "24 Hours Volume" +FROM + seven_day_volume AS seven + LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project" +WHERE + NOT seven.usd_volume IS NULL +GROUP BY + 2 +ORDER BY + 3 DESC NULLS FIRST \ No newline at end of file diff --git a/queries/query_3237723.sql b/queries/query_3237723.sql new file mode 100644 index 0000000..1d8cce0 --- /dev/null +++ b/queries/query_3237723.sql @@ -0,0 +1,27 @@ +-- Weekly DEX volume by chain +-- https://dune.com/queries/3237723 + + +SELECT + blockchain, + DATE_TRUNC('week', block_time), + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume +FROM + dex."trades" AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ +WHERE + block_time > NOW() - INTERVAL '365' day +GROUP BY + 1, + 2 +-- Weekly DEX volume by chain +SELECT + blockchain, + DATE_TRUNC('week', block_time), + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume +FROM + dex."trades" AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ +WHERE + block_time > NOW() - INTERVAL '365' day +GROUP BY + 1, + 2 \ No newline at end of file diff --git a/queries/query_3237726.sql b/queries/query_3237726.sql new file mode 100644 index 0000000..4d996fd --- /dev/null +++ b/queries/query_3237726.sql @@ -0,0 +1,81 @@ +-- Aggregator by volume 📢 +-- https://dune.com/queries/3237726 + + +WITH + seven_day_volume AS ( + SELECT + project AS "Project", + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume + FROM + dex_aggregator.trades AS t + WHERE + block_time > CURRENT_TIMESTAMP - INTERVAL '7' day + GROUP BY + 1 + ), + one_day_volume AS ( + SELECT + project AS "Project", + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume + FROM + dex_aggregator.trades AS t + WHERE + block_time > CURRENT_TIMESTAMP - INTERVAL '1' day + GROUP BY + 1 + ) +SELECT + ROW_NUMBER() OVER ( + ORDER BY + SUM(seven.usd_volume) DESC NULLS FIRST + ) AS "Rank", + seven."Project", + SUM(seven.usd_volume) AS "7 Days Volume", + SUM(one.usd_volume) AS "24 Hours Volume" +FROM + seven_day_volume AS seven + LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project" +GROUP BY + 2 +ORDER BY + 3 DESC NULLS FIRST +-- Aggregator by volume 📢 +WITH + seven_day_volume AS ( + SELECT + project AS "Project", + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume + FROM + dex_aggregator.trades AS t + WHERE + block_time > CURRENT_TIMESTAMP - INTERVAL '7' day + GROUP BY + 1 + ), + one_day_volume AS ( + SELECT + project AS "Project", + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume + FROM + dex_aggregator.trades AS t + WHERE + block_time > CURRENT_TIMESTAMP - INTERVAL '1' day + GROUP BY + 1 + ) +SELECT + ROW_NUMBER() OVER ( + ORDER BY + SUM(seven.usd_volume) DESC NULLS FIRST + ) AS "Rank", + seven."Project", + SUM(seven.usd_volume) AS "7 Days Volume", + SUM(one.usd_volume) AS "24 Hours Volume" +FROM + seven_day_volume AS seven + LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project" +GROUP BY + 2 +ORDER BY + 3 DESC NULLS FIRST \ No newline at end of file diff --git a/queries/query_3237738.sql b/queries/query_3237738.sql new file mode 100644 index 0000000..3af3aee --- /dev/null +++ b/queries/query_3237738.sql @@ -0,0 +1,27 @@ +-- Weekly DEX volume +-- https://dune.com/queries/3237738 + + +SELECT + project, + DATE_TRUNC('week', block_time), + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume +FROM + dex."trades" AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ +WHERE + block_time > NOW() - INTERVAL '365' day +GROUP BY + 1, + 2 +-- Weekly DEX volume +SELECT + project, + DATE_TRUNC('week', block_time), + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume +FROM + dex."trades" AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ +WHERE + block_time > NOW() - INTERVAL '365' day +GROUP BY + 1, + 2 \ No newline at end of file diff --git a/queries/query_3237742.sql b/queries/query_3237742.sql new file mode 100644 index 0000000..b049bb2 --- /dev/null +++ b/queries/query_3237742.sql @@ -0,0 +1,27 @@ +-- Weekly DEX Aggregator volume +-- https://dune.com/queries/3237742 + + +SELECT + project, + DATE_TRUNC('week', block_time), + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume +FROM + dex_aggregator.trades AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ +WHERE + block_time > NOW() - INTERVAL '365' day +GROUP BY + 1, + 2 +-- Weekly DEX Aggregator volume +SELECT + project, + DATE_TRUNC('week', block_time), + SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume +FROM + dex_aggregator.trades AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ +WHERE + block_time > NOW() - INTERVAL '365' day +GROUP BY + 1, + 2 \ No newline at end of file diff --git a/queries/query_3237745.sql b/queries/query_3237745.sql new file mode 100644 index 0000000..acad8a6 --- /dev/null +++ b/queries/query_3237745.sql @@ -0,0 +1,69 @@ +-- EVM DEX Traders by Bucket +-- https://dune.com/queries/3237745 + + +with + all_traders as ( + SELECT + date_trunc('week',block_time) as week + , tx_from + , sum(amount_usd) as volume + FROM (SELECT + block_time + , tx_hash + , tx_from + , max(amount_usd) as amount_usd + FROM dex.trades + group by 1,2,3 + ) + WHERE amount_usd is not null + group by 1,2 + ) + +SELECT +week +, case + when volume < 1e2 then '< $100' + when volume >= 1e2 and volume < 1e3 then '< $1,000' + when volume >= 1e3 and volume < 1e4 then '< $10,000' + when volume >= 1e4 and volume < 1e5 then '< $100,000' + when volume >= 1e5 and volume < 1e6 then '< $1,000,000' + when volume >= 1e6 then '$1m+' +end as trader_bucket +, count(*) +FROM all_traders +WHERE week >= NOW() - INTERVAL '365' day +group by 1,2 +-- EVM DEX Traders by Bucket +with + all_traders as ( + SELECT + date_trunc('week',block_time) as week + , tx_from + , sum(amount_usd) as volume + FROM (SELECT + block_time + , tx_hash + , tx_from + , max(amount_usd) as amount_usd + FROM dex.trades + group by 1,2,3 + ) + WHERE amount_usd is not null + group by 1,2 + ) + +SELECT +week +, case + when volume < 1e2 then '< $100' + when volume >= 1e2 and volume < 1e3 then '< $1,000' + when volume >= 1e3 and volume < 1e4 then '< $10,000' + when volume >= 1e4 and volume < 1e5 then '< $100,000' + when volume >= 1e5 and volume < 1e6 then '< $1,000,000' + when volume >= 1e6 then '$1m+' +end as trader_bucket +, count(*) +FROM all_traders +WHERE week >= NOW() - INTERVAL '365' day +group by 1,2 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 803982d..16bde8f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ dune-client==1.3.0 -pyyaml \ No newline at end of file +pyyaml +python-dotenv \ No newline at end of file