This commit is contained in:
2026-04-30 10:50:37 +02:00
parent 523bbe0860
commit 37d54887d6
16 changed files with 1495 additions and 9 deletions

View File

@@ -0,0 +1,216 @@
// file: kb_lib/src/db/queries/pair_candle.rs
//! Queries for `kb_pair_candles`.
/// Inserts or updates one pair-candle row and returns its stable internal id.
pub async fn upsert_pair_candle(
database: &crate::KbDatabase,
dto: &crate::KbPairCandleDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_pair_candles (
pair_id,
timeframe_seconds,
bucket_start_unix,
bucket_end_unix,
open_price_quote_per_base,
high_price_quote_per_base,
low_price_quote_per_base,
close_price_quote_per_base,
trade_count,
buy_count,
sell_count,
base_volume_raw,
quote_volume_raw,
first_trade_signature,
last_trade_signature,
created_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(pair_id, timeframe_seconds, bucket_start_unix) DO UPDATE SET
bucket_end_unix = excluded.bucket_end_unix,
open_price_quote_per_base = excluded.open_price_quote_per_base,
high_price_quote_per_base = excluded.high_price_quote_per_base,
low_price_quote_per_base = excluded.low_price_quote_per_base,
close_price_quote_per_base = excluded.close_price_quote_per_base,
trade_count = excluded.trade_count,
buy_count = excluded.buy_count,
sell_count = excluded.sell_count,
base_volume_raw = excluded.base_volume_raw,
quote_volume_raw = excluded.quote_volume_raw,
first_trade_signature = excluded.first_trade_signature,
last_trade_signature = excluded.last_trade_signature,
updated_at = excluded.updated_at
"#,
)
.bind(dto.pair_id)
.bind(dto.timeframe_seconds)
.bind(dto.bucket_start_unix)
.bind(dto.bucket_end_unix)
.bind(dto.open_price_quote_per_base)
.bind(dto.high_price_quote_per_base)
.bind(dto.low_price_quote_per_base)
.bind(dto.close_price_quote_per_base)
.bind(dto.trade_count)
.bind(dto.buy_count)
.bind(dto.sell_count)
.bind(dto.base_volume_raw.clone())
.bind(dto.quote_volume_raw.clone())
.bind(dto.first_trade_signature.clone())
.bind(dto.last_trade_signature.clone())
.bind(dto.created_at.to_rfc3339())
.bind(dto.updated_at.to_rfc3339())
.execute(pool)
.await;
if let Err(error) = query_result {
return Err(crate::KbError::Db(format!(
"cannot upsert kb_pair_candles on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_pair_candles
WHERE pair_id = ? AND timeframe_seconds = ? AND bucket_start_unix = ?
LIMIT 1
"#,
)
.bind(dto.pair_id)
.bind(dto.timeframe_seconds)
.bind(dto.bucket_start_unix)
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_pair_candles id for pair_id '{}' timeframe '{}' bucket '{}' on sqlite: {}",
dto.pair_id, dto.timeframe_seconds, dto.bucket_start_unix, error
))),
}
}
}
}
/// Returns one pair-candle row identified by `(pair_id, timeframe_seconds, bucket_start_unix)`.
pub async fn get_pair_candle_by_key(
database: &crate::KbDatabase,
pair_id: i64,
timeframe_seconds: i64,
bucket_start_unix: i64,
) -> Result<std::option::Option<crate::KbPairCandleDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbPairCandleEntity>(
r#"
SELECT
id,
pair_id,
timeframe_seconds,
bucket_start_unix,
bucket_end_unix,
open_price_quote_per_base,
high_price_quote_per_base,
low_price_quote_per_base,
close_price_quote_per_base,
trade_count,
buy_count,
sell_count,
base_volume_raw,
quote_volume_raw,
first_trade_signature,
last_trade_signature,
created_at,
updated_at
FROM kb_pair_candles
WHERE pair_id = ? AND timeframe_seconds = ? AND bucket_start_unix = ?
LIMIT 1
"#,
)
.bind(pair_id)
.bind(timeframe_seconds)
.bind(bucket_start_unix)
.fetch_optional(pool)
.await;
let entity_option = match query_result {
Ok(entity_option) => entity_option,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot read kb_pair_candles by key on sqlite: {}",
error
)));
}
};
match entity_option {
Some(entity) => crate::KbPairCandleDto::try_from(entity).map(Some),
None => Ok(None),
}
}
}
}
/// Lists candles for one pair and one timeframe ordered by bucket start.
pub async fn list_pair_candles_by_pair_and_timeframe(
database: &crate::KbDatabase,
pair_id: i64,
timeframe_seconds: i64,
) -> Result<std::vec::Vec<crate::KbPairCandleDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbPairCandleEntity>(
r#"
SELECT
id,
pair_id,
timeframe_seconds,
bucket_start_unix,
bucket_end_unix,
open_price_quote_per_base,
high_price_quote_per_base,
low_price_quote_per_base,
close_price_quote_per_base,
trade_count,
buy_count,
sell_count,
base_volume_raw,
quote_volume_raw,
first_trade_signature,
last_trade_signature,
created_at,
updated_at
FROM kb_pair_candles
WHERE pair_id = ? AND timeframe_seconds = ?
ORDER BY bucket_start_unix ASC, id ASC
"#,
)
.bind(pair_id)
.bind(timeframe_seconds)
.fetch_all(pool)
.await;
let entities = match query_result {
Ok(entities) => entities,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot list kb_pair_candles by pair_id '{}' timeframe '{}' on sqlite: {}",
pair_id, timeframe_seconds, error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbPairCandleDto::try_from(entity);
let dto = match dto_result {
Ok(dto) => dto,
Err(error) => return Err(error),
};
dtos.push(dto);
}
Ok(dtos)
}
}
}

View File

@@ -199,6 +199,66 @@ ORDER BY created_at ASC, id ASC
}
}
/// Lists trade-event rows for one transaction id ordered by id.
pub async fn list_trade_events_by_transaction_id(
database: &crate::KbDatabase,
transaction_id: i64,
) -> Result<std::vec::Vec<crate::KbTradeEventDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbTradeEventEntity>(
r#"
SELECT
id,
dex_id,
pool_id,
pair_id,
transaction_id,
decoded_event_id,
signature,
slot,
trade_side,
base_token_id,
quote_token_id,
base_amount_raw,
quote_amount_raw,
price_quote_per_base,
source_kind,
source_endpoint_name,
payload_json,
created_at,
updated_at
FROM kb_trade_events
WHERE transaction_id = ?
ORDER BY id ASC
"#,
)
.bind(transaction_id)
.fetch_all(pool)
.await;
let entities = match query_result {
Ok(entities) => entities,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot list kb_trade_events by transaction_id '{}' on sqlite: {}",
transaction_id, error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbTradeEventDto::try_from(entity);
let dto = match dto_result {
Ok(dto) => dto,
Err(error) => return Err(error),
};
dtos.push(dto);
}
Ok(dtos)
}
}
}
fn kb_trade_side_to_string(value: crate::KbSwapTradeSide) -> &'static str {
match value {
crate::KbSwapTradeSide::BuyBase => "BuyBase",