0.7.16
This commit is contained in:
186
kb_lib/src/db/queries/pair_metric.rs
Normal file
186
kb_lib/src/db/queries/pair_metric.rs
Normal file
@@ -0,0 +1,186 @@
|
||||
// file: kb_lib/src/db/queries/pair_metric.rs
|
||||
|
||||
//! Queries for `kb_pair_metrics`.
|
||||
|
||||
/// Inserts or updates one pair-metric row and returns its stable internal id.
|
||||
pub async fn upsert_pair_metric(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbPairMetricDto,
|
||||
) -> Result<i64, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_pair_metrics (
|
||||
pair_id,
|
||||
first_slot,
|
||||
last_slot,
|
||||
first_signature,
|
||||
last_signature,
|
||||
trade_count,
|
||||
buy_count,
|
||||
sell_count,
|
||||
cumulative_base_amount_raw,
|
||||
cumulative_quote_amount_raw,
|
||||
last_price_quote_per_base,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(pair_id) DO UPDATE SET
|
||||
first_slot = excluded.first_slot,
|
||||
last_slot = excluded.last_slot,
|
||||
first_signature = excluded.first_signature,
|
||||
last_signature = excluded.last_signature,
|
||||
trade_count = excluded.trade_count,
|
||||
buy_count = excluded.buy_count,
|
||||
sell_count = excluded.sell_count,
|
||||
cumulative_base_amount_raw = excluded.cumulative_base_amount_raw,
|
||||
cumulative_quote_amount_raw = excluded.cumulative_quote_amount_raw,
|
||||
last_price_quote_per_base = excluded.last_price_quote_per_base,
|
||||
updated_at = excluded.updated_at
|
||||
"#,
|
||||
)
|
||||
.bind(dto.pair_id)
|
||||
.bind(dto.first_slot)
|
||||
.bind(dto.last_slot)
|
||||
.bind(dto.first_signature.clone())
|
||||
.bind(dto.last_signature.clone())
|
||||
.bind(dto.trade_count)
|
||||
.bind(dto.buy_count)
|
||||
.bind(dto.sell_count)
|
||||
.bind(dto.cumulative_base_amount_raw.clone())
|
||||
.bind(dto.cumulative_quote_amount_raw.clone())
|
||||
.bind(dto.last_price_quote_per_base)
|
||||
.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_pair_metrics on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
|
||||
r#"
|
||||
SELECT id
|
||||
FROM kb_pair_metrics
|
||||
WHERE pair_id = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(dto.pair_id)
|
||||
.fetch_one(pool)
|
||||
.await;
|
||||
match id_result {
|
||||
Ok(id) => Ok(id),
|
||||
Err(error) => Err(crate::KbError::Db(format!(
|
||||
"cannot fetch kb_pair_metrics id for pair_id '{}' on sqlite: {}",
|
||||
dto.pair_id, error
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns one pair-metric row identified by its pair id, if it exists.
|
||||
pub async fn get_pair_metric_by_pair_id(
|
||||
database: &crate::KbDatabase,
|
||||
pair_id: i64,
|
||||
) -> Result<std::option::Option<crate::KbPairMetricDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbPairMetricEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
pair_id,
|
||||
first_slot,
|
||||
last_slot,
|
||||
first_signature,
|
||||
last_signature,
|
||||
trade_count,
|
||||
buy_count,
|
||||
sell_count,
|
||||
cumulative_base_amount_raw,
|
||||
cumulative_quote_amount_raw,
|
||||
last_price_quote_per_base,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_pair_metrics
|
||||
WHERE pair_id = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(pair_id)
|
||||
.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_pair_metrics by pair_id '{}' on sqlite: {}",
|
||||
pair_id, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
match entity_option {
|
||||
Some(entity) => crate::KbPairMetricDto::try_from(entity).map(Some),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists all pair-metric rows ordered by pair id.
|
||||
pub async fn list_pair_metrics(
|
||||
database: &crate::KbDatabase,
|
||||
) -> Result<std::vec::Vec<crate::KbPairMetricDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbPairMetricEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
pair_id,
|
||||
first_slot,
|
||||
last_slot,
|
||||
first_signature,
|
||||
last_signature,
|
||||
trade_count,
|
||||
buy_count,
|
||||
sell_count,
|
||||
cumulative_base_amount_raw,
|
||||
cumulative_quote_amount_raw,
|
||||
last_price_quote_per_base,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_pair_metrics
|
||||
ORDER BY pair_id ASC
|
||||
"#,
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list kb_pair_metrics on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbPairMetricDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
208
kb_lib/src/db/queries/trade_event.rs
Normal file
208
kb_lib/src/db/queries/trade_event.rs
Normal file
@@ -0,0 +1,208 @@
|
||||
// file: kb_lib/src/db/queries/trade_event.rs
|
||||
|
||||
//! Queries for `kb_trade_events`.
|
||||
|
||||
/// Inserts or updates one trade-event row and returns its stable internal id.
|
||||
pub async fn upsert_trade_event(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbTradeEventDto,
|
||||
) -> Result<i64, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_trade_events (
|
||||
dex_id,
|
||||
pool_id,
|
||||
pair_id,
|
||||
transaction_id,
|
||||
decoded_event_id,
|
||||
signature,
|
||||
slot,
|
||||
trade_side,
|
||||
base_token_id,
|
||||
quote_token_id,
|
||||
base_amount_raw,
|
||||
quote_amount_raw,
|
||||
price_quote_per_base,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
payload_json,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(decoded_event_id) DO UPDATE SET
|
||||
updated_at = excluded.updated_at
|
||||
"#,
|
||||
)
|
||||
.bind(dto.dex_id)
|
||||
.bind(dto.pool_id)
|
||||
.bind(dto.pair_id)
|
||||
.bind(dto.transaction_id)
|
||||
.bind(dto.decoded_event_id)
|
||||
.bind(dto.signature.clone())
|
||||
.bind(dto.slot)
|
||||
.bind(kb_trade_side_to_string(dto.trade_side))
|
||||
.bind(dto.base_token_id)
|
||||
.bind(dto.quote_token_id)
|
||||
.bind(dto.base_amount_raw.clone())
|
||||
.bind(dto.quote_amount_raw.clone())
|
||||
.bind(dto.price_quote_per_base)
|
||||
.bind(dto.source_kind.to_i16())
|
||||
.bind(dto.source_endpoint_name.clone())
|
||||
.bind(dto.payload_json.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_trade_events on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
|
||||
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
|
||||
r#"
|
||||
SELECT id
|
||||
FROM kb_trade_events
|
||||
WHERE decoded_event_id = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(dto.decoded_event_id)
|
||||
.fetch_one(pool)
|
||||
.await;
|
||||
match id_result {
|
||||
Ok(id) => Ok(id),
|
||||
Err(error) => Err(crate::KbError::Db(format!(
|
||||
"cannot fetch kb_trade_events id for decoded_event_id '{}' on sqlite: {}",
|
||||
dto.decoded_event_id, error
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns one trade-event row identified by its decoded-event id, if it exists.
|
||||
pub async fn get_trade_event_by_decoded_event_id(
|
||||
database: &crate::KbDatabase,
|
||||
decoded_event_id: i64,
|
||||
) -> Result<std::option::Option<crate::KbTradeEventDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbTradeEventEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
dex_id,
|
||||
pool_id,
|
||||
pair_id,
|
||||
transaction_id,
|
||||
decoded_event_id,
|
||||
signature,
|
||||
slot,
|
||||
trade_side,
|
||||
base_token_id,
|
||||
quote_token_id,
|
||||
base_amount_raw,
|
||||
quote_amount_raw,
|
||||
price_quote_per_base,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
payload_json,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_trade_events
|
||||
WHERE decoded_event_id = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(decoded_event_id)
|
||||
.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_trade_events by decoded_event_id '{}' on sqlite: {}",
|
||||
decoded_event_id, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
match entity_option {
|
||||
Some(entity) => crate::KbTradeEventDto::try_from(entity).map(Some),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists trade-event rows for one pair id ordered by creation time then id.
|
||||
pub async fn list_trade_events_by_pair_id(
|
||||
database: &crate::KbDatabase,
|
||||
pair_id: i64,
|
||||
) -> Result<std::vec::Vec<crate::KbTradeEventDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbTradeEventEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
dex_id,
|
||||
pool_id,
|
||||
pair_id,
|
||||
transaction_id,
|
||||
decoded_event_id,
|
||||
signature,
|
||||
slot,
|
||||
trade_side,
|
||||
base_token_id,
|
||||
quote_token_id,
|
||||
base_amount_raw,
|
||||
quote_amount_raw,
|
||||
price_quote_per_base,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
payload_json,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_trade_events
|
||||
WHERE pair_id = ?
|
||||
ORDER BY created_at ASC, id ASC
|
||||
"#,
|
||||
)
|
||||
.bind(pair_id)
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list kb_trade_events by pair_id '{}' on sqlite: {}",
|
||||
pair_id, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbTradeEventDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn kb_trade_side_to_string(value: crate::KbSwapTradeSide) -> &'static str {
|
||||
match value {
|
||||
crate::KbSwapTradeSide::BuyBase => "BuyBase",
|
||||
crate::KbSwapTradeSide::SellBase => "SellBase",
|
||||
crate::KbSwapTradeSide::Unknown => "Unknown",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user