Skip to content

Commit

Permalink
action to update/add queries
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhong5297 committed Nov 28, 2023
1 parent c458a9b commit 0c1c2ee
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .env.test
Original file line number Diff line number Diff line change
@@ -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=
GITHUB_TOKEN=
34 changes: 29 additions & 5 deletions github_workflows/action.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
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
query_ids = [id for id in data['query_ids']]

for id in query_ids:
query = dune.get_query(id)
print(query)
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}')


14 changes: 0 additions & 14 deletions github_workflows/update_queries.py

This file was deleted.

85 changes: 85 additions & 0 deletions queries/query_3237721.sql
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions queries/query_3237723.sql
Original file line number Diff line number Diff line change
@@ -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
81 changes: 81 additions & 0 deletions queries/query_3237726.sql
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions queries/query_3237738.sql
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions queries/query_3237742.sql
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 0c1c2ee

Please sign in to comment.