From cf573472403a885c34033ddc560194c2917ff759 Mon Sep 17 00:00:00 2001 From: Jakob Notland Date: Fri, 20 Mar 2026 11:44:34 +0100 Subject: [PATCH] cleanup --- src/db/cauldron/ohlcv.rs | 9 ++++----- src/rpc/candlesticks.rs | 23 ++++++++--------------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/db/cauldron/ohlcv.rs b/src/db/cauldron/ohlcv.rs index 93ffaf3..365b0f2 100644 --- a/src/db/cauldron/ohlcv.rs +++ b/src/db/cauldron/ohlcv.rs @@ -94,19 +94,18 @@ pub async fn rebuild_range( let select_sql = r#" WITH per_pool_tx_raw AS ( SELECT - uf.token_id, + phe.token_id, phe.txid, - tx.effective_timestamp AS ts, + phe.effective_timestamp AS ts, phe.utxo, phe.sats_delta, phe.token_delta, phe.sequence FROM pool_history_entry AS phe - JOIN utxo_funding AS uf ON phe.utxo = uf.new_utxo_hash JOIN tx ON tx.txid = phe.txid WHERE tx.blockhash IS NOT NULL - AND tx.effective_timestamp >= ? - AND tx.effective_timestamp < ? + AND phe.effective_timestamp >= ? + AND phe.effective_timestamp < ? ), per_pool_tx AS ( SELECT diff --git a/src/rpc/candlesticks.rs b/src/rpc/candlesticks.rs index 1e5f237..d793579 100644 --- a/src/rpc/candlesticks.rs +++ b/src/rpc/candlesticks.rs @@ -233,16 +233,16 @@ async fn fetch_raw_trades( let sql = r#" WITH per_pool_tx_raw AS ( SELECT - tx.txid, - COALESCE(tx.first_seen_timestamp, tx.mtp_timestamp) AS effective_timestamp, + phe.txid, + phe.effective_timestamp, phe.utxo, phe.sats_delta, phe.token_delta, phe.sequence FROM pool_history_entry AS phe - JOIN utxo_funding AS uf ON phe.utxo = uf.new_utxo_hash - JOIN tx ON tx.txid = phe.txid - WHERE uf.token_id = ? + WHERE phe.token_id = ? + AND phe.effective_timestamp >= ? + AND phe.effective_timestamp < ? ), per_pool_tx AS ( SELECT @@ -255,7 +255,6 @@ per_pool_tx AS ( SUM(ABS(sats_delta)) AS volume_sats_pool, SUM(ABS(token_delta)) AS volume_tokens_pool FROM per_pool_tx_raw - WHERE effective_timestamp >= ? AND effective_timestamp < ? GROUP BY txid, effective_timestamp, utxo ), tx_trades AS ( @@ -318,18 +317,12 @@ pub async fn candlesticks( { let ohlcv_end = ohlcv_materialized_end.min(timestamp_end); - // Align start to 1-hour bucket boundary for the ohlcv query. - let ohlcv_start = timestamp_start / 3600 * 3600; - + // timestamp_start is guaranteed hour-aligned by the entry condition above. let ohlcv_rows = - ohlcv::get_active_candles(pool, &token_blob, ohlcv_start, ohlcv_end).await?; + ohlcv::get_active_candles(pool, &token_blob, timestamp_start, ohlcv_end).await?; let (mut result, found_first, last_close) = - fill_ohlcv_candles(ohlcv_rows, ohlcv_start, ohlcv_end, false, None); - - // If the requested start doesn't align to ohlcv_start, trim leading flat candles - // that fall before timestamp_start. - result.retain(|c| c.time >= timestamp_start); + fill_ohlcv_candles(ohlcv_rows, timestamp_start, ohlcv_end, false, None); if ohlcv_end < timestamp_end { // Tail: query raw for [ohlcv_end, timestamp_end) and append.