0.7.27 +Refactor
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
/// One wallet-observation result.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbWalletObservationResult {
|
||||
pub struct WalletObservationResult {
|
||||
/// Related wallet id.
|
||||
pub wallet_id: i64,
|
||||
/// Wallet address.
|
||||
@@ -23,15 +23,15 @@ pub struct KbWalletObservationResult {
|
||||
|
||||
/// Wallet-observation service.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct KbWalletObservationService {
|
||||
database: std::sync::Arc<crate::KbDatabase>,
|
||||
persistence: crate::KbDetectionPersistenceService,
|
||||
pub struct WalletObservationService {
|
||||
database: std::sync::Arc<crate::Database>,
|
||||
persistence: crate::DetectionPersistenceService,
|
||||
}
|
||||
|
||||
impl KbWalletObservationService {
|
||||
impl WalletObservationService {
|
||||
/// Creates a new wallet-observation 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 };
|
||||
}
|
||||
|
||||
@@ -39,9 +39,10 @@ impl KbWalletObservationService {
|
||||
pub async fn record_transaction_by_signature(
|
||||
&self,
|
||||
signature: &str,
|
||||
) -> Result<std::vec::Vec<crate::KbWalletObservationResult>, crate::KbError> {
|
||||
) -> Result<std::vec::Vec<crate::WalletObservationResult>, 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),
|
||||
@@ -49,7 +50,7 @@ impl KbWalletObservationService {
|
||||
let transaction = match transaction_option {
|
||||
Some(transaction) => transaction,
|
||||
None => {
|
||||
return Err(crate::KbError::InvalidState(format!(
|
||||
return Err(crate::Error::InvalidState(format!(
|
||||
"cannot record wallet observations for unknown transaction '{}'",
|
||||
signature
|
||||
)));
|
||||
@@ -58,13 +59,13 @@ impl KbWalletObservationService {
|
||||
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,
|
||||
)
|
||||
@@ -78,16 +79,18 @@ impl KbWalletObservationService {
|
||||
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(),
|
||||
));
|
||||
},
|
||||
};
|
||||
let pool_id = match decoded_event.pool_account.clone() {
|
||||
Some(pool_address) => {
|
||||
let pool_result =
|
||||
crate::get_pool_by_address(self.database.as_ref(), pool_address.as_str())
|
||||
.await;
|
||||
let pool_result = 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),
|
||||
@@ -102,7 +105,7 @@ impl KbWalletObservationService {
|
||||
let pair_id = match pool_id {
|
||||
Some(pool_id) => {
|
||||
let pair_result =
|
||||
crate::get_pair_by_pool_id(self.database.as_ref(), pool_id).await;
|
||||
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),
|
||||
@@ -119,17 +122,19 @@ impl KbWalletObservationService {
|
||||
let payload = match payload_result {
|
||||
Ok(payload) => payload,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Json(format!(
|
||||
return Err(crate::Error::Json(format!(
|
||||
"cannot parse decoded_event payload_json '{}': {}",
|
||||
decoded_event.payload_json, error
|
||||
)));
|
||||
},
|
||||
};
|
||||
let observed_roles = kb_collect_wallet_roles(&payload);
|
||||
let observed_roles = collect_wallet_roles(&payload);
|
||||
for (role, wallet_address) in observed_roles {
|
||||
let wallet_result =
|
||||
crate::get_wallet_by_address(self.database.as_ref(), wallet_address.as_str())
|
||||
.await;
|
||||
let wallet_result = crate::query_wallets_get_by_address(
|
||||
self.database.as_ref(),
|
||||
wallet_address.as_str(),
|
||||
)
|
||||
.await;
|
||||
let wallet_option = match wallet_result {
|
||||
Ok(wallet_option) => wallet_option,
|
||||
Err(error) => return Err(error),
|
||||
@@ -137,56 +142,59 @@ impl KbWalletObservationService {
|
||||
let wallet_id = match wallet_option {
|
||||
Some(existing) => match existing.id {
|
||||
Some(wallet_id) => {
|
||||
let dto = crate::KbWalletDto::new(
|
||||
let dto = crate::WalletDto::new(
|
||||
wallet_address.clone(),
|
||||
existing.label.clone(),
|
||||
);
|
||||
let upsert_result =
|
||||
crate::upsert_wallet(self.database.as_ref(), &dto).await;
|
||||
crate::query_wallets_upsert(self.database.as_ref(), &dto).await;
|
||||
match upsert_result {
|
||||
Ok(_) => wallet_id,
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
},
|
||||
None => {
|
||||
return Err(crate::KbError::InvalidState(
|
||||
return Err(crate::Error::InvalidState(
|
||||
"wallet has no internal id".to_string(),
|
||||
));
|
||||
},
|
||||
},
|
||||
None => {
|
||||
let dto = crate::KbWalletDto::new(wallet_address.clone(), None);
|
||||
let dto = crate::WalletDto::new(wallet_address.clone(), None);
|
||||
let upsert_result =
|
||||
crate::upsert_wallet(self.database.as_ref(), &dto).await;
|
||||
crate::query_wallets_upsert(self.database.as_ref(), &dto).await;
|
||||
match upsert_result {
|
||||
Ok(wallet_id) => wallet_id,
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
},
|
||||
};
|
||||
let participation_dto = crate::KbWalletParticipationDto::new(
|
||||
let participation_dto = crate::WalletParticipationDto::new(
|
||||
wallet_id,
|
||||
transaction_id,
|
||||
Some(decoded_event_id),
|
||||
pool_id,
|
||||
pair_id,
|
||||
role.clone(),
|
||||
crate::KbObservationSourceKind::HttpRpc,
|
||||
crate::ObservationSourceKind::HttpRpc,
|
||||
transaction.source_endpoint_name.clone(),
|
||||
);
|
||||
let existing_participation_result = crate::get_wallet_participation_by_unique_key(
|
||||
self.database.as_ref(),
|
||||
participation_dto.unique_key.as_str(),
|
||||
)
|
||||
.await;
|
||||
let existing_participation_result =
|
||||
crate::query_wallet_participations_get_by_unique_key(
|
||||
self.database.as_ref(),
|
||||
participation_dto.unique_key.as_str(),
|
||||
)
|
||||
.await;
|
||||
let existing_participation_option = match existing_participation_result {
|
||||
Ok(existing_participation_option) => existing_participation_option,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let created_participation = existing_participation_option.is_none();
|
||||
let participation_id_result =
|
||||
crate::upsert_wallet_participation(self.database.as_ref(), &participation_dto)
|
||||
.await;
|
||||
let participation_id_result = crate::query_wallet_participations_upsert(
|
||||
self.database.as_ref(),
|
||||
&participation_dto,
|
||||
)
|
||||
.await;
|
||||
let wallet_participation_id = match participation_id_result {
|
||||
Ok(wallet_participation_id) => wallet_participation_id,
|
||||
Err(error) => return Err(error),
|
||||
@@ -203,9 +211,9 @@ impl KbWalletObservationService {
|
||||
});
|
||||
let observation_result = self
|
||||
.persistence
|
||||
.record_observation(&crate::KbDetectionObservationInput::new(
|
||||
.record_observation(&crate::DetectionObservationInput::new(
|
||||
"wallet.participation".to_string(),
|
||||
crate::KbObservationSourceKind::HttpRpc,
|
||||
crate::ObservationSourceKind::HttpRpc,
|
||||
transaction.source_endpoint_name.clone(),
|
||||
transaction.signature.clone(),
|
||||
transaction.slot,
|
||||
@@ -218,9 +226,9 @@ impl KbWalletObservationService {
|
||||
};
|
||||
let signal_result = self
|
||||
.persistence
|
||||
.record_signal(&crate::KbDetectionSignalInput::new(
|
||||
.record_signal(&crate::DetectionSignalInput::new(
|
||||
"signal.wallet.participation.observed".to_string(),
|
||||
crate::KbAnalysisSignalSeverity::Low,
|
||||
crate::AnalysisSignalSeverity::Low,
|
||||
transaction.signature.clone(),
|
||||
Some(observation_id),
|
||||
None,
|
||||
@@ -231,7 +239,7 @@ impl KbWalletObservationService {
|
||||
return Err(error);
|
||||
}
|
||||
}
|
||||
results.push(crate::KbWalletObservationResult {
|
||||
results.push(crate::WalletObservationResult {
|
||||
wallet_id,
|
||||
wallet_address,
|
||||
wallet_participation_id,
|
||||
@@ -246,7 +254,7 @@ impl KbWalletObservationService {
|
||||
}
|
||||
}
|
||||
|
||||
fn kb_collect_wallet_roles(
|
||||
fn collect_wallet_roles(
|
||||
payload: &serde_json::Value,
|
||||
) -> std::vec::Vec<(std::string::String, std::string::String)> {
|
||||
let role_mappings = [
|
||||
@@ -258,7 +266,7 @@ fn kb_collect_wallet_roles(
|
||||
let mut seen = std::collections::HashSet::<std::string::String>::new();
|
||||
let mut results = std::vec::Vec::new();
|
||||
for (role, keys) in role_mappings {
|
||||
let addresses = kb_extract_strings_for_candidate_keys(payload, &keys);
|
||||
let addresses = extract_strings_for_candidate_keys(payload, &keys);
|
||||
for address in addresses {
|
||||
let dedupe_key = format!("{}:{}", role, address);
|
||||
if seen.contains(&dedupe_key) {
|
||||
@@ -271,16 +279,16 @@ fn kb_collect_wallet_roles(
|
||||
return results;
|
||||
}
|
||||
|
||||
fn kb_extract_strings_for_candidate_keys(
|
||||
fn extract_strings_for_candidate_keys(
|
||||
value: &serde_json::Value,
|
||||
candidate_keys: &[&str],
|
||||
) -> std::vec::Vec<std::string::String> {
|
||||
let mut values = std::vec::Vec::new();
|
||||
kb_extract_strings_for_candidate_keys_inner(value, candidate_keys, &mut values);
|
||||
extract_strings_for_candidate_keys_inner(value, candidate_keys, &mut values);
|
||||
return values;
|
||||
}
|
||||
|
||||
fn kb_extract_strings_for_candidate_keys_inner(
|
||||
fn extract_strings_for_candidate_keys_inner(
|
||||
value: &serde_json::Value,
|
||||
candidate_keys: &[&str],
|
||||
values: &mut std::vec::Vec<std::string::String>,
|
||||
@@ -296,30 +304,30 @@ fn kb_extract_strings_for_candidate_keys_inner(
|
||||
}
|
||||
}
|
||||
for nested_value in object.values() {
|
||||
kb_extract_strings_for_candidate_keys_inner(nested_value, candidate_keys, values);
|
||||
extract_strings_for_candidate_keys_inner(nested_value, candidate_keys, values);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if let Some(array) = value.as_array() {
|
||||
for nested_value in array {
|
||||
kb_extract_strings_for_candidate_keys_inner(nested_value, candidate_keys, values);
|
||||
extract_strings_for_candidate_keys_inner(nested_value, candidate_keys, values);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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("wallet_observation.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,
|
||||
@@ -328,7 +336,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),
|
||||
@@ -336,13 +344,10 @@ mod tests {
|
||||
return std::sync::Arc::new(database);
|
||||
}
|
||||
|
||||
async fn seed_fluxbeam_transaction(
|
||||
database: std::sync::Arc<crate::KbDatabase>,
|
||||
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);
|
||||
async fn seed_fluxbeam_transaction(database: std::sync::Arc<crate::Database>, signature: &str) {
|
||||
let transaction_model = crate::TransactionModelService::new(database.clone());
|
||||
let dex_decode = crate::DexDecodeService::new(database.clone());
|
||||
let dex_detect = crate::DexDetectService::new(database);
|
||||
let resolved_transaction = serde_json::json!({
|
||||
"slot": 930001,
|
||||
"blockTime": 1779300001,
|
||||
@@ -351,14 +356,14 @@ mod tests {
|
||||
"message": {
|
||||
"instructions": [
|
||||
{
|
||||
"programId": crate::KB_FLUXBEAM_PROGRAM_ID,
|
||||
"programId": crate::FLUXBEAM_PROGRAM_ID,
|
||||
"program": "fluxbeam",
|
||||
"stackHeight": 1,
|
||||
"accounts": [
|
||||
"WalletObsPool111",
|
||||
"WalletObsLpMint111",
|
||||
"WalletObsTokenA111",
|
||||
"So11111111111111111111111111111111111111112",
|
||||
crate::WSOL_MINT_ID,
|
||||
"WalletObserved111"
|
||||
],
|
||||
"parsed": {
|
||||
@@ -367,7 +372,7 @@ mod tests {
|
||||
"pool": "WalletObsPool111",
|
||||
"lpMint": "WalletObsLpMint111",
|
||||
"tokenA": "WalletObsTokenA111",
|
||||
"tokenB": "So11111111111111111111111111111111111111112",
|
||||
"tokenB": crate::WSOL_MINT_ID,
|
||||
"payer": "WalletObserved111"
|
||||
}
|
||||
},
|
||||
@@ -407,7 +412,7 @@ mod tests {
|
||||
async fn record_transaction_by_signature_creates_wallet_and_participations() {
|
||||
let database = make_database().await;
|
||||
seed_fluxbeam_transaction(database.clone(), "sig-wallet-observation-1").await;
|
||||
let service = crate::KbWalletObservationService::new(database.clone());
|
||||
let service = crate::WalletObservationService::new(database.clone());
|
||||
let record_result =
|
||||
service.record_transaction_by_signature("sig-wallet-observation-1").await;
|
||||
let results = match record_result {
|
||||
@@ -415,7 +420,7 @@ mod tests {
|
||||
Err(error) => panic!("wallet observation must succeed: {}", error),
|
||||
};
|
||||
assert_eq!(results.len(), 2);
|
||||
let wallets_result = crate::list_wallets(database.as_ref()).await;
|
||||
let wallets_result = crate::query_wallets_list(database.as_ref()).await;
|
||||
let wallets = match wallets_result {
|
||||
Ok(wallets) => wallets,
|
||||
Err(error) => panic!("wallet list must succeed: {}", error),
|
||||
@@ -427,7 +432,8 @@ mod tests {
|
||||
None => panic!("wallet must have an id"),
|
||||
};
|
||||
let participations_result =
|
||||
crate::list_wallet_participations_by_wallet_id(database.as_ref(), wallet_id).await;
|
||||
crate::query_wallet_participations_list_by_wallet_id(database.as_ref(), wallet_id)
|
||||
.await;
|
||||
let participations = match participations_result {
|
||||
Ok(participations) => participations,
|
||||
Err(error) => panic!("participation list must succeed: {}", error),
|
||||
@@ -445,7 +451,7 @@ mod tests {
|
||||
async fn record_transaction_by_signature_is_idempotent() {
|
||||
let database = make_database().await;
|
||||
seed_fluxbeam_transaction(database.clone(), "sig-wallet-observation-2").await;
|
||||
let service = crate::KbWalletObservationService::new(database.clone());
|
||||
let service = crate::WalletObservationService::new(database.clone());
|
||||
let first_result =
|
||||
service.record_transaction_by_signature("sig-wallet-observation-2").await;
|
||||
let first_results = match first_result {
|
||||
@@ -464,7 +470,7 @@ mod tests {
|
||||
assert_eq!(second_results.len(), 2);
|
||||
assert!(!second_results[0].created_participation);
|
||||
assert!(!second_results[1].created_participation);
|
||||
let wallets_result = crate::list_wallets(database.as_ref()).await;
|
||||
let wallets_result = crate::query_wallets_list(database.as_ref()).await;
|
||||
let wallets = match wallets_result {
|
||||
Ok(wallets) => wallets,
|
||||
Err(error) => panic!("wallet list must succeed: {}", error),
|
||||
@@ -472,7 +478,8 @@ mod tests {
|
||||
assert_eq!(wallets.len(), 1);
|
||||
let wallet_id = wallets[0].id.unwrap_or_default();
|
||||
let participations_result =
|
||||
crate::list_wallet_participations_by_wallet_id(database.as_ref(), wallet_id).await;
|
||||
crate::query_wallet_participations_list_by_wallet_id(database.as_ref(), wallet_id)
|
||||
.await;
|
||||
let participations = match participations_result {
|
||||
Ok(participations) => participations,
|
||||
Err(error) => panic!("participation list must succeed: {}", error),
|
||||
|
||||
Reference in New Issue
Block a user