Query editor
-- Query for Stat 1 (REVISED): query_feem_cumulative_claimed_value_usd-- Cumulative USD value of all FeeM rewards, valued at the time of each claim.WITH claims AS (-- Get all individual claim events and the amount in S tokensSELECTevt_block_time,TRY_CAST(amount AS DOUBLE) / 1e18 AS s_amount_claimedFROMsonic_sonic.GasMonetization_evt_RewardClaimCompletedWHERETRY_CAST(amount AS DOUBLE) > 0),-- Get all relevant wS/stablecoin trades to build a price feedwS_price_feed AS (SELECTblock_time,(amount_usd /CASEWHEN LOWER(token_bought_symbol) = 'ws' THEN token_bought_amountELSE token_sold_amountEND) AS price_usdFROMdex.tradesWHEREblockchain = 'sonic'AND ((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')))AND amount_usd > 0 AND token_bought_amount > 0 AND token_sold_amount > 0),-- Find the price for each claim using ASOF JOIN logicclaims_with_price AS (SELECTc.evt_block_time,c.s_amount_claimed,p.price_usd AS price_at_claim_time_usd,-- Rank the prices for each claim to find the one that occurred just before or at the claim timeROW_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 -- Join all prices that occurred before or at the claim time)-- Calculate cumulative value using only the most recent price for each claim (rn=1)SELECTevt_block_time AS claim_time,s_amount_claimed,price_at_claim_time_usd,(s_amount_claimed * price_at_claim_time_usd) AS usd_value_of_claim,SUM(s_amount_claimed * price_at_claim_time_usd) OVER (ORDER BY evt_block_time) AS cumulative_claimed_value_usdFROMclaims_with_priceWHERErn = 1 -- This is the ASOF JOIN trick: pick the most recent price for each claimORDER BYevt_block_time ASC;