Query editor
-- This query used incompatible data types from Dune SQL alpha and may need to be updated.-- More details can be found on https://dune.com/docs/query/dunesql-changes/-- Thanks to @codingtalent for the reference. Forking to add some additional metricswith date_filter as (select (casewhen '{{date_period}}' = '30 Days' then now() - interval '30' daywhen '{{date_period}}' = '3 Months' then now() - interval '3' monthwhen '{{date_period}}' = '6 Months' then now() - interval '6' monthwhen '{{date_period}}' = '1 Year' then now() - interval '1' yearelse date('2021-01-01')end) as start_date),daily_transaction as (select date_trunc('day', block_time) as block_date,"to" as contract_address,count(*) as transaction_count,sum(cast(effective_gas_price as double) * gas_used) / 1e18 as gas_feefrom arbitrum.transactionsinner join date_filter on truewhere success = trueand block_time >= date_filter.start_dategroup by 1, 2),top_contract_by_gas_fee as (select contract_address,sum(gas_fee) as gas_fee_consumed,sum(transaction_count) as total_transaction_count,sum(gas_fee) / sum(transaction_count) as gas_per_txnfrom daily_transactionwhere contract_address is not null -- Why there is null value here?group by contract_addressorder by sum(gas_fee) desclimit 20),contract_info as (select distinct address,first_value(name) over (partition by address order by created_at) as contract_namefrom labels.allwhere blockchain = 'arbitrum'and address in (select contract_address from top_contract_by_gas_fee))select row_number() over (order by gas_fee_consumed desc) as rank_id,contract_address,coalesce(c.contract_name, contract_address) as name,'<a href=https://arbiscan.io/address/' || contract_address || ' target=_blank>ArbiScan</a>' as link,gas_fee_consumed,total_transaction_count,gas_fee_consumed / total_transaction_count as gas_per_txnfrom top_contract_by_gas_fee tleft join contract_info c on t.contract_address = c.addressorder by gas_per_txn desc, gas_fee_consumed desc