Query editor
WITH daily_events AS (SELECTp.contract_address AS address,p.token0,p.token1,e.*,date_trunc('day', e.evt_block_time) AS event_day,ROW_NUMBER() OVER (PARTITION BY p.contract_address, date_trunc('day', e.evt_block_time)ORDER BY e.evt_block_time DESC) AS rnFROM dune.pinacu.result_pairs_reserves pJOIN shadow_sonic.pair_evt_sync eON p.contract_address = e.contract_addressWHERE e.evt_block_time >= TIMESTAMP '2025-02-15 00:00:00'),deduped_events AS (SELECT *FROM daily_eventsWHERE rn = 1),final AS (SELECTde.evt_block_time AS "timestamp",CAST(de.event_day AS date) AS block_date,146 AS chain_id,de.token1 AS underlying_token_address,1 AS underlying_token_index,de.address AS pool_address,de.reserve1 / 1e18 AS underlying_token_amount,(de.reserve0 / de.reserve1) AS token_price, -- price of reserve1 in reserve0 unitsde.reserve1/1e18 * (de.reserve0 / de.reserve1) * t.price_usd AS underlying_token_amount_usd -- multiply by reserve0 priceFROM deduped_events deLEFT JOIN LATERAL (SELECT t.price_usdFROM tokens.transfers tWHERE t.block_time = de.evt_block_timeAND t.blockchain = 'sonic'AND t.contract_address = de.token0 -- Using reserve0's priceAND t.price_usd IS NOT NULLORDER BY t.block_time DESCLIMIT 1) t ON true)SELECT"timestamp",block_date,chain_id,underlying_token_address,underlying_token_index,pool_address,underlying_token_amount,underlying_token_amount_usdFROM final;