This commit is contained in:
2026-04-29 15:54:03 +02:00
parent 5813c526ae
commit f8a2309173
17 changed files with 1281 additions and 10 deletions

View File

@@ -0,0 +1,141 @@
// file: kb_lib/src/db/queries/wallet.rs
//! Queries for `kb_wallets`.
/// Inserts or updates one wallet row and returns its stable internal id.
pub async fn upsert_wallet(
database: &crate::KbDatabase,
dto: &crate::KbWalletDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_wallets (
address,
label,
first_seen_at,
last_seen_at
)
VALUES (?, ?, ?, ?)
ON CONFLICT(address) DO UPDATE SET
label = COALESCE(excluded.label, kb_wallets.label),
last_seen_at = excluded.last_seen_at
"#,
)
.bind(dto.address.clone())
.bind(dto.label.clone())
.bind(dto.first_seen_at.to_rfc3339())
.bind(dto.last_seen_at.to_rfc3339())
.execute(pool)
.await;
if let Err(error) = query_result {
return Err(crate::KbError::Db(format!(
"cannot upsert kb_wallets on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_wallets
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_wallets id for address '{}' on sqlite: {}",
dto.address, error
))),
}
}
}
}
/// Returns one wallet identified by its address, if it exists.
pub async fn get_wallet_by_address(
database: &crate::KbDatabase,
address: &str,
) -> Result<std::option::Option<crate::KbWalletDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbWalletEntity>(
r#"
SELECT
id,
address,
label,
first_seen_at,
last_seen_at
FROM kb_wallets
WHERE address = ?
LIMIT 1
"#,
)
.bind(address)
.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_wallets '{}' on sqlite: {}",
address, error
)));
}
};
match entity_option {
Some(entity) => crate::KbWalletDto::try_from(entity).map(Some),
None => Ok(None),
}
}
}
}
/// Lists all persisted wallets ordered by address.
pub async fn list_wallets(
database: &crate::KbDatabase,
) -> Result<std::vec::Vec<crate::KbWalletDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbWalletEntity>(
r#"
SELECT
id,
address,
label,
first_seen_at,
last_seen_at
FROM kb_wallets
ORDER BY address ASC
"#,
)
.fetch_all(pool)
.await;
let entities = match query_result {
Ok(entities) => entities,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot list kb_wallets on sqlite: {}",
error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbWalletDto::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,224 @@
// file: kb_lib/src/db/queries/wallet_participation.rs
//! Queries for `kb_wallet_participations`.
/// Inserts or updates one wallet-participation row and returns its stable internal id.
pub async fn upsert_wallet_participation(
database: &crate::KbDatabase,
dto: &crate::KbWalletParticipationDto,
) -> Result<i64, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query(
r#"
INSERT INTO kb_wallet_participations (
wallet_id,
transaction_id,
decoded_event_id,
pool_id,
pair_id,
role,
unique_key,
source_kind,
source_endpoint_name,
created_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(unique_key) DO UPDATE SET
updated_at = excluded.updated_at
"#,
)
.bind(dto.wallet_id)
.bind(dto.transaction_id)
.bind(dto.decoded_event_id)
.bind(dto.pool_id)
.bind(dto.pair_id)
.bind(dto.role.clone())
.bind(dto.unique_key.clone())
.bind(dto.source_kind.to_i16())
.bind(dto.source_endpoint_name.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_wallet_participations on sqlite: {}",
error
)));
}
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
r#"
SELECT id
FROM kb_wallet_participations
WHERE unique_key = ?
LIMIT 1
"#,
)
.bind(dto.unique_key.clone())
.fetch_one(pool)
.await;
match id_result {
Ok(id) => Ok(id),
Err(error) => Err(crate::KbError::Db(format!(
"cannot fetch kb_wallet_participations id for unique_key '{}' on sqlite: {}",
dto.unique_key, error
))),
}
}
}
}
/// Returns one wallet-participation identified by its unique key, if it exists.
pub async fn get_wallet_participation_by_unique_key(
database: &crate::KbDatabase,
unique_key: &str,
) -> Result<std::option::Option<crate::KbWalletParticipationDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbWalletParticipationEntity>(
r#"
SELECT
id,
wallet_id,
transaction_id,
decoded_event_id,
pool_id,
pair_id,
role,
unique_key,
source_kind,
source_endpoint_name,
created_at,
updated_at
FROM kb_wallet_participations
WHERE unique_key = ?
LIMIT 1
"#,
)
.bind(unique_key)
.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_wallet_participations by unique_key '{}' on sqlite: {}",
unique_key, error
)));
}
};
match entity_option {
Some(entity) => crate::KbWalletParticipationDto::try_from(entity).map(Some),
None => Ok(None),
}
}
}
}
/// Lists wallet-participation rows for one wallet id.
pub async fn list_wallet_participations_by_wallet_id(
database: &crate::KbDatabase,
wallet_id: i64,
) -> Result<std::vec::Vec<crate::KbWalletParticipationDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbWalletParticipationEntity>(
r#"
SELECT
id,
wallet_id,
transaction_id,
decoded_event_id,
pool_id,
pair_id,
role,
unique_key,
source_kind,
source_endpoint_name,
created_at,
updated_at
FROM kb_wallet_participations
WHERE wallet_id = ?
ORDER BY created_at ASC, id ASC
"#,
)
.bind(wallet_id)
.fetch_all(pool)
.await;
let entities = match query_result {
Ok(entities) => entities,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot list kb_wallet_participations by wallet_id '{}' on sqlite: {}",
wallet_id, error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbWalletParticipationDto::try_from(entity);
let dto = match dto_result {
Ok(dto) => dto,
Err(error) => return Err(error),
};
dtos.push(dto);
}
Ok(dtos)
}
}
}
/// Lists wallet-participation rows for one pool id.
pub async fn list_wallet_participations_by_pool_id(
database: &crate::KbDatabase,
pool_id: i64,
) -> Result<std::vec::Vec<crate::KbWalletParticipationDto>, crate::KbError> {
match database.connection() {
crate::KbDatabaseConnection::Sqlite(pool) => {
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbWalletParticipationEntity>(
r#"
SELECT
id,
wallet_id,
transaction_id,
decoded_event_id,
pool_id,
pair_id,
role,
unique_key,
source_kind,
source_endpoint_name,
created_at,
updated_at
FROM kb_wallet_participations
WHERE pool_id = ?
ORDER BY created_at ASC, id ASC
"#,
)
.bind(pool_id)
.fetch_all(pool)
.await;
let entities = match query_result {
Ok(entities) => entities,
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot list kb_wallet_participations by pool_id '{}' on sqlite: {}",
pool_id, error
)));
}
};
let mut dtos = std::vec::Vec::new();
for entity in entities {
let dto_result = crate::KbWalletParticipationDto::try_from(entity);
let dto = match dto_result {
Ok(dto) => dto,
Err(error) => return Err(error),
};
dtos.push(dto);
}
Ok(dtos)
}
}
}