This commit is contained in:
2026-04-26 12:34:23 +02:00
parent ac5bf10af6
commit 081758995a
16 changed files with 1302 additions and 37 deletions

View File

@@ -22,6 +22,7 @@ mod token_mint_event;
mod chain_instruction;
mod chain_slot;
mod chain_transaction;
mod dex_decoded_event;
pub use analysis_signal::KbAnalysisSignalDto;
pub use db_metadata::KbDbMetadataDto;
@@ -43,3 +44,4 @@ pub use token_mint_event::KbTokenMintEventDto;
pub use chain_instruction::KbChainInstructionDto;
pub use chain_slot::KbChainSlotDto;
pub use chain_transaction::KbChainTransactionDto;
pub use dex_decoded_event::KbDexDecodedEventDto;

View File

@@ -0,0 +1,100 @@
// file: kb_lib/src/db/dtos/dex_decoded_event.rs
//! Application-facing decoded DEX event DTO.
/// Application-facing decoded DEX event DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KbDexDecodedEventDto {
/// Optional numeric primary key.
pub id: std::option::Option<i64>,
/// Parent transaction id.
pub transaction_id: i64,
/// Optional parent instruction id.
pub instruction_id: std::option::Option<i64>,
/// Decoded protocol name.
pub protocol_name: std::string::String,
/// Program id that produced the decoded event.
pub program_id: std::string::String,
/// Event kind.
pub event_kind: std::string::String,
/// Optional decoded pool account.
pub pool_account: std::option::Option<std::string::String>,
/// Optional decoded lp mint.
pub lp_mint: std::option::Option<std::string::String>,
/// Optional decoded token A mint.
pub token_a_mint: std::option::Option<std::string::String>,
/// Optional decoded token B mint.
pub token_b_mint: std::option::Option<std::string::String>,
/// Optional decoded market account.
pub market_account: std::option::Option<std::string::String>,
/// Serialized decoded payload.
pub payload_json: std::string::String,
/// Creation timestamp.
pub created_at: chrono::DateTime<chrono::Utc>,
}
impl KbDexDecodedEventDto {
/// Creates a new decoded DEX event DTO.
#[allow(clippy::too_many_arguments)]
pub fn new(
transaction_id: i64,
instruction_id: std::option::Option<i64>,
protocol_name: std::string::String,
program_id: std::string::String,
event_kind: std::string::String,
pool_account: std::option::Option<std::string::String>,
lp_mint: std::option::Option<std::string::String>,
token_a_mint: std::option::Option<std::string::String>,
token_b_mint: std::option::Option<std::string::String>,
market_account: std::option::Option<std::string::String>,
payload_json: std::string::String,
) -> Self {
Self {
id: None,
transaction_id,
instruction_id,
protocol_name,
program_id,
event_kind,
pool_account,
lp_mint,
token_a_mint,
token_b_mint,
market_account,
payload_json,
created_at: chrono::Utc::now(),
}
}
}
impl TryFrom<crate::KbDexDecodedEventEntity> for KbDexDecodedEventDto {
type Error = crate::KbError;
fn try_from(entity: crate::KbDexDecodedEventEntity) -> Result<Self, Self::Error> {
let created_at_result = chrono::DateTime::parse_from_rfc3339(&entity.created_at);
let created_at = match created_at_result {
Ok(created_at) => created_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse dex decoded event created_at '{}': {}",
entity.created_at, error
)));
}
};
Ok(Self {
id: Some(entity.id),
transaction_id: entity.transaction_id,
instruction_id: entity.instruction_id,
protocol_name: entity.protocol_name,
program_id: entity.program_id,
event_kind: entity.event_kind,
pool_account: entity.pool_account,
lp_mint: entity.lp_mint,
token_a_mint: entity.token_a_mint,
token_b_mint: entity.token_b_mint,
market_account: entity.market_account,
payload_json: entity.payload_json,
created_at,
})
}
}

View File

@@ -24,6 +24,7 @@ mod token_mint_event;
mod chain_instruction;
mod chain_slot;
mod chain_transaction;
mod dex_decoded_event;
pub use analysis_signal::KbAnalysisSignalEntity;
pub use db_metadata::KbDbMetadataEntity;
@@ -45,3 +46,4 @@ pub use token_mint_event::KbTokenMintEventEntity;
pub use chain_instruction::KbChainInstructionEntity;
pub use chain_slot::KbChainSlotEntity;
pub use chain_transaction::KbChainTransactionEntity;
pub use dex_decoded_event::KbDexDecodedEventEntity;

View File

@@ -0,0 +1,34 @@
// file: kb_lib/src/db/entities/dex_decoded_event.rs
//! Database entity for one decoded DEX event.
/// Persisted decoded DEX event row.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, sqlx::FromRow)]
pub struct KbDexDecodedEventEntity {
/// Internal row id.
pub id: i64,
/// Parent transaction id.
pub transaction_id: i64,
/// Optional parent instruction id.
pub instruction_id: std::option::Option<i64>,
/// Decoded protocol name.
pub protocol_name: std::string::String,
/// Program id that produced the decoded event.
pub program_id: std::string::String,
/// Event kind.
pub event_kind: std::string::String,
/// Optional decoded pool account.
pub pool_account: std::option::Option<std::string::String>,
/// Optional decoded lp mint.
pub lp_mint: std::option::Option<std::string::String>,
/// Optional decoded token A mint.
pub token_a_mint: std::option::Option<std::string::String>,
/// Optional decoded token B mint.
pub token_b_mint: std::option::Option<std::string::String>,
/// Optional decoded market account.
pub market_account: std::option::Option<std::string::String>,
/// Serialized decoded payload.
pub payload_json: std::string::String,
/// Creation timestamp.
pub created_at: std::string::String,
}

View File

@@ -26,6 +26,7 @@ mod token_mint_event;
mod chain_instruction;
mod chain_slot;
mod chain_transaction;
mod dex_decoded_event;
pub use analysis_signal::insert_analysis_signal;
pub use analysis_signal::list_recent_analysis_signals;
@@ -78,3 +79,6 @@ pub use chain_slot::get_chain_slot;
pub use chain_transaction::list_recent_chain_transactions;
pub use chain_transaction::upsert_chain_transaction;
pub use chain_transaction::get_chain_transaction_by_signature;
pub use dex_decoded_event::get_dex_decoded_event_by_key;
pub use dex_decoded_event::list_dex_decoded_events_by_transaction_id;
pub use dex_decoded_event::upsert_dex_decoded_event;

View File

@@ -0,0 +1,322 @@
// file: kb_lib/src/db/queries/dex_decoded_event.rs
//! Queries for `kb_dex_decoded_events`.
/// Inserts or updates one decoded DEX event row.
pub async fn upsert_dex_decoded_event(
database: &crate::KbDatabase,
dto: &crate::KbDexDecodedEventDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_dex_decoded_events (
transaction_id,
instruction_id,
protocol_name,
program_id,
event_kind,
pool_account,
lp_mint,
token_a_mint,
token_b_mint,
market_account,
payload_json,
created_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(transaction_id, instruction_id, event_kind) DO UPDATE SET
protocol_name = excluded.protocol_name,
program_id = excluded.program_id,
pool_account = excluded.pool_account,
lp_mint = excluded.lp_mint,
token_a_mint = excluded.token_a_mint,
token_b_mint = excluded.token_b_mint,
market_account = excluded.market_account,
payload_json = excluded.payload_json
"#,
)
.bind(dto.transaction_id)
.bind(dto.instruction_id)
.bind(dto.protocol_name.clone())
.bind(dto.program_id.clone())
.bind(dto.event_kind.clone())
.bind(dto.pool_account.clone())
.bind(dto.lp_mint.clone())
.bind(dto.token_a_mint.clone())
.bind(dto.token_b_mint.clone())
.bind(dto.market_account.clone())
.bind(dto.payload_json.clone())
.bind(dto.created_at.to_rfc3339())
.execute(pool)
.await;
if let Err(error) = query_result {
return Err(crate::KbError::Db(format!(
"cannot upsert kb_dex_decoded_events on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_dex_decoded_events
WHERE transaction_id = ?
AND (
(instruction_id IS NULL AND ? IS NULL)
OR instruction_id = ?
)
AND event_kind = ?
LIMIT 1
"#,
)
.bind(dto.transaction_id)
.bind(dto.instruction_id)
.bind(dto.instruction_id)
.bind(dto.event_kind.clone())
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_dex_decoded_events id on sqlite: {}",
error
))),
}
}
}
}
/// Reads one decoded DEX event by its natural key.
pub async fn get_dex_decoded_event_by_key(
database: &crate::KbDatabase,
transaction_id: i64,
instruction_id: std::option::Option<i64>,
event_kind: &str,
) -> Result<std::option::Option<crate::KbDexDecodedEventDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbDexDecodedEventEntity>(
r#"
SELECT
id,
transaction_id,
instruction_id,
protocol_name,
program_id,
event_kind,
pool_account,
lp_mint,
token_a_mint,
token_b_mint,
market_account,
payload_json,
created_at
FROM kb_dex_decoded_events
WHERE transaction_id = ?
AND (
(instruction_id IS NULL AND ? IS NULL)
OR instruction_id = ?
)
AND event_kind = ?
LIMIT 1
"#,
)
.bind(transaction_id)
.bind(instruction_id)
.bind(instruction_id)
.bind(event_kind.to_string())
.fetch_optional(pool)
.await;
let entity_option = match query_result {
Ok(entity_option) => entity_option,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot fetch kb_dex_decoded_events on sqlite: {}",
error
)));
}
};
match entity_option {
Some(entity) => {
let dto_result = crate::KbDexDecodedEventDto::try_from(entity);
match dto_result {
Ok(dto) => Ok(Some(dto)),
Err(error) => Err(error),
}
}
None => Ok(None),
}
}
}
}
/// Lists decoded DEX events for one transaction.
pub async fn list_dex_decoded_events_by_transaction_id(
database: &crate::KbDatabase,
transaction_id: i64,
) -> Result<std::vec::Vec<crate::KbDexDecodedEventDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbDexDecodedEventEntity>(
r#"
SELECT
id,
transaction_id,
instruction_id,
protocol_name,
program_id,
event_kind,
pool_account,
lp_mint,
token_a_mint,
token_b_mint,
market_account,
payload_json,
created_at
FROM kb_dex_decoded_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_dex_decoded_events on sqlite: {}",
error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbDexDecodedEventDto::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 {
async fn make_database() -> crate::KbDatabase {
let tempdir_result = tempfile::tempdir();
let tempdir = match tempdir_result {
Ok(tempdir) => tempdir,
Err(error) => panic!("tempdir must succeed: {}", error),
};
let database_path = tempdir.path().join("dex_decoded_event.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_result = crate::KbDatabase::connect_and_initialize(&config).await;
match database_result {
Ok(database) => database,
Err(error) => panic!("database init must succeed: {}", error),
}
}
#[tokio::test]
async fn dex_decoded_event_roundtrip_works() {
let database = make_database().await;
let transaction_dto = crate::KbChainTransactionDto::new(
"sig-dex-event-test-1".to_string(),
None,
None,
Some("helius_primary_http".to_string()),
Some("0".to_string()),
None,
None,
r#"{"transaction":{"message":{"instructions":[]}}}"#.to_string(),
);
let transaction_id_result =
crate::upsert_chain_transaction(&database, &transaction_dto).await;
let transaction_id = match transaction_id_result {
Ok(transaction_id) => transaction_id,
Err(error) => panic!("transaction upsert must succeed: {}", error),
};
let instruction_dto = crate::KbChainInstructionDto::new(
transaction_id,
None,
0,
None,
Some(crate::KB_RAYDIUM_AMM_V4_PROGRAM_ID.to_string()),
Some("raydium-amm-v4".to_string()),
Some(1),
r#"["Account0","Pool111","Lp111","TokenA111","TokenB111"]"#.to_string(),
None,
None,
None,
);
let instruction_id_result =
crate::insert_chain_instruction(&database, &instruction_dto).await;
let instruction_id = match instruction_id_result {
Ok(instruction_id) => instruction_id,
Err(error) => panic!("instruction insert must succeed: {}", error),
};
let dto = crate::KbDexDecodedEventDto::new(
transaction_id,
Some(instruction_id),
"raydium_amm_v4".to_string(),
crate::KB_RAYDIUM_AMM_V4_PROGRAM_ID.to_string(),
"raydium_amm_v4.initialize2_pool".to_string(),
Some("Pool111".to_string()),
Some("Lp111".to_string()),
Some("TokenA111".to_string()),
Some("TokenB111".to_string()),
Some("Market111".to_string()),
r#"{"k":"v"}"#.to_string(),
);
let event_id_result = crate::upsert_dex_decoded_event(&database, &dto).await;
let event_id = match event_id_result {
Ok(event_id) => event_id,
Err(error) => panic!("event upsert must succeed: {}", error),
};
assert!(event_id > 0);
let fetched_result = crate::get_dex_decoded_event_by_key(
&database,
transaction_id,
Some(instruction_id),
"raydium_amm_v4.initialize2_pool",
)
.await;
let fetched_option = match fetched_result {
Ok(fetched_option) => fetched_option,
Err(error) => panic!("event fetch must succeed: {}", error),
};
let fetched = match fetched_option {
Some(fetched) => fetched,
None => panic!("event must exist"),
};
assert_eq!(fetched.id, Some(event_id));
assert_eq!(fetched.transaction_id, transaction_id);
assert_eq!(fetched.instruction_id, Some(instruction_id));
assert_eq!(fetched.protocol_name, "raydium_amm_v4");
assert_eq!(fetched.pool_account, Some("Pool111".to_string()));
let listed_result =
crate::list_dex_decoded_events_by_transaction_id(&database, transaction_id).await;
let listed = match listed_result {
Ok(listed) => listed,
Err(error) => panic!("event list must succeed: {}", error),
};
assert_eq!(listed.len(), 1);
assert_eq!(listed[0].event_kind, "raydium_amm_v4.initialize2_pool");
}
}

View File

@@ -218,7 +218,18 @@ pub(crate) async fn ensure_schema(database: &crate::KbDatabase) -> Result<(), cr
if let Err(error) = result {
return Err(error);
}
let result = create_kb_dex_decoded_events_table(pool).await;
if let Err(error) = result {
return Err(error);
}
let result = create_kb_idx_dex_decoded_events_transaction_id(pool).await;
if let Err(error) = result {
return Err(error);
}
let result = create_kb_uq_dex_decoded_events_transaction_instruction_event(pool).await;
if let Err(error) = result {
return Err(error);
}
Ok(())
}
}
@@ -240,6 +251,21 @@ async fn execute_sqlite_schema_statement(
}
}
/// Updates the persisted schema version metadata entry.
async fn update_schema_version_metadata(
database: &crate::KbDatabase,
) -> Result<(), crate::KbError> {
let schema_version = crate::KbDbMetadataDto::new(
"schema_version".to_string(),
env!("CARGO_PKG_VERSION").to_string(),
);
let upsert_result = crate::upsert_db_metadata(database, &schema_version).await;
match upsert_result {
Ok(_) => Ok(()),
Err(error) => Err(error),
}
}
/// Creates `kb_db_metadata`.
async fn create_kb_db_metadata_table(pool: &sqlx::SqlitePool) -> Result<(), crate::KbError> {
execute_sqlite_schema_statement(
@@ -1073,9 +1099,7 @@ CREATE TABLE IF NOT EXISTS kb_chain_slots (
}
/// Creates `kb_chain_transactions`.
async fn create_kb_chain_transactions_table(
pool: &sqlx::SqlitePool,
) -> Result<(), crate::KbError> {
async fn create_kb_chain_transactions_table(pool: &sqlx::SqlitePool) -> Result<(), crate::KbError> {
execute_sqlite_schema_statement(
pool,
"create_kb_chain_transactions_table",
@@ -1115,9 +1139,7 @@ ON kb_chain_transactions (slot)
}
/// Creates `kb_chain_instructions`.
async fn create_kb_chain_instructions_table(
pool: &sqlx::SqlitePool,
) -> Result<(), crate::KbError> {
async fn create_kb_chain_instructions_table(pool: &sqlx::SqlitePool) -> Result<(), crate::KbError> {
execute_sqlite_schema_statement(
pool,
"create_kb_chain_instructions_table",
@@ -1174,17 +1196,60 @@ ON kb_chain_instructions (program_id)
.await
}
/// Updates the persisted schema version metadata entry.
async fn update_schema_version_metadata(
database: &crate::KbDatabase,
) -> Result<(), crate::KbError> {
let schema_version = crate::KbDbMetadataDto::new(
"schema_version".to_string(),
env!("CARGO_PKG_VERSION").to_string(),
);
let upsert_result = crate::upsert_db_metadata(database, &schema_version).await;
match upsert_result {
Ok(_) => Ok(()),
Err(error) => Err(error),
}
/// Creates `kb_dex_decoded_events`.
async fn create_kb_dex_decoded_events_table(pool: &sqlx::SqlitePool) -> Result<(), crate::KbError> {
execute_sqlite_schema_statement(
pool,
"create_kb_dex_decoded_events_table",
r#"
CREATE TABLE IF NOT EXISTS kb_dex_decoded_events (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
transaction_id INTEGER NOT NULL,
instruction_id INTEGER NULL,
protocol_name TEXT NOT NULL,
program_id TEXT NOT NULL,
event_kind TEXT NOT NULL,
pool_account TEXT NULL,
lp_mint TEXT NULL,
token_a_mint TEXT NULL,
token_b_mint TEXT NULL,
market_account TEXT NULL,
payload_json TEXT NOT NULL,
created_at TEXT NOT NULL,
FOREIGN KEY(transaction_id) REFERENCES kb_chain_transactions(id),
FOREIGN KEY(instruction_id) REFERENCES kb_chain_instructions(id)
)
"#,
)
.await
}
/// Creates index on `kb_dex_decoded_events(transaction_id)`.
async fn create_kb_idx_dex_decoded_events_transaction_id(
pool: &sqlx::SqlitePool,
) -> Result<(), crate::KbError> {
execute_sqlite_schema_statement(
pool,
"create_kb_idx_dex_decoded_events_transaction_id",
r#"
CREATE INDEX IF NOT EXISTS kb_idx_dex_decoded_events_transaction_id
ON kb_dex_decoded_events (transaction_id)
"#,
)
.await
}
/// Creates unique index on `(transaction_id, instruction_id, event_kind)`.
async fn create_kb_uq_dex_decoded_events_transaction_instruction_event(
pool: &sqlx::SqlitePool,
) -> Result<(), crate::KbError> {
execute_sqlite_schema_statement(
pool,
"create_kb_uq_dex_decoded_events_transaction_instruction_event",
r#"
CREATE UNIQUE INDEX IF NOT EXISTS kb_uq_dex_decoded_events_transaction_instruction_event
ON kb_dex_decoded_events (transaction_id, instruction_id, event_kind)
"#,
)
.await
}