This commit is contained in:
Jakob Notland 2026-03-20 11:44:34 +01:00
parent f6dfa3158f
commit cf57347240
2 changed files with 12 additions and 20 deletions

View file

@ -94,19 +94,18 @@ pub async fn rebuild_range(
let select_sql = r#" let select_sql = r#"
WITH per_pool_tx_raw AS ( WITH per_pool_tx_raw AS (
SELECT SELECT
uf.token_id, phe.token_id,
phe.txid, phe.txid,
tx.effective_timestamp AS ts, phe.effective_timestamp AS ts,
phe.utxo, phe.utxo,
phe.sats_delta, phe.sats_delta,
phe.token_delta, phe.token_delta,
phe.sequence phe.sequence
FROM pool_history_entry AS phe 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 JOIN tx ON tx.txid = phe.txid
WHERE tx.blockhash IS NOT NULL WHERE tx.blockhash IS NOT NULL
AND tx.effective_timestamp >= ? AND phe.effective_timestamp >= ?
AND tx.effective_timestamp < ? AND phe.effective_timestamp < ?
), ),
per_pool_tx AS ( per_pool_tx AS (
SELECT SELECT

View file

@ -233,16 +233,16 @@ async fn fetch_raw_trades(
let sql = r#" let sql = r#"
WITH per_pool_tx_raw AS ( WITH per_pool_tx_raw AS (
SELECT SELECT
tx.txid, phe.txid,
COALESCE(tx.first_seen_timestamp, tx.mtp_timestamp) AS effective_timestamp, phe.effective_timestamp,
phe.utxo, phe.utxo,
phe.sats_delta, phe.sats_delta,
phe.token_delta, phe.token_delta,
phe.sequence phe.sequence
FROM pool_history_entry AS phe FROM pool_history_entry AS phe
JOIN utxo_funding AS uf ON phe.utxo = uf.new_utxo_hash WHERE phe.token_id = ?
JOIN tx ON tx.txid = phe.txid AND phe.effective_timestamp >= ?
WHERE uf.token_id = ? AND phe.effective_timestamp < ?
), ),
per_pool_tx AS ( per_pool_tx AS (
SELECT SELECT
@ -255,7 +255,6 @@ per_pool_tx AS (
SUM(ABS(sats_delta)) AS volume_sats_pool, SUM(ABS(sats_delta)) AS volume_sats_pool,
SUM(ABS(token_delta)) AS volume_tokens_pool SUM(ABS(token_delta)) AS volume_tokens_pool
FROM per_pool_tx_raw FROM per_pool_tx_raw
WHERE effective_timestamp >= ? AND effective_timestamp < ?
GROUP BY txid, effective_timestamp, utxo GROUP BY txid, effective_timestamp, utxo
), ),
tx_trades AS ( tx_trades AS (
@ -318,18 +317,12 @@ pub async fn candlesticks(
{ {
let ohlcv_end = ohlcv_materialized_end.min(timestamp_end); let ohlcv_end = ohlcv_materialized_end.min(timestamp_end);
// Align start to 1-hour bucket boundary for the ohlcv query. // timestamp_start is guaranteed hour-aligned by the entry condition above.
let ohlcv_start = timestamp_start / 3600 * 3600;
let ohlcv_rows = 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) = let (mut result, found_first, last_close) =
fill_ohlcv_candles(ohlcv_rows, ohlcv_start, ohlcv_end, false, None); fill_ohlcv_candles(ohlcv_rows, timestamp_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);
if ohlcv_end < timestamp_end { if ohlcv_end < timestamp_end {
// Tail: query raw for [ohlcv_end, timestamp_end) and append. // Tail: query raw for [ohlcv_end, timestamp_end) and append.