Query editor
WITH event_union AS (SELECT 'Swap' AS event_schema, evt_block_time AS block_time,evt_block_slot AS block_slot, evt_tx_id AS tx_id,coalesce(evt_tx_index, 0) AS tx_index,coalesce(evt_outer_instruction_index, 0) AS outer_index,coalesce(evt_inner_instruction_index, -1) AS inner_index,coalesce(lbPair, lb_pair) AS pool, "from" AS swap_actor,coalesce(startBinId, start_bin_id) AS start_bin,coalesce(endBinId, end_bin_id) AS end_bin,CASE WHEN coalesce(swapForY, swap_for_y) THEN 'X' ELSE 'Y' END AS fee_side,CAST(fee AS decimal(38,0)) AS legacy_total_fee_raw,CAST(NULL AS decimal(38,0)) AS mm_fee_raw,CAST(NULL AS decimal(38,0)) AS limit_order_fee_raw,CAST(coalesce(protocolFee, protocol_fee) AS decimal(38,0)) AS protocol_fee_raw,CAST(coalesce(hostFee, host_fee) AS decimal(38,0)) AS host_fee_raw,CAST(amountIn AS decimal(38,0)) AS amount_in_raw,CAST(amountOut AS decimal(38,0)) AS amount_out_raw,CAST(0 AS decimal(38,0)) AS amount_left_raw,true AS fees_on_inputFROM dlmm_solana.lb_clmm_evt_swapWHERE evt_block_date >= current_date - INTERVAL '1' DAYAND evt_block_time >= CAST(current_timestamp AS timestamp) - INTERVAL '24' HOURAND evt_executing_account = 'LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo'-- Match the official Spellbook filter: exclude post-upgrade empty Swap shells.AND amountIn IS NOT NULLUNION ALLSELECT 'Swap2Evt', evt_block_time, evt_block_slot, evt_tx_id,coalesce(evt_tx_index, 0), coalesce(evt_outer_instruction_index, 0),coalesce(evt_inner_instruction_index, -1), lb_pair, "from",start_bin_id, end_bin_id, CASE WHEN fees_on_token_x THEN 'X' ELSE 'Y' END,CAST(NULL AS decimal(38,0)), CAST(mm_fee AS decimal(38,0)),CAST(limit_order_fee AS decimal(38,0)), CAST(protocol_fee AS decimal(38,0)),CAST(host_fee AS decimal(38,0)), CAST(amount_in AS decimal(38,0)),CAST(amount_out AS decimal(38,0)), CAST(amount_left AS decimal(38,0)), fees_on_inputFROM dlmm_solana.lb_clmm_evt_swap2evtWHERE evt_block_date >= current_date - INTERVAL '1' DAYAND evt_block_time >= CAST(current_timestamp AS timestamp) - INTERVAL '24' HOURAND evt_executing_account = 'LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo' AND amount_in IS NOT NULL), ranked AS (SELECT *, row_number() OVER (PARTITION BY block_slot, tx_id, outer_index, inner_indexORDER BY CASE WHEN event_schema = 'Swap2Evt' THEN 0 ELSE 1 END) AS rnFROM event_union), clean AS (SELECT * FROM ranked WHERE rn = 1)SELECT date_trunc('hour', block_time) AS hour, pool, event_schema, fee_side,count(*) AS event_count,count_if(amount_in_raw > amount_left_raw AND amount_out_raw > 0) AS exchange_count,count(DISTINCT swap_actor) AS distinct_actors_within_hour_pool_side,sum(legacy_total_fee_raw) AS legacy_total_fee_raw,sum(mm_fee_raw) AS mm_fee_raw, sum(limit_order_fee_raw) AS limit_order_fee_raw,sum(protocol_fee_raw) AS protocol_fee_raw, sum(host_fee_raw) AS host_fee_raw,max(abs(end_bin - start_bin)) AS max_bins_crossed,max_by(end_bin, ROW(block_slot, tx_index, outer_index, inner_index)) AS latest_end_bin,-- Actual legacy fee/input ratio. Swap2 modes and host accounting are kept separate.max(CASE WHEN event_schema = 'Swap' AND amount_in_raw > 0THEN CAST(legacy_total_fee_raw AS double) / CAST(amount_in_raw AS double) END) AS max_legacy_fee_input_ratio,max(block_time) AS source_latest_event_at,max_by(tx_id, ROW(block_slot, tx_index, outer_index, inner_index)) AS latest_tx,CAST(current_timestamp AS timestamp) AS materialized_atFROM clean