0.7.14
This commit is contained in:
@@ -3,51 +3,53 @@
|
||||
//! Database data transfer objects.
|
||||
|
||||
mod analysis_signal;
|
||||
mod chain_instruction;
|
||||
mod chain_slot;
|
||||
mod chain_transaction;
|
||||
mod db_metadata;
|
||||
mod db_runtime_event;
|
||||
mod dex;
|
||||
mod dex_decoded_event;
|
||||
mod known_http_endpoint;
|
||||
mod known_ws_endpoint;
|
||||
mod launch_attribution;
|
||||
mod launch_surface;
|
||||
mod launch_surface_key;
|
||||
mod liquidity_event;
|
||||
mod observed_token;
|
||||
mod onchain_observation;
|
||||
mod pair;
|
||||
mod pool;
|
||||
mod pool_listing;
|
||||
mod pool_origin;
|
||||
mod pool_token;
|
||||
mod swap;
|
||||
mod token;
|
||||
mod token_burn_event;
|
||||
mod token_mint_event;
|
||||
mod chain_instruction;
|
||||
mod chain_slot;
|
||||
mod chain_transaction;
|
||||
mod dex_decoded_event;
|
||||
mod launch_surface;
|
||||
mod launch_surface_key;
|
||||
mod launch_attribution;
|
||||
|
||||
pub use analysis_signal::KbAnalysisSignalDto;
|
||||
pub use chain_instruction::KbChainInstructionDto;
|
||||
pub use chain_slot::KbChainSlotDto;
|
||||
pub use chain_transaction::KbChainTransactionDto;
|
||||
pub use db_metadata::KbDbMetadataDto;
|
||||
pub use db_runtime_event::KbDbRuntimeEventDto;
|
||||
pub use dex::KbDexDto;
|
||||
pub use dex_decoded_event::KbDexDecodedEventDto;
|
||||
pub use known_http_endpoint::KbKnownHttpEndpointDto;
|
||||
pub use known_ws_endpoint::KbKnownWsEndpointDto;
|
||||
pub use launch_attribution::KbLaunchAttributionDto;
|
||||
pub use launch_surface::KbLaunchSurfaceDto;
|
||||
pub use launch_surface_key::KbLaunchSurfaceKeyDto;
|
||||
pub use liquidity_event::KbLiquidityEventDto;
|
||||
pub use observed_token::KbObservedTokenDto;
|
||||
pub use onchain_observation::KbOnchainObservationDto;
|
||||
pub use pair::KbPairDto;
|
||||
pub use pool::KbPoolDto;
|
||||
pub use pool_listing::KbPoolListingDto;
|
||||
pub use pool_origin::KbPoolOriginDto;
|
||||
pub use pool_token::KbPoolTokenDto;
|
||||
pub use swap::KbSwapDto;
|
||||
pub use token::KbTokenDto;
|
||||
pub use token_burn_event::KbTokenBurnEventDto;
|
||||
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;
|
||||
pub use launch_surface::KbLaunchSurfaceDto;
|
||||
pub use launch_surface_key::KbLaunchSurfaceKeyDto;
|
||||
pub use launch_attribution::KbLaunchAttributionDto;
|
||||
|
||||
124
kb_lib/src/db/dtos/pool_origin.rs
Normal file
124
kb_lib/src/db/dtos/pool_origin.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
// file: kb_lib/src/db/dtos/pool_origin.rs
|
||||
|
||||
//! Pool origin DTO.
|
||||
|
||||
/// Application-facing pool-origin DTO.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbPoolOriginDto {
|
||||
/// Optional numeric primary key.
|
||||
pub id: std::option::Option<i64>,
|
||||
/// Related DEX id.
|
||||
pub dex_id: i64,
|
||||
/// Related pool id.
|
||||
pub pool_id: i64,
|
||||
/// Optional related pair id.
|
||||
pub pair_id: std::option::Option<i64>,
|
||||
/// Optional related pool listing id.
|
||||
pub pool_listing_id: std::option::Option<i64>,
|
||||
/// Founding transaction id in the local model.
|
||||
pub founding_transaction_id: i64,
|
||||
/// Founding decoded-event id in the local model.
|
||||
pub founding_decoded_event_id: i64,
|
||||
/// Founding signature in the local model.
|
||||
pub founding_signature: std::string::String,
|
||||
/// Founding protocol name.
|
||||
pub founding_protocol_name: std::string::String,
|
||||
/// Founding event kind.
|
||||
pub founding_event_kind: std::string::String,
|
||||
/// Discovery source kind.
|
||||
pub source_kind: crate::KbObservationSourceKind,
|
||||
/// Optional source endpoint logical name.
|
||||
pub source_endpoint_name: std::option::Option<std::string::String>,
|
||||
/// Optional related launch attribution id.
|
||||
pub launch_attribution_id: std::option::Option<i64>,
|
||||
/// Creation timestamp.
|
||||
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||
/// Update timestamp.
|
||||
pub updated_at: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
impl KbPoolOriginDto {
|
||||
/// Creates a new pool-origin DTO.
|
||||
pub fn new(
|
||||
dex_id: i64,
|
||||
pool_id: i64,
|
||||
pair_id: std::option::Option<i64>,
|
||||
pool_listing_id: std::option::Option<i64>,
|
||||
founding_transaction_id: i64,
|
||||
founding_decoded_event_id: i64,
|
||||
founding_signature: std::string::String,
|
||||
founding_protocol_name: std::string::String,
|
||||
founding_event_kind: std::string::String,
|
||||
source_kind: crate::KbObservationSourceKind,
|
||||
source_endpoint_name: std::option::Option<std::string::String>,
|
||||
launch_attribution_id: std::option::Option<i64>,
|
||||
) -> Self {
|
||||
let now = chrono::Utc::now();
|
||||
Self {
|
||||
id: None,
|
||||
dex_id,
|
||||
pool_id,
|
||||
pair_id,
|
||||
pool_listing_id,
|
||||
founding_transaction_id,
|
||||
founding_decoded_event_id,
|
||||
founding_signature,
|
||||
founding_protocol_name,
|
||||
founding_event_kind,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
launch_attribution_id,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<crate::KbPoolOriginEntity> for KbPoolOriginDto {
|
||||
type Error = crate::KbError;
|
||||
|
||||
fn try_from(entity: crate::KbPoolOriginEntity) -> 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 pool_origin 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 pool_origin updated_at '{}': {}",
|
||||
entity.updated_at, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
Ok(Self {
|
||||
id: Some(entity.id),
|
||||
dex_id: entity.dex_id,
|
||||
pool_id: entity.pool_id,
|
||||
pair_id: entity.pair_id,
|
||||
pool_listing_id: entity.pool_listing_id,
|
||||
founding_transaction_id: entity.founding_transaction_id,
|
||||
founding_decoded_event_id: entity.founding_decoded_event_id,
|
||||
founding_signature: entity.founding_signature,
|
||||
founding_protocol_name: entity.founding_protocol_name,
|
||||
founding_event_kind: entity.founding_event_kind,
|
||||
source_kind,
|
||||
source_endpoint_name: entity.source_endpoint_name,
|
||||
launch_attribution_id: entity.launch_attribution_id,
|
||||
created_at,
|
||||
updated_at,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -5,51 +5,53 @@
|
||||
//! These types are close to persisted rows and SQL query results.
|
||||
|
||||
mod analysis_signal;
|
||||
mod chain_instruction;
|
||||
mod chain_slot;
|
||||
mod chain_transaction;
|
||||
mod db_metadata;
|
||||
mod db_runtime_event;
|
||||
mod dex;
|
||||
mod dex_decoded_event;
|
||||
mod known_http_endpoint;
|
||||
mod known_ws_endpoint;
|
||||
mod launch_attribution;
|
||||
mod launch_surface;
|
||||
mod launch_surface_key;
|
||||
mod liquidity_event;
|
||||
mod observed_token;
|
||||
mod onchain_observation;
|
||||
mod pair;
|
||||
mod pool;
|
||||
mod pool_listing;
|
||||
mod pool_origin;
|
||||
mod pool_token;
|
||||
mod swap;
|
||||
mod token;
|
||||
mod token_burn_event;
|
||||
mod token_mint_event;
|
||||
mod chain_instruction;
|
||||
mod chain_slot;
|
||||
mod chain_transaction;
|
||||
mod dex_decoded_event;
|
||||
mod launch_surface;
|
||||
mod launch_surface_key;
|
||||
mod launch_attribution;
|
||||
|
||||
pub use analysis_signal::KbAnalysisSignalEntity;
|
||||
pub use chain_instruction::KbChainInstructionEntity;
|
||||
pub use chain_slot::KbChainSlotEntity;
|
||||
pub use chain_transaction::KbChainTransactionEntity;
|
||||
pub use db_metadata::KbDbMetadataEntity;
|
||||
pub use db_runtime_event::KbDbRuntimeEventEntity;
|
||||
pub use dex::KbDexEntity;
|
||||
pub use dex_decoded_event::KbDexDecodedEventEntity;
|
||||
pub use known_http_endpoint::KbKnownHttpEndpointEntity;
|
||||
pub use known_ws_endpoint::KbKnownWsEndpointEntity;
|
||||
pub use launch_attribution::KbLaunchAttributionEntity;
|
||||
pub use launch_surface::KbLaunchSurfaceEntity;
|
||||
pub use launch_surface_key::KbLaunchSurfaceKeyEntity;
|
||||
pub use liquidity_event::KbLiquidityEventEntity;
|
||||
pub use observed_token::KbObservedTokenEntity;
|
||||
pub use onchain_observation::KbOnchainObservationEntity;
|
||||
pub use pair::KbPairEntity;
|
||||
pub use pool::KbPoolEntity;
|
||||
pub use pool_listing::KbPoolListingEntity;
|
||||
pub use pool_origin::KbPoolOriginEntity;
|
||||
pub use pool_token::KbPoolTokenEntity;
|
||||
pub use swap::KbSwapEntity;
|
||||
pub use token::KbTokenEntity;
|
||||
pub use token_burn_event::KbTokenBurnEventEntity;
|
||||
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;
|
||||
pub use launch_surface::KbLaunchSurfaceEntity;
|
||||
pub use launch_surface_key::KbLaunchSurfaceKeyEntity;
|
||||
pub use launch_attribution::KbLaunchAttributionEntity;
|
||||
|
||||
38
kb_lib/src/db/entities/pool_origin.rs
Normal file
38
kb_lib/src/db/entities/pool_origin.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
// file: kb_lib/src/db/entities/pool_origin.rs
|
||||
|
||||
//! Pool origin entity.
|
||||
|
||||
/// Persisted pool-origin row.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, sqlx::FromRow)]
|
||||
pub struct KbPoolOriginEntity {
|
||||
/// Numeric primary key.
|
||||
pub id: i64,
|
||||
/// Related DEX id.
|
||||
pub dex_id: i64,
|
||||
/// Related pool id.
|
||||
pub pool_id: i64,
|
||||
/// Optional related pair id.
|
||||
pub pair_id: std::option::Option<i64>,
|
||||
/// Optional related pool listing id.
|
||||
pub pool_listing_id: std::option::Option<i64>,
|
||||
/// Founding transaction id in the local model.
|
||||
pub founding_transaction_id: i64,
|
||||
/// Founding decoded-event id in the local model.
|
||||
pub founding_decoded_event_id: i64,
|
||||
/// Founding signature in the local model.
|
||||
pub founding_signature: std::string::String,
|
||||
/// Founding protocol name.
|
||||
pub founding_protocol_name: std::string::String,
|
||||
/// Founding event kind.
|
||||
pub founding_event_kind: std::string::String,
|
||||
/// Discovery source kind.
|
||||
pub source_kind: i16,
|
||||
/// Optional source endpoint logical name.
|
||||
pub source_endpoint_name: std::option::Option<std::string::String>,
|
||||
/// Optional related launch attribution id.
|
||||
pub launch_attribution_id: std::option::Option<i64>,
|
||||
/// 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,
|
||||
}
|
||||
@@ -7,32 +7,42 @@
|
||||
//! Database queries.
|
||||
|
||||
mod analysis_signal;
|
||||
mod chain_instruction;
|
||||
mod chain_slot;
|
||||
mod chain_transaction;
|
||||
mod db_metadata;
|
||||
mod db_runtime_event;
|
||||
mod dex;
|
||||
mod dex_decoded_event;
|
||||
mod known_http_endpoint;
|
||||
mod known_ws_endpoint;
|
||||
mod launch_attribution;
|
||||
mod launch_surface;
|
||||
mod launch_surface_key;
|
||||
mod liquidity_event;
|
||||
mod observed_token;
|
||||
mod onchain_observation;
|
||||
mod pair;
|
||||
mod pool;
|
||||
mod pool_listing;
|
||||
mod pool_origin;
|
||||
mod pool_token;
|
||||
mod swap;
|
||||
mod token;
|
||||
mod token_burn_event;
|
||||
mod token_mint_event;
|
||||
mod chain_instruction;
|
||||
mod chain_slot;
|
||||
mod chain_transaction;
|
||||
mod dex_decoded_event;
|
||||
mod launch_surface;
|
||||
mod launch_surface_key;
|
||||
mod launch_attribution;
|
||||
|
||||
pub use analysis_signal::insert_analysis_signal;
|
||||
pub use analysis_signal::list_recent_analysis_signals;
|
||||
pub use chain_instruction::delete_chain_instructions_by_transaction_id;
|
||||
pub use chain_instruction::insert_chain_instruction;
|
||||
pub use chain_instruction::list_chain_instructions_by_transaction_id;
|
||||
pub use chain_slot::get_chain_slot;
|
||||
pub use chain_slot::list_recent_chain_slots;
|
||||
pub use chain_slot::upsert_chain_slot;
|
||||
pub use chain_transaction::get_chain_transaction_by_signature;
|
||||
pub use chain_transaction::list_recent_chain_transactions;
|
||||
pub use chain_transaction::upsert_chain_transaction;
|
||||
pub use db_metadata::get_db_metadata;
|
||||
pub use db_metadata::list_db_metadata;
|
||||
pub use db_metadata::upsert_db_metadata;
|
||||
@@ -41,12 +51,24 @@ pub use db_runtime_event::list_recent_db_runtime_events;
|
||||
pub use dex::get_dex_by_code;
|
||||
pub use dex::list_dexes;
|
||||
pub use dex::upsert_dex;
|
||||
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;
|
||||
pub use known_http_endpoint::get_known_http_endpoint;
|
||||
pub use known_http_endpoint::list_known_http_endpoints;
|
||||
pub use known_http_endpoint::upsert_known_http_endpoint;
|
||||
pub use known_ws_endpoint::get_known_ws_endpoint;
|
||||
pub use known_ws_endpoint::list_known_ws_endpoints;
|
||||
pub use known_ws_endpoint::upsert_known_ws_endpoint;
|
||||
pub use launch_attribution::get_launch_attribution_by_decoded_event_id;
|
||||
pub use launch_attribution::list_launch_attributions_by_pool_id;
|
||||
pub use launch_attribution::upsert_launch_attribution;
|
||||
pub use launch_surface::get_launch_surface_by_code;
|
||||
pub use launch_surface::list_launch_surfaces;
|
||||
pub use launch_surface::upsert_launch_surface;
|
||||
pub use launch_surface_key::get_launch_surface_key_by_match;
|
||||
pub use launch_surface_key::list_launch_surface_keys_by_surface_id;
|
||||
pub use launch_surface_key::upsert_launch_surface_key;
|
||||
pub use liquidity_event::list_recent_liquidity_events;
|
||||
pub use liquidity_event::upsert_liquidity_event;
|
||||
pub use observed_token::get_observed_token_by_mint;
|
||||
@@ -63,6 +85,9 @@ pub use pool::upsert_pool;
|
||||
pub use pool_listing::get_pool_listing_by_pool_id;
|
||||
pub use pool_listing::list_pool_listings;
|
||||
pub use pool_listing::upsert_pool_listing;
|
||||
pub use pool_origin::get_pool_origin_by_pool_id;
|
||||
pub use pool_origin::list_pool_origins;
|
||||
pub use pool_origin::upsert_pool_origin;
|
||||
pub use pool_token::list_pool_tokens_by_pool_id;
|
||||
pub use pool_token::upsert_pool_token;
|
||||
pub use swap::list_recent_swaps;
|
||||
@@ -73,24 +98,3 @@ pub use token_burn_event::list_recent_token_burn_events;
|
||||
pub use token_burn_event::upsert_token_burn_event;
|
||||
pub use token_mint_event::list_recent_token_mint_events;
|
||||
pub use token_mint_event::upsert_token_mint_event;
|
||||
pub use chain_instruction::list_chain_instructions_by_transaction_id;
|
||||
pub use chain_instruction::insert_chain_instruction;
|
||||
pub use chain_instruction::delete_chain_instructions_by_transaction_id;
|
||||
pub use chain_slot::list_recent_chain_slots;
|
||||
pub use chain_slot::upsert_chain_slot;
|
||||
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;
|
||||
pub use launch_surface::upsert_launch_surface;
|
||||
pub use launch_surface::get_launch_surface_by_code;
|
||||
pub use launch_surface::list_launch_surfaces;
|
||||
pub use launch_surface_key::upsert_launch_surface_key;
|
||||
pub use launch_surface_key::get_launch_surface_key_by_match;
|
||||
pub use launch_surface_key::list_launch_surface_keys_by_surface_id;
|
||||
pub use launch_attribution::upsert_launch_attribution;
|
||||
pub use launch_attribution::get_launch_attribution_by_decoded_event_id;
|
||||
pub use launch_attribution::list_launch_attributions_by_pool_id;
|
||||
|
||||
183
kb_lib/src/db/queries/pool_origin.rs
Normal file
183
kb_lib/src/db/queries/pool_origin.rs
Normal file
@@ -0,0 +1,183 @@
|
||||
// file: kb_lib/src/db/queries/pool_origin.rs
|
||||
|
||||
//! Queries for `kb_pool_origins`.
|
||||
|
||||
/// Inserts or updates one pool-origin row and returns its stable internal id.
|
||||
pub async fn upsert_pool_origin(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbPoolOriginDto,
|
||||
) -> Result<i64, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_pool_origins (
|
||||
dex_id,
|
||||
pool_id,
|
||||
pair_id,
|
||||
pool_listing_id,
|
||||
founding_transaction_id,
|
||||
founding_decoded_event_id,
|
||||
founding_signature,
|
||||
founding_protocol_name,
|
||||
founding_event_kind,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
launch_attribution_id,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(pool_id) DO UPDATE SET
|
||||
pair_id = COALESCE(kb_pool_origins.pair_id, excluded.pair_id),
|
||||
pool_listing_id = COALESCE(kb_pool_origins.pool_listing_id, excluded.pool_listing_id),
|
||||
launch_attribution_id = COALESCE(kb_pool_origins.launch_attribution_id, excluded.launch_attribution_id),
|
||||
updated_at = excluded.updated_at
|
||||
"#,
|
||||
)
|
||||
.bind(dto.dex_id)
|
||||
.bind(dto.pool_id)
|
||||
.bind(dto.pair_id)
|
||||
.bind(dto.pool_listing_id)
|
||||
.bind(dto.founding_transaction_id)
|
||||
.bind(dto.founding_decoded_event_id)
|
||||
.bind(dto.founding_signature.clone())
|
||||
.bind(dto.founding_protocol_name.clone())
|
||||
.bind(dto.founding_event_kind.clone())
|
||||
.bind(dto.source_kind.to_i16())
|
||||
.bind(dto.source_endpoint_name.clone())
|
||||
.bind(dto.launch_attribution_id)
|
||||
.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_pool_origins on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
|
||||
r#"
|
||||
SELECT id
|
||||
FROM kb_pool_origins
|
||||
WHERE pool_id = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(dto.pool_id)
|
||||
.fetch_one(pool)
|
||||
.await;
|
||||
match id_result {
|
||||
Ok(id) => Ok(id),
|
||||
Err(error) => Err(crate::KbError::Db(format!(
|
||||
"cannot fetch kb_pool_origins id for pool_id '{}' on sqlite: {}",
|
||||
dto.pool_id, error
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns one pool-origin row identified by its pool id, if it exists.
|
||||
pub async fn get_pool_origin_by_pool_id(
|
||||
database: &crate::KbDatabase,
|
||||
pool_id: i64,
|
||||
) -> Result<std::option::Option<crate::KbPoolOriginDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbPoolOriginEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
dex_id,
|
||||
pool_id,
|
||||
pair_id,
|
||||
pool_listing_id,
|
||||
founding_transaction_id,
|
||||
founding_decoded_event_id,
|
||||
founding_signature,
|
||||
founding_protocol_name,
|
||||
founding_event_kind,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
launch_attribution_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_pool_origins
|
||||
WHERE pool_id = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(pool_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_pool_origins by pool_id '{}' on sqlite: {}",
|
||||
pool_id, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
match entity_option {
|
||||
Some(entity) => crate::KbPoolOriginDto::try_from(entity).map(Some),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists all pool-origin rows ordered by creation time then id.
|
||||
pub async fn list_pool_origins(
|
||||
database: &crate::KbDatabase,
|
||||
) -> Result<std::vec::Vec<crate::KbPoolOriginDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbPoolOriginEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
dex_id,
|
||||
pool_id,
|
||||
pair_id,
|
||||
pool_listing_id,
|
||||
founding_transaction_id,
|
||||
founding_decoded_event_id,
|
||||
founding_signature,
|
||||
founding_protocol_name,
|
||||
founding_event_kind,
|
||||
source_kind,
|
||||
source_endpoint_name,
|
||||
launch_attribution_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_pool_origins
|
||||
ORDER BY created_at ASC, 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_pool_origins on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbPoolOriginDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -254,6 +254,26 @@ pub(crate) async fn ensure_schema(database: &crate::KbDatabase) -> Result<(), cr
|
||||
if let Err(error) = result {
|
||||
return Err(error);
|
||||
}
|
||||
let result = create_kb_pool_origins_table(pool).await;
|
||||
if let Err(error) = result {
|
||||
return Err(error);
|
||||
}
|
||||
let result = create_kb_idx_pool_origins_dex_id(pool).await;
|
||||
if let Err(error) = result {
|
||||
return Err(error);
|
||||
}
|
||||
let result = create_kb_idx_pool_origins_pair_id(pool).await;
|
||||
if let Err(error) = result {
|
||||
return Err(error);
|
||||
}
|
||||
let result = create_kb_idx_pool_origins_listing_id(pool).await;
|
||||
if let Err(error) = result {
|
||||
return Err(error);
|
||||
}
|
||||
let result = create_kb_idx_pool_origins_transaction_id(pool).await;
|
||||
if let Err(error) = result {
|
||||
return Err(error);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1392,3 +1412,89 @@ ON kb_launch_attributions(pool_id)
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn create_kb_pool_origins_table(pool: &sqlx::SqlitePool) -> Result<(), crate::KbError> {
|
||||
execute_sqlite_schema_statement(
|
||||
pool,
|
||||
"create_kb_pool_origins_table",
|
||||
r#"
|
||||
CREATE TABLE IF NOT EXISTS kb_pool_origins (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
dex_id INTEGER NOT NULL,
|
||||
pool_id INTEGER NOT NULL UNIQUE,
|
||||
pair_id INTEGER NULL,
|
||||
pool_listing_id INTEGER NULL,
|
||||
founding_transaction_id INTEGER NOT NULL,
|
||||
founding_decoded_event_id INTEGER NOT NULL UNIQUE,
|
||||
founding_signature TEXT NOT NULL,
|
||||
founding_protocol_name TEXT NOT NULL,
|
||||
founding_event_kind TEXT NOT NULL,
|
||||
source_kind INTEGER NOT NULL,
|
||||
source_endpoint_name TEXT NULL,
|
||||
launch_attribution_id INTEGER NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL,
|
||||
FOREIGN KEY(dex_id) REFERENCES kb_dexes(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(pool_id) REFERENCES kb_pools(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(pair_id) REFERENCES kb_pairs(id) ON DELETE SET NULL,
|
||||
FOREIGN KEY(pool_listing_id) REFERENCES kb_pool_listings(id) ON DELETE SET NULL,
|
||||
FOREIGN KEY(founding_transaction_id) REFERENCES kb_chain_transactions(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(founding_decoded_event_id) REFERENCES kb_dex_decoded_events(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(launch_attribution_id) REFERENCES kb_launch_attributions(id) ON DELETE SET NULL
|
||||
)
|
||||
"#,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn create_kb_idx_pool_origins_dex_id(pool: &sqlx::SqlitePool) -> Result<(), crate::KbError> {
|
||||
execute_sqlite_schema_statement(
|
||||
pool,
|
||||
"create_kb_idx_pool_origins_dex_id",
|
||||
r#"
|
||||
CREATE INDEX IF NOT EXISTS kb_idx_pool_origins_dex_id
|
||||
ON kb_pool_origins(dex_id)
|
||||
"#,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn create_kb_idx_pool_origins_pair_id(pool: &sqlx::SqlitePool) -> Result<(), crate::KbError> {
|
||||
execute_sqlite_schema_statement(
|
||||
pool,
|
||||
"create_kb_idx_pool_origins_pair_id",
|
||||
r#"
|
||||
CREATE INDEX IF NOT EXISTS kb_idx_pool_origins_pair_id
|
||||
ON kb_pool_origins(pair_id)
|
||||
"#,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn create_kb_idx_pool_origins_listing_id(
|
||||
pool: &sqlx::SqlitePool,
|
||||
) -> Result<(), crate::KbError> {
|
||||
execute_sqlite_schema_statement(
|
||||
pool,
|
||||
"create_kb_idx_pool_origins_listing_id",
|
||||
r#"
|
||||
CREATE INDEX IF NOT EXISTS kb_idx_pool_origins_listing_id
|
||||
ON kb_pool_origins(pool_listing_id)
|
||||
"#,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn create_kb_idx_pool_origins_transaction_id(
|
||||
pool: &sqlx::SqlitePool,
|
||||
) -> Result<(), crate::KbError> {
|
||||
execute_sqlite_schema_statement(
|
||||
pool,
|
||||
"create_kb_idx_pool_origins_transaction_id",
|
||||
r#"
|
||||
CREATE INDEX IF NOT EXISTS kb_idx_pool_origins_transaction_id
|
||||
ON kb_pool_origins(founding_transaction_id)
|
||||
"#,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user