Skip to content

Commit afc48f1

Browse files
add preview script
1 parent 3290337 commit afc48f1

File tree

7 files changed

+49
-137
lines changed

7 files changed

+49
-137
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,28 @@ A template for creating repos to manage your Dune queries using the CRUD API. Th
88

99
2. Copy and paste that list into the `queries.yml` file (or any list of query ids, doesn't have to be linked to a dashboard).
1010

11-
3. Install the python requirements and run the `pull_from_dune.py` script. You can input the following lines into a terminal/CLI:
12-
```
13-
pip install -r requirements.txt
14-
python scripts/pull_from_dune.py
15-
```
16-
This will bring in your query ids into `/query_{id}.sql` files within the `/queries` folder. You can run `pull_from_dune.py` anytime you need to update your queries in this repo with edits from the Dune app.
17-
18-
4. Make any changes you need to directly in the repo, and any time you push a commit `push_to_dune.py` will run and save your changes into Dune directly. You can also run the script manually:
19-
```
20-
python scripts/push_to_dune.py
21-
```
11+
3. Then, run `pull_from_dune.py` to bring in all queries into `/query_{id}.sql` files within the `/queries` folder. Directions are below.
12+
13+
4. Make any changes you need to directly in the repo. Any time you push a commit `push_to_dune.py` will run and save your changes into Dune directly.
2214

2315
💡: We use the [Dune CRUD API](https://dune.com/docs/api/api-reference/edit-queries/) to manage queries - this does not change how your queries behave in app.
2416

2517
---
2618

19+
### Scripts
20+
21+
You'll need python installed to run the script commands. Install the required packages first:
22+
23+
```
24+
pip install -r requirements.txt
25+
```
26+
27+
| Script | Action | Command |
28+
|---|---|---|
29+
| `pull_from_dune.py` | updates/adds queries to your repo based on ids in queries.yml | `python scripts/pull_from_dune.py` |
30+
| `push_to_dune.py` | updates queries to Dune based on files in your `queries` folder | `python scripts/push_to_dune.py` |
31+
| `preview_query.py` | gives you the first 20 rows of results by running a query from your `queries` folder. Specify the id. | `python scripts/preview_query.py 3237723` |
32+
2733
### For Contributors
2834

2935
I've set up four types of issues right now:

queries/query_3237721.sql

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,6 @@
33
-- query link: https://dune.com/queries/3237721
44

55

6-
WITH
7-
seven_day_volume AS (
8-
SELECT
9-
project AS "Project",
10-
SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume
11-
FROM
12-
dex."trades" AS t
13-
WHERE
14-
block_time > NOW() - INTERVAL '7' day
15-
GROUP BY
16-
1
17-
),
18-
one_day_volume AS (
19-
SELECT
20-
project AS "Project",
21-
SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume
22-
FROM
23-
dex."trades" AS t
24-
WHERE
25-
block_time > NOW() - INTERVAL '1' day
26-
GROUP BY
27-
1
28-
)
29-
SELECT
30-
ROW_NUMBER() OVER (
31-
ORDER BY
32-
SUM(seven.usd_volume) DESC NULLS FIRST
33-
) AS "Rank",
34-
seven."Project",
35-
SUM(seven.usd_volume) AS "7 Days Volume",
36-
SUM(one.usd_volume) AS "24 Hours Volume"
37-
FROM
38-
seven_day_volume AS seven
39-
LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project"
40-
WHERE
41-
NOT seven.usd_volume IS NULL
42-
GROUP BY
43-
2
44-
ORDER BY
45-
3 DESC NULLS FIRST
46-
-- DEX by volume 🏦
476
WITH
487
seven_day_volume AS (
498
SELECT

queries/query_3237723.sql

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@
33
-- query link: https://dune.com/queries/3237723
44

55

6-
SELECT
7-
blockchain,
8-
DATE_TRUNC('week', block_time),
9-
SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume
10-
FROM
11-
dex."trades" AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */
12-
WHERE
13-
block_time > NOW() - INTERVAL '365' day
14-
GROUP BY
15-
1,
16-
2
17-
-- Weekly DEX volume by chain
186
SELECT
197
blockchain,
208
DATE_TRUNC('week', block_time),

queries/query_3237726.sql

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,6 @@
33
-- query link: https://dune.com/queries/3237726
44

55

6-
WITH
7-
seven_day_volume AS (
8-
SELECT
9-
project AS "Project",
10-
SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume
11-
FROM
12-
dex_aggregator.trades AS t
13-
WHERE
14-
block_time > CURRENT_TIMESTAMP - INTERVAL '7' day
15-
GROUP BY
16-
1
17-
),
18-
one_day_volume AS (
19-
SELECT
20-
project AS "Project",
21-
SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume
22-
FROM
23-
dex_aggregator.trades AS t
24-
WHERE
25-
block_time > CURRENT_TIMESTAMP - INTERVAL '1' day
26-
GROUP BY
27-
1
28-
)
29-
SELECT
30-
ROW_NUMBER() OVER (
31-
ORDER BY
32-
SUM(seven.usd_volume) DESC NULLS FIRST
33-
) AS "Rank",
34-
seven."Project",
35-
SUM(seven.usd_volume) AS "7 Days Volume",
36-
SUM(one.usd_volume) AS "24 Hours Volume"
37-
FROM
38-
seven_day_volume AS seven
39-
LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project"
40-
GROUP BY
41-
2
42-
ORDER BY
43-
3 DESC NULLS FIRST
44-
-- Aggregator by volume 📢
456
WITH
467
seven_day_volume AS (
478
SELECT

queries/query_3237745.sql

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,6 @@
33
-- query link: https://dune.com/queries/3237745
44

55

6-
with
7-
all_traders as (
8-
SELECT
9-
date_trunc('week',block_time) as week
10-
, tx_from
11-
, sum(amount_usd) as volume
12-
FROM (SELECT
13-
block_time
14-
, tx_hash
15-
, tx_from
16-
, max(amount_usd) as amount_usd
17-
FROM dex.trades
18-
group by 1,2,3
19-
)
20-
WHERE amount_usd is not null
21-
group by 1,2
22-
)
23-
24-
SELECT
25-
week
26-
, case
27-
when volume < 1e2 then '< $100'
28-
when volume >= 1e2 and volume < 1e3 then '< $1,000'
29-
when volume >= 1e3 and volume < 1e4 then '< $10,000'
30-
when volume >= 1e4 and volume < 1e5 then '< $100,000'
31-
when volume >= 1e5 and volume < 1e6 then '< $1,000,000'
32-
when volume >= 1e6 then '$1m+'
33-
end as trader_bucket
34-
, count(*)
35-
FROM all_traders
36-
WHERE week >= NOW() - INTERVAL '365' day
37-
group by 1,2
38-
-- EVM DEX Traders by Bucket
396
with
407
all_traders as (
418
SELECT

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dune-client==1.3.0
22
pyyaml
3-
python-dotenv
3+
python-dotenv
4+
pandas

scripts/preview_query.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
from dune_client.client import DuneClient
3+
from dotenv import load_dotenv
4+
import sys
5+
import pandas as pd
6+
7+
dotenv_path = os.path.join(os.path.dirname(__file__), '..', '.env')
8+
load_dotenv(dotenv_path)
9+
10+
dune = DuneClient.from_env()
11+
12+
#get id passed in python script invoke
13+
id = sys.argv[1]
14+
15+
query_file = os.path.join(os.path.dirname(__file__), '..', 'queries', f'query_{id}.sql')
16+
17+
print('getting 20 line preview for query {}...'.format(id))
18+
19+
with open(query_file, 'r', encoding='utf-8') as file:
20+
query_text = file.read()
21+
22+
results = dune.run_sql(query_text + '\n limit 20')
23+
# print(results.result.rows)
24+
results = pd.DataFrame(data=results.result.rows)
25+
print('\n')
26+
print(results)
27+
print('\n')
28+
print(results.describe())
29+
print('\n')
30+
print(results.info())

0 commit comments

Comments
 (0)