This commit is contained in:
2026-04-24 08:53:40 +02:00
parent a7030d7d0f
commit 54778373b8
31 changed files with 1706 additions and 164 deletions

View File

@@ -0,0 +1,113 @@
// file: kb_lib/src/db/queries/dex.rs
//! Queries for `kb_dexes`.
/// Inserts or updates one normalized DEX row by code.
pub async fn upsert_dex(
database: &crate::KbDatabase,
dto: &crate::KbDexDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_dexes (
code,
name,
program_id,
router_program_id,
is_enabled,
created_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(code) DO UPDATE SET
name = excluded.name,
program_id = excluded.program_id,
router_program_id = excluded.router_program_id,
is_enabled = excluded.is_enabled,
updated_at = excluded.updated_at
"#,
)
.bind(dto.code.clone())
.bind(dto.name.clone())
.bind(dto.program_id.clone())
.bind(dto.router_program_id.clone())
.bind(if dto.is_enabled { 1_i64 } else { 0_i64 })
.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_dexes on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_dexes
WHERE code = ?
LIMIT 1
"#,
)
.bind(dto.code.clone())
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_dexes id for code '{}' on sqlite: {}",
dto.code, error
))),
}
}
}
}
/// Lists normalized DEX rows.
pub async fn list_dexes(
database: &crate::KbDatabase,
) -> Result<std::vec::Vec<crate::KbDexDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbDexEntity>(
r#"
SELECT
id,
code,
name,
program_id,
router_program_id,
is_enabled,
created_at,
updated_at
FROM kb_dexes
ORDER BY code ASC
"#,
)
.fetch_all(pool)
.await;
let entities = match query_result {
Ok(entities) => entities,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot list kb_dexes on sqlite: {}",
error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbDexDto::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,68 @@
// file: kb_lib/src/db/queries/pair.rs
//! Queries for `kb_pairs`.
/// Inserts or updates one normalized pair row by pool id.
pub async fn upsert_pair(
database: &crate::KbDatabase,
dto: &crate::KbPairDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_pairs (
dex_id,
pool_id,
base_token_id,
quote_token_id,
symbol,
first_seen_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(pool_id) DO UPDATE SET
dex_id = excluded.dex_id,
base_token_id = excluded.base_token_id,
quote_token_id = excluded.quote_token_id,
symbol = excluded.symbol,
updated_at = excluded.updated_at
"#,
)
.bind(dto.dex_id)
.bind(dto.pool_id)
.bind(dto.base_token_id)
.bind(dto.quote_token_id)
.bind(dto.symbol.clone())
.bind(dto.first_seen_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_pairs on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_pairs
WHERE pool_id = ?
LIMIT 1
"#,
)
.bind(dto.pool_id)
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_pairs id for pool_id '{}' on sqlite: {}",
dto.pool_id,
error
))),
}
},
}
}

View File

@@ -0,0 +1,64 @@
// file: kb_lib/src/db/queries/pool.rs
//! Queries for `kb_pools`.
/// Inserts or updates one normalized pool row by address.
pub async fn upsert_pool(
database: &crate::KbDatabase,
dto: &crate::KbPoolDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_pools (
dex_id,
address,
pool_kind,
status,
first_seen_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(address) DO UPDATE SET
dex_id = excluded.dex_id,
pool_kind = excluded.pool_kind,
status = excluded.status,
updated_at = excluded.updated_at
"#,
)
.bind(dto.dex_id)
.bind(dto.address.clone())
.bind(dto.pool_kind.to_i16())
.bind(dto.status.to_i16())
.bind(dto.first_seen_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_pools on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_pools
WHERE address = ?
LIMIT 1
"#,
)
.bind(dto.address.clone())
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_pools id for address '{}' on sqlite: {}",
dto.address, error
))),
}
}
}
}

View File

@@ -0,0 +1,211 @@
// file: kb_lib/src/db/queries/pool_listing.rs
//! Queries for `kb_pool_listings`.
/// Inserts or updates one normalized pool listing row by pool id.
pub async fn upsert_pool_listing(
database: &crate::KbDatabase,
dto: &crate::KbPoolListingDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_pool_listings (
dex_id,
pool_id,
pair_id,
source_kind,
source_endpoint_name,
detected_at,
initial_base_reserve,
initial_quote_reserve,
initial_price_quote,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(pool_id) DO UPDATE SET
dex_id = excluded.dex_id,
pair_id = excluded.pair_id,
source_kind = excluded.source_kind,
source_endpoint_name = excluded.source_endpoint_name,
detected_at = excluded.detected_at,
initial_base_reserve = excluded.initial_base_reserve,
initial_quote_reserve = excluded.initial_quote_reserve,
initial_price_quote = excluded.initial_price_quote,
updated_at = excluded.updated_at
"#,
)
.bind(dto.dex_id)
.bind(dto.pool_id)
.bind(dto.pair_id)
.bind(dto.source_kind.to_i16())
.bind(dto.source_endpoint_name.clone())
.bind(dto.detected_at.to_rfc3339())
.bind(dto.initial_base_reserve)
.bind(dto.initial_quote_reserve)
.bind(dto.initial_price_quote)
.bind(dto.updated_at.to_rfc3339())
.execute(pool)
.await;
if let Err(error) = query_result {
return Err(crate::KbError::Db(format!(
"cannot upsert kb_pool_listings on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_pool_listings
WHERE pool_id = ?
LIMIT 1
"#,
)
.bind(dto.pool_id)
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_pool_listings id for pool_id '{}' on sqlite: {}",
dto.pool_id, error
))),
}
}
}
}
#[cfg(test)]
mod tests {
#[tokio::test]
async fn normalized_model_roundtrip_works() {
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
let database_path = tempdir.path().join("normalized_model.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(
"So11111111111111111111111111111111111111112".to_string(),
Some("WSOL".to_string()),
Some("Wrapped SOL".to_string()),
Some(9),
crate::SPL_TOKEN_PROGRAM_ID.to_string(),
true,
),
)
.await
.expect("base token upsert must succeed");
let quote_token_id = crate::upsert_token(
&database,
&crate::KbTokenDto::new(
"DezX111111111111111111111111111111111111111".to_string(),
Some("TEST".to_string()),
Some("Test Token".to_string()),
Some(6),
crate::SPL_TOKEN_PROGRAM_ID.to_string(),
false,
),
)
.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,
quote_token_id,
base_token_id,
Some("TEST/WSOL".to_string()),
),
)
.await
.expect("pair upsert must succeed");
let _pool_token_a_id = crate::upsert_pool_token(
&database,
&crate::KbPoolTokenDto::new(
pool_id,
quote_token_id,
crate::KbPoolTokenRole::Base,
None,
Some(0),
),
)
.await
.expect("pool token a upsert must succeed");
let _pool_token_b_id = crate::upsert_pool_token(
&database,
&crate::KbPoolTokenDto::new(
pool_id,
base_token_id,
crate::KbPoolTokenRole::Quote,
None,
Some(1),
),
)
.await
.expect("pool token b upsert must succeed");
let listing_id = crate::upsert_pool_listing(
&database,
&crate::KbPoolListingDto::new(
dex_id,
pool_id,
Some(pair_id),
crate::KbObservationSourceKind::WsRpc,
Some("mainnet_public_ws_slots".to_string()),
Some(1000.0),
Some(42.0),
Some(0.042),
),
)
.await
.expect("pool listing upsert must succeed");
assert!(listing_id > 0);
let dexes = crate::list_dexes(&database)
.await
.expect("dex list must succeed");
assert_eq!(dexes.len(), 1);
let token =
crate::get_token_by_mint(&database, "So11111111111111111111111111111111111111112")
.await
.expect("token get must succeed");
assert!(token.is_some());
}
}

View File

@@ -0,0 +1,67 @@
// file: kb_lib/src/db/queries/pool_token.rs
//! Queries for `kb_pool_tokens`.
/// Inserts or updates one normalized pool token composition row.
pub async fn upsert_pool_token(
database: &crate::KbDatabase,
dto: &crate::KbPoolTokenDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_pool_tokens (
pool_id,
token_id,
role,
vault_address,
token_order,
created_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(pool_id, token_id, role) DO UPDATE SET
vault_address = excluded.vault_address,
token_order = excluded.token_order,
updated_at = excluded.updated_at
"#,
)
.bind(dto.pool_id)
.bind(dto.token_id)
.bind(dto.role.to_i16())
.bind(dto.vault_address.clone())
.bind(dto.token_order)
.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_pool_tokens on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_pool_tokens
WHERE pool_id = ? AND token_id = ? AND role = ?
LIMIT 1
"#,
)
.bind(dto.pool_id)
.bind(dto.token_id)
.bind(dto.role.to_i16())
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_pool_tokens id on sqlite: {}",
error
))),
}
},
}
}

View File

@@ -0,0 +1,121 @@
// file: kb_lib/src/db/queries/token.rs
//! Queries for `kb_tokens`.
/// Inserts or updates one normalized token row by mint.
pub async fn upsert_token(
database: &crate::KbDatabase,
dto: &crate::KbTokenDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let decimals = dto.decimals.map(i64::from);
let query_result = sqlx::query(
r#"
INSERT INTO kb_tokens (
mint,
symbol,
name,
decimals,
token_program,
is_quote_token,
first_seen_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(mint) DO UPDATE SET
symbol = excluded.symbol,
name = excluded.name,
decimals = excluded.decimals,
token_program = excluded.token_program,
is_quote_token = excluded.is_quote_token,
updated_at = excluded.updated_at
"#,
)
.bind(dto.mint.clone())
.bind(dto.symbol.clone())
.bind(dto.name.clone())
.bind(decimals)
.bind(dto.token_program.clone())
.bind(if dto.is_quote_token { 1_i64 } else { 0_i64 })
.bind(dto.first_seen_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_tokens on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_tokens
WHERE mint = ?
LIMIT 1
"#,
)
.bind(dto.mint.clone())
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_tokens id for mint '{}' on sqlite: {}",
dto.mint, error
))),
}
}
}
}
/// Reads one normalized token row by mint.
pub async fn get_token_by_mint(
database: &crate::KbDatabase,
mint: &str,
) -> Result<std::option::Option<crate::KbTokenDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbTokenEntity>(
r#"
SELECT
id,
mint,
symbol,
name,
decimals,
token_program,
is_quote_token,
first_seen_at,
updated_at
FROM kb_tokens
WHERE mint = ?
LIMIT 1
"#,
)
.bind(mint)
.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_tokens '{}' on sqlite: {}",
mint, error
)));
}
};
match entity_option {
Some(entity) => {
let dto_result = crate::KbTokenDto::try_from(entity);
match dto_result {
Ok(dto) => Ok(Some(dto)),
Err(error) => Err(error),
}
}
None => Ok(None),
}
}
}
}