0.7.27 +Refactor
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
/// One recorded pool-origin result.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbPoolOriginResult {
|
||||
pub struct PoolOriginResult {
|
||||
/// Persisted pool-origin id.
|
||||
pub pool_origin_id: i64,
|
||||
/// Related pool id.
|
||||
@@ -19,15 +19,15 @@ pub struct KbPoolOriginResult {
|
||||
|
||||
/// Pool-origin consolidation service.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct KbPoolOriginService {
|
||||
database: std::sync::Arc<crate::KbDatabase>,
|
||||
persistence: crate::KbDetectionPersistenceService,
|
||||
pub struct PoolOriginService {
|
||||
database: std::sync::Arc<crate::Database>,
|
||||
persistence: crate::DetectionPersistenceService,
|
||||
}
|
||||
|
||||
impl KbPoolOriginService {
|
||||
impl PoolOriginService {
|
||||
/// Creates a new pool-origin service.
|
||||
pub fn new(database: std::sync::Arc<crate::KbDatabase>) -> Self {
|
||||
let persistence = crate::KbDetectionPersistenceService::new(database.clone());
|
||||
pub fn new(database: std::sync::Arc<crate::Database>) -> Self {
|
||||
let persistence = crate::DetectionPersistenceService::new(database.clone());
|
||||
return Self { database, persistence };
|
||||
}
|
||||
|
||||
@@ -35,9 +35,10 @@ impl KbPoolOriginService {
|
||||
pub async fn record_transaction_by_signature(
|
||||
&self,
|
||||
signature: &str,
|
||||
) -> Result<std::vec::Vec<crate::KbPoolOriginResult>, crate::KbError> {
|
||||
) -> Result<std::vec::Vec<crate::PoolOriginResult>, crate::Error> {
|
||||
let transaction_result =
|
||||
crate::get_chain_transaction_by_signature(self.database.as_ref(), signature).await;
|
||||
crate::query_chain_transactions_get_by_signature(self.database.as_ref(), signature)
|
||||
.await;
|
||||
let transaction_option = match transaction_result {
|
||||
Ok(transaction_option) => transaction_option,
|
||||
Err(error) => return Err(error),
|
||||
@@ -45,7 +46,7 @@ impl KbPoolOriginService {
|
||||
let transaction = match transaction_option {
|
||||
Some(transaction) => transaction,
|
||||
None => {
|
||||
return Err(crate::KbError::InvalidState(format!(
|
||||
return Err(crate::Error::InvalidState(format!(
|
||||
"cannot record pool origins for unknown transaction '{}'",
|
||||
signature
|
||||
)));
|
||||
@@ -55,14 +56,14 @@ impl KbPoolOriginService {
|
||||
let transaction_id = match transaction.id {
|
||||
Some(transaction_id) => transaction_id,
|
||||
None => {
|
||||
return Err(crate::KbError::InvalidState(format!(
|
||||
return Err(crate::Error::InvalidState(format!(
|
||||
"transaction '{}' has no internal id",
|
||||
signature
|
||||
)));
|
||||
},
|
||||
};
|
||||
|
||||
let decoded_events_result = crate::list_dex_decoded_events_by_transaction_id(
|
||||
let decoded_events_result = crate::query_dex_decoded_events_list_by_transaction_id(
|
||||
self.database.as_ref(),
|
||||
transaction_id,
|
||||
)
|
||||
@@ -79,7 +80,7 @@ impl KbPoolOriginService {
|
||||
let decoded_event_id = match decoded_event.id {
|
||||
Some(decoded_event_id) => decoded_event_id,
|
||||
None => {
|
||||
return Err(crate::KbError::InvalidState(
|
||||
return Err(crate::Error::InvalidState(
|
||||
"decoded event has no internal id".to_string(),
|
||||
));
|
||||
},
|
||||
@@ -91,7 +92,8 @@ impl KbPoolOriginService {
|
||||
};
|
||||
|
||||
let pool_result =
|
||||
crate::get_pool_by_address(self.database.as_ref(), pool_address.as_str()).await;
|
||||
crate::query_pools_get_by_address(self.database.as_ref(), pool_address.as_str())
|
||||
.await;
|
||||
let pool_option = match pool_result {
|
||||
Ok(pool_option) => pool_option,
|
||||
Err(error) => return Err(error),
|
||||
@@ -104,7 +106,7 @@ impl KbPoolOriginService {
|
||||
let pool_id = match pool.id {
|
||||
Some(pool_id) => pool_id,
|
||||
None => {
|
||||
return Err(crate::KbError::InvalidState(format!(
|
||||
return Err(crate::Error::InvalidState(format!(
|
||||
"pool '{}' has no internal id",
|
||||
pool.address
|
||||
)));
|
||||
@@ -116,7 +118,8 @@ impl KbPoolOriginService {
|
||||
}
|
||||
seen_pool_ids.insert(pool_id);
|
||||
|
||||
let pair_result = crate::get_pair_by_pool_id(self.database.as_ref(), pool_id).await;
|
||||
let pair_result =
|
||||
crate::query_pairs_get_by_pool_id(self.database.as_ref(), pool_id).await;
|
||||
let pair_option = match pair_result {
|
||||
Ok(pair_option) => pair_option,
|
||||
Err(error) => return Err(error),
|
||||
@@ -127,7 +130,7 @@ impl KbPoolOriginService {
|
||||
};
|
||||
|
||||
let listing_result =
|
||||
crate::get_pool_listing_by_pool_id(self.database.as_ref(), pool_id).await;
|
||||
crate::query_pool_listings_get_by_pool_id(self.database.as_ref(), pool_id).await;
|
||||
let listing_option = match listing_result {
|
||||
Ok(listing_option) => listing_option,
|
||||
Err(error) => return Err(error),
|
||||
@@ -137,11 +140,12 @@ impl KbPoolOriginService {
|
||||
None => None,
|
||||
};
|
||||
|
||||
let launch_attribution_result = crate::get_launch_attribution_by_decoded_event_id(
|
||||
self.database.as_ref(),
|
||||
decoded_event_id,
|
||||
)
|
||||
.await;
|
||||
let launch_attribution_result =
|
||||
crate::query_launch_attributions_get_by_decoded_event_id(
|
||||
self.database.as_ref(),
|
||||
decoded_event_id,
|
||||
)
|
||||
.await;
|
||||
let launch_attribution_option = match launch_attribution_result {
|
||||
Ok(launch_attribution_option) => launch_attribution_option,
|
||||
Err(error) => return Err(error),
|
||||
@@ -152,14 +156,14 @@ impl KbPoolOriginService {
|
||||
};
|
||||
|
||||
let existing_result =
|
||||
crate::get_pool_origin_by_pool_id(self.database.as_ref(), pool_id).await;
|
||||
crate::query_pool_origins_get_by_pool_id(self.database.as_ref(), pool_id).await;
|
||||
let existing_option = match existing_result {
|
||||
Ok(existing_option) => existing_option,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let created_origin = existing_option.is_none();
|
||||
|
||||
let dto = crate::KbPoolOriginDto::new(
|
||||
let dto = crate::PoolOriginDto::new(
|
||||
pool.dex_id,
|
||||
pool_id,
|
||||
pair_id,
|
||||
@@ -169,12 +173,13 @@ impl KbPoolOriginService {
|
||||
transaction.signature.clone(),
|
||||
decoded_event.protocol_name.clone(),
|
||||
decoded_event.event_kind.clone(),
|
||||
crate::KbObservationSourceKind::HttpRpc,
|
||||
crate::ObservationSourceKind::HttpRpc,
|
||||
transaction.source_endpoint_name.clone(),
|
||||
launch_attribution_id,
|
||||
);
|
||||
|
||||
let upsert_result = crate::upsert_pool_origin(self.database.as_ref(), &dto).await;
|
||||
let upsert_result =
|
||||
crate::query_pool_origins_upsert(self.database.as_ref(), &dto).await;
|
||||
let pool_origin_id = match upsert_result {
|
||||
Ok(pool_origin_id) => pool_origin_id,
|
||||
Err(error) => return Err(error),
|
||||
@@ -193,9 +198,9 @@ impl KbPoolOriginService {
|
||||
|
||||
let observation_result = self
|
||||
.persistence
|
||||
.record_observation(&crate::KbDetectionObservationInput::new(
|
||||
.record_observation(&crate::DetectionObservationInput::new(
|
||||
"dex.pool_origin".to_string(),
|
||||
crate::KbObservationSourceKind::HttpRpc,
|
||||
crate::ObservationSourceKind::HttpRpc,
|
||||
transaction.source_endpoint_name.clone(),
|
||||
transaction.signature.clone(),
|
||||
transaction.slot,
|
||||
@@ -209,9 +214,9 @@ impl KbPoolOriginService {
|
||||
|
||||
let signal_result = self
|
||||
.persistence
|
||||
.record_signal(&crate::KbDetectionSignalInput::new(
|
||||
.record_signal(&crate::DetectionSignalInput::new(
|
||||
"signal.dex.pool_origin.recorded".to_string(),
|
||||
crate::KbAnalysisSignalSeverity::Low,
|
||||
crate::AnalysisSignalSeverity::Low,
|
||||
transaction.signature.clone(),
|
||||
Some(observation_id),
|
||||
None,
|
||||
@@ -223,7 +228,7 @@ impl KbPoolOriginService {
|
||||
}
|
||||
}
|
||||
|
||||
results.push(crate::KbPoolOriginResult {
|
||||
results.push(crate::PoolOriginResult {
|
||||
pool_origin_id,
|
||||
pool_id,
|
||||
pair_id,
|
||||
@@ -237,17 +242,17 @@ impl KbPoolOriginService {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
async fn make_database() -> std::sync::Arc<crate::KbDatabase> {
|
||||
async fn make_database() -> std::sync::Arc<crate::Database> {
|
||||
let tempdir_result = tempfile::tempdir();
|
||||
let tempdir = match tempdir_result {
|
||||
Ok(tempdir) => tempdir,
|
||||
Err(error) => panic!("tempdir must succeed: {}", error),
|
||||
};
|
||||
let database_path = tempdir.path().join("pool_origin.sqlite3");
|
||||
let config = crate::KbDatabaseConfig {
|
||||
let config = crate::DatabaseConfig {
|
||||
enabled: true,
|
||||
backend: crate::KbDatabaseBackend::Sqlite,
|
||||
sqlite: crate::KbSqliteDatabaseConfig {
|
||||
backend: crate::DatabaseBackend::Sqlite,
|
||||
sqlite: crate::SqliteDatabaseConfig {
|
||||
path: database_path.to_string_lossy().to_string(),
|
||||
create_if_missing: true,
|
||||
busy_timeout_ms: 5000,
|
||||
@@ -256,7 +261,7 @@ mod tests {
|
||||
use_wal: true,
|
||||
},
|
||||
};
|
||||
let database_result = crate::KbDatabase::connect_and_initialize(&config).await;
|
||||
let database_result = crate::Database::connect_and_initialize(&config).await;
|
||||
let database = match database_result {
|
||||
Ok(database) => database,
|
||||
Err(error) => panic!("database init must succeed: {}", error),
|
||||
@@ -265,13 +270,13 @@ mod tests {
|
||||
}
|
||||
|
||||
async fn seed_bags_backed_meteora_dbc_transaction(
|
||||
database: std::sync::Arc<crate::KbDatabase>,
|
||||
database: std::sync::Arc<crate::Database>,
|
||||
signature: &str,
|
||||
) {
|
||||
let transaction_model = crate::KbTransactionModelService::new(database.clone());
|
||||
let dex_decode = crate::KbDexDecodeService::new(database.clone());
|
||||
let dex_detect = crate::KbDexDetectService::new(database.clone());
|
||||
let launch_origin = crate::KbLaunchOriginService::new(database.clone());
|
||||
let transaction_model = crate::TransactionModelService::new(database.clone());
|
||||
let dex_decode = crate::DexDecodeService::new(database.clone());
|
||||
let dex_detect = crate::DexDetectService::new(database.clone());
|
||||
let launch_origin = crate::LaunchOriginService::new(database.clone());
|
||||
|
||||
let bags_register_result = launch_origin
|
||||
.register_bags_pool_mapping(
|
||||
@@ -293,13 +298,13 @@ mod tests {
|
||||
"message": {
|
||||
"instructions": [
|
||||
{
|
||||
"programId": crate::KB_METEORA_DBC_PROGRAM_ID,
|
||||
"programId": crate::METEORA_DBC_PROGRAM_ID,
|
||||
"program": "meteora-dbc",
|
||||
"stackHeight": 1,
|
||||
"accounts": [
|
||||
"DbcOriginPool111",
|
||||
"DbcOriginToken111",
|
||||
"So11111111111111111111111111111111111111112",
|
||||
crate::WSOL_MINT_ID,
|
||||
"DbcOriginConfig111",
|
||||
"DbcOriginCreator111"
|
||||
],
|
||||
@@ -307,7 +312,7 @@ mod tests {
|
||||
"info": {
|
||||
"pool": "DbcOriginPool111",
|
||||
"baseMint": "DbcOriginToken111",
|
||||
"quoteMint": "So11111111111111111111111111111111111111112",
|
||||
"quoteMint": crate::WSOL_MINT_ID,
|
||||
"poolConfig": "DbcOriginConfig111",
|
||||
"creator": "DbcOriginCreator111"
|
||||
}
|
||||
@@ -357,7 +362,7 @@ mod tests {
|
||||
let database = make_database().await;
|
||||
seed_bags_backed_meteora_dbc_transaction(database.clone(), "sig-pool-origin-1").await;
|
||||
|
||||
let service = crate::KbPoolOriginService::new(database.clone());
|
||||
let service = crate::PoolOriginService::new(database.clone());
|
||||
let record_result = service.record_transaction_by_signature("sig-pool-origin-1").await;
|
||||
let results = match record_result {
|
||||
Ok(results) => results,
|
||||
@@ -368,7 +373,7 @@ mod tests {
|
||||
assert!(results[0].created_origin);
|
||||
|
||||
let fetched_result =
|
||||
crate::get_pool_origin_by_pool_id(database.as_ref(), results[0].pool_id).await;
|
||||
crate::query_pool_origins_get_by_pool_id(database.as_ref(), results[0].pool_id).await;
|
||||
let fetched_option = match fetched_result {
|
||||
Ok(fetched_option) => fetched_option,
|
||||
Err(error) => panic!("pool-origin fetch must succeed: {}", error),
|
||||
@@ -390,7 +395,7 @@ mod tests {
|
||||
let database = make_database().await;
|
||||
seed_bags_backed_meteora_dbc_transaction(database.clone(), "sig-pool-origin-2").await;
|
||||
|
||||
let service = crate::KbPoolOriginService::new(database.clone());
|
||||
let service = crate::PoolOriginService::new(database.clone());
|
||||
|
||||
let first_result = service.record_transaction_by_signature("sig-pool-origin-2").await;
|
||||
let first_results = match first_result {
|
||||
@@ -408,7 +413,7 @@ mod tests {
|
||||
assert_eq!(second_results.len(), 1);
|
||||
assert!(!second_results[0].created_origin);
|
||||
|
||||
let listed_result = crate::list_pool_origins(database.as_ref()).await;
|
||||
let listed_result = crate::query_pool_origins_list(database.as_ref()).await;
|
||||
let listed = match listed_result {
|
||||
Ok(listed) => listed,
|
||||
Err(error) => panic!("pool-origin list must succeed: {}", error),
|
||||
|
||||
Reference in New Issue
Block a user