Query editor
-- Query to edit: query_feem_kpis-- ADJUSTED to calculate total_claimed_so_far_usd based on value at the time of each claim.WITH project_counts AS (-- This CTE gets the project and contract counts from your materialized raw data view.SELECTCOUNT(DISTINCT project_id) AS projects_registered,SUM(contract_cnt) AS contracts_trackedFROMdune.soniclabs.result_view_feem_projects_raw),usd_unclaimed_total AS (-- This CTE gets the CURRENT total claimable USD from your materialized price view.SELECTSUM(rewards_to_claim_usd) AS total_claimable_usdFROMdune.soniclabs.result_view_feem_projects_with_price),-- This block calculates the true historical value of all claims made so far.claims AS (-- Gets all individual claim events from on-chain data.SELECTevt_block_time,TRY_CAST(amount AS DOUBLE) / 1e18 AS s_amount_claimedFROMsonic_sonic.GasMonetization_evt_RewardClaimCompletedWHERETRY_CAST(amount AS DOUBLE) > 0),wS_price_feed AS (-- Creates a price feed from DEX trades for Wrapped Sonic (wS).SELECTblock_time,(amount_usd /CASEWHEN LOWER(token_bought_symbol) = 'ws' THEN token_bought_amountELSE token_sold_amountEND) AS price_usdFROMdex.tradesWHEREblockchain = 'sonic' AND amount_usd > 0 AND token_bought_amount > 0 AND token_sold_amount > 0AND ((LOWER(token_bought_symbol) = 'ws' AND LOWER(token_sold_symbol) IN ('usdc', 'usdt', 'dai', 'usd+', 'frax', 'usdb')) OR(LOWER(token_sold_symbol) = 'ws' AND LOWER(token_bought_symbol) IN ('usdc', 'usdt', 'dai', 'usd+', 'frax', 'usdb')))),claims_with_price AS (-- Matches each claim with the price at that point in time (ASOF JOIN logic).SELECTc.evt_block_time,c.s_amount_claimed,p.price_usd AS price_at_claim_time_usd,ROW_NUMBER() OVER (PARTITION BY c.evt_block_time, c.s_amount_claimed ORDER BY p.block_time DESC) as rnFROMclaims cLEFT JOINwS_price_feed p ON p.block_time <= c.evt_block_time),