Query editor
WITH ranked_transfers AS (SELECTcontract_address,block_number,ROW_NUMBER() OVER (PARTITION BY contract_address ORDER BY block_number DESC) AS rnFROM tokens.transfersWHERE blockchain = 'sonic'AND tx_to = 0xe6E0A10eb298F0aC4170f2502cF7b201375BBc85),addresses AS (-- Filter to only the latest transfer per contract that exists in pair_evt_syncSELECT r.contract_addressFROM ranked_transfers rWHERE r.rn = 1AND EXISTS (SELECT 1FROM shadow_sonic.pair_evt_sync pWHERE p.contract_address = r.contract_address)),latest_evt_filtered AS (-- Get the latest event per contract using MAX aggregation.SELECTcontract_address,MAX(evt_block_time) AS evt_block_timeFROM shadow_sonic.pair_evt_syncGROUP BY contract_address),token0_data AS (-- Retrieve token0 from the pair_call_token0 table.SELECT contract_address, output_0 AS token0FROM shadow_sonic.pair_call_token0),token1_data AS (-- Retrieve token1 from the pair_call_token1 table.SELECT contract_address, output_0 AS token1FROM shadow_sonic.pair_call_token1),filtered_trades AS (-- Efficiently fetch the latest trade per token pair using ROW_NUMBER()SELECT token_pair, token_sold_address, token_bought_address, block_timeFROM (SELECTtoken_pair,token_sold_address,token_bought_address,block_time,ROW_NUMBER() OVER (PARTITION BY LEAST(token_sold_address, token_bought_address),GREATEST(token_sold_address, token_bought_address)ORDER BY block_time DESC) AS rnFROM dex.tradesWHERE blockchain = 'sonic') subWHERE rn = 1)SELECT DISTINCTa.contract_address AS pool_address,146 AS chain_id,