From 2d2cd562c52caa8a4435e66bfb14ce3919bc3ac7 Mon Sep 17 00:00:00 2001 From: Andrew <47720952+andrewhong5297@users.noreply.github.com> Date: Wed, 29 Nov 2023 16:01:44 -0500 Subject: [PATCH] push it! --- .github/workflows/push_to_dune.yml | 1 + queries.yml | 8 +- ..._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 | 42 -- ...doswap_v2_collections_summar___2615781.sql | 435 ++++++++++++++++++ queries/sudoswap_v2_trends___2615782.sql | 15 + ...weekly_dex_aggregator_volume___3237742.sql | 21 - queries/weekly_dex_volume___3237738.sql | 21 - .../weekly_dex_volume_by_chain___3237723.sql | 21 - 10 files changed, 453 insertions(+), 209 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 create mode 100644 queries/sudoswap_v2_collections_summar___2615781.sql create mode 100644 queries/sudoswap_v2_trends___2615782.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/.github/workflows/push_to_dune.yml b/.github/workflows/push_to_dune.yml index 5d438d7..737cfef 100644 --- a/.github/workflows/push_to_dune.yml +++ b/.github/workflows/push_to_dune.yml @@ -29,4 +29,5 @@ jobs: - name: Update all queries from Dune, by overwriting queries with repo query text env: DUNE_API_KEY: ${{ secrets.DUNE_API_KEY }} + DUNE_API_BASE_URL: ${{ secrets.DUNE_API_BASE_URL }} run: python -u scripts/push_to_dune.py diff --git a/queries.yml b/queries.yml index d802dbd..4c240ef 100644 --- a/queries.yml +++ b/queries.yml @@ -1,7 +1,3 @@ query_ids: - - 3237721 - - 3237738 - - 3237745 - - 3237723 - - 3237726 - - 3237742 \ No newline at end of file + - 2615782 + - 2615781 \ No newline at end of file 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 f900bd8..0000000 --- a/queries/evm_dex_traders_by_bucket___3237745.sql +++ /dev/null @@ -1,42 +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 - 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/sudoswap_v2_collections_summar___2615781.sql b/queries/sudoswap_v2_collections_summar___2615781.sql new file mode 100644 index 0000000..5c98f5c --- /dev/null +++ b/queries/sudoswap_v2_collections_summar___2615781.sql @@ -0,0 +1,435 @@ +-- already part of a query repo +-- query name: Sudoswap V2 Collections Summary +-- query link: https://dune.com/queries/2615781 + +--test test + +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 new file mode 100644 index 0000000..052cc34 --- /dev/null +++ b/queries/sudoswap_v2_trends___2615782.sql @@ -0,0 +1,15 @@ +-- already part of a query repo +-- query name: Sudoswap V2 Trends +-- query link: https://dune.com/queries/2615782 + + +SELECT +date_trunc('week', block_time) as week +, {{column type}} as col +, count(*) as trades +--get traders later +, sum(amount_usd) as usd_volume +, count(distinct tx_from) as traders +, sum(protocol_fee_amount_usd + trade_fee_amount_usd + royalty_fee_amount_usd) as all_fees +FROM dune.dune.result_sudoswap_v_2_trades +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