Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
add queries (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan authored Feb 18, 2024
1 parent 9de39d3 commit 2127243
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 53 deletions.
19 changes: 0 additions & 19 deletions PULL_REQUEST_TEMPLATE.md

This file was deleted.

40 changes: 8 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
# Dune Query Repo
# Euphartes Stats Dune Dashboard
query management for https://dune.com/euphrates/euphrates-stats

A template for creating repos to [manage your Dune queries](https://dune.mintlify.app/api-reference/crud/endpoint/create) and any [CSVs as Dune tables](https://dune.mintlify.app/api-reference/upload/endpoint/upload).
### Setup
Generate an API key from your Dune account and put that in both your `.env` file and [github action secrets](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository) (name it `DUNE_API_KEY`). You can create a key under your Dune team settings. *The api key must be from a plus plan for this repo to work.*

### Setup Your Repo

1. Generate an API key from your Dune account and put that in both your `.env` file and [github action secrets](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository) (name it `DUNE_API_KEY`). You can create a key under your Dune team settings. *The api key must be from a plus plan for this repo to work.*

2. Type your intended query ids into the `queries.yml` file. The id can be found from the link `https://dune.com/queries/<query_id>/...`. If you're creating this for a dashboard, go to the dashboard you want to create a repo and click on the "github" button in the top right of your dashboard to see the query ids.

3. Then, run `pull_from_dune.py` to bring in all queries into `/query_{id}.sql` files within the `/queries` folder. Directions to setup and run this python script are below.

### Updating Queries or CSV Tables

1. Make any changes you need to directly in the repo. Any time you push a commit to MAIN branch, `push_to_dune.py` will save your changes into Dune directly. You can run this manually too if you want.
You'll need python and pip installed to run the script commands. If you don't have a package manager set up, then use either [conda](https://www.anaconda.com/download) or [poetry](https://python-poetry.org/) . Then install the required packages:

2. For CSVs, update the files in the `/uploads` folder. `upload_to_dune.py` will run on commit, or can be run manually. The table name in Dune will be `dune.team_name.dataset_<filename>`.
```
pip3 install -r requirements.txt
```

---

### Query Management Scripts

You'll need python and pip installed to run the script commands. If you don't have a package manager set up, then use either [conda](https://www.anaconda.com/download) or [poetry](https://python-poetry.org/) . Then install the required packages:

```
pip install -r requirements.txt
```

| Script | Action | Command |
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| `pull_from_dune.py` | updates/adds queries to your repo based on ids in `queries.yml` | `python scripts/pull_from_dune.py` |
Expand All @@ -46,15 +34,3 @@ pip install -r requirements.txt
➕: If you want to add a query, add it in Dune app first then pull the query id (from URL `dune.com/queries/{id}/other_stuff`) into `queries.yml`

🛑: If you accidently merge a PR or push a commit that messes up your query in Dune, you can roll back any changes using [query version history](https://dune.com/docs/app/query-editor/version-history).

---

### For Contributors

I've set up four types of issues right now:
- `bugs`: This is for data quality issues like miscalculations or broken queries.
- `chart improvements`: This is for suggesting improvements to the visualizations.
- `query improvements`: This is for suggesting improvements to the query itself, such as adding an extra column or table that enhances the results.
- `generic questions`: This is a catch all for other questions or suggestions you may have about the dashboard.

If you want to contribute, either start an issue or go directly into making a PR (using the same labels as above). Once the PR is merged, the queries will get updated in the frontend.
8 changes: 7 additions & 1 deletion queries.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
query_ids:
- 123456
- 3396975
- 3393781
- 3397059
- 3397041
- 3396989
- 3397026
- 3393722
20 changes: 20 additions & 0 deletions queries/daily_dot_locked___3396989.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- part of a query repo
-- query name: daily_dot_locked
-- query link: https://dune.com/queries/3396989


SELECT
eds."day_timestamp",
eds."net_stake" / 1e10 AS "daily_total",
eds."total_stake" / 1e10 AS "daily_stake",
eds."total_unstake" / 1e10 AS "daily_unstake",
(eds."stake_pool_0" - eds."unstake_pool_0") / 1e10 AS "lcdot_ldot",
(eds."stake_pool_1" - eds."unstake_pool_1") / 1e10 AS "lcdot_tdot",
(eds."stake_pool_2" - eds."unstake_pool_2") / 1e10 AS "dot_ldot",
(eds."stake_pool_3" - eds."unstake_pool_3") / 1e10 AS "dot_tdot",
(eds."stake_pool_4" - eds."unstake_pool_4") / 1e10 AS "dot_starlay",
(eds."stake_pool_5" - eds."unstake_pool_5") / 1e10 AS "ldot_starlay",
SUM(eds."net_stake") OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "dot_tvl"
FROM dune.euphrates.result_euphrates_daily_stake AS eds
ORDER BY eds."day_timestamp" ASC
LIMIT 1000
30 changes: 30 additions & 0 deletions queries/euphrates_stake___3393722.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- part of a query repo
-- query name: euphrates_stake
-- query link: https://dune.com/queries/3393722



SELECT
from_iso8601_timestamp("timestamp") as "timestamp",
"pool_id",
"amount",
"amount" / 1e10 as "dot_amount",
"from",
"from" as "user_addr",
"type",
"tx_hash",
"block_number",
CASE
WHEN "pool_id" = 0 THEN 'lcdot_ldot'
WHEN "pool_id" = 1 THEN 'lcdot_tdot'
WHEN "pool_id" = 2 THEN 'dot_ldot'
WHEN "pool_id" = 3 THEN 'dot_tdot'
WHEN "pool_id" = 4 THEN 'dot_starlay'
WHEN "pool_id" = 5 THEN 'ldot_starlay'
ELSE '???'
END as "pool_name",
CASE
WHEN "type" = 1 THEN 'stake' ELSE 'unstake'
END as "action"
FROM dune.euphrates.dataset_euphrates_txs
ORDER by 1
1 change: 0 additions & 1 deletion queries/filler.txt

This file was deleted.

28 changes: 28 additions & 0 deletions queries/latest_overall_stats___3397059.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- part of a query repo
-- query name: latest_overall_stats
-- query link: https://dune.com/queries/3397059


SELECT
t1.day_timestamp,
t1.TOTAL as total_value_locked,
t2.total as total_dot_locked,
t2."lcdot_ldot",
t2."lcdot_tdot",
t2."dot_ldot",
t2."dot_tdot",
t2."cumulative_dot" + t2."cumulative_lcdot" + t2."cumulative_ldot" as total_dot_volume,
t3.total_tx_count
FROM
query_3396975 t1 -- total value locked
JOIN
query_3393781 t2 -- total dot locked
ON
t1.day_timestamp = t2.day_timestamp
JOIN
query_3397026 t3 -- transaction & users
ON
t1.day_timestamp = t3.day_timestamp
ORDER BY
t1.day_timestamp DESC
LIMIT 1;
41 changes: 41 additions & 0 deletions queries/total_dot_locked___3393781.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- part of a query repo
-- query name: total_dot_locked
-- query link: https://dune.com/queries/3393781


SELECT
dot_tvl."day_timestamp",
dot_tvl."lcdot_ldot",
dot_tvl."lcdot_tdot",
dot_tvl."dot_ldot",
dot_tvl."dot_tdot",
dot_tvl."dot_starlay",
dot_tvl."ldot_starlay",
dot_tvl."total",
dot_tvl."lcdot_ldot" + dot_tvl."lcdot_tdot" AS "lcdot",
dot_tvl."dot_ldot" + dot_tvl."dot_tdot" + dot_tvl."dot_starlay" AS "dot",
dot_tvl."ldot_starlay" AS "ldot",
dot_tvl."lcdot_ldot_volume" + dot_tvl."lcdot_tdot_volume" AS "cumulative_lcdot",
dot_tvl."dot_ldot_volume" + dot_tvl."dot_tdot_volume" + dot_tvl."dot_idot_volume" AS "cumulative_dot",
dot_tvl."ldot_starlay" AS "cumulative_ldot"
from (
SELECT
eds."day_timestamp",
SUM((eds."stake_pool_0" - eds."unstake_pool_0")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "lcdot_ldot",
SUM((eds."stake_pool_1" - eds."unstake_pool_1")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "lcdot_tdot",
SUM((eds."stake_pool_2" - eds."unstake_pool_2")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "dot_ldot",
SUM((eds."stake_pool_3" - eds."unstake_pool_3")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "dot_tdot",
SUM((eds."stake_pool_4" - eds."unstake_pool_4")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "dot_starlay",
SUM((eds."stake_pool_5" - eds."unstake_pool_5")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "ldot_starlay",

SUM((eds."stake_pool_0")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "lcdot_ldot_volume",
SUM((eds."stake_pool_1")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "lcdot_tdot_volume",
SUM((eds."stake_pool_2")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "dot_ldot_volume",
SUM((eds."stake_pool_3")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "dot_tdot_volume",
SUM((eds."stake_pool_4")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "dot_idot_volume",
SUM((eds."stake_pool_5")) OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "ldot_ildot_volume",
SUM(eds."net_stake") OVER (ORDER BY eds."day_timestamp" ASC) / 1e10 AS "total"
FROM dune.euphrates.result_euphrates_daily_stake AS eds
ORDER BY eds."day_timestamp" ASC
) as dot_tvl
LIMIT 1000
29 changes: 29 additions & 0 deletions queries/total_value_locked___3396975.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- part of a query repo
-- query name: total_value_locked
-- query link: https://dune.com/queries/3396975


SELECT
dot_tvl."day_timestamp",
dot_tvl."lcdot_ldot" * lastPrice."dot" AS lcdot_ldot,
dot_tvl."lcdot_tdot" * lastPrice."dot" AS lcdot_tdot,
dot_tvl."dot_ldot" * lastPrice."dot" AS dot_ldot,
dot_tvl."dot_tdot" * lastPrice."dot" AS dot_tdot,
dot_tvl."dot_starlay" * lastPrice."dot" AS dot_starlay,
dot_tvl."ldot_starlay" * lastPrice."dot" AS ldot_starlay,

dot_tvl."lcdot" * lastPrice."dot" AS lcdot,
dot_tvl."dot" * lastPrice."dot" AS dot,
dot_tvl."ldot" * lastPrice."dot" AS ldot,

dot_tvl."total" * lastPrice."dot" AS total
FROM query_3393781 AS dot_tvl
CROSS JOIN (
SELECT price AS dot
FROM prices.usd
WHERE symbol = 'DOT'
ORDER BY minute DESC
LIMIT 1
) AS lastPrice
ORDER BY dot_tvl."day_timestamp" ASC
LIMIT 1000
36 changes: 36 additions & 0 deletions queries/user_total_staked_dot_distribu___3397041.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- part of a query repo
-- query name: user_total_staked_dot_distribution
-- query link: https://dune.com/queries/3397041


WITH total_staked_distribution AS (
SELECT
COUNT(*) as total_users,
SUM(CASE WHEN total_staked_dot < 1000 THEN 1 ELSE 0 END) as "<1k",
SUM(CASE WHEN total_staked_dot BETWEEN 1000 AND 10000 THEN 1 ELSE 0 END) as "1k-10k",
SUM(CASE WHEN total_staked_dot > 10000 THEN 1 ELSE 0 END) as ">10k"
FROM query_3397040 -- total_user_staked
)

-- transpose the above table
SELECT
'<1k' as total_staked_dot,
"<1k" as count,
"<1k" * 100.0 / total_users as percentage
FROM total_staked_distribution

UNION ALL

SELECT
'1k-10k' as total_staked_dot,
"1k-10k" as count,
"1k-10k" * 100.0 / total_users as percentage
FROM total_staked_distribution

UNION ALL

SELECT
'>10k' as total_staked_dot,
">10k" as count,
">10k" * 100.0 / total_users as percentage
FROM total_staked_distribution;
14 changes: 14 additions & 0 deletions queries/users_and_transactions___3397026.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- part of a query repo
-- query name: users_and_transactions
-- query link: https://dune.com/queries/3397026


SELECT
eds."day_timestamp",
eds."daily_tx_count",
SUM(eds."daily_tx_count") OVER (ORDER BY eds."day_timestamp" ASC) AS "total_tx_count",
eds."daily_users",
SUM(eds."daily_users") OVER (ORDER BY eds."day_timestamp" ASC) AS "cumulative_users"
FROM dune.euphrates.result_euphrates_daily_stake AS eds
ORDER BY eds."day_timestamp" ASC
LIMIT 1000

0 comments on commit 2127243

Please sign in to comment.