Query editor
-- Query Name: Total FeeM Revenue by Time Period (v9 - Final)-- Description: Calculates the aggregate FeeM revenue earned by all registered applications on Sonic within a given start and end timestamp.-- Parameters are defined in the Dune UI as Text, with valid ISO 8601 default values.WITH registered_feem_contracts AS (-- CTE to get a unique list of all contracts that have EVER been registered for FeeM.SELECT DISTINCT"contractAddress" AS addressFROMsonic_sonic.GasMonetization_evt_ProjectContractAddedWHERE-- Filter to ensure we're only looking at events from the one, true GasMonetization contract.contract_address = 0x0b5f073135df3f5671710f08b08c0c9258aecc35),transactions_in_period AS (-- CTE to get relevant transaction details, filtering by the requested time period first.SELECThash,TRY_CAST(gas_price AS DOUBLE) / 1e18 AS gas_price_s -- Gas price in the native token (S)FROMsonic.transactionsWHERE-- This is the correct, robust syntax for Dune TIMESTAMP parameters passed as strings.block_time >= from_iso8601_timestamp('{{start_time}}')AND block_time < from_iso8601_timestamp('{{end_time}}'))-- Main CalculationSELECT-- Sum up the FeeM for all relevant traces within the specified periodCOALESCE(SUM(-- Gas cost of the specific internal call (trace) in the native token (S)(TRY_CAST(tr.gas_used AS DOUBLE) * tx.gas_price_s)-- Applying the 90% FeeM factor* 0.90),0 -- Return 0 if the sum is NULL (i.e., no matching transactions found)) AS total_feem_revenueFROMsonic.traces tr-- Ensure the trace is for a registered FeeM contractINNER JOINregistered_feem_contracts rfc ON tr.to = rfc.address-- Join to get the gas price from the parent transaction.INNER JOINtransactions_in_period tx ON tr.tx_hash = tx.hashWHEREtr.success = true -- Only successful internal calls generate rewardsAND tr.type = 'call' -- Focus on standard contract callsAND TRY_CAST(tr.gas_used AS DOUBLE) > 0;