This commit is contained in:
2026-04-24 12:01:35 +02:00
parent 54778373b8
commit 0ad6145091
23 changed files with 1653 additions and 5 deletions

View File

@@ -0,0 +1,167 @@
// file: kb_lib/src/db/queries/liquidity_event.rs
//! Queries for `kb_liquidity_events`.
/// Inserts or updates one normalized liquidity event row.
pub async fn upsert_liquidity_event(
database: &crate::KbDatabase,
dto: &crate::KbLiquidityEventDto,
) -> Result<i64, crate::KbError> {
let slot_i64 = match dto.slot {
Some(slot) => {
let slot_result = i64::try_from(slot);
match slot_result {
Ok(slot) => Some(slot),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot convert liquidity event slot '{}' to i64: {}",
slot, error
)));
}
}
}
None => None,
};
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_liquidity_events (
dex_id,
pool_id,
pair_id,
signature,
instruction_index,
slot,
event_kind,
actor_wallet,
base_token_id,
quote_token_id,
lp_token_id,
base_amount,
quote_amount,
lp_amount,
executed_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(signature, instruction_index) DO UPDATE SET
dex_id = excluded.dex_id,
pool_id = excluded.pool_id,
pair_id = excluded.pair_id,
slot = excluded.slot,
event_kind = excluded.event_kind,
actor_wallet = excluded.actor_wallet,
base_token_id = excluded.base_token_id,
quote_token_id = excluded.quote_token_id,
lp_token_id = excluded.lp_token_id,
base_amount = excluded.base_amount,
quote_amount = excluded.quote_amount,
lp_amount = excluded.lp_amount,
executed_at = excluded.executed_at
"#,
)
.bind(dto.dex_id)
.bind(dto.pool_id)
.bind(dto.pair_id)
.bind(dto.signature.clone())
.bind(dto.instruction_index)
.bind(slot_i64)
.bind(dto.event_kind.to_i16())
.bind(dto.actor_wallet.clone())
.bind(dto.base_token_id)
.bind(dto.quote_token_id)
.bind(dto.lp_token_id)
.bind(dto.base_amount.clone())
.bind(dto.quote_amount.clone())
.bind(dto.lp_amount.clone())
.bind(dto.executed_at.to_rfc3339())
.execute(pool)
.await;
if let Err(error) = query_result {
return Err(crate::KbError::Db(format!(
"cannot upsert kb_liquidity_events on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_liquidity_events
WHERE signature = ? AND instruction_index = ?
LIMIT 1
"#,
)
.bind(dto.signature.clone())
.bind(dto.instruction_index)
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_liquidity_events id for signature '{}' and instruction_index '{}' on sqlite: {}",
dto.signature, dto.instruction_index, error
))),
}
}
}
}
/// Lists recent liquidity events ordered from newest to oldest.
pub async fn list_recent_liquidity_events(
database: &crate::KbDatabase,
limit: u32,
) -> Result<std::vec::Vec<crate::KbLiquidityEventDto>, crate::KbError> {
if limit == 0 {
return Ok(std::vec::Vec::new());
}
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbLiquidityEventEntity>(
r#"
SELECT
id,
dex_id,
pool_id,
pair_id,
signature,
instruction_index,
slot,
event_kind,
actor_wallet,
base_token_id,
quote_token_id,
lp_token_id,
base_amount,
quote_amount,
lp_amount,
executed_at
FROM kb_liquidity_events
ORDER BY id DESC
LIMIT ?
"#,
)
.bind(i64::from(limit))
.fetch_all(pool)
.await;
let entities = match query_result {
Ok(entities) => entities,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot list kb_liquidity_events on sqlite: {}",
error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbLiquidityEventDto::try_from(entity);
let dto = match dto_result {
Ok(dto) => dto,
Err(error) => return Err(error),
};
dtos.push(dto);
}
Ok(dtos)
}
}
}

View File

@@ -0,0 +1,163 @@
// file: kb_lib/src/db/queries/swap.rs
//! Queries for `kb_swaps`.
/// Inserts or updates one normalized swap row.
pub async fn upsert_swap(
database: &crate::KbDatabase,
dto: &crate::KbSwapDto,
) -> Result<i64, crate::KbError> {
let slot_i64 = match dto.slot {
Some(slot) => {
let slot_result = i64::try_from(slot);
match slot_result {
Ok(slot) => Some(slot),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot convert swap slot '{}' to i64: {}",
slot, error
)));
}
}
}
None => None,
};
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_swaps (
dex_id,
pool_id,
pair_id,
signature,
instruction_index,
slot,
trader_wallet,
base_token_id,
quote_token_id,
base_amount,
quote_amount,
price_quote,
trade_side,
executed_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(signature, instruction_index) DO UPDATE SET
dex_id = excluded.dex_id,
pool_id = excluded.pool_id,
pair_id = excluded.pair_id,
slot = excluded.slot,
trader_wallet = excluded.trader_wallet,
base_token_id = excluded.base_token_id,
quote_token_id = excluded.quote_token_id,
base_amount = excluded.base_amount,
quote_amount = excluded.quote_amount,
price_quote = excluded.price_quote,
trade_side = excluded.trade_side,
executed_at = excluded.executed_at
"#,
)
.bind(dto.dex_id)
.bind(dto.pool_id)
.bind(dto.pair_id)
.bind(dto.signature.clone())
.bind(dto.instruction_index)
.bind(slot_i64)
.bind(dto.trader_wallet.clone())
.bind(dto.base_token_id)
.bind(dto.quote_token_id)
.bind(dto.base_amount.clone())
.bind(dto.quote_amount.clone())
.bind(dto.price_quote.clone())
.bind(dto.trade_side.to_i16())
.bind(dto.executed_at.to_rfc3339())
.execute(pool)
.await;
if let Err(error) = query_result {
return Err(crate::KbError::Db(format!(
"cannot upsert kb_swaps on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_swaps
WHERE signature = ? AND instruction_index = ?
LIMIT 1
"#,
)
.bind(dto.signature.clone())
.bind(dto.instruction_index)
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_swaps id for signature '{}' and instruction_index '{}' on sqlite: {}",
dto.signature, dto.instruction_index, error
))),
}
}
}
}
/// Lists recent swaps ordered from newest to oldest.
pub async fn list_recent_swaps(
database: &crate::KbDatabase,
limit: u32,
) -> Result<std::vec::Vec<crate::KbSwapDto>, crate::KbError> {
if limit == 0 {
return Ok(std::vec::Vec::new());
}
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbSwapEntity>(
r#"
SELECT
id,
dex_id,
pool_id,
pair_id,
signature,
instruction_index,
slot,
trader_wallet,
base_token_id,
quote_token_id,
base_amount,
quote_amount,
price_quote,
trade_side,
executed_at
FROM kb_swaps
ORDER BY id DESC
LIMIT ?
"#,
)
.bind(i64::from(limit))
.fetch_all(pool)
.await;
let entities = match query_result {
Ok(entities) => entities,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot list kb_swaps on sqlite: {}",
error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbSwapDto::try_from(entity);
let dto = match dto_result {
Ok(dto) => dto,
Err(error) => return Err(error),
};
dtos.push(dto);
}
Ok(dtos)
}
}
}

View File

@@ -0,0 +1,319 @@
// file: kb_lib/src/db/queries/token_burn_event.rs
//! Queries for `kb_token_burn_events`.
/// Inserts or updates one normalized token burn event row.
pub async fn upsert_token_burn_event(
database: &crate::KbDatabase,
dto: &crate::KbTokenBurnEventDto,
) -> Result<i64, crate::KbError> {
let slot_i64 = match dto.slot {
Some(slot) => {
let slot_result = i64::try_from(slot);
match slot_result {
Ok(slot) => Some(slot),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot convert token burn event slot '{}' to i64: {}",
slot, error
)));
}
}
}
None => None,
};
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_token_burn_events (
token_id,
signature,
instruction_index,
slot,
authority_wallet,
source_wallet,
amount,
supply_after,
executed_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(signature, instruction_index) DO UPDATE SET
token_id = excluded.token_id,
slot = excluded.slot,
authority_wallet = excluded.authority_wallet,
source_wallet = excluded.source_wallet,
amount = excluded.amount,
supply_after = excluded.supply_after,
executed_at = excluded.executed_at
"#,
)
.bind(dto.token_id)
.bind(dto.signature.clone())
.bind(dto.instruction_index)
.bind(slot_i64)
.bind(dto.authority_wallet.clone())
.bind(dto.source_wallet.clone())
.bind(dto.amount.clone())
.bind(dto.supply_after.clone())
.bind(dto.executed_at.to_rfc3339())
.execute(pool)
.await;
if let Err(error) = query_result {
return Err(crate::KbError::Db(format!(
"cannot upsert kb_token_burn_events on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_token_burn_events
WHERE signature = ? AND instruction_index = ?
LIMIT 1
"#,
)
.bind(dto.signature.clone())
.bind(dto.instruction_index)
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_token_burn_events id for signature '{}' and instruction_index '{}' on sqlite: {}",
dto.signature, dto.instruction_index, error
))),
}
}
}
}
/// Lists recent token burn events ordered from newest to oldest.
pub async fn list_recent_token_burn_events(
database: &crate::KbDatabase,
limit: u32,
) -> Result<std::vec::Vec<crate::KbTokenBurnEventDto>, crate::KbError> {
if limit == 0 {
return Ok(std::vec::Vec::new());
}
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbTokenBurnEventEntity>(
r#"
SELECT
id,
token_id,
signature,
instruction_index,
slot,
authority_wallet,
source_wallet,
amount,
supply_after,
executed_at
FROM kb_token_burn_events
ORDER BY id DESC
LIMIT ?
"#,
)
.bind(i64::from(limit))
.fetch_all(pool)
.await;
let entities = match query_result {
Ok(entities) => entities,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot list kb_token_burn_events on sqlite: {}",
error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbTokenBurnEventDto::try_from(entity);
let dto = match dto_result {
Ok(dto) => dto,
Err(error) => return Err(error),
};
dtos.push(dto);
}
Ok(dtos)
}
}
}
#[cfg(test)]
mod tests {
#[tokio::test]
async fn normalized_activity_roundtrip_works() {
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
let database_path = tempdir.path().join("normalized_activity.sqlite3");
let config = crate::KbDatabaseConfig {
enabled: true,
backend: crate::KbDatabaseBackend::Sqlite,
sqlite: crate::KbSqliteDatabaseConfig {
path: database_path.to_string_lossy().to_string(),
create_if_missing: true,
busy_timeout_ms: 5000,
max_connections: 1,
auto_initialize_schema: true,
use_wal: true,
},
};
let database = crate::KbDatabase::connect_and_initialize(&config)
.await
.expect("database init must succeed");
let dex_id = crate::upsert_dex(
&database,
&crate::KbDexDto::new(
"raydium".to_string(),
"Raydium".to_string(),
None,
None,
true,
),
)
.await
.expect("dex upsert must succeed");
let base_token_id = crate::upsert_token(
&database,
&crate::KbTokenDto::new(
"Base111111111111111111111111111111111111111".to_string(),
Some("BASE".to_string()),
Some("Base Token".to_string()),
Some(6),
crate::SPL_TOKEN_PROGRAM_ID.to_string(),
false,
),
)
.await
.expect("base token upsert must succeed");
let quote_token_id = crate::upsert_token(
&database,
&crate::KbTokenDto::new(
"So11111111111111111111111111111111111111112".to_string(),
Some("WSOL".to_string()),
Some("Wrapped SOL".to_string()),
Some(9),
crate::SPL_TOKEN_PROGRAM_ID.to_string(),
true,
),
)
.await
.expect("quote token upsert must succeed");
let pool_id = crate::upsert_pool(
&database,
&crate::KbPoolDto::new(
dex_id,
"Pool111111111111111111111111111111111111111".to_string(),
crate::KbPoolKind::Amm,
crate::KbPoolStatus::Active,
),
)
.await
.expect("pool upsert must succeed");
let pair_id = crate::upsert_pair(
&database,
&crate::KbPairDto::new(
dex_id,
pool_id,
base_token_id,
quote_token_id,
Some("BASE/WSOL".to_string()),
),
)
.await
.expect("pair upsert must succeed");
let swap_id = crate::upsert_swap(
&database,
&crate::KbSwapDto::new(
dex_id,
pool_id,
Some(pair_id),
"swap-signature-1".to_string(),
0,
Some(1000),
Some("Trader11111111111111111111111111111111111".to_string()),
base_token_id,
quote_token_id,
"1000.50".to_string(),
"2.75".to_string(),
Some("0.002748625687156422".to_string()),
crate::KbSwapTradeSide::BuyBase,
),
)
.await
.expect("swap upsert must succeed");
let liquidity_id = crate::upsert_liquidity_event(
&database,
&crate::KbLiquidityEventDto::new(
dex_id,
pool_id,
Some(pair_id),
"liq-signature-1".to_string(),
1,
Some(1001),
crate::KbLiquidityEventKind::Add,
Some("LpUser1111111111111111111111111111111111".to_string()),
base_token_id,
quote_token_id,
None,
"5000".to_string(),
"15".to_string(),
None,
),
)
.await
.expect("liquidity event upsert must succeed");
let mint_id = crate::upsert_token_mint_event(
&database,
&crate::KbTokenMintEventDto::new(
base_token_id,
"mint-signature-1".to_string(),
2,
Some(1002),
Some("MintAuthority111111111111111111111111111".to_string()),
Some("Dest1111111111111111111111111111111111111".to_string()),
"1000000".to_string(),
Some("5000000".to_string()),
),
)
.await
.expect("token mint event upsert must succeed");
let burn_id = crate::upsert_token_burn_event(
&database,
&crate::KbTokenBurnEventDto::new(
base_token_id,
"burn-signature-1".to_string(),
3,
Some(1003),
Some("BurnAuthority111111111111111111111111111".to_string()),
Some("Source11111111111111111111111111111111111".to_string()),
"25000".to_string(),
Some("4975000".to_string()),
),
)
.await
.expect("token burn event upsert must succeed");
assert!(swap_id > 0);
assert!(liquidity_id > 0);
assert!(mint_id > 0);
assert!(burn_id > 0);
let swaps = crate::list_recent_swaps(&database, 10)
.await
.expect("swaps list must succeed");
let liquidity_events = crate::list_recent_liquidity_events(&database, 10)
.await
.expect("liquidity list must succeed");
let mint_events = crate::list_recent_token_mint_events(&database, 10)
.await
.expect("mint events list must succeed");
let burn_events = crate::list_recent_token_burn_events(&database, 10)
.await
.expect("burn events list must succeed");
assert_eq!(swaps.len(), 1);
assert_eq!(liquidity_events.len(), 1);
assert_eq!(mint_events.len(), 1);
assert_eq!(burn_events.len(), 1);
}
}

View File

@@ -0,0 +1,143 @@
// file: kb_lib/src/db/queries/token_mint_event.rs
//! Queries for `kb_token_mint_events`.
/// Inserts or updates one normalized token mint event row.
pub async fn upsert_token_mint_event(
database: &crate::KbDatabase,
dto: &crate::KbTokenMintEventDto,
) -> Result<i64, crate::KbError> {
let slot_i64 = match dto.slot {
Some(slot) => {
let slot_result = i64::try_from(slot);
match slot_result {
Ok(slot) => Some(slot),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot convert token mint event slot '{}' to i64: {}",
slot, error
)));
}
}
}
None => None,
};
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_token_mint_events (
token_id,
signature,
instruction_index,
slot,
authority_wallet,
destination_wallet,
amount,
supply_after,
executed_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(signature, instruction_index) DO UPDATE SET
token_id = excluded.token_id,
slot = excluded.slot,
authority_wallet = excluded.authority_wallet,
destination_wallet = excluded.destination_wallet,
amount = excluded.amount,
supply_after = excluded.supply_after,
executed_at = excluded.executed_at
"#,
)
.bind(dto.token_id)
.bind(dto.signature.clone())
.bind(dto.instruction_index)
.bind(slot_i64)
.bind(dto.authority_wallet.clone())
.bind(dto.destination_wallet.clone())
.bind(dto.amount.clone())
.bind(dto.supply_after.clone())
.bind(dto.executed_at.to_rfc3339())
.execute(pool)
.await;
if let Err(error) = query_result {
return Err(crate::KbError::Db(format!(
"cannot upsert kb_token_mint_events on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_token_mint_events
WHERE signature = ? AND instruction_index = ?
LIMIT 1
"#,
)
.bind(dto.signature.clone())
.bind(dto.instruction_index)
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_token_mint_events id for signature '{}' and instruction_index '{}' on sqlite: {}",
dto.signature, dto.instruction_index, error
))),
}
}
}
}
/// Lists recent token mint events ordered from newest to oldest.
pub async fn list_recent_token_mint_events(
database: &crate::KbDatabase,
limit: u32,
) -> Result<std::vec::Vec<crate::KbTokenMintEventDto>, crate::KbError> {
if limit == 0 {
return Ok(std::vec::Vec::new());
}
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbTokenMintEventEntity>(
r#"
SELECT
id,
token_id,
signature,
instruction_index,
slot,
authority_wallet,
destination_wallet,
amount,
supply_after,
executed_at
FROM kb_token_mint_events
ORDER BY id DESC
LIMIT ?
"#,
)
.bind(i64::from(limit))
.fetch_all(pool)
.await;
let entities = match query_result {
Ok(entities) => entities,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot list kb_token_mint_events on sqlite: {}",
error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbTokenMintEventDto::try_from(entity);
let dto = match dto_result {
Ok(dto) => dto,
Err(error) => return Err(error),
};
dtos.push(dto);
}
Ok(dtos)
}
}
}