0.7.27 +Refactor
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
// file: kb_lib/src/db/queries/liquidity_event.rs
|
||||
|
||||
//! Queries for `kb_liquidity_events`.
|
||||
//! Queries for `k_sol_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> {
|
||||
pub async fn query_liquidity_events_upsert(
|
||||
database: &crate::Database,
|
||||
dto: &crate::LiquidityEventDto,
|
||||
) -> Result<i64, crate::Error> {
|
||||
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!(
|
||||
return Err(crate::Error::Db(format!(
|
||||
"cannot convert liquidity event slot '{}' to i64: {}",
|
||||
slot, error
|
||||
)));
|
||||
@@ -23,10 +23,10 @@ pub async fn upsert_liquidity_event(
|
||||
None => None,
|
||||
};
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
crate::DatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_liquidity_events (
|
||||
INSERT INTO k_sol_liquidity_events (
|
||||
dex_id,
|
||||
pool_id,
|
||||
pair_id,
|
||||
@@ -78,15 +78,15 @@ ON CONFLICT(signature, instruction_index) DO UPDATE SET
|
||||
.execute(pool)
|
||||
.await;
|
||||
if let Err(error) = query_result {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot upsert kb_liquidity_events on sqlite: {}",
|
||||
return Err(crate::Error::Db(format!(
|
||||
"cannot upsert k_sol_liquidity_events on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
|
||||
r#"
|
||||
SELECT id
|
||||
FROM kb_liquidity_events
|
||||
FROM k_sol_liquidity_events
|
||||
WHERE signature = ? AND instruction_index = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
@@ -98,8 +98,8 @@ LIMIT 1
|
||||
match id_result {
|
||||
Ok(id) => return Ok(id),
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot fetch kb_liquidity_events id for signature '{}' and instruction_index '{}' on sqlite: {}",
|
||||
return Err(crate::Error::Db(format!(
|
||||
"cannot fetch k_sol_liquidity_events id for signature '{}' and instruction_index '{}' on sqlite: {}",
|
||||
dto.signature, dto.instruction_index, error
|
||||
)));
|
||||
},
|
||||
@@ -109,16 +109,16 @@ LIMIT 1
|
||||
}
|
||||
|
||||
/// Lists recent liquidity events ordered from newest to oldest.
|
||||
pub async fn list_recent_liquidity_events(
|
||||
database: &crate::KbDatabase,
|
||||
pub async fn query_liquidity_events_list_recent(
|
||||
database: &crate::Database,
|
||||
limit: u32,
|
||||
) -> Result<std::vec::Vec<crate::KbLiquidityEventDto>, crate::KbError> {
|
||||
) -> Result<std::vec::Vec<crate::LiquidityEventDto>, crate::Error> {
|
||||
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>(
|
||||
crate::DatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::LiquidityEventEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
@@ -137,7 +137,7 @@ SELECT
|
||||
quote_amount,
|
||||
lp_amount,
|
||||
executed_at
|
||||
FROM kb_liquidity_events
|
||||
FROM k_sol_liquidity_events
|
||||
ORDER BY id DESC
|
||||
LIMIT ?
|
||||
"#,
|
||||
@@ -148,15 +148,15 @@ LIMIT ?
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list kb_liquidity_events on sqlite: {}",
|
||||
return Err(crate::Error::Db(format!(
|
||||
"cannot list k_sol_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_result = crate::LiquidityEventDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
|
||||
Reference in New Issue
Block a user