Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhong5297 committed Feb 6, 2024
1 parent 32d045b commit 18029e6
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 11 deletions.
4 changes: 4 additions & 0 deletions queries.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
query_ids:
- 3405541
- 3408002
- 3410544
- 3410549
- 3410550
- 3405947
- 3405929
11 changes: 5 additions & 6 deletions queries/all_grants___3405947.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
SELECT
grant_source
, grant_blockchain
, grant_date
, grant_name
, grant_round
, p.symbol as grant_token
, count(distinct grant_name || cast(grant_round as varchar)) as grant_rounds
, p.symbol
, sum(grant_amount) as grant_amount
, sum(grant_amount*p.price) as grant_amount_usd
, count(distinct grantee) as grantees
, approx_percentile(grant_amount*p.price, 0.5) as median_grant_amount_usd
FROM dune.cryptodatabytes.dataset_evm_grants gc
LEFT JOIN prices.usd p ON p.blockchain = 'arbitrum'
LEFT JOIN prices.usd p ON p.blockchain = gc.grant_blockchain
and p.contract_address = gc.grant_token_address
and p.minute = DATE_PARSE(gc.grant_date, '%m/%d/%Y')
GROUP BY 1, 2, 3, 4, 5, 6
GROUP BY grant_source, grant_blockchain, p.symbol
ORDER BY grant_amount_usd desc
23 changes: 23 additions & 0 deletions queries/arbitrum_grants___3408002.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- part of a query repo
-- query name: Arbitrum Grants
-- query link: https://dune.com/queries/3408002


SELECT
grant_source
, grant_blockchain
, DATE_PARSE(gc.grant_date, '%m/%d/%Y') as grant_date
, grant_name
, grant_round
, p.symbol as grant_token
, sum(grant_amount) as grant_amount
, sum(grant_amount*p.price) as grant_amount_usd
, count(distinct grantee) as grantees
, approx_percentile(grant_amount*p.price, 0.5) as median_grant_amount_usd
FROM dune.cryptodatabytes.dataset_evm_grants gc
LEFT JOIN prices.usd p ON p.blockchain = gc.grant_blockchain
and p.contract_address = gc.grant_token_address
and p.minute = DATE_PARSE(gc.grant_date, '%m/%d/%Y')
WHERE grant_blockchain = 'arbitrum'
GROUP BY 1, 2, 3, 4, 5, 6
ORDER BY 3 desc
4 changes: 2 additions & 2 deletions queries/arbitrum_grants_charts___3405541.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ with
FROM arbitrum.traces tr
JOIN grant_contracts gc ON tr.to = gc.address
JOIN arbitrum.transactions tx ON tx.hash = tr.tx_hash
WHERE tr.block_time >= now() - interval '30' day
AND tx.block_time >= now() - interval '30' day
WHERE tr.block_time >= timestamp '2023-12-01 00:00:00'
AND tx.block_time >= timestamp '2023-12-01 00:00:00'
AND gc.latest = 1
)

Expand Down
18 changes: 15 additions & 3 deletions queries/arbitrum_grants_summary___3405929.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ with
AND gc.latest = 1
)

, blockchain_summary as (
SELECT
blockchain as blockchain
, approx_distinct(tx.hash) as txs_30d
, approx_distinct(tx."from") as users_30d
, sum(tx.gas_used*tx.gas_price/1e18) as gas_fees_eth_30d
FROM evms.transactions tx
WHERE tx.block_time >= now() - interval '30' day
AND blockchain IN ('arbitrum') --add all arbitrum chains later
GROUP BY 1
)

, quality_users as (
with base as (
SELECT
Expand All @@ -60,8 +72,8 @@ with
)

SELECT
case when gc.dashboard_link is null then gc.grantee
else get_href(gc.dashboard_link, '📊 ' || gc.grantee)
case when gc.dashboard_link is null then substring(gc.grantee,1,20)
else get_href(gc.dashboard_link, '📊 ' || substring(gc.grantee,1,20))
end as grantee
, gc.grant_amount*p.price as grant_amount_usd
, COALESCE(approx_distinct(tr.tx_hash),0) as txs_30d
Expand All @@ -76,6 +88,6 @@ LEFT JOIN quality_users qu ON qu.grantee = gc.grantee
LEFT JOIN prices.usd p ON p.blockchain = 'arbitrum'
and p.contract_address = gc.grant_token_address
and p.minute = DATE_PARSE(gc.grant_date, '%m/%d/%Y')
LEFT JOIN query_3408002 blk ON blk.blockchain = gc.grant_blockchain --potential issues if protocol is deployed on multiple chains. How to handle?
LEFT JOIN blockchain_summary blk ON blk.blockchain = gc.grant_blockchain --potential issues if protocol is deployed on multiple chains. How to handle?
GROUP BY 1,2,qu.q_users_30d
order by grant_amount_usd desc
23 changes: 23 additions & 0 deletions queries/optimism_grants___3410544.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- part of a query repo
-- query name: Optimism Grants
-- query link: https://dune.com/queries/3410544


SELECT
grant_source
, grant_blockchain
, DATE_PARSE(gc.grant_date, '%m/%d/%Y') as grant_date
, grant_name
, grant_round
, p.symbol as grant_token
, sum(grant_amount) as grant_amount
, sum(grant_amount*p.price) as grant_amount_usd
, count(distinct grantee) as grantees
, approx_percentile(grant_amount*p.price, 0.5) as median_grant_amount_usd
FROM dune.cryptodatabytes.dataset_evm_grants gc
LEFT JOIN prices.usd p ON p.blockchain = gc.grant_blockchain
and p.contract_address = gc.grant_token_address
and p.minute = DATE_PARSE(gc.grant_date, '%m/%d/%Y')
WHERE grant_blockchain = 'optimism'
GROUP BY 1, 2, 3, 4, 5, 6
ORDER BY 3 desc
47 changes: 47 additions & 0 deletions queries/optimism_grants_charts___3410550.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- part of a query repo
-- query name: Optimism Grants Charts
-- query link: https://dune.com/queries/3410550


with
grant_contracts as (
SELECT
c.address
, c.namespace
, c.name
, g.*
, row_number() over (partition by c.address order by created_at desc) as latest
FROM dune.cryptodatabytes.dataset_evm_grants g
LEFT JOIN evms.contracts c ON g.grant_blockchain = c.blockchain and contains(split(g.dune_namespaces,','),c.namespace)
)

, ordered_traces as (
SELECT
gc.grantee
, gc.grant_blockchain
, tr.tx_hash
, tr.gas_used
, tr.trace_address
, tr.block_time
, tx."from" as tx_from
, tx.gas_price
--we only want to keep the highest order trace call by a grantee contract. We do want to keep potential multi-calls so we use rank instead of row_number().
, rank() over (partition by gc.grantee, tr.tx_hash order by cardinality(trace_address) asc) as trace_order
FROM optimism.traces tr
JOIN grant_contracts gc ON tr.to = gc.address
JOIN optimism.transactions tx ON tx.hash = tr.tx_hash
WHERE tr.block_time >= timestamp '2023-12-01 00:00:00'
AND tx.block_time >= timestamp '2023-12-01 00:00:00'
AND gc.latest = 1
)

SELECT
date_trunc('week',block_time) as week
, grantee
, approx_distinct(tx_hash) as txs
, approx_distinct(tx_from) as users
, sum(gas_used*gas_price/1e18) as gas_fees_eth
FROM ordered_traces
WHERE trace_order = 1
GROUP BY 1,2
ORDER BY week DESC, txs DESC
93 changes: 93 additions & 0 deletions queries/optimism_grants_summary___3410549.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
-- part of a query repo
-- query name: Optimism Grants Summary
-- query link: https://dune.com/queries/3410549


with
grant_contracts as (
SELECT
c.address
, c.namespace
, c.name
, g.*
, row_number() over (partition by c.address order by created_at desc) as latest
FROM dune.cryptodatabytes.dataset_evm_grants g
LEFT JOIN evms.contracts c ON g.grant_blockchain = c.blockchain and contains(split(g.dune_namespaces,','),c.namespace)
WHERE g.grant_blockchain = 'optimism'
)

, ordered_traces as (
SELECT
gc.grantee
, gc.grant_blockchain
, tr.tx_hash
, tr.gas_used
, tr.trace_address
, tr.block_time
, tx."from" as tx_from
, tx.gas_price
--we only want to keep the highest order trace call by a grantee contract. We do want to keep potential multi-calls so we use rank instead of row_number().
, rank() over (partition by gc.grantee, tr.tx_hash order by cardinality(trace_address) asc) as trace_order
FROM optimism.traces tr
JOIN grant_contracts gc ON tr.to = gc.address
JOIN optimism.transactions tx ON tx.hash = tr.tx_hash
WHERE tr.block_time >= now() - interval '30' day
AND tx.block_time >= now() - interval '30' day
AND gc.latest = 1
)

, blockchain_summary as (
SELECT
blockchain as blockchain
, approx_distinct(tx.hash) as txs_30d
, approx_distinct(tx."from") as users_30d
, sum(tx.gas_used*tx.gas_price/1e18) as gas_fees_eth_30d
FROM evms.transactions tx
WHERE tx.block_time >= now() - interval '30' day
AND blockchain IN ('optimism') --add all optimism chains later
GROUP BY 1
)

, quality_users as (
with base as (
SELECT
gc.grantee
, tx."from" as user
, count(distinct tx.hash) as txs
FROM grant_contracts gc
JOIN optimism.traces tr ON tr.to = gc.address AND gc.grant_blockchain = 'optimism'
JOIN optimism.transactions tx ON tx.hash = tr.tx_hash AND tx.block_time = tr.block_time
WHERE latest = 1
AND tr.block_time >= now() - interval '30' day
AND tx.block_time >= now() - interval '30' day
GROUP BY 1,2
HAVING count(distinct tx.hash) >= 5
)

SELECT
grantee
, count(*) as q_users_30d
FROM base
group by 1
)

SELECT
case when gc.dashboard_link is null then substring(gc.grantee,1,20)
else get_href(gc.dashboard_link, '📊 ' || substring(gc.grantee,1,20))
end as grantee
, gc.grant_amount*p.price as grant_amount_usd
, COALESCE(approx_distinct(tr.tx_hash),0) as txs_30d
, COALESCE(approx_distinct(tr.tx_from),0) as users_30d
, COALESCE(qu.q_users_30d,0) as q_users_30d
, COALESCE(sum(tr.gas_used*tr.gas_price/1e18),0) as gas_fees_eth_30d
, COALESCE(sum(cast(tr.gas_used*tr.gas_price/1e18 as double)/cast(blk.gas_fees_eth_30d as double)),0) as percent_total_fees_30d
, array_agg(distinct grant_name || ' (#' || cast(grant_round as varchar) || ')') as grants
FROM dune.cryptodatabytes.dataset_evm_grants gc
LEFT JOIN ordered_traces tr ON tr.grantee = gc.grantee AND tr.trace_order = 1
LEFT JOIN quality_users qu ON qu.grantee = gc.grantee
LEFT JOIN prices.usd p ON p.blockchain = 'optimism'
and p.contract_address = gc.grant_token_address
and p.minute = DATE_PARSE(gc.grant_date, '%m/%d/%Y')
LEFT JOIN blockchain_summary blk ON blk.blockchain = gc.grant_blockchain --potential issues if protocol is deployed on multiple chains. How to handle?
GROUP BY 1,2,qu.q_users_30d
order by grant_amount_usd desc

0 comments on commit 18029e6

Please sign in to comment.