Query editor
WITH farms_logs AS (SELECTLOWER('0x' || substring(to_hex(topic0) FROM 25 FOR 40)) AS decoded_topic0,LOWER('0x' || substring(to_hex(topic1) FROM 25 FOR 40)) AS decoded_topic1,LOWER('0x' || substring(to_hex(topic2) FROM 25 FOR 40)) AS decoded_topic2,LOWER('0x' || substring(to_hex(topic3) FROM 25 FOR 40)) AS decoded_topic3FROM evms.logsWHERE blockchain = 'sonic'AND tx_to = 0xfe6915a0983a304f4d131da635664030da06bcd2AND LOWER('0x' || substring(to_hex(topic1) FROM 25 FOR 40)) = '0x3333111a391cc08fa51353e9195526a70b333333'AND topic2 IS NOT NULL),addresses AS (SELECT DISTINCTdecoded_topic2,from_hex(substr(decoded_topic2, 3)) AS contract_address_vbinaryFROM farms_logs),latest_evt AS (SELECTcontract_address,evt_block_time,row_number() OVER (PARTITION BY contract_address ORDER BY evt_block_time DESC) AS rnFROM shadow_sonic.pair_evt_sync),latest_evt_filtered AS (SELECT contract_address, evt_block_timeFROM latest_evtWHERE rn = 1),token0_data AS (SELECT contract_address, output_0 AS token0FROM shadow_sonic.pair_call_token0),token1_data AS (SELECT contract_address, output_0 AS token1FROM shadow_sonic.pair_call_token1),-- Pre-filter relevant trades instead of scanning everythingfiltered_trades AS (SELECT token_pair, token_bought_address, token_sold_address, block_time,ROW_NUMBER() OVER (PARTITION BY token_bought_address ORDER BY block_time DESC) AS rnFROM dex.tradesWHERE blockchain = 'sonic'),final AS (SELECT DISTINCTa.decoded_topic2 AS pool_address,146 AS chain_id,sc.created_at AS "timestamp",t0.token0 AS underlying_token_address,0 AS underlying_token_index,token0_info.symbol AS underlying_token_symbol,token0_info.decimals AS underlying_token_decimals,t1.token1 AS receipt_token_address,token1_info.symbol AS receipt_token_symbol,token1_info.decimals AS receipt_token_decimals,tp.token_pair AS pool_symbol -- Optimized token pair fetchingFROM addresses aJOIN latest_evt_filtered le