0.7.19
This commit is contained in:
@@ -30,6 +30,7 @@ mod token_burn_event;
|
||||
mod token_mint_event;
|
||||
mod trade_event;
|
||||
mod wallet;
|
||||
mod wallet_holding;
|
||||
mod wallet_participation;
|
||||
|
||||
pub use analysis_signal::KbAnalysisSignalDto;
|
||||
@@ -60,4 +61,5 @@ pub use token_burn_event::KbTokenBurnEventDto;
|
||||
pub use token_mint_event::KbTokenMintEventDto;
|
||||
pub use trade_event::KbTradeEventDto;
|
||||
pub use wallet::KbWalletDto;
|
||||
pub use wallet_holding::KbWalletHoldingDto;
|
||||
pub use wallet_participation::KbWalletParticipationDto;
|
||||
|
||||
124
kb_lib/src/db/dtos/wallet_holding.rs
Normal file
124
kb_lib/src/db/dtos/wallet_holding.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
// file: kb_lib/src/db/dtos/wallet_holding.rs
|
||||
|
||||
//! Wallet-holding DTO.
|
||||
|
||||
/// Application-facing wallet-holding DTO.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbWalletHoldingDto {
|
||||
/// Optional numeric primary key.
|
||||
pub id: std::option::Option<i64>,
|
||||
/// Related wallet id.
|
||||
pub wallet_id: i64,
|
||||
/// Related token id.
|
||||
pub token_id: i64,
|
||||
/// First transaction id that observed this wallet/token relationship.
|
||||
pub first_transaction_id: i64,
|
||||
/// Last transaction id that observed this wallet/token relationship.
|
||||
pub last_transaction_id: i64,
|
||||
/// Optional last decoded-event id.
|
||||
pub last_decoded_event_id: std::option::Option<i64>,
|
||||
/// Optional last related pool id.
|
||||
pub last_pool_id: std::option::Option<i64>,
|
||||
/// Optional last related pair id.
|
||||
pub last_pair_id: std::option::Option<i64>,
|
||||
/// Optional last observed role.
|
||||
pub last_role: std::option::Option<std::string::String>,
|
||||
/// Optional last observed raw balance-like value.
|
||||
pub balance_raw: std::option::Option<std::string::String>,
|
||||
/// Optional last observed slot.
|
||||
pub last_slot_observed: std::option::Option<i64>,
|
||||
/// Observation source kind.
|
||||
pub source_kind: crate::KbObservationSourceKind,
|
||||
/// Optional logical source endpoint name.
|
||||
pub source_endpoint_name: std::option::Option<std::string::String>,
|
||||
/// Creation timestamp.
|
||||
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||
/// Update timestamp.
|
||||
pub updated_at: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
impl KbWalletHoldingDto {
|
||||
/// Creates a new wallet-holding DTO.
|
||||
pub fn new(
|
||||
wallet_id: i64,
|
||||
token_id: i64,
|
||||
first_transaction_id: i64,
|
||||
last_transaction_id: i64,
|
||||
last_decoded_event_id: std::option::Option<i64>,
|
||||
last_pool_id: std::option::Option<i64>,
|
||||
last_pair_id: std::option::Option<i64>,
|
||||
last_role: std::option::Option<std::string::String>,
|
||||
balance_raw: std::option::Option<std::string::String>,
|
||||
last_slot_observed: std::option::Option<i64>,
|
||||
source_kind: crate::KbObservationSourceKind,
|
||||
source_endpoint_name: std::option::Option<std::string::String>,
|
||||
) -> Self {
|
||||
let now = chrono::Utc::now();
|
||||
Self {
|
||||
id: None,
|
||||
wallet_id,
|
||||
token_id,
|
||||
first_transaction_id,
|
||||
last_transaction_id,
|
||||
last_decoded_event_id,
|
||||
last_pool_id,
|
||||
last_pair_id,
|
||||
last_role,
|
||||
balance_raw,
|
||||
last_slot_observed,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<crate::KbWalletHoldingEntity> for KbWalletHoldingDto {
|
||||
type Error = crate::KbError;
|
||||
|
||||
fn try_from(entity: crate::KbWalletHoldingEntity) -> Result<Self, Self::Error> {
|
||||
let source_kind_result = crate::KbObservationSourceKind::from_i16(entity.source_kind);
|
||||
let source_kind = match source_kind_result {
|
||||
Ok(source_kind) => source_kind,
|
||||
Err(error) => return Err(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 wallet_holding created_at '{}': {}",
|
||||
entity.created_at, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let updated_at_result = chrono::DateTime::parse_from_rfc3339(&entity.updated_at);
|
||||
let updated_at = match updated_at_result {
|
||||
Ok(updated_at) => updated_at.with_timezone(&chrono::Utc),
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot parse wallet_holding updated_at '{}': {}",
|
||||
entity.updated_at, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
Ok(Self {
|
||||
id: Some(entity.id),
|
||||
wallet_id: entity.wallet_id,
|
||||
token_id: entity.token_id,
|
||||
first_transaction_id: entity.first_transaction_id,
|
||||
last_transaction_id: entity.last_transaction_id,
|
||||
last_decoded_event_id: entity.last_decoded_event_id,
|
||||
last_pool_id: entity.last_pool_id,
|
||||
last_pair_id: entity.last_pair_id,
|
||||
last_role: entity.last_role,
|
||||
balance_raw: entity.balance_raw,
|
||||
last_slot_observed: entity.last_slot_observed,
|
||||
source_kind,
|
||||
source_endpoint_name: entity.source_endpoint_name,
|
||||
created_at,
|
||||
updated_at,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@ mod token_burn_event;
|
||||
mod token_mint_event;
|
||||
mod trade_event;
|
||||
mod wallet;
|
||||
mod wallet_holding;
|
||||
mod wallet_participation;
|
||||
|
||||
pub use analysis_signal::KbAnalysisSignalEntity;
|
||||
@@ -62,4 +63,5 @@ pub use token_burn_event::KbTokenBurnEventEntity;
|
||||
pub use token_mint_event::KbTokenMintEventEntity;
|
||||
pub use trade_event::KbTradeEventEntity;
|
||||
pub use wallet::KbWalletEntity;
|
||||
pub use wallet_holding::KbWalletHoldingEntity;
|
||||
pub use wallet_participation::KbWalletParticipationEntity;
|
||||
|
||||
38
kb_lib/src/db/entities/wallet_holding.rs
Normal file
38
kb_lib/src/db/entities/wallet_holding.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
// file: kb_lib/src/db/entities/wallet_holding.rs
|
||||
|
||||
//! Wallet-holding entity.
|
||||
|
||||
/// Persisted wallet-holding row.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, sqlx::FromRow)]
|
||||
pub struct KbWalletHoldingEntity {
|
||||
/// Numeric primary key.
|
||||
pub id: i64,
|
||||
/// Related wallet id.
|
||||
pub wallet_id: i64,
|
||||
/// Related token id.
|
||||
pub token_id: i64,
|
||||
/// First transaction id that observed this wallet/token relationship.
|
||||
pub first_transaction_id: i64,
|
||||
/// Last transaction id that observed this wallet/token relationship.
|
||||
pub last_transaction_id: i64,
|
||||
/// Optional last decoded-event id.
|
||||
pub last_decoded_event_id: std::option::Option<i64>,
|
||||
/// Optional last related pool id.
|
||||
pub last_pool_id: std::option::Option<i64>,
|
||||
/// Optional last related pair id.
|
||||
pub last_pair_id: std::option::Option<i64>,
|
||||
/// Optional last observed role.
|
||||
pub last_role: std::option::Option<std::string::String>,
|
||||
/// Optional last observed raw balance-like value.
|
||||
pub balance_raw: std::option::Option<std::string::String>,
|
||||
/// Optional last observed slot.
|
||||
pub last_slot_observed: std::option::Option<i64>,
|
||||
/// Observation source kind.
|
||||
pub source_kind: i16,
|
||||
/// Optional logical source endpoint name.
|
||||
pub source_endpoint_name: std::option::Option<std::string::String>,
|
||||
/// Creation timestamp encoded as RFC3339 UTC text.
|
||||
pub created_at: std::string::String,
|
||||
/// Update timestamp encoded as RFC3339 UTC text.
|
||||
pub updated_at: std::string::String,
|
||||
}
|
||||
@@ -34,6 +34,7 @@ mod token_burn_event;
|
||||
mod token_mint_event;
|
||||
mod trade_event;
|
||||
mod wallet;
|
||||
mod wallet_holding;
|
||||
mod wallet_participation;
|
||||
|
||||
pub use analysis_signal::insert_analysis_signal;
|
||||
@@ -111,6 +112,9 @@ pub use trade_event::upsert_trade_event;
|
||||
pub use wallet::get_wallet_by_address;
|
||||
pub use wallet::list_wallets;
|
||||
pub use wallet::upsert_wallet;
|
||||
pub use wallet_holding::get_wallet_holding_by_wallet_and_token;
|
||||
pub use wallet_holding::list_wallet_holdings_by_wallet_id;
|
||||
pub use wallet_holding::upsert_wallet_holding;
|
||||
pub use wallet_participation::get_wallet_participation_by_unique_key;
|
||||
pub use wallet_participation::list_wallet_participations_by_pool_id;
|
||||
pub use wallet_participation::list_wallet_participations_by_wallet_id;
|
||||
|
||||
195
kb_lib/src/db/queries/wallet_holding.rs
Normal file
195
kb_lib/src/db/queries/wallet_holding.rs
Normal file
@@ -0,0 +1,195 @@
|
||||
// file: kb_lib/src/db/queries/wallet_holding.rs
|
||||
|
||||
//! Queries for `kb_wallet_holdings`.
|
||||
|
||||
/// Inserts or updates one wallet-holding row and returns its stable internal id.
|
||||
pub async fn upsert_wallet_holding(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbWalletHoldingDto,
|
||||
) -> Result<i64, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_wallet_holdings (
|
||||
wallet_id,
|
||||
token_id,
|
||||
first_transaction_id,
|
||||
last_transaction_id,
|
||||
last_decoded_event_id,
|
||||
last_pool_id,
|
||||
last_pair_id,
|
||||
last_role,
|
||||
balance_raw,
|
||||
last_slot_observed,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(wallet_id, token_id) DO UPDATE SET
|
||||
last_transaction_id = excluded.last_transaction_id,
|
||||
last_decoded_event_id = excluded.last_decoded_event_id,
|
||||
last_pool_id = excluded.last_pool_id,
|
||||
last_pair_id = excluded.last_pair_id,
|
||||
last_role = excluded.last_role,
|
||||
balance_raw = COALESCE(excluded.balance_raw, kb_wallet_holdings.balance_raw),
|
||||
last_slot_observed = excluded.last_slot_observed,
|
||||
source_kind = excluded.source_kind,
|
||||
source_endpoint_name = excluded.source_endpoint_name,
|
||||
updated_at = excluded.updated_at
|
||||
"#,
|
||||
)
|
||||
.bind(dto.wallet_id)
|
||||
.bind(dto.token_id)
|
||||
.bind(dto.first_transaction_id)
|
||||
.bind(dto.last_transaction_id)
|
||||
.bind(dto.last_decoded_event_id)
|
||||
.bind(dto.last_pool_id)
|
||||
.bind(dto.last_pair_id)
|
||||
.bind(dto.last_role.clone())
|
||||
.bind(dto.balance_raw.clone())
|
||||
.bind(dto.last_slot_observed)
|
||||
.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_holdings on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
|
||||
r#"
|
||||
SELECT id
|
||||
FROM kb_wallet_holdings
|
||||
WHERE wallet_id = ? AND token_id = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(dto.wallet_id)
|
||||
.bind(dto.token_id)
|
||||
.fetch_one(pool)
|
||||
.await;
|
||||
match id_result {
|
||||
Ok(id) => Ok(id),
|
||||
Err(error) => Err(crate::KbError::Db(format!(
|
||||
"cannot fetch kb_wallet_holdings id for wallet_id '{}' token_id '{}' on sqlite: {}",
|
||||
dto.wallet_id, dto.token_id, error
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns one wallet-holding row identified by `(wallet_id, token_id)`, if it exists.
|
||||
pub async fn get_wallet_holding_by_wallet_and_token(
|
||||
database: &crate::KbDatabase,
|
||||
wallet_id: i64,
|
||||
token_id: i64,
|
||||
) -> Result<std::option::Option<crate::KbWalletHoldingDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbWalletHoldingEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
wallet_id,
|
||||
token_id,
|
||||
first_transaction_id,
|
||||
last_transaction_id,
|
||||
last_decoded_event_id,
|
||||
last_pool_id,
|
||||
last_pair_id,
|
||||
last_role,
|
||||
balance_raw,
|
||||
last_slot_observed,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_wallet_holdings
|
||||
WHERE wallet_id = ? AND token_id = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(wallet_id)
|
||||
.bind(token_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_wallet_holdings by wallet_id '{}' token_id '{}' on sqlite: {}",
|
||||
wallet_id, token_id, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
match entity_option {
|
||||
Some(entity) => crate::KbWalletHoldingDto::try_from(entity).map(Some),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists wallet-holding rows for one wallet id.
|
||||
pub async fn list_wallet_holdings_by_wallet_id(
|
||||
database: &crate::KbDatabase,
|
||||
wallet_id: i64,
|
||||
) -> Result<std::vec::Vec<crate::KbWalletHoldingDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbWalletHoldingEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
wallet_id,
|
||||
token_id,
|
||||
first_transaction_id,
|
||||
last_transaction_id,
|
||||
last_decoded_event_id,
|
||||
last_pool_id,
|
||||
last_pair_id,
|
||||
last_role,
|
||||
balance_raw,
|
||||
last_slot_observed,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_wallet_holdings
|
||||
WHERE wallet_id = ?
|
||||
ORDER BY token_id 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_holdings by wallet_id '{}' on sqlite: {}",
|
||||
wallet_id, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbWalletHoldingDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -310,6 +310,18 @@ pub(crate) async fn ensure_schema(database: &crate::KbDatabase) -> Result<(), cr
|
||||
if let Err(error) = result {
|
||||
return Err(error);
|
||||
}
|
||||
let result = create_kb_wallet_holdings_table(pool).await;
|
||||
if let Err(error) = result {
|
||||
return Err(error);
|
||||
}
|
||||
let result = create_kb_idx_wallet_holdings_wallet_id(pool).await;
|
||||
if let Err(error) = result {
|
||||
return Err(error);
|
||||
}
|
||||
let result = create_kb_idx_wallet_holdings_token_id(pool).await;
|
||||
if let Err(error) = result {
|
||||
return Err(error);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1713,3 +1725,66 @@ CREATE TABLE IF NOT EXISTS kb_pair_metrics (
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn create_kb_wallet_holdings_table(pool: &sqlx::SqlitePool) -> Result<(), crate::KbError> {
|
||||
execute_sqlite_schema_statement(
|
||||
pool,
|
||||
"create_kb_wallet_holdings_table",
|
||||
r#"
|
||||
CREATE TABLE IF NOT EXISTS kb_wallet_holdings (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
wallet_id INTEGER NOT NULL,
|
||||
token_id INTEGER NOT NULL,
|
||||
first_transaction_id INTEGER NOT NULL,
|
||||
last_transaction_id INTEGER NOT NULL,
|
||||
last_decoded_event_id INTEGER NULL,
|
||||
last_pool_id INTEGER NULL,
|
||||
last_pair_id INTEGER NULL,
|
||||
last_role TEXT NULL,
|
||||
balance_raw TEXT NULL,
|
||||
last_slot_observed INTEGER NULL,
|
||||
source_kind INTEGER NOT NULL,
|
||||
source_endpoint_name TEXT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL,
|
||||
UNIQUE(wallet_id, token_id),
|
||||
FOREIGN KEY(wallet_id) REFERENCES kb_wallets(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(token_id) REFERENCES kb_tokens(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(first_transaction_id) REFERENCES kb_chain_transactions(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(last_transaction_id) REFERENCES kb_chain_transactions(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(last_decoded_event_id) REFERENCES kb_dex_decoded_events(id) ON DELETE SET NULL,
|
||||
FOREIGN KEY(last_pool_id) REFERENCES kb_pools(id) ON DELETE SET NULL,
|
||||
FOREIGN KEY(last_pair_id) REFERENCES kb_pairs(id) ON DELETE SET NULL
|
||||
)
|
||||
"#,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn create_kb_idx_wallet_holdings_wallet_id(
|
||||
pool: &sqlx::SqlitePool,
|
||||
) -> Result<(), crate::KbError> {
|
||||
execute_sqlite_schema_statement(
|
||||
pool,
|
||||
"create_kb_idx_wallet_holdings_wallet_id",
|
||||
r#"
|
||||
CREATE INDEX IF NOT EXISTS kb_idx_wallet_holdings_wallet_id
|
||||
ON kb_wallet_holdings(wallet_id)
|
||||
"#,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn create_kb_idx_wallet_holdings_token_id(
|
||||
pool: &sqlx::SqlitePool,
|
||||
) -> Result<(), crate::KbError> {
|
||||
execute_sqlite_schema_statement(
|
||||
pool,
|
||||
"create_kb_idx_wallet_holdings_token_id",
|
||||
r#"
|
||||
CREATE INDEX IF NOT EXISTS kb_idx_wallet_holdings_token_id
|
||||
ON kb_wallet_holdings(token_id)
|
||||
"#,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user