Skip to content

Commit

Permalink
updated queries
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhong5297 committed Feb 5, 2024
1 parent 51fe4a9 commit 3b94a62
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 39 deletions.
42 changes: 25 additions & 17 deletions queries/arbitrum_grants_charts___3405541.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,33 @@ with
LEFT JOIN evms.contracts c ON g.grant_blockchain = c.blockchain and contains(split(g.dune_namespaces,','),c.namespace)
)

, summary as (
, ordered_traces as (
SELECT
date_trunc('week',tr.block_time) as week
, gc.grantee
, gc.grant_amount
, approx_distinct(tr.tx_hash) as txs
, approx_distinct(tx."from") as users
, sum(tr.gas_used*tx.gas_price/1e18) as gas_fees_eth
FROM grant_contracts gc
LEFT JOIN arbitrum.traces tr ON tr.to = gc.address AND gc.grant_blockchain = 'arbitrum'
LEFT JOIN arbitrum.transactions tx ON tx.hash = tr.tx_hash
WHERE latest = 1
AND tr.block_time >= timestamp '2024-01-01 00:00:00'
AND tx.block_time >= timestamp '2024-01-01 00:00:00'
GROUP BY 1,2,3
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 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
AND gc.latest = 1
)

SELECT
*
, grant_amount/users as user_grant_ratio
FROM summary
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
60 changes: 38 additions & 22 deletions queries/arbitrum_grants_summary___3405929.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ with
, 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 = 'arbitrum'
)

, 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 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
AND gc.latest = 1
)

, quality_users as (
Expand All @@ -22,13 +43,13 @@ with
, tx."from" as user
, count(distinct tx.hash) as txs
FROM grant_contracts gc
LEFT JOIN arbitrum.traces tr ON tr.to = gc.address AND gc.grant_blockchain = 'arbitrum'
LEFT JOIN arbitrum.transactions tx ON tx.hash = tr.tx_hash
JOIN arbitrum.traces tr ON tr.to = gc.address AND gc.grant_blockchain = 'arbitrum'
JOIN arbitrum.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) >= 10
HAVING count(distinct tx.hash) >= 5
)

SELECT
Expand All @@ -39,27 +60,22 @@ with
)

SELECT
gc.grantee
, p.symbol as grant_token
, gc.grant_amount
case when gc.dashboard_link is null then gc.grantee
else get_href(gc.dashboard_link, '📊 ' || gc.grantee)
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
-- , approx_distinct(case when tx.block_time >= now() - interval '7' day then tr.tx_hash else null end) as txs_7d
, approx_distinct(tr.tx_hash) as txs_30d
-- , approx_distinct(case when tx.block_time >= now() - interval '7' day then tx."from" else null end) as users_7d
, approx_distinct(tx."from") as users_30d
, qu.q_users_30d
-- , sum(case when tx.block_time >= now() - interval '7' day then tr.gas_used*tx.gas_price/1e18 else null end) as gas_fees_eth_7d
, sum(tr.gas_used*tx.gas_price/1e18) as gas_fees_eth_30d
FROM grant_contracts gc
LEFT JOIN arbitrum.traces tr ON tr.to = gc.address AND gc.grant_blockchain = 'arbitrum'
LEFT JOIN arbitrum.transactions tx ON tx.hash = tr.tx_hash
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 = 'arbitrum'
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')
WHERE latest = 1
AND tr.block_time >= now() - interval '30' day
AND tx.block_time >= now() - interval '30' day
GROUP BY 1,2,3,4, qu.q_users_30d
order by gc.grant_amount desc
LEFT JOIN query_3408002 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 3b94a62

Please sign in to comment.