Query editor
WITH raw_bid_events AS (SELECTl.block_time AS block_time,l.tx_hash AS tx_hash,l.tx_index AS evt_index,tx."from" AS bidder,l.topic1 AS entity_id_topic,bytearray_to_uint256(bytearray_substring(l.data, 1, 32)) AS price_ticks,bytearray_to_uint256(bytearray_substring(l.data,33, 32)) AS amount_raw,bytearray_to_uint256(bytearray_substring(l.data,65, 32)) AS lockup_intFROM ethereum.logs lJOIN ethereum.transactions txON tx.hash = l.tx_hashWHERE l.contract_address = 0xaB02bf85a7a851b6A379eA3D5bD3B9b4f5Dd8461AND l.topic0 = 0x804b42e7e171589ed3879c1882898c8dd15f3d4168afc3292253f5d9ebabc7beAND l.block_time >= TIMESTAMP '2025-10-26 00:00:00 UTC'AND l.block_time <= TIMESTAMP '2025-10-31 00:00:00 UTC'),bids_enriched AS (SELECTblock_time,DATE_TRUNC('hour', block_time) AS hour_bucket,tx_hash,evt_index,bytearray_substring(entity_id_topic, 1, 16) AS entity_id,CAST(amount_raw AS DOUBLE) / 1e6 AS amount_usdt,CASE WHEN lockup_int > 0 THEN TRUE ELSE FALSE END AS lockupFROM raw_bid_events),-- list of hours we care abouthours AS (SELECT DISTINCT hour_bucketFROM bids_enriched),-- for each entity and each hour, pick that entity's latest bidentity_state_per_hour AS (SELECTh.hour_bucket,b.entity_id,b.amount_usdt,b.lockup,ROW_NUMBER() OVER (PARTITION BY h.hour_bucket, b.entity_idORDER BY b.block_time DESC, b.evt_index DESC) AS rnFROM hours hJOIN bids_enriched b-- include any bid from that entity up to the END of this hourON b.block_time < h.hour_bucket + INTERVAL '1' hour),-- keep only the latest row per entity per hourlatest_entity_state_per_hour AS (SELECThour_bucket,entity_id,amount_usdt,