Query editor
WITH swap_events AS (SELECTblock_time,block_number,tx_hash,bytearray_to_uint256(bytearray_substring(data, 1, 32)) as amount0In,bytearray_to_uint256(bytearray_substring(data, 33, 32)) as amount0Out,bytearray_substring(data, 77, 20) as tokenIn_address, -- Extract the last 20 bytes of the 32-byte fieldCASEWHEN bytearray_substring(data, 97, 32) = 0x0000000000000000000000000000000000000000000000000000000000000000 THEN falseELSE trueEND as is_stable,bytearray_substring(topic1, 13, 20) as sender,bytearray_substring(topic2, 13, 20) as recipientFROM avalanche_c.logsWHERE contract_address = 0xCaD684775d7879E63f5d319dAcC8086EeCC01B01AND topic0 = 0xe7857fba7d66e2e29213fda7a0394efbaa80d435dfc7b4c1f147d78c2f9caa62AND block_time >= NOW() - INTERVAL '7' day),hourly_stats AS (SELECTDATE_TRUNC('hour', block_time) as hour,COUNT(*) as swap_count,COUNT(DISTINCT sender) as unique_swappers,SUM(amount0In / 1e18) as total_amount_in,SUM(amount0Out / 1e18) as total_amount_out,AVG(amount0In / 1e18) as avg_amount_in,AVG(amount0Out / 1e18) as avg_amount_out,SUM(CASE WHEN is_stable THEN 1 ELSE 0 END) as stable_swaps,SUM(CASE WHEN NOT is_stable THEN 1 ELSE 0 END) as volatile_swapsFROM swap_eventsGROUP BY DATE_TRUNC('hour', block_time)),token_rankings_per_hour AS (SELECTDATE_TRUNC('hour', se.block_time) as hour,se.tokenIn_address,COALESCE(t.symbol, CONCAT('0x', TO_HEX(se.tokenIn_address))) as token_symbol,COUNT(*) as token_swap_count,SUM(se.amount0In / 1e18) as token_volume,ROW_NUMBER() OVER (PARTITION BY DATE_TRUNC('hour', se.block_time)ORDER BY COUNT(*) DESC, SUM(se.amount0In / 1e18) DESC) as token_rankFROM swap_events seLEFT JOIN tokens.erc20 t ON se.tokenIn_address = t.contract_addressWHERE se.tokenIn_address != 0x0000000000000000000000000000000000000000 -- Exclude null addressesAND se.tokenIn_address != 0x000000000000000000000000000000000000000000000000000000000000 -- Exclude zero addressesGROUP BY DATE_TRUNC('hour', se.block_time), se.tokenIn_address, t.symbol),top_tokens_per_hour AS (SELECThour,token_symbol as top_token_symbol,token_swap_count as top_token_swaps,CAST(token_volume AS DECIMAL(20,6)) as top_token_volumeFROM token_rankings_per_hourWHERE token_rank = 1),medium_tokens_per_hour AS (