Skip to content

Commit

Permalink
feat: add revenue queries
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdesalle committed Feb 12, 2025
1 parent 960b5d9 commit 9781d85
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .sqlfluff
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ dialect = trino
max_line_length = 120
# Allow up to 100k bytes for file sizes
large_file_skip_byte_limit = 100000
exclude_rules=RF02,AL01
exclude_rules=RF02,AL01,AL02,ST06
6 changes: 6 additions & 0 deletions queries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ query_ids:
- 4611255 # Lockup: Grouped Withdrawal Data
- 4611179 # Lockup: Remaining Balances
- 4665542 # Lockup: Remaining Balances (Accounted for Cancelations)
- 4676715 # Lockup: Airdrop Revenues
- 4611374 # Lockup: Adjusted Remaining Balances
- 4611349 # Lockup: Priced Adjusted Remaining Balances
- 4611402 # Lockup: Total Value Locked (TVL)
Expand All @@ -64,6 +65,11 @@ query_ids:
- 4666198 # Lockup: Most Used Stablecoins
- 4606813 # Unified: Cumulative Stablecoin Volume
- 4606999 # Unified: Median Stablecoin Deposit
- 4687435 # Unified: Withdrawal Revenues
- 4600695 # Unified: Stream Creation Count
- 4600977 # Unified: Unique User Count
- 4646207 # Unified: Number of Created Streams (Past 24 Hours)
- 4714783 # Unified: Median Daily Revenue
- 4687721 # Unified: Total Protocol Revenues
- 4687516 # Unified: Protocol Revenues (Protocol Revenue Distribution by Category, Protocol Revenue Distribution by Chain)
- 4687748 # Unified: Daily Protocol Revenues
208 changes: 208 additions & 0 deletions queries/lockup:_airdrop_revenues___4676715.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
-- part of a query repo
-- query name: Lockup: Airdrop Revenues
-- query link: https://dune.com/queries/4676715


SELECT
'Ethereum' AS chain,
'ETH' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
ethereum.logs t1
LEFT JOIN ethereum.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'Base' AS chain,
'ETH' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
base.logs t1
LEFT JOIN base.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'Arbitrum' AS chain,
'ETH' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
arbitrum.logs t1
LEFT JOIN arbitrum.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'Optimism' AS chain,
'ETH' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
optimism.logs t1
LEFT JOIN optimism.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'Linea' AS chain,
'ETH' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
linea.logs t1
LEFT JOIN linea.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'zkSync' AS chain,
'ETH' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
zksync.logs t1
LEFT JOIN zksync.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'Scroll' AS chain,
'ETH' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
scroll.logs t1
LEFT JOIN scroll.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'Abstract' AS chain,
'ETH' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
abstract.logs t1
LEFT JOIN abstract.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'Avalanche' AS chain,
'AVAX' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
avalanche_c.logs t1
LEFT JOIN avalanche_c.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'Polygon' AS chain,
'POL' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
polygon.logs t1
LEFT JOIN polygon.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'BNB Chain' AS chain,
'BNB' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
bnb.logs t1
LEFT JOIN bnb.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
UNION ALL
SELECT
'Gnosis Chain' AS chain,
'GNO' AS currency,
t1.block_time,
COALESCE(t2.value, 0) / 1e18 airdrop_fee
FROM
gnosis.logs t1
LEFT JOIN gnosis.transactions t2 ON t1.tx_hash = t2.hash
WHERE
(
(
t1.topic0 = 0x28b58397e03322f670d6b223cc863f8c148e368b8b615412e6798a641a22842d
OR t1.topic0 = 0x1dcd2362ae467d43bf31cbcac0526c0958b23eb063e011ab49a5179c839ed9a9
)
AND t2.value > 0
)
1 change: 0 additions & 1 deletion queries/lockup:_grouped_withdrawal_dat___4611255.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ STREAMAGGREGATION AS (
CHAIN,
CONTRACT_ADDRESS,
RELEASE_VERSION,
CONTRACT,
STREAMID,
TOKEN
)
Expand Down
12 changes: 12 additions & 0 deletions queries/unified:_daily_protocol_revenu___4687748.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- part of a query repo
-- query name: Unified: Daily Protocol Revenues
-- query link: https://dune.com/queries/4687748


SELECT
date_trunc('day', block_time) AS "day",
sum(protocol_revenues_usd) AS revenue
FROM
query_4687516
GROUP BY
date_trunc('day', block_time)
16 changes: 16 additions & 0 deletions queries/unified:_median_daily_revenue___4714783.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- part of a query repo
-- query name: Unified: Median Daily Revenue
-- query link: https://dune.com/queries/4714783


SELECT approx_percentile(revenue, 0.5) AS median
FROM
(
SELECT
date_trunc('day', block_time) AS "day",
sum(protocol_revenues_usd) AS revenue
FROM
query_4687516
GROUP BY
date_trunc('day', block_time)
)
56 changes: 56 additions & 0 deletions queries/unified:_protocol_revenues___4687516.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
-- part of a query repo
-- query name: Unified: Protocol Revenues
-- query link: https://dune.com/queries/4687516


WITH price_lookup AS (
SELECT
currency,
price
FROM (
SELECT
CASE blockchain
WHEN 'avalanche_c' THEN 'AVAX'
WHEN 'ethereum' THEN 'ETH'
WHEN 'bnb' THEN 'BNB'
WHEN 'polygon' THEN 'POL'
WHEN 'gnosis' THEN 'XDAI'
END AS currency,
price,
ROW_NUMBER() OVER (
PARTITION BY blockchain
ORDER BY timestamp DESC
) AS rn
FROM prices.day
WHERE
blockchain IN ('avalanche_c', 'ethereum', 'bnb', 'polygon', 'gnosis')
AND contract_address = 0x0000000000000000000000000000000000000000
) sub
WHERE rn = 1
)

SELECT
t.category,
t.chain,
t.currency,
t.block_time,
t.protocol_revenues,
t.protocol_revenues * p.price AS protocol_revenues_usd
FROM (
SELECT
'Airdrops' AS category,
chain,
currency,
block_time,
airdrop_fee AS protocol_revenues
FROM query_4676715
UNION ALL
SELECT
'Withdrawals' AS category,
chain,
currency,
block_time,
withdrawal_fee AS protocol_revenues
FROM query_4687435
) t
LEFT JOIN price_lookup p ON t.currency = p.currency;
6 changes: 6 additions & 0 deletions queries/unified:_total_protocol_revenu___4687721.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- part of a query repo
-- query name: Unified: Total Protocol Revenues
-- query link: https://dune.com/queries/4687721


SELECT SUM(protocol_revenues_usd) FROM query_4687516
Loading

0 comments on commit 9781d85

Please sign in to comment.