From 6d1b0762fe77b2331dff455f78d5de7d7efac136 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 28 Nov 2023 21:19:10 -0500 Subject: [PATCH 1/7] blurgh --- queries/query_3237742.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/queries/query_3237742.sql b/queries/query_3237742.sql index ceff17f..c2fd980 100644 --- a/queries/query_3237742.sql +++ b/queries/query_3237742.sql @@ -2,7 +2,7 @@ -- query name: Weekly DEX Aggregator volume -- query link: https://dune.com/queries/3237742 - +--small change SELECT project, DATE_TRUNC('week', block_time), From 48ada6486e82dc2dabf035c98eab8168c4bae62e Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 28 Nov 2023 21:48:36 -0500 Subject: [PATCH 2/7] fix --- queries/query_3237745.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/queries/query_3237745.sql b/queries/query_3237745.sql index 1693129..653baa9 100644 --- a/queries/query_3237745.sql +++ b/queries/query_3237745.sql @@ -18,6 +18,7 @@ with group by 1,2,3 ) WHERE amount_usd is not null + and tx_from NOT IN (SELECT distinct tx_from FROM dex.sandwiches) --remove sandwich volume group by 1,2 ) From 873de42488763cadc79faa13795522b9883bfb19 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 28 Nov 2023 21:49:58 -0500 Subject: [PATCH 3/7] change back --- queries/query_3237742.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/queries/query_3237742.sql b/queries/query_3237742.sql index c2fd980..293ce49 100644 --- a/queries/query_3237742.sql +++ b/queries/query_3237742.sql @@ -2,7 +2,6 @@ -- query name: Weekly DEX Aggregator volume -- query link: https://dune.com/queries/3237742 ---small change SELECT project, DATE_TRUNC('week', block_time), From 56fa555a25b532126df15c50c84a49615e000071 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Tue, 28 Nov 2023 21:50:13 -0500 Subject: [PATCH 4/7] 111 --- queries/query_3237742.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/queries/query_3237742.sql b/queries/query_3237742.sql index 293ce49..ceff17f 100644 --- a/queries/query_3237742.sql +++ b/queries/query_3237742.sql @@ -2,6 +2,7 @@ -- query name: Weekly DEX Aggregator volume -- query link: https://dune.com/queries/3237742 + SELECT project, DATE_TRUNC('week', block_time), From 51d040fe954919368070fd81e59f1006c6241c15 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 29 Nov 2023 16:07:46 -0500 Subject: [PATCH 5/7] remove --- ..._by_volume_\360\237\223\242___3237726.sql" | 48 ------------------ ..._by_volume_\360\237\217\246___3237721.sql" | 50 ------------------- .../evm_dex_traders_by_bucket___3237745.sql | 43 ---------------- ...weekly_dex_aggregator_volume___3237742.sql | 21 -------- queries/weekly_dex_volume___3237738.sql | 21 -------- .../weekly_dex_volume_by_chain___3237723.sql | 21 -------- 6 files changed, 204 deletions(-) delete mode 100644 "queries/aggregator_by_volume_\360\237\223\242___3237726.sql" delete mode 100644 "queries/dex_by_volume_\360\237\217\246___3237721.sql" delete mode 100644 queries/evm_dex_traders_by_bucket___3237745.sql delete mode 100644 queries/weekly_dex_aggregator_volume___3237742.sql delete mode 100644 queries/weekly_dex_volume___3237738.sql delete mode 100644 queries/weekly_dex_volume_by_chain___3237723.sql diff --git "a/queries/aggregator_by_volume_\360\237\223\242___3237726.sql" "b/queries/aggregator_by_volume_\360\237\223\242___3237726.sql" deleted file mode 100644 index ad14846..0000000 --- "a/queries/aggregator_by_volume_\360\237\223\242___3237726.sql" +++ /dev/null @@ -1,48 +0,0 @@ --- already part of a query repo --- query name: Aggregator by volume 📢 --- query link: https://dune.com/queries/3237726 - - -WITH - seven_day_volume AS ( - SELECT - project AS "Project", - SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume - FROM - dex_aggregator.trades AS t - WHERE - block_time > CURRENT_TIMESTAMP - INTERVAL '7' day - GROUP BY - 1 - ), - one_day_volume AS ( - SELECT - project AS "Project", - SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume - FROM - dex_aggregator.trades AS t - WHERE - block_time > CURRENT_TIMESTAMP - INTERVAL '1' day - GROUP BY - 1 - ) -SELECT - ROW_NUMBER() OVER ( - ORDER BY - SUM(seven.usd_volume) DESC NULLS FIRST - ) AS "Rank", - seven."Project", - SUM(seven.usd_volume) AS "7 Days Volume", - SUM(one.usd_volume) AS "24 Hours Volume" -FROM - seven_day_volume AS seven - LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project" -GROUP BY - 2 -ORDER BY - 3 DESC NULLS FIRSTme AS seven - LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project" -GROUP BY - 2 -ORDER BY - 3 DESC NULLS FIRST \ No newline at end of file diff --git "a/queries/dex_by_volume_\360\237\217\246___3237721.sql" "b/queries/dex_by_volume_\360\237\217\246___3237721.sql" deleted file mode 100644 index 4e0381a..0000000 --- "a/queries/dex_by_volume_\360\237\217\246___3237721.sql" +++ /dev/null @@ -1,50 +0,0 @@ --- already part of a query repo --- query name: DEX by volume 🏦 --- query link: https://dune.com/queries/3237721 - -WITH - seven_day_volume AS ( - SELECT - project AS "Project", - SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume - FROM - dex."trades" AS t - WHERE - block_time > NOW() - INTERVAL '7' day - GROUP BY - 1 - ), - one_day_volume AS ( - SELECT - project AS "Project", - SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume - FROM - dex."trades" AS t - WHERE - block_time > NOW() - INTERVAL '1' day - GROUP BY - 1 - ) -SELECT - ROW_NUMBER() OVER ( - ORDER BY - SUM(seven.usd_volume) DESC NULLS FIRST - ) AS "Rank", - seven."Project", - SUM(seven.usd_volume) AS "7 Days Volume", - SUM(one.usd_volume) AS "24 Hours Volume" -FROM - seven_day_volume AS seven - LEFT JOIN one_day_volume AS one ON seven."Project" = one."Project" -WHERE - NOT seven.usd_volume IS NULL -GROUP BY - 2 -ORDER BY - 3 DESC NULLS FIRST ON seven."Project" = one."Project" -WHERE - NOT seven.usd_volume IS NULL -GROUP BY - 2 -ORDER BY - 3 DESC NULLS FIRST \ No newline at end of file diff --git a/queries/evm_dex_traders_by_bucket___3237745.sql b/queries/evm_dex_traders_by_bucket___3237745.sql deleted file mode 100644 index a7a7b7b..0000000 --- a/queries/evm_dex_traders_by_bucket___3237745.sql +++ /dev/null @@ -1,43 +0,0 @@ --- already part of a query repo --- query name: EVM DEX Traders by Bucket --- query link: https://dune.com/queries/3237745 - - -with - all_traders as ( - SELECT - date_trunc('week',block_time) as week - , tx_from - , sum(amount_usd) as volume - FROM (SELECT - block_time - , tx_hash - , tx_from - , max(amount_usd) as amount_usd - FROM dex.trades - group by 1,2,3 - ) - WHERE amount_usd is not null - and tx_from NOT IN (SELECT distinct tx_from FROM dex.sandwiches) --remove sandwich volume - group by 1,2 - ) - -SELECT -week -, case - when volume < 1e2 then '< $100' - when volume >= 1e2 and volume < 1e3 then '< $1,000' - when volume >= 1e3 and volume < 1e4 then '< $10,000' - when volume >= 1e4 and volume < 1e5 then '< $100,000' - when volume >= 1e5 and volume < 1e6 then '< $1,000,000' - when volume >= 1e6 then '$1m+' -end as trader_bucket -, count(*) -FROM all_traders -WHERE week >= NOW() - INTERVAL '365' day -group by 1,2e >= 1e6 then '$1m+' -end as trader_bucket -, count(*) -FROM all_traders -WHERE week >= NOW() - INTERVAL '365' day -group by 1,2 \ No newline at end of file diff --git a/queries/weekly_dex_aggregator_volume___3237742.sql b/queries/weekly_dex_aggregator_volume___3237742.sql deleted file mode 100644 index 8b497cd..0000000 --- a/queries/weekly_dex_aggregator_volume___3237742.sql +++ /dev/null @@ -1,21 +0,0 @@ --- already part of a query repo --- query name: Weekly DEX Aggregator volume --- query link: https://dune.com/queries/3237742 - - -SELECT - project, - DATE_TRUNC('week', block_time), - SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume -FROM - dex_aggregator.trades AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ -WHERE - block_time > NOW() - INTERVAL '365' day -GROUP BY - 1, - 2k', Now()) -- Add this line to see stats from current week */ -WHERE - block_time > NOW() - INTERVAL '365' day -GROUP BY - 1, - 2 \ No newline at end of file diff --git a/queries/weekly_dex_volume___3237738.sql b/queries/weekly_dex_volume___3237738.sql deleted file mode 100644 index 0f8a614..0000000 --- a/queries/weekly_dex_volume___3237738.sql +++ /dev/null @@ -1,21 +0,0 @@ --- already part of a query repo --- query name: Weekly DEX volume --- query link: https://dune.com/queries/3237738 - - -SELECT - project, - DATE_TRUNC('week', block_time), - SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume -FROM - dex."trades" AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ -WHERE - block_time > NOW() - INTERVAL '365' day -GROUP BY - 1, - 2-- Add this line to see stats from current week */ -WHERE - block_time > NOW() - INTERVAL '365' day -GROUP BY - 1, - 2 \ No newline at end of file diff --git a/queries/weekly_dex_volume_by_chain___3237723.sql b/queries/weekly_dex_volume_by_chain___3237723.sql deleted file mode 100644 index 241b1ec..0000000 --- a/queries/weekly_dex_volume_by_chain___3237723.sql +++ /dev/null @@ -1,21 +0,0 @@ --- already part of a query repo --- query name: Weekly DEX volume by chain --- query link: https://dune.com/queries/3237723 - - -SELECT - blockchain, - DATE_TRUNC('week', block_time), - SUM(CAST(amount_usd AS DOUBLE)) AS usd_volume -FROM - dex."trades" AS t /* AND block_time < date_trunc('week', Now()) -- Add this line to see stats from current week */ -WHERE - block_time > NOW() - INTERVAL '365' day -GROUP BY - 1, - 2, Now()) -- Add this line to see stats from current week */ -WHERE - block_time > NOW() - INTERVAL '365' day -GROUP BY - 1, - 2 \ No newline at end of file From af87361fb74438fc6a4d603ed10885c086f726ce Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 29 Nov 2023 16:34:15 -0500 Subject: [PATCH 6/7] boop --- ...doswap_v2_collections_summar___2615781.sql | 215 +----------------- queries/sudoswap_v2_trends___2615782.sql | 2 +- 2 files changed, 2 insertions(+), 215 deletions(-) diff --git a/queries/sudoswap_v2_collections_summar___2615781.sql b/queries/sudoswap_v2_collections_summar___2615781.sql index 64c5b23..89aad3d 100644 --- a/queries/sudoswap_v2_collections_summar___2615781.sql +++ b/queries/sudoswap_v2_collections_summar___2615781.sql @@ -2,432 +2,219 @@ -- query name: Sudoswap V2 Collections Summary -- query link: https://dune.com/queries/2615781 -WITH +WITH pairs_created as ( - SELECT - * - FROM query_2615780 - ), - - --we also need to get token balance of each pair currently - erc721_balances as ( - SELECT - contract_address as nft_contract_address - -- , array_agg(tokenId) as tokenids_held - , count(*) as tokens_held - FROM ( - SELECT - row_number() OVER (partition by contract_address, tokenId order by evt_block_number desc, evt_index desc) as last_held - , * - FROM erc721_ethereum.evt_Transfer tr - where tr.contract_address IN (select distinct nft_contract_address from pairs_created) --get last holders for all nft contracts (need to include erc1155 later) - ) a - WHERE last_held = 1 - AND a.to IN (SELECT pool_address FROM pairs_created) --keep only tokens held by pairs - GROUP BY 1 - ), - - erc1155_balances as ( - with t_in as ( - SELECT - to as pool - , contract_address - , token_id - , sum(amount) as transferred_in - FROM nft.transfers - WHERE blockchain = 'ethereum' - and token_standard = 'erc1155' - and to IN (SELECT pool_address FROM pairs_created) - group by 1,2,3 - ) - - , t_out as ( - SELECT - "from" as pool - , contract_address - , token_id - , sum(amount) as transferred_out - FROM nft.transfers - WHERE blockchain = 'ethereum' - and token_standard = 'erc1155' - and "from" IN (SELECT pool_address FROM pairs_created) - group by 1,2,3 - ) - - SELECT - pc.nft_contract_address - , sum(t_in.transferred_in - COALESCE(t_out.transferred_out,cast(0 as uint256))) as tokens_held - FROM t_in - LEFT JOIN t_out ON t_in.pool = t_out.pool AND t_in.contract_address = t_out.contract_address AND t_in.token_id = t_out.token_id - JOIN pairs_created pc ON pc.pool_address = t_in.pool - group by 1 - ), - - --we need eth_balances for liquidity tracking purposes - eth_balances as ( - WITH eth_in as ( - SELECT - tr.to as holder_address - , SUM(tr.value/1e18) as eth_funded - FROM ethereum.traces tr - WHERE tr.block_time > timestamp '2022-04-23' - AND tr.success=true - AND tr.type='call' - AND (tr.call_type NOT IN ('delegatecall', 'callcode', 'staticcall') OR tr.call_type IS null) - AND tr.to IN (SELECT pool_address FROM pairs_created) - GROUP BY 1 - ), - - eth_out as ( - SELECT - tr."from" as holder_address - , SUM(tr.value/1e18) as eth_spent - FROM ethereum.traces tr - WHERE tr.block_time > timestamp '2022-04-23' - AND tr.success=true - AND tr.type='call' - AND (tr.call_type NOT IN ('delegatecall', 'callcode', 'staticcall') OR tr.call_type IS null) - AND tr."from" IN (SELECT pool_address FROM pairs_created) - GROUP BY 1 - ) - - SELECT - pc.nft_contract_address - , SUM(COALESCE(eth_funded,0) - COALESCE(eth_spent, 0)) as eth_balance - FROM eth_in - LEFT JOIN eth_out ON eth_in.holder_address = eth_out.holder_address - JOIN pairs_created pc ON pc.pool_address = eth_in.holder_address - WHERE COALESCE(eth_funded,0) - COALESCE(eth_spent, 0) > 0 --for some reason some balances are calculated negative. - GROUP BY 1 - ), - - erc20_balances as ( - WITH erc20_in as ( - SELECT - tr.to as holder - , contract_address - , SUM(cast(tr.value as double)) as token_funded - FROM erc20_ethereum.evt_Transfer tr - WHERE tr.to IN (SELECT pool_address FROM pairs_created) - -- AND contract_address IN (SELECT address FROM valid_tokens) - GROUP BY 1,2 - ), - - erc20_out as ( - SELECT - tr."from" as holder - , contract_address - , SUM(cast(tr.value as double)) as token_spent - FROM erc20_ethereum.evt_Transfer tr - WHERE tr."from" IN (SELECT pool_address FROM pairs_created) - -- AND contract_address IN (SELECT address FROM valid_tokens) - GROUP BY 1,2 - ) - - , contract_balance_sum as ( - SELECT - pc.nft_contract_address - , tk.symbol as symbol - , erc20_in.contract_address - , sum(token_funded/pow(10,COALESCE(tk.decimals,18)) - COALESCE(token_spent, 0)/pow(10,COALESCE(tk.decimals,18))) as tokens_held - FROM erc20_in - LEFT JOIN erc20_out ON erc20_in.holder = erc20_out.holder - LEFT JOIN tokens.erc20 tk ON tk.contract_address = erc20_in.contract_address - JOIN pairs_created pc ON pc.pool_address = erc20_in.holder - WHERE COALESCE(token_funded,0) - COALESCE(token_spent, 0) > 0 --due to overflow, some balances are calculated negative. - AND blockchain = 'ethereum' - group by 1,2,3 - ) - - SELECT - nft_contract_address - , array_agg(json_object('token':COALESCE(symbol,cast(contract_address as varchar)), 'balance': round(tokens_held,4))) as tokens_held - FROM contract_balance_sum - group by 1 - ), - - trading_totals as ( - SELECT - nft_contract_address - , nft_name - , sum(amount_usd) as usd_volume - -- get 7 day volume - , sum(number_of_items) as nfts_traded - , sum(trade_fee_amount_usd) as trade_fee_amount_usd - , sum(case when block_time >= now() - interval '7' day then amount_usd else 0 end) as last_7_days - , sum(case when block_time >= now() - interval '7' day then trade_fee_amount_usd else 0 end) as last_7_days_fees - , sum(protocol_fee_amount_usd) as protocol_fee_amount_usd - FROM dune.dune.result_sudoswap_v_2_trades - GROUP BY 1,2 - ), - - last_price as ( - SELECT - nft_contract_address - , amount_usd - FROM ( - SELECT - * - , row_number() OVER (partition by nft_contract_address order by block_time desc) as rn - FROM dune.dune.result_sudoswap_v_2_trades - WHERE amount_usd is not null - ) a - WHERE rn = 1 - ), - - all_collections_cleaned as ( - SELECT - pc.* - , nft.name as nft_name - , COALESCE(bal_721.tokens_held,0) as nfts_721 --buyable liquidity - , COALESCE(bal_1155.tokens_held,cast(0 as uint256)) as nfts_1155 --buyable liquidity - , COALESCE(bal_20.tokens_held,array['0']) as erc20_balances --fix logic using transform agg later - , COALESCE(eth_bal.eth_balance,0) as eth_liq --sellable liquidity - FROM ( - SELECT - nft_contract_address - , count(distinct pool_address) as num_pairs - FROM pairs_created - GROUP BY 1 - ) pc - LEFT JOIN erc721_balances bal_721 ON bal_721.nft_contract_address = pc.nft_contract_address - LEFT JOIN erc1155_balances bal_1155 ON bal_1155.nft_contract_address = pc.nft_contract_address - LEFT JOIN erc20_balances bal_20 ON bal_20.nft_contract_address = pc.nft_contract_address - LEFT JOIN eth_balances eth_bal ON eth_bal.nft_contract_address = pc.nft_contract_address - LEFT JOIN tokens.nft nft ON nft.blockchain = 'ethereum' and nft.contract_address = pc.nft_contract_address - ) - - SELECT - CONCAT(' swap now! ') as s_link - , CONCAT('',COALESCE(acc.nft_name, cast(acc.nft_contract_address as varchar),'')) as collection - , num_pairs - , '||' as split - , spot.amount_usd as last_spot - , COALESCE(trade.last_7_days, 0) as last_7_days - , COALESCE(trade.usd_volume, 0) as usd_volume - , COALESCE(trade.nfts_traded, cast(0 as uint256)) as nfts_traded - , COALESCE(trade.trade_fee_amount_usd, 0) as trade_fee_amount_usd - , COALESCE(trade.protocol_fee_amount_usd, 0) as protocol_fee_amount_usd - , '||' as split_2 - , nfts_721 - , nfts_1155 - , erc20_balances - , eth_liq - FROM all_collections_cleaned acc - LEFT JOIN trading_totals trade ON trade.nft_contract_address = acc.nft_contract_address - LEFT JOIN last_price spot ON spot.nft_contract_address = acc.nft_contract_address - ORDER BY last_7_days DESC \ No newline at end of file diff --git a/queries/sudoswap_v2_trends___2615782.sql b/queries/sudoswap_v2_trends___2615782.sql index 052cc34..6e245ef 100644 --- a/queries/sudoswap_v2_trends___2615782.sql +++ b/queries/sudoswap_v2_trends___2615782.sql @@ -5,7 +5,7 @@ SELECT date_trunc('week', block_time) as week -, {{column type}} as col +, pool_type as col , count(*) as trades --get traders later , sum(amount_usd) as usd_volume From f8758235002031a301111f3b992891098e73a2b0 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 29 Nov 2023 16:34:38 -0500 Subject: [PATCH 7/7] edited --- queries/sudoswap_v2_collections_summar___2615781.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/queries/sudoswap_v2_collections_summar___2615781.sql b/queries/sudoswap_v2_collections_summar___2615781.sql index 89aad3d..2183644 100644 --- a/queries/sudoswap_v2_collections_summar___2615781.sql +++ b/queries/sudoswap_v2_collections_summar___2615781.sql @@ -209,12 +209,15 @@ SELECT , COALESCE(trade.nfts_traded, cast(0 as uint256)) as nfts_traded , COALESCE(trade.trade_fee_amount_usd, 0) as trade_fee_amount_usd , COALESCE(trade.protocol_fee_amount_usd, 0) as protocol_fee_amount_usd + , cast(COALESCE(last_7_days_fees,0)*52 as double)/cast(spot.amount_usd*COALESCE(cast(nfts_721 as int),cast(nfts_1155 as int)) + eth_liq*p.price as double) as apy , '||' as split_2 , nfts_721 , nfts_1155 , erc20_balances , eth_liq + , cast(spot.amount_usd*COALESCE(cast(nfts_721 as int),cast(nfts_1155 as int)) + eth_liq*p.price as double) as usd_liquidity FROM all_collections_cleaned acc LEFT JOIN trading_totals trade ON trade.nft_contract_address = acc.nft_contract_address LEFT JOIN last_price spot ON spot.nft_contract_address = acc.nft_contract_address +LEFT JOIN prices.usd_latest p ON p.blockchain = 'ethereum' and symbol = 'WETH' ORDER BY last_7_days DESC \ No newline at end of file