// file: kb_lib/src/dex_event_coverage.rs

//! Event coverage synchronization and reporting service.
//!
//! This service bridges the read-only upstream registry and the persisted
//! coverage table. It does not decode transactions and never materializes
//! trades, metrics or candles.

/// Result of one event coverage synchronization pass.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DexEventCoverageSyncResult {
    /// Optional decoder filter used for this synchronization pass.
    pub decoder_code: std::option::Option<std::string::String>,
    /// Number of upstream registry entries selected by the filter.
    pub upstream_entry_count: usize,
    /// Number of coverage rows upserted from the upstream registry.
    pub upserted_entry_count: usize,
    /// Number of coverage rows touched by the local observation refresh.
    pub refreshed_entry_count: u64,
    /// Aggregated coverage summaries after synchronization.
    pub summaries: std::vec::Vec<crate::DexEventCoverageSummaryDto>,
}

/// Service used to persist and refresh DEX event coverage rows.
#[derive(Debug, Clone)]
pub struct DexEventCoverageService {
    database: std::sync::Arc<crate::Database>,
    upstream_registry: crate::UpstreamRegistryService,
}

impl DexEventCoverageService {
    /// Creates a new event coverage service.
    pub fn new(database: std::sync::Arc<crate::Database>) -> Self {
        return Self {
            database,
            upstream_registry: crate::UpstreamRegistryService::new(),
        };
    }

    async fn upsert_upstream_registry_rows(
        &self,
        decoder_code: std::option::Option<std::string::String>,
    ) -> Result<(usize, usize), crate::Error> {
        let request = crate::UpstreamRegistrySearchRequestDto {
            decoder_code: decoder_code.clone(),
            program_id: None,
            program_family: None,
            surface_kind: None,
            entry_kind: None,
            proof_status: None,
            limit: None,
        };
        let search_result = self.upstream_registry.search(&request);
        let mut upserted_entry_count = 0_usize;
        for entry in &search_result.entries {
            let coverage_entry = build_coverage_entry_from_upstream(entry);
            let upsert_result = crate::query_dex_event_coverage_entries_upsert(
                self.database.as_ref(),
                &coverage_entry,
            )
            .await;
            match upsert_result {
                Ok(_) => upserted_entry_count += 1,
                Err(error) => return Err(error),
            }
        }
        let mut supplemental_entries = std::vec::Vec::new();
        if decoder_code.as_deref().is_none() || decoder_code.as_deref() == Some("meteora_dbc") {
            supplemental_entries.extend(local_meteora_dbc_registry_entries());
        }
        if decoder_code.as_deref().is_none() || decoder_code.as_deref() == Some("meteora_dlmm") {
            supplemental_entries.extend(local_meteora_dlmm_registry_entries());
        }
        for entry in &supplemental_entries {
            let coverage_entry = build_coverage_entry_from_upstream(entry);
            let upsert_result = crate::query_dex_event_coverage_entries_upsert(
                self.database.as_ref(),
                &coverage_entry,
            )
            .await;
            match upsert_result {
                Ok(_) => upserted_entry_count += 1,
                Err(error) => return Err(error),
            }
        }
        return Ok((
            search_result.entries.len() + supplemental_entries.len(),
            upserted_entry_count,
        ));
    }

    async fn ensure_upstream_registry_rows_if_needed(
        &self,
        decoder_code: std::option::Option<std::string::String>,
    ) -> Result<(usize, usize), crate::Error> {
        return self.upsert_upstream_registry_rows(decoder_code).await;
    }

    /// Synchronizes static upstream registry entries into SQLite coverage rows.
    ///
    /// The resulting rows are still discovery/audit metadata. A row can become
    /// observed or materialized only through local corpus replay and explicit
    /// count refreshes.
    pub async fn sync_upstream_registry(
        &self,
        decoder_code: std::option::Option<std::string::String>,
    ) -> Result<crate::DexEventCoverageSyncResult, crate::Error> {
        let sync_counts = self.upsert_upstream_registry_rows(decoder_code.clone()).await;
        let (upstream_entry_count, upserted_entry_count) = match sync_counts {
            Ok(sync_counts) => sync_counts,
            Err(error) => return Err(error),
        };
        let cleanup_result =
            self.cleanup_deprecated_pump_swap_observed_unknown_rows(&decoder_code).await;
        if let Err(error) = cleanup_result {
            return Err(error);
        }
        let duplicate_cleanup_result =
            self.cleanup_duplicate_pump_swap_logical_coverage_rows(&decoder_code).await;
        if let Err(error) = duplicate_cleanup_result {
            return Err(error);
        }
        let meteora_dbc_duplicate_cleanup_result =
            self.cleanup_duplicate_meteora_dbc_logical_coverage_rows(&decoder_code).await;
        if let Err(error) = meteora_dbc_duplicate_cleanup_result {
            return Err(error);
        }
        let meteora_dlmm_duplicate_cleanup_result =
            self.cleanup_duplicate_meteora_dlmm_logical_coverage_rows(&decoder_code).await;
        if let Err(error) = meteora_dlmm_duplicate_cleanup_result {
            return Err(error);
        }
        let refreshed_entry_count = match &decoder_code {
            Some(decoder_code) => {
                let refresh_result =
                    crate::query_dex_event_coverage_entries_refresh_local_counts_by_decoder(
                        self.database.as_ref(),
                        decoder_code.as_str(),
                    )
                    .await;
                match refresh_result {
                    Ok(refreshed_entry_count) => refreshed_entry_count,
                    Err(error) => return Err(error),
                }
            },
            None => {
                let refresh_result = crate::query_dex_event_coverage_entries_refresh_local_counts(
                    self.database.as_ref(),
                )
                .await;
                match refresh_result {
                    Ok(refreshed_entry_count) => refreshed_entry_count,
                    Err(error) => return Err(error),
                }
            },
        };
        let summaries_result =
            crate::query_dex_event_coverage_entries_list_summary_by_decoder(self.database.as_ref())
                .await;
        let summaries = match summaries_result {
            Ok(summaries) => summaries,
            Err(error) => return Err(error),
        };
        return Ok(crate::DexEventCoverageSyncResult {
            decoder_code,
            upstream_entry_count,
            upserted_entry_count,
            refreshed_entry_count,
            summaries,
        });
    }

    /// Refreshes observed, materialized and proof-status counters from local DB rows.
    pub async fn refresh_local_counts(
        &self,
        decoder_code: std::option::Option<std::string::String>,
    ) -> Result<crate::DexEventCoverageSyncResult, crate::Error> {
        let sync_counts = self.ensure_upstream_registry_rows_if_needed(decoder_code.clone()).await;
        let (upstream_entry_count, upserted_entry_count) = match sync_counts {
            Ok(sync_counts) => sync_counts,
            Err(error) => return Err(error),
        };
        let cleanup_result =
            self.cleanup_deprecated_pump_swap_observed_unknown_rows(&decoder_code).await;
        if let Err(error) = cleanup_result {
            return Err(error);
        }
        let duplicate_cleanup_result =
            self.cleanup_duplicate_pump_swap_logical_coverage_rows(&decoder_code).await;
        if let Err(error) = duplicate_cleanup_result {
            return Err(error);
        }
        let meteora_dbc_duplicate_cleanup_result =
            self.cleanup_duplicate_meteora_dbc_logical_coverage_rows(&decoder_code).await;
        if let Err(error) = meteora_dbc_duplicate_cleanup_result {
            return Err(error);
        }
        let meteora_dlmm_duplicate_cleanup_result =
            self.cleanup_duplicate_meteora_dlmm_logical_coverage_rows(&decoder_code).await;
        if let Err(error) = meteora_dlmm_duplicate_cleanup_result {
            return Err(error);
        }
        let refreshed_entry_count = match &decoder_code {
            Some(decoder_code) => {
                let refresh_result =
                    crate::query_dex_event_coverage_entries_refresh_local_counts_by_decoder(
                        self.database.as_ref(),
                        decoder_code.as_str(),
                    )
                    .await;
                match refresh_result {
                    Ok(refreshed_entry_count) => refreshed_entry_count,
                    Err(error) => return Err(error),
                }
            },
            None => {
                let refresh_result = crate::query_dex_event_coverage_entries_refresh_local_counts(
                    self.database.as_ref(),
                )
                .await;
                match refresh_result {
                    Ok(refreshed_entry_count) => refreshed_entry_count,
                    Err(error) => return Err(error),
                }
            },
        };
        let summaries_result =
            crate::query_dex_event_coverage_entries_list_summary_by_decoder(self.database.as_ref())
                .await;
        let summaries = match summaries_result {
            Ok(summaries) => summaries,
            Err(error) => return Err(error),
        };
        return Ok(crate::DexEventCoverageSyncResult {
            decoder_code,
            upstream_entry_count,
            upserted_entry_count,
            refreshed_entry_count,
            summaries,
        });
    }

    async fn cleanup_deprecated_pump_swap_observed_unknown_rows(
        &self,
        decoder_code: &std::option::Option<std::string::String>,
    ) -> Result<u64, crate::Error> {
        if let Some(decoder_code) = decoder_code {
            if decoder_code != "pump_swap" {
                return Ok(0);
            }
        }
        match self.database.connection() {
            crate::DatabaseConnection::Sqlite(pool) => {
                let query_result = sqlx::query(
                    r#"
DELETE FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'pump_swap'
  AND (
      entry_name LIKE 'observed_unknown_%'
      OR local_event_kind LIKE 'pump_swap.observed_unknown_%'
  )
                    "#,
                )
                .execute(pool)
                .await;
                match query_result {
                    Ok(query_result) => return Ok(query_result.rows_affected()),
                    Err(error) => {
                        return Err(crate::Error::Db(format!(
                            "cannot delete deprecated PumpSwap observed_unknown coverage rows on sqlite: {}",
                            error
                        )));
                    },
                }
            },
        }
    }

    async fn cleanup_duplicate_pump_swap_logical_coverage_rows(
        &self,
        decoder_code: &std::option::Option<std::string::String>,
    ) -> Result<u64, crate::Error> {
        if let Some(decoder_code) = decoder_code {
            if decoder_code != "pump_swap" {
                return Ok(0);
            }
        }
        match self.database.connection() {
            crate::DatabaseConnection::Sqlite(pool) => {
                let query_result = sqlx::query(
                    r#"
DELETE FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'pump_swap'
  AND id NOT IN (
      SELECT MIN(id)
      FROM k_sol_dex_event_coverage_entries
      WHERE decoder_code = 'pump_swap'
      GROUP BY
          decoder_code,
          COALESCE(program_id, ''),
          entry_kind,
          entry_name,
          COALESCE(discriminator_hex, ''),
          COALESCE(local_event_kind, '')
  )
                    "#,
                )
                .execute(pool)
                .await;
                match query_result {
                    Ok(query_result) => return Ok(query_result.rows_affected()),
                    Err(error) => {
                        return Err(crate::Error::Db(format!(
                            "cannot delete duplicate PumpSwap logical coverage rows on sqlite: {}",
                            error
                        )));
                    },
                }
            },
        }
    }

    async fn cleanup_duplicate_meteora_dbc_logical_coverage_rows(
        &self,
        decoder_code: &std::option::Option<std::string::String>,
    ) -> Result<u64, crate::Error> {
        if let Some(decoder_code) = decoder_code {
            if decoder_code != "meteora_dbc" {
                return Ok(0);
            }
        }
        match self.database.connection() {
            crate::DatabaseConnection::Sqlite(pool) => {
                let query_result = sqlx::query(
                    r#"
DELETE FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'meteora_dbc'
  AND id NOT IN (
      SELECT MIN(id)
      FROM k_sol_dex_event_coverage_entries
      WHERE decoder_code = 'meteora_dbc'
      GROUP BY
          decoder_code,
          COALESCE(program_id, ''),
          entry_kind,
          entry_name,
          COALESCE(discriminator_hex, ''),
          COALESCE(local_event_kind, '')
  )
                    "#,
                )
                .execute(pool)
                .await;
                match query_result {
                    Ok(query_result) => return Ok(query_result.rows_affected()),
                    Err(error) => {
                        return Err(crate::Error::Db(format!(
                            "cannot delete duplicate Meteora DBC logical coverage rows on sqlite: {}",
                            error
                        )));
                    },
                }
            },
        }
    }

    async fn cleanup_duplicate_meteora_dlmm_logical_coverage_rows(
        &self,
        decoder_code: &std::option::Option<std::string::String>,
    ) -> Result<u64, crate::Error> {
        if let Some(decoder_code) = decoder_code {
            if decoder_code != "meteora_dlmm" {
                return Ok(0);
            }
        }
        match self.database.connection() {
            crate::DatabaseConnection::Sqlite(pool) => {
                let query_result = sqlx::query(
                    r#"
DELETE FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'meteora_dlmm'
  AND id NOT IN (
      SELECT MIN(id)
      FROM k_sol_dex_event_coverage_entries
      WHERE decoder_code = 'meteora_dlmm'
      GROUP BY
          decoder_code,
          COALESCE(program_id, ''),
          entry_kind,
          entry_name,
          COALESCE(discriminator_hex, ''),
          COALESCE(local_event_kind, '')
  )
                    "#,
                )
                .execute(pool)
                .await;
                match query_result {
                    Ok(query_result) => return Ok(query_result.rows_affected()),
                    Err(error) => {
                        return Err(crate::Error::Db(format!(
                            "cannot delete duplicate Meteora DLMM logical coverage rows on sqlite: {}",
                            error
                        )));
                    },
                }
            },
        }
    }
}

fn build_coverage_entry_from_upstream(
    entry: &crate::UpstreamRegistryEntryDto,
) -> crate::DexEventCoverageEntryDto {
    let event_family = infer_event_family_for_entry(
        entry.decoder_code.as_str(),
        entry.entry_name.as_str(),
        entry.entry_kind.as_str(),
    );
    let expected_db_target = infer_expected_db_target_for_entry(
        entry.decoder_code.as_str(),
        entry.entry_name.as_str(),
        event_family.as_deref(),
        entry.entry_kind.as_str(),
    );
    let local_event_kind =
        known_local_event_kind(entry.decoder_code.as_str(), entry.entry_name.as_str());
    let mut coverage_entry = crate::DexEventCoverageEntryDto::from_upstream_registry_entry(
        entry,
        event_family,
        expected_db_target,
        local_event_kind.clone(),
    );
    if local_event_kind.is_some() && coverage_entry.observed_count == 0 {
        coverage_entry.proof_status =
            crate::PROOF_STATUS_UPSTREAM_GIT_MAPPED_UNVERIFIED.to_string();
    }
    return coverage_entry;
}

fn local_meteora_dbc_registry_entries() -> std::vec::Vec<crate::UpstreamRegistryEntryDto> {
    let mut entries = std::vec::Vec::new();
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "anchor_self_cpi_log",
        Some("e445a52e51cb9a1d"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "instruction_audit",
        None,
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "claim_creator_trading_fee",
        Some("52dcfabd03556b2d"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "claim_partner_pool_creation_fee",
        Some("faee1a048b0a65f8"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "claim_protocol_fee",
        Some("a5e4853063f9ff21"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "claim_protocol_pool_creation_fee",
        Some("72cd53bcf0991936"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "claim_trading_fee",
        Some("08ec5931987db151"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_claim_protocol_fee_operator",
        Some("082957235030791a"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_operator_account",
        Some("ab09d54a7817031d"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "create_config",
        Some("c9cff3724b6f2fbd"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "create_locker",
        Some("a75a899a4b2f1154"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "create_operator_account",
        Some("dd40f695f099e5a3"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "create_partner_metadata",
        Some("c0a8eabfbce2e3ff"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "create_virtual_pool_metadata",
        Some("2d61bb67fe6d7c86"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "creator_withdraw_surplus",
        Some("a50389071c864c50"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_virtual_pool_with_spl_token",
        Some("8c55d7b06636684f"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_virtual_pool_with_token2022",
        Some("a976334e916edc9b"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "migrate_meteora_damm",
        Some("1b013016b43f76d9"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "migrate_meteora_damm_claim_lp_token",
        Some("8b85021e5b917f9a"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "migrate_meteora_damm_lock_lp_token",
        Some("b137ee9dfb58a52a"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "migration_damm_v2",
        Some("9ca9e66735e45040"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "migration_damm_v2_create_metadata",
        Some("6dbd1324c3b7de52"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "migration_meteora_damm_create_metadata",
        Some("2f5e7e73dde2c285"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "partner_withdraw_surplus",
        Some("a8ad4864c962265c"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "swap",
        Some("f8c69e91e17587c8"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "swap2",
        Some("414b3f4ceb5b5b88"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "transfer_pool_creator",
        Some("1407a9213a93a621"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "withdraw_leftover",
        Some("14c6caedebf3b742"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "withdraw_migration_fee",
        Some("ed8e2d178106dea2"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "zap_protocol_fee",
        Some("d59bbb2238b65bf0"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_claim_creator_trading_fee_event_local_idl",
        Some("9ae4d7ca859bd68a"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_claim_pool_creation_fee_event_local_idl",
        Some("956f952c8840af3e"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_claim_protocol_fee_event_local_idl",
        Some("baf44bfbbc0d1921"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_claim_protocol_liquidity_migration_fee_event_local_idl",
        Some("51a8741fa1561b23"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_claim_trading_fee_event_local_idl",
        Some("1a5375f05cca70fe"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_close_claim_fee_operator_event_local_idl",
        Some("6f2725376ed8c217"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_create_claim_fee_operator_event_local_idl",
        Some("1506997844741cb1"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_create_config_event_local_idl",
        Some("83cfb4aeb449a536"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_create_config_v2_event_local_idl",
        Some("a34a42bb77c31a90"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_create_meteora_migration_metadata_event_local_idl",
        Some("63a7853fd68faf8b"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_creator_withdraw_surplus_event_local_idl",
        Some("9849150f4257359d"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_curve_complete_event_local_idl",
        Some("e5e756549c864b18"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_initialize_pool_event_local_idl",
        Some("e432f655cb428625"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_partner_claim_pool_creation_fee_event_local_idl",
        Some("aedf2c96916259c3"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_partner_metadata_event_local_idl",
        Some("c87f06370d200896"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_partner_withdraw_migration_fee_event_local_idl",
        Some("b5697f4308bb7839"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_partner_withdraw_surplus_event_local_idl",
        Some("c3389809e8482316"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_swap_event_local_idl",
        Some("1b3c15d58aaabb93"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_swap2_event_local_idl",
        Some("bd4233a826507599"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_update_pool_creator_event_local_idl",
        Some("6be1a5ed5b9ed5dc"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_virtual_pool_metadata_event_local_idl",
        Some("bc12484cc35b264a"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_withdraw_leftover_event_local_idl",
        Some("bfbd688f6f9c5ee5"),
    );
    add_local_meteora_dbc_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "evt_withdraw_migration_fee_event_local_idl",
        Some("1acb5455a11764d6"),
    );
    return entries;
}

fn add_local_meteora_dbc_entry(
    entries: &mut std::vec::Vec<crate::UpstreamRegistryEntryDto>,
    entry_kind: &str,
    entry_name: &str,
    discriminator_hex: std::option::Option<&str>,
) {
    entries.push(crate::UpstreamRegistryEntryDto {
        source_repo: Some("local_idl".to_string()),
        source_path: Some(
            "idls/meteora_dbc.dbcij3LWUppWqq96dh6gJWwBifmcGfLSB5D4DuSMaqN.json".to_string(),
        ),
        decoder_code: "meteora_dbc".to_string(),
        program_id: Some(crate::METEORA_DBC_PROGRAM_ID.to_string()),
        program_family: "meteora".to_string(),
        surface_kind: "launch_bonding_curve".to_string(),
        entry_kind: entry_kind.to_string(),
        entry_name: entry_name.to_string(),
        discriminator_hex: discriminator_hex.map(|value| return value.to_string()),
        discriminator_len: discriminator_hex.map(|_| return 8_u16),
        proof_status: crate::PROOF_STATUS_UPSTREAM_GIT_MAPPED_UNVERIFIED.to_string(),
        notes: "supplemental local Meteora DBC IDL coverage row".to_string(),
    });
}

fn local_meteora_dlmm_registry_entries() -> std::vec::Vec<crate::UpstreamRegistryEntryDto> {
    let mut entries = std::vec::Vec::new();
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "instruction_audit",
        None,
    );
    add_local_meteora_dlmm_entry(&mut entries, crate::ENTRY_KIND_EVENT, "create_pool", None);
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "anchor_self_cpi_log",
        Some("e445a52e51cb9a1d"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "add_liquidity",
        Some("b59d59438fb63448"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "add_liquidity2",
        Some("e4a24e1c46db7473"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "add_liquidity_by_strategy",
        Some("0703967f94283dc8"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "add_liquidity_by_strategy2",
        Some("03dd95da6f8d76d5"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "add_liquidity_by_strategy_one_side",
        Some("2905eeaf64e106cd"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "add_liquidity_by_weight",
        Some("1c8cee63e7a21595"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "add_liquidity_by_weight2",
        Some("d13b3f5b6fc899e4"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "add_liquidity_one_side",
        Some("5e9b6797465fdca5"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "add_liquidity_one_side_precise",
        Some("a1c26754ab47fa9a"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "add_liquidity_one_side_precise2",
        Some("2133a3c975627de7"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "cancel_limit_order",
        Some("849c841f4328e861"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "claim_fee",
        Some("a9204f8988e84689"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "claim_fee2",
        Some("70bf65ab1c907fbb"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "claim_reward",
        Some("955fb5f25e5a9ea2"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "claim_reward2",
        Some("be037f77b2579db7"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_bin_array",
        Some("44ae5850b5cc13e0"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_claim_fee_operator_account",
        Some("b8d5581fb3658224"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_limit_order_if_empty",
        Some("397c249b7ef95dab"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_operator_account",
        Some("ab09d54a7817031d"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_position",
        Some("7b86510031446262"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_position2",
        Some("ae5a2373ba2893e2"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_position_if_empty",
        Some("3b7cd4765b986e9d"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_preset_parameter",
        Some("04949164861ab53d"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_preset_parameter2",
        Some("27195f6b7411731c"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "close_token_badge",
        Some("6c92566eb3fe0a68"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "create_operator_account",
        Some("dd40f695f099e5a3"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "decrease_position_length",
        Some("c2db882019606925"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "for_idl_type_generation_do_not_call",
        Some("b46945505f32496c"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "fund_reward",
        Some("bc32f9a55d97263f"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "go_to_a_bin",
        Some("9248aee028fd54ae"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "increase_oracle_length",
        Some("be3d7d57674f9ead"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "increase_position_length",
        Some("505375d3420d2195"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "increase_position_length2",
        Some("ffd2cc477389e171"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_bin_array",
        Some("235613b94ed44bd3"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_bin_array_bitmap_extension",
        Some("2f9de2b40cf02147"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_customizable_permissionless_lb_pair",
        Some("2e2729876fb7c840"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_customizable_permissionless_lb_pair2",
        Some("f349817e3313f16b"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_lb_pair",
        Some("2d9aedd2dd0fa65c"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_lb_pair2",
        Some("493b2478ed536cc6"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_permission_lb_pair",
        Some("6c66d555fb033515"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_position",
        Some("dbc0ea47bebf6650"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_position2",
        Some("8f13f291d50f6873"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_position_by_operator",
        Some("fbbdbef475fe2394"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_position_pda",
        Some("2e527d92558de499"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_preset_parameter",
        Some("42bc47d3626d0eba"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_preset_parameter_v2",
        Some("75c73e67068e1fcb"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_reward",
        Some("5f87c0c4f281e644"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "initialize_token_badge",
        Some("fd4dcd5f1be059df"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "place_limit_order",
        Some("6cb021ba92e501c5"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "rebalance_liquidity",
        Some("5c04b0c177b95309"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "remove_all_liquidity",
        Some("0a333d2370691855"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "remove_liquidity",
        Some("5055d14818ceb16c"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "remove_liquidity2",
        Some("e6d7527ff165e392"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "remove_liquidity_by_range",
        Some("1a526698f04a691a"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "remove_liquidity_by_range2",
        Some("cc02c391359191cd"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "set_activation_point",
        Some("5bf90fa51a81fe7d"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "set_pair_status",
        Some("43f8e7899a95d9ae"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "set_pair_status_permissionless",
        Some("4e3b98d346b72ed0"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "set_permissionless_operation_bits",
        Some("543acb8ba351beba"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "set_pre_activation_duration",
        Some("a53dc9f4829f1664"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "set_pre_activation_swap_address",
        Some("398b2f7bd850df0a"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "swap",
        Some("f8c69e91e17587c8"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "swap2",
        Some("414b3f4ceb5b5b88"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "swap_exact_out",
        Some("fa49652126cf4bb8"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "swap_exact_out2",
        Some("2bd7f784893cf351"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "swap_with_price_impact",
        Some("38ade6d0ade49ccd"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "swap_with_price_impact2",
        Some("4a62c0d6b1334b33"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "update_base_fee_parameters",
        Some("4ba8dfa110c3032f"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "update_dynamic_fee_parameters",
        Some("5ca12ef6ffbd1616"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "update_fees_and_reward2",
        Some("208eb89a6741b858"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "update_fees_and_rewards",
        Some("9ae6fa0decd14bdf"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "update_position_operator",
        Some("cab8678fb4bf74d9"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "update_reward_duration",
        Some("8aaec4a9d5ebfe6b"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "update_reward_funder",
        Some("d31c3020d7a02317"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "withdraw_ineligible_reward",
        Some("94ce2ac3f7316708"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "withdraw_protocol_fee",
        Some("9ec99ebd215da267"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_INSTRUCTION,
        "zap_protocol_fee",
        Some("d59bbb2238b65bf0"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "add_liquidity_event",
        Some("1f5e7d5ae3343dba"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "cancel_limit_order_evt",
        Some("83eac285090ebdd1"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "claim_fee_event",
        Some("4b7a9a308c4a7ba3"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "claim_fee2_event",
        Some("e8abf2613a4d232d"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "claim_reward_event",
        Some("947486cc16ab555f"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "claim_reward2_event",
        Some("1b8ff421502b6e92"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "close_limit_order_evt",
        Some("8e87084c5c3f7653"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "composition_fee_event",
        Some("80977b6a1166718e"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "decrease_position_length_event",
        Some("3476eb55aca90f80"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "dynamic_fee_parameter_update_event",
        Some("5858b287c2925bf3"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "fee_parameter_update_event",
        Some("304cf17590d7f22c"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "fund_reward_event",
        Some("f6e43a8291aa4fcc"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "go_to_a_bin_event",
        Some("3b8a4c448a83b043"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "increase_observation_event",
        Some("63f91179a69ccfd7"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "increase_position_length_event",
        Some("9def2acc1e38df2e"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "initialize_reward_event",
        Some("d399583e953cb146"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "lb_pair_create_event",
        Some("b94afc7d1bd7bc6f"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "place_limit_order_evt",
        Some("2b4f1ba9f41ce13f"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "position_close_event",
        Some("ffc4106b1cca3580"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "position_create_event",
        Some("908efc549d352579"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "rebalancing_event",
        Some("006d75b33d5bc7c8"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "remove_liquidity_event",
        Some("74f461e8671f983a"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "set_position_permissionless_operation_bits_evt",
        Some("c3e593f51d7d30a8"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "swap_event",
        Some("516ce3becdd00ac4"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "swap2_evt",
        Some("2e7452d7941b544d"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "update_position_lock_release_point_event",
        Some("85d642e0400c07bf"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "update_position_operator_event",
        Some("277330ccf62f4239"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "update_reward_duration_event",
        Some("dff5e099311da3ac"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "update_reward_funder_event",
        Some("e0b2ae4afca555b4"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_EVENT,
        "withdraw_ineligible_reward_event",
        Some("e7bd419566d79af4"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "bin_array",
        Some("5c8e5cdc059446b5"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "bin_array_bitmap_extension",
        Some("506f7c7137ed1205"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "claim_fee_operator",
        Some("a630865622c8bc96"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "dummy_zc_account",
        Some("5e6bee50d030b408"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "lb_pair",
        Some("210b3162b565b10d"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "limit_order",
        Some("89b7d45b731d8de3"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "operator",
        Some("db1fbc91458bcc75"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "oracle",
        Some("8bc283b38cb3e5f4"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "position_v2",
        Some("75b0d4c7f5b485b6"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "preset_parameter",
        Some("f23ef422b5703aaa"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "preset_parameter2",
        Some("abec9473a271deae"),
    );
    add_local_meteora_dlmm_entry(
        &mut entries,
        crate::ENTRY_KIND_ACCOUNT,
        "token_badge",
        Some("74dbcce5f974ff96"),
    );
    return entries;
}

fn add_local_meteora_dlmm_entry(
    entries: &mut std::vec::Vec<crate::UpstreamRegistryEntryDto>,
    entry_kind: &str,
    entry_name: &str,
    discriminator_hex: std::option::Option<&str>,
) {
    entries.push(crate::UpstreamRegistryEntryDto {
        source_repo: Some("local_idl".to_string()),
        source_path: Some(
            "idls/meteora_dlmm.LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo.json".to_string(),
        ),
        decoder_code: "meteora_dlmm".to_string(),
        program_id: Some(crate::METEORA_DLMM_PROGRAM_ID.to_string()),
        program_family: "meteora".to_string(),
        surface_kind: "dlmm".to_string(),
        entry_kind: entry_kind.to_string(),
        entry_name: entry_name.to_string(),
        discriminator_hex: discriminator_hex.map(|value| return value.to_string()),
        discriminator_len: discriminator_hex.map(|_| return 8_u16),
        proof_status: crate::PROOF_STATUS_UPSTREAM_GIT_MAPPED_UNVERIFIED.to_string(),
        notes: "supplemental local Meteora DLMM IDL coverage row".to_string(),
    });
}

fn infer_expected_db_target_for_entry(
    decoder_code: &str,
    entry_name: &str,
    event_family: std::option::Option<&str>,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if decoder_code == "pump_swap" {
        return infer_pump_swap_expected_db_target(entry_name, entry_kind);
    }
    if decoder_code == "pump_fun" {
        return infer_pump_fun_expected_db_target(entry_name, entry_kind);
    }
    if decoder_code == "pump_fees" {
        return infer_pump_fees_expected_db_target(entry_name, entry_kind);
    }
    if decoder_code == "meteora_dbc" {
        return infer_meteora_dbc_expected_db_target(entry_name, entry_kind);
    }
    if decoder_code == "meteora_dlmm" {
        return infer_meteora_dlmm_expected_db_target(entry_name, entry_kind);
    }
    if decoder_code == "raydium_cpmm"
        && (entry_name == "swap_event" || entry_name == "anchor_idl_instruction")
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
    }
    if decoder_code == "raydium_amm_v4" {
        if entry_name == "swap_base_in"
            || entry_name == "swap_base_out"
            || entry_name == "swap_base_in_v2"
            || entry_name == "swap_base_out_v2"
        {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS.to_string());
        }
        if entry_name == "initialize"
            || entry_name == "initialize2"
            || entry_name == "pre_initialize"
        {
            return Some(
                crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string(),
            );
        }
        if entry_name == "deposit" || entry_name == "withdraw" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS.to_string());
        }
        if entry_name == "withdraw_pnl" || entry_name == "withdraw_srm" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS.to_string());
        }
        if entry_name == "admin_cancel_orders" || entry_name == "migrate_to_open_book" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_ORDERBOOK_EVENTS.to_string());
        }
        if entry_name == "monitor_step" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_ORDERBOOK_EVENTS.to_string());
        }
        if entry_name == "create_config_account"
            || entry_name == "update_config_account"
            || entry_name == "set_params"
        {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string());
        }
        if entry_name == "simulate_info" {
            return Some(
                crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string(),
            );
        }
    }
    if decoder_code == "raydium_stable_swap" {
        if entry_name == "initialize" || entry_name == "pre_initialize" {
            return Some(
                crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string(),
            );
        }
        if entry_name == "deposit" || entry_name == "withdraw" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS.to_string());
        }
        if entry_name == "swap_base_in" || entry_name == "swap_base_out" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS.to_string());
        }
        if entry_name == "withdraw_pnl" || entry_name == "withdraw_srm" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS.to_string());
        }
        if entry_name == "set_params" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string());
        }
        if entry_name == "monitor_step" || entry_name == "admin_cancel_orders" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_ORDERBOOK_EVENTS.to_string());
        }
        if entry_name == "update_model_data" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string());
        }
        if entry_name == "init_model_data"
            || entry_name == "simulate_info"
            || entry_name == "swap_event"
        {
            return Some(
                crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string(),
            );
        }
    }
    if decoder_code == "raydium_clmm" {
        if entry_name == "initialize_reward" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_REWARD_EVENTS.to_string());
        }
        if entry_name == "swap_event" || entry_name == "swap_router_base_in" {
            return Some(
                crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string(),
            );
        }
        if entry_name == "open_position"
            || entry_name == "close_position"
            || entry_name == "close_protocol_position"
        {
            return Some(
                crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string(),
            );
        }
    }
    if decoder_code == "raydium_launchpad" {
        if entry_name == "trade_event" {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS.to_string());
        }
        if entry_name == "buy_exact_in"
            || entry_name == "buy_exact_out"
            || entry_name == "sell_exact_in"
            || entry_name == "sell_exact_out"
        {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_LAUNCH_EVENTS.to_string());
        }
        if entry_name == "initialize"
            || entry_name == "initialize_v2"
            || entry_name == "initialize_with_token_2022"
            || entry_name == "pool_create_event"
        {
            return Some(
                crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string(),
            );
        }
        if entry_name == "claim_creator_fee"
            || entry_name == "claim_platform_fee"
            || entry_name == "claim_platform_fee_from_vault"
            || entry_name == "collect_fee"
            || entry_name == "collect_migrate_fee"
        {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS.to_string());
        }
        if entry_name == "close_platform_global_access"
            || entry_name == "create_config"
            || entry_name == "create_platform_config"
            || entry_name == "create_platform_global_access"
            || entry_name == "remove_platform_curve_param"
            || entry_name == "update_config"
            || entry_name == "update_platform_config"
            || entry_name == "update_platform_curve_param"
        {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string());
        }
        if entry_name == "claim_vested_event"
            || entry_name == "claim_vested_token"
            || entry_name == "create_platform_vesting_account"
            || entry_name == "create_vesting_account"
            || entry_name == "create_vesting_event"
            || entry_name == "migrate_to_amm"
            || entry_name == "migrate_to_cpswap"
        {
            return Some(crate::DexEventCoverageEntryDto::DB_TARGET_LAUNCH_EVENTS.to_string());
        }
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
    }
    return infer_expected_db_target(event_family, entry_kind);
}

fn infer_expected_db_target(
    event_family: std::option::Option<&str>,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM || entry_kind == crate::ENTRY_KIND_ACCOUNT {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
    }
    let family = match event_family {
        Some(family) => family,
        None => {
            return Some(
                crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string(),
            );
        },
    };
    let target = match family {
        "swap" => crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS,
        "pool_create" => crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS,
        "liquidity" => crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS,
        "liquidity_add" => crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS,
        "liquidity_remove" => crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS,
        "liquidity_change" => crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS,
        "position_open" => crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS,
        "position_close" => crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS,
        "fee" => crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS,
        "reward" => crate::DexEventCoverageEntryDto::DB_TARGET_REWARD_EVENTS,
        "admin_config" => crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS,
        "mint" => crate::DexEventCoverageEntryDto::DB_TARGET_TOKEN_MINT_EVENTS,
        "burn" => crate::DexEventCoverageEntryDto::DB_TARGET_TOKEN_BURN_EVENTS,
        "transfer" => crate::DexEventCoverageEntryDto::DB_TARGET_TOKEN_TRANSFER_EVENTS,
        "account_create" => crate::DexEventCoverageEntryDto::DB_TARGET_TOKEN_ACCOUNT_EVENTS,
        "account_close" => crate::DexEventCoverageEntryDto::DB_TARGET_TOKEN_ACCOUNT_EVENTS,
        "wrap_sol" => crate::DexEventCoverageEntryDto::DB_TARGET_TOKEN_ACCOUNT_EVENTS,
        "unwrap_sol" => crate::DexEventCoverageEntryDto::DB_TARGET_TOKEN_ACCOUNT_EVENTS,
        "order_place" => crate::DexEventCoverageEntryDto::DB_TARGET_ORDERBOOK_EVENTS,
        "order_cancel" => crate::DexEventCoverageEntryDto::DB_TARGET_ORDERBOOK_EVENTS,
        "order_fill" => crate::DexEventCoverageEntryDto::DB_TARGET_ORDERBOOK_EVENTS,
        "consume_events" => crate::DexEventCoverageEntryDto::DB_TARGET_ORDERBOOK_EVENTS,
        "settle_funds" => crate::DexEventCoverageEntryDto::DB_TARGET_ORDERBOOK_EVENTS,
        "vault_deposit" => crate::DexEventCoverageEntryDto::DB_TARGET_VAULT_EVENTS,
        "vault_withdraw" => crate::DexEventCoverageEntryDto::DB_TARGET_VAULT_EVENTS,
        "lock" => crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_LOCK_EVENTS,
        "unlock" => crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_LOCK_EVENTS,
        "launch" => crate::DexEventCoverageEntryDto::DB_TARGET_LAUNCH_EVENTS,
        "migration" => crate::DexEventCoverageEntryDto::DB_TARGET_LAUNCH_EVENTS,
        "vesting" => crate::DexEventCoverageEntryDto::DB_TARGET_LAUNCH_EVENTS,
        "liquidity_calculation" => crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY,
        "cpi_transport" => crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY,
        "idl_management" => crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY,
        "stake" => crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY,
        "unstake" => crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY,
        _ => crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY,
    };
    return Some(target.to_string());
}

fn infer_pump_fees_expected_db_target(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM || entry_kind == crate::ENTRY_KIND_ACCOUNT {
        return None;
    }
    if entry_name == "get_fees" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
    }
    if entry_name == "claim_social_fee_pda"
        || entry_name == "claim_social_fee_pda_v2"
        || entry_name == "social_fee_pda_claimed"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_REWARD_EVENTS.to_string());
    }
    if entry_name == "crank_donation_fee_pda"
        || entry_name == "donation_fee_pda_cranked"
        || entry_name == "sweep_buyback"
        || entry_name == "sweep_buyback_event"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS.to_string());
    }
    if entry_name.contains("create_")
        || entry_name.contains("created")
        || entry_name.contains("initialize_")
        || entry_name == "extend_fee_config"
        || entry_name == "extend_fee_config_event"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string());
    }
    if entry_name.contains("config")
        || entry_name.contains("authority")
        || entry_name.contains("admin")
        || entry_name.contains("tier")
        || entry_name.contains("disable")
        || entry_name.contains("reset_fee_sharing")
        || entry_name.contains("update_fee_shares")
        || entry_name.contains("buyback_claim_rate_limit")
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string());
    }
    return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
}

fn infer_pump_fun_expected_db_target(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    if entry_name == "buy"
        || entry_name == "sell"
        || entry_name == "buy_v2"
        || entry_name == "sell_v2"
        || entry_name == "buy_exact_sol_in"
        || entry_name == "buy_exact_quote_in_v2"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS.to_string());
    }
    if entry_name == "trade_event" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS.to_string());
    }
    if entry_name == "create"
        || entry_name == "create_event"
        || entry_name == "create_v2"
        || entry_name == "create_v2_token"
        || entry_name == "complete_event"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_LAUNCH_EVENTS.to_string());
    }
    if entry_name == "initialize" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string());
    }
    if entry_name == "migrate"
        || entry_name == "migrate_v2"
        || entry_name == "migrate_bonding_curve_creator"
        || entry_name == "migrate_bonding_curve_creator_event"
        || entry_name == "complete_pump_amm_migration_event"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_LAUNCH_EVENTS.to_string());
    }
    if entry_name == "collect_creator_fee"
        || entry_name == "collect_creator_fee_v2"
        || entry_name == "collect_creator_fee_event"
        || entry_name == "distribute_creator_fees"
        || entry_name == "distribute_creator_fees_v2"
        || entry_name == "distribute_creator_fees_event"
        || entry_name == "get_minimum_distributable_fee"
        || entry_name == "minimum_distributable_fee_event"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS.to_string());
    }
    if entry_name == "claim_cashback"
        || entry_name == "claim_cashback_v2"
        || entry_name == "claim_cashback_event"
        || entry_name == "claim_token_incentives"
        || entry_name == "claim_token_incentives_event"
        || entry_name == "admin_update_token_incentives"
        || entry_name == "admin_update_token_incentives_event"
        || entry_name == "init_user_volume_accumulator"
        || entry_name == "init_user_volume_accumulator_event"
        || entry_name == "sync_user_volume_accumulator"
        || entry_name == "sync_user_volume_accumulator_event"
        || entry_name == "close_user_volume_accumulator"
        || entry_name == "close_user_volume_accumulator_event"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_REWARD_EVENTS.to_string());
    }
    if entry_name == "admin_set_creator"
        || entry_name == "admin_set_creator_event"
        || entry_name == "admin_set_idl_authority"
        || entry_name == "admin_set_idl_authority_event"
        || entry_name == "add_quote_mint"
        || entry_name == "remove_quote_mint"
        || entry_name == "extend_account"
        || entry_name == "extend_account_event"
        || entry_name == "set_creator"
        || entry_name == "set_creator_event"
        || entry_name == "set_mayhem_virtual_params"
        || entry_name == "update_mayhem_virtual_params_event"
        || entry_name == "set_metaplex_creator"
        || entry_name == "set_metaplex_creator_event"
        || entry_name == "set_params"
        || entry_name == "set_params_event"
        || entry_name == "set_reserved_fee_recipients"
        || entry_name == "reserved_fee_recipients_event"
        || entry_name == "set_virtual_quote_reserves"
        || entry_name == "toggle_cashback_enabled"
        || entry_name == "toggle_create_v2"
        || entry_name == "toggle_mayhem_mode"
        || entry_name == "update_buyback_config"
        || entry_name == "update_global_authority"
        || entry_name == "update_global_authority_event"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string());
    }
    return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
}

fn infer_pump_swap_expected_db_target(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    if entry_name == "buy" || entry_name == "sell" || entry_name == "buy_exact_quote_in" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS.to_string());
    }
    if entry_name == "buy_event" || entry_name == "sell_event" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string());
    }
    if entry_name.starts_with("observed_unknown_") {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
    }
    if entry_name == "deposit"
        || entry_name == "deposit_event"
        || entry_name == "withdraw"
        || entry_name == "withdraw_event"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS.to_string());
    }
    if entry_name == "create_pool" || entry_name == "create_pool_event" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string());
    }
    if entry_name == "collect_coin_creator_fee"
        || entry_name == "collect_coin_creator_fee_event"
        || entry_name == "transfer_creator_fees_to_pump"
        || entry_name == "transfer_creator_fees_to_pump_v2"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS.to_string());
    }
    if entry_name == "claim_cashback"
        || entry_name == "claim_cashback_event"
        || entry_name == "claim_token_incentives"
        || entry_name == "claim_token_incentives_event"
        || entry_name == "admin_update_token_incentives"
        || entry_name == "admin_update_token_incentives_event"
        || entry_name == "init_user_volume_accumulator"
        || entry_name == "init_user_volume_accumulator_event"
        || entry_name == "sync_user_volume_accumulator"
        || entry_name == "sync_user_volume_accumulator_event"
        || entry_name == "close_user_volume_accumulator"
        || entry_name == "close_user_volume_accumulator_event"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_REWARD_EVENTS.to_string());
    }
    if entry_name == "admin_set_coin_creator"
        || entry_name == "admin_set_coin_creator_event"
        || entry_name == "create_config"
        || entry_name == "create_config_event"
        || entry_name == "disable"
        || entry_name == "disable_event"
        || entry_name == "extend_account"
        || entry_name == "extend_account_event"
        || entry_name == "migrate_pool_coin_creator"
        || entry_name == "migrate_pool_coin_creator_event"
        || entry_name == "reserved_fee_recipients_event"
        || entry_name == "set_bonding_curve_coin_creator_event"
        || entry_name == "set_coin_creator"
        || entry_name == "set_metaplex_coin_creator_event"
        || entry_name == "set_reserved_fee_recipient"
        || entry_name == "set_reserved_fee_recipients"
        || entry_name == "toggle_cashback_enabled"
        || entry_name == "toggle_mayhem_mode"
        || entry_name == "update_admin"
        || entry_name == "update_buyback_config"
        || entry_name == "update_admin_event"
        || entry_name == "update_fee_config"
        || entry_name == "update_fee_config_event"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string());
    }
    return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
}

fn infer_pump_swap_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    if entry_name == "buy" || entry_name == "sell" || entry_name == "buy_exact_quote_in" {
        return Some("swap".to_string());
    }
    if entry_name == "buy_event" || entry_name == "sell_event" {
        return Some("swap_log".to_string());
    }
    if entry_name == "deposit" || entry_name == "deposit_event" {
        return Some("liquidity_add".to_string());
    }
    if entry_name == "withdraw" || entry_name == "withdraw_event" {
        return Some("liquidity_remove".to_string());
    }
    if entry_name == "create_pool" || entry_name == "create_pool_event" {
        return Some("pool_create".to_string());
    }
    if entry_name.starts_with("observed_unknown_") {
        return Some("observed_unknown_instruction".to_string());
    }
    if entry_name == "toggle_cashback_enabled"
        || entry_name == "set_reserved_fee_recipient"
        || entry_name == "set_reserved_fee_recipients"
        || entry_name == "reserved_fee_recipients_event"
    {
        return Some("admin_config".to_string());
    }
    if entry_name.contains("creator_fee") || entry_name.contains("fee_recipient") {
        return Some("fee".to_string());
    }
    if entry_name.contains("cashback")
        || entry_name.contains("incentive")
        || entry_name.contains("volume_accumulator")
    {
        return Some("reward".to_string());
    }
    if entry_name.contains("config")
        || entry_name.contains("admin")
        || entry_name.contains("disable")
        || entry_name.contains("toggle")
        || entry_name.contains("coin_creator")
        || entry_name.contains("extend_account")
    {
        return Some("admin_config".to_string());
    }
    return infer_event_family(entry_name, entry_kind);
}

fn infer_pump_fees_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM || entry_kind == crate::ENTRY_KIND_ACCOUNT {
        return None;
    }
    if entry_name == "get_fees" {
        return Some("audit".to_string());
    }
    if entry_name == "claim_social_fee_pda"
        || entry_name == "claim_social_fee_pda_v2"
        || entry_name == "social_fee_pda_claimed"
    {
        return Some("reward".to_string());
    }
    if entry_name == "crank_donation_fee_pda"
        || entry_name == "donation_fee_pda_cranked"
        || entry_name == "sweep_buyback"
        || entry_name == "sweep_buyback_event"
    {
        return Some("fee".to_string());
    }
    if entry_name.contains("create_")
        || entry_name.contains("created")
        || entry_name.contains("initialize_")
        || entry_name == "extend_fee_config"
        || entry_name == "extend_fee_config_event"
    {
        return Some("pool_create".to_string());
    }
    if entry_name.contains("config")
        || entry_name.contains("authority")
        || entry_name.contains("admin")
        || entry_name.contains("tier")
        || entry_name.contains("disable")
        || entry_name.contains("reset_fee_sharing")
        || entry_name.contains("update_fee_shares")
        || entry_name.contains("buyback_claim_rate_limit")
    {
        return Some("admin_config".to_string());
    }
    return Some("audit".to_string());
}

fn pump_fees_local_event_kind(entry_name: &str) -> std::option::Option<std::string::String> {
    if entry_name == "program" || entry_name == "program_alias" {
        return None;
    }
    return Some(format!("pump_fees.{}", entry_name));
}

fn infer_pump_fun_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    match entry_name {
        "buy"
        | "sell"
        | "buy_v2"
        | "sell_v2"
        | "buy_exact_quote_in_v2"
        | "buy_exact_sol_in"
        | "trade_event" => return Some("swap".to_string()),
        "create" | "create_event" | "create_v2" | "create_v2_token" | "complete_event" => {
            return Some("launch".to_string());
        },
        "migrate"
        | "migrate_v2"
        | "migrate_bonding_curve_creator"
        | "migrate_bonding_curve_creator_event"
        | "complete_pump_amm_migration_event" => return Some("migration".to_string()),
        "claim_cashback"
        | "claim_cashback_v2"
        | "claim_cashback_event"
        | "claim_token_incentives"
        | "claim_token_incentives_event"
        | "admin_update_token_incentives"
        | "admin_update_token_incentives_event"
        | "init_user_volume_accumulator"
        | "init_user_volume_accumulator_event"
        | "sync_user_volume_accumulator"
        | "sync_user_volume_accumulator_event"
        | "close_user_volume_accumulator"
        | "close_user_volume_accumulator_event" => return Some("reward".to_string()),
        "collect_creator_fee"
        | "collect_creator_fee_v2"
        | "collect_creator_fee_event"
        | "distribute_creator_fees"
        | "distribute_creator_fees_v2"
        | "distribute_creator_fees_event"
        | "get_minimum_distributable_fee"
        | "minimum_distributable_fee_event" => return Some("fee".to_string()),
        "add_quote_mint"
        | "remove_quote_mint"
        | "admin_set_creator"
        | "admin_set_creator_event"
        | "admin_set_idl_authority"
        | "admin_set_idl_authority_event"
        | "extend_account"
        | "extend_account_event"
        | "set_creator"
        | "set_creator_event"
        | "set_mayhem_virtual_params"
        | "update_mayhem_virtual_params_event"
        | "set_metaplex_creator"
        | "set_metaplex_creator_event"
        | "set_params"
        | "set_params_event"
        | "set_reserved_fee_recipients"
        | "reserved_fee_recipients_event"
        | "set_virtual_quote_reserves"
        | "toggle_cashback_enabled"
        | "toggle_create_v2"
        | "toggle_mayhem_mode"
        | "update_buyback_config"
        | "update_global_authority"
        | "update_global_authority_event" => return Some("admin_config".to_string()),
        "initialize" => return Some("pool_create".to_string()),
        _ => return infer_event_family(entry_name, entry_kind),
    }
}

fn pump_fun_local_event_kind(entry_name: &str) -> std::option::Option<std::string::String> {
    if entry_name.ends_with("_event") {
        return Some(format!("pump_fun.{}", entry_name));
    }
    match entry_name {
        "buy" => return Some("pump_fun.buy".to_string()),
        "sell" => return Some("pump_fun.sell".to_string()),
        "create_v2" => return Some("pump_fun.create_v2_token".to_string()),
        "add_quote_mint" => return Some("pump_fun.add_quote_mint".to_string()),
        "admin_set_creator" => return Some("pump_fun.admin_set_creator".to_string()),
        "admin_set_idl_authority" => {
            return Some("pump_fun.admin_set_idl_authority".to_string());
        },
        "admin_update_token_incentives" => {
            return Some("pump_fun.admin_update_token_incentives".to_string());
        },
        "buy_exact_quote_in_v2" => {
            return Some("pump_fun.buy_exact_quote_in_v2".to_string());
        },
        "buy_exact_sol_in" => return Some("pump_fun.buy_exact_sol_in".to_string()),
        "buy_v2" => return Some("pump_fun.buy_v2".to_string()),
        "claim_cashback" => return Some("pump_fun.claim_cashback".to_string()),
        "claim_cashback_v2" => return Some("pump_fun.claim_cashback_v2".to_string()),
        "claim_token_incentives" => {
            return Some("pump_fun.claim_token_incentives".to_string());
        },
        "close_user_volume_accumulator" => {
            return Some("pump_fun.close_user_volume_accumulator".to_string());
        },
        "collect_creator_fee" => return Some("pump_fun.collect_creator_fee".to_string()),
        "collect_creator_fee_v2" => return Some("pump_fun.collect_creator_fee_v2".to_string()),
        "create" => return Some("pump_fun.create".to_string()),
        "distribute_creator_fees" => {
            return Some("pump_fun.distribute_creator_fees".to_string());
        },
        "distribute_creator_fees_v2" => {
            return Some("pump_fun.distribute_creator_fees_v2".to_string());
        },
        "extend_account" => return Some("pump_fun.extend_account".to_string()),
        "get_minimum_distributable_fee" => {
            return Some("pump_fun.get_minimum_distributable_fee".to_string());
        },
        "init_user_volume_accumulator" => {
            return Some("pump_fun.init_user_volume_accumulator".to_string());
        },
        "initialize" => return Some("pump_fun.initialize".to_string()),
        "migrate" => return Some("pump_fun.migrate".to_string()),
        "migrate_bonding_curve_creator" => {
            return Some("pump_fun.migrate_bonding_curve_creator".to_string());
        },
        "migrate_v2" => return Some("pump_fun.migrate_v2".to_string()),
        "remove_quote_mint" => return Some("pump_fun.remove_quote_mint".to_string()),
        "sell_v2" => return Some("pump_fun.sell_v2".to_string()),
        "set_creator" => return Some("pump_fun.set_creator".to_string()),
        "set_mayhem_virtual_params" => {
            return Some("pump_fun.set_mayhem_virtual_params".to_string());
        },
        "set_metaplex_creator" => return Some("pump_fun.set_metaplex_creator".to_string()),
        "set_params" => return Some("pump_fun.set_params".to_string()),
        "set_reserved_fee_recipients" => {
            return Some("pump_fun.set_reserved_fee_recipients".to_string());
        },
        "set_virtual_quote_reserves" => {
            return Some("pump_fun.set_virtual_quote_reserves".to_string());
        },
        "sync_user_volume_accumulator" => {
            return Some("pump_fun.sync_user_volume_accumulator".to_string());
        },
        "toggle_cashback_enabled" => {
            return Some("pump_fun.toggle_cashback_enabled".to_string());
        },
        "toggle_create_v2" => return Some("pump_fun.toggle_create_v2".to_string()),
        "toggle_mayhem_mode" => return Some("pump_fun.toggle_mayhem_mode".to_string()),
        "update_buyback_config" => return Some("pump_fun.update_buyback_config".to_string()),
        "update_global_authority" => {
            return Some("pump_fun.update_global_authority".to_string());
        },
        _ => return None,
    }
}

fn pump_swap_local_event_kind(entry_name: &str) -> std::option::Option<std::string::String> {
    if entry_name.ends_with("_event") {
        return Some(format!("pump_swap.{}", entry_name));
    }
    match entry_name {
        "admin_set_coin_creator" => return Some("pump_swap.admin_set_coin_creator".to_string()),
        "admin_update_token_incentives" => {
            return Some("pump_swap.admin_update_token_incentives".to_string());
        },
        "buy" => return Some("pump_swap.buy".to_string()),
        "buy_exact_quote_in" => return Some("pump_swap.buy_exact_quote_in".to_string()),
        "claim_cashback" => return Some("pump_swap.claim_cashback".to_string()),
        "claim_token_incentives" => return Some("pump_swap.claim_token_incentives".to_string()),
        "close_user_volume_accumulator" => {
            return Some("pump_swap.close_user_volume_accumulator".to_string());
        },
        "collect_coin_creator_fee" => {
            return Some("pump_swap.collect_coin_creator_fee".to_string());
        },
        "create_config" => return Some("pump_swap.create_config".to_string()),
        "create_pool" => return Some("pump_swap.create_pool".to_string()),
        "deposit" => return Some("pump_swap.deposit".to_string()),
        "disable" => return Some("pump_swap.disable".to_string()),
        "extend_account" => return Some("pump_swap.extend_account".to_string()),
        "init_user_volume_accumulator" => {
            return Some("pump_swap.init_user_volume_accumulator".to_string());
        },
        "migrate_pool_coin_creator" => {
            return Some("pump_swap.migrate_pool_coin_creator".to_string());
        },
        "sell" => return Some("pump_swap.sell".to_string()),
        "set_coin_creator" => return Some("pump_swap.set_coin_creator".to_string()),
        "set_reserved_fee_recipients" => {
            return Some("pump_swap.set_reserved_fee_recipients".to_string());
        },
        "sync_user_volume_accumulator" => {
            return Some("pump_swap.sync_user_volume_accumulator".to_string());
        },
        "toggle_cashback_enabled" => return Some("pump_swap.toggle_cashback_enabled".to_string()),
        "toggle_mayhem_mode" => return Some("pump_swap.toggle_mayhem_mode".to_string()),
        "transfer_creator_fees_to_pump" => {
            return Some("pump_swap.transfer_creator_fees_to_pump".to_string());
        },
        "transfer_creator_fees_to_pump_v2" => {
            return Some("pump_swap.transfer_creator_fees_to_pump_v2".to_string());
        },
        "update_admin" => return Some("pump_swap.update_admin".to_string()),
        "update_buyback_config" => return Some("pump_swap.update_buyback_config".to_string()),
        "update_fee_config" => return Some("pump_swap.update_fee_config".to_string()),
        "withdraw" => return Some("pump_swap.withdraw".to_string()),
        "set_reserved_fee_recipient" => {
            return Some("pump_swap.set_reserved_fee_recipient".to_string());
        },
        _ => return None,
    }
}

fn infer_event_family_for_entry(
    decoder_code: &str,
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if decoder_code == "pump_fun" {
        return infer_pump_fun_event_family(entry_name, entry_kind);
    }
    if decoder_code == "pump_fees" {
        return infer_pump_fees_event_family(entry_name, entry_kind);
    }
    if decoder_code == "pump_swap" {
        return infer_pump_swap_event_family(entry_name, entry_kind);
    }
    if decoder_code == "raydium_launchpad" {
        return infer_raydium_launchpad_event_family(entry_name, entry_kind);
    }
    if decoder_code == "raydium_amm_v4" {
        return infer_raydium_amm_v4_event_family(entry_name, entry_kind);
    }
    if decoder_code == "raydium_clmm" {
        return infer_raydium_clmm_event_family(entry_name, entry_kind);
    }
    if decoder_code == "raydium_cpmm" {
        return infer_raydium_cpmm_event_family(entry_name, entry_kind);
    }
    if decoder_code == "raydium_stable_swap" {
        return infer_raydium_stable_swap_event_family(entry_name, entry_kind);
    }
    if decoder_code == "meteora_dbc" {
        return infer_meteora_dbc_event_family(entry_name, entry_kind);
    }
    if decoder_code == "meteora_dlmm" {
        return infer_meteora_dlmm_event_family(entry_name, entry_kind);
    }
    return infer_event_family(entry_name, entry_kind);
}

fn infer_meteora_dlmm_expected_db_target(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM || entry_kind == crate::ENTRY_KIND_ACCOUNT {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
    }
    let family = infer_meteora_dlmm_event_family(entry_name, entry_kind);
    let family = match family {
        Some(family) => family,
        None => {
            return Some(
                crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string(),
            );
        },
    };
    if family == "swap_log" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string());
    }
    if family == "swap" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS.to_string());
    }
    if family == "pool_create" || family == "pool_lifecycle" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string());
    }
    if family == "liquidity"
        || family == "liquidity_add"
        || family == "liquidity_remove"
        || family == "liquidity_change"
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS.to_string());
    }
    if family == "position_open" || family == "position_close" || family == "position_update" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string());
    }
    if family == "fee" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS.to_string());
    }
    if family == "reward" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_REWARD_EVENTS.to_string());
    }
    if family == "admin_config" || family == "admin_audit" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string());
    }
    if family == "order_place" || family == "order_cancel" || family == "order_close" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_ORDERBOOK_EVENTS.to_string());
    }
    return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
}

fn infer_meteora_dlmm_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_ACCOUNT || entry_kind == crate::ENTRY_KIND_PROGRAM {
        return Some("metadata".to_string());
    }
    if entry_name == "instruction_audit" {
        return Some("admin_audit".to_string());
    }
    if entry_name == "migrate_position" {
        return Some("position_update".to_string());
    }
    if matches!(
        entry_name,
        "swap"
            | "swap2"
            | "swap_exact_out"
            | "swap_exact_out2"
            | "swap_with_price_impact"
            | "swap_with_price_impact2"
    ) {
        return Some("swap".to_string());
    }
    if matches!(entry_name, "swap_event" | "swap2_evt") {
        return Some("swap_log".to_string());
    }
    if matches!(
        entry_name,
        "create_pool"
            | "initialize_lb_pair"
            | "initialize_lb_pair2"
            | "initialize_permission_lb_pair"
            | "initialize_customizable_permissionless_lb_pair"
            | "initialize_customizable_permissionless_lb_pair2"
            | "lb_pair_create_event"
    ) {
        return Some("pool_create".to_string());
    }
    if entry_name.starts_with("add_liquidity") || entry_name == "add_liquidity_event" {
        return Some("liquidity_add".to_string());
    }
    if entry_name.starts_with("remove_liquidity")
        || entry_name == "remove_all_liquidity"
        || entry_name == "remove_liquidity_event"
    {
        return Some("liquidity_remove".to_string());
    }
    if entry_name == "rebalance_liquidity" || entry_name == "rebalancing_event" {
        return Some("liquidity_change".to_string());
    }
    if entry_name.starts_with("initialize_position") || entry_name == "position_create_event" {
        return Some("position_open".to_string());
    }
    if entry_name.starts_with("close_position") || entry_name == "position_close_event" {
        return Some("position_close".to_string());
    }
    if entry_name.contains("position")
        && (entry_name.contains("operator")
            || entry_name.contains("length")
            || entry_name.contains("lock_release_point")
            || entry_name.contains("permissionless"))
    {
        return Some("position_update".to_string());
    }
    if matches!(
        entry_name,
        "close_claim_fee_operator_account"
            | "close_claim_protocol_fee_operator"
            | "create_claim_protocol_fee_operator"
            | "update_fees_and_reward2"
            | "update_fees_and_rewards"
            | "update_reward_duration"
            | "update_reward_duration_event"
            | "update_reward_funder"
            | "update_reward_funder_event"
    ) {
        return Some("admin_config".to_string());
    }
    if entry_name.contains("claim_fee")
        || entry_name == "withdraw_protocol_fee"
        || entry_name == "zap_protocol_fee"
        || entry_name == "composition_fee_event"
    {
        return Some("fee".to_string());
    }
    if entry_name.contains("reward")
        || entry_name == "withdraw_ineligible_reward"
        || entry_name == "withdraw_ineligible_reward_event"
    {
        return Some("reward".to_string());
    }
    if entry_name.contains("limit_order") {
        if entry_name.starts_with("place") {
            return Some("order_place".to_string());
        }
        if entry_name.starts_with("cancel") {
            return Some("order_cancel".to_string());
        }
        return Some("order_close".to_string());
    }
    if entry_name.contains("fee_parameter")
        || entry_name.contains("dynamic_fee")
        || entry_name.contains("base_fee")
        || entry_name.contains("pair_status")
        || entry_name.contains("activation")
        || entry_name.contains("operator")
        || entry_name.contains("preset_parameter")
        || entry_name.contains("token_badge")
        || entry_name == "set_permissionless_operation_bits"
        || entry_name == "update_fees_and_rewards"
        || entry_name == "update_fees_and_reward2"
    {
        return Some("admin_config".to_string());
    }
    if entry_name.contains("bin_array")
        || entry_name == "go_to_a_bin"
        || entry_name == "go_to_a_bin_event"
        || entry_name == "increase_observation_event"
        || entry_name == "increase_oracle_length"
    {
        return Some("pool_lifecycle".to_string());
    }
    return Some("decoded_only".to_string());
}

fn meteora_dlmm_local_event_kind(entry_name: &str) -> std::option::Option<std::string::String> {
    if entry_name == "program" {
        return None;
    }
    if entry_name == "instruction_audit" {
        return Some("meteora_dlmm.instruction_audit".to_string());
    }
    if entry_name == "anchor_self_cpi_log" {
        return Some("meteora_dlmm.anchor_self_cpi_log".to_string());
    }
    return Some(format!("meteora_dlmm.{}", entry_name));
}

fn infer_meteora_dbc_expected_db_target(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM || entry_kind == crate::ENTRY_KIND_ACCOUNT {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
    }
    let family = infer_meteora_dbc_event_family(entry_name, entry_kind);
    let family = match family {
        Some(family) => family,
        None => {
            return Some(
                crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string(),
            );
        },
    };
    let normalized_entry_name = entry_name.strip_suffix("_local_idl").unwrap_or(entry_name);
    if family == "swap" && (normalized_entry_name == "swap" || normalized_entry_name == "swap2") {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS.to_string());
    }
    if family == "swap" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
    }
    if entry_kind == crate::ENTRY_KIND_EVENT
        && meteora_dbc_anchor_event_is_decoded_only_source(normalized_entry_name)
    {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
    }
    if family == "pool_create" || family == "migration" || family == "pool_lifecycle" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string());
    }
    if family == "liquidity" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS.to_string());
    }
    if family == "fee" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS.to_string());
    }
    if family == "admin_config" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string());
    }
    if family == "audit" {
        return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
    }
    return Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string());
}

fn meteora_dbc_anchor_event_is_decoded_only_source(entry_name: &str) -> bool {
    return matches!(
        entry_name,
        "evt_claim_creator_trading_fee_event"
            | "evt_claim_pool_creation_fee_event"
            | "evt_claim_protocol_fee_event"
            | "evt_claim_protocol_liquidity_migration_fee_event"
            | "evt_claim_trading_fee_event"
            | "evt_close_claim_fee_operator_event"
            | "evt_create_claim_fee_operator_event"
            | "evt_create_config_event"
            | "evt_create_config_v2_event"
            | "evt_create_meteora_migration_metadata_event"
            | "evt_creator_withdraw_surplus_event"
            | "evt_initialize_pool_event"
            | "evt_partner_claim_pool_creation_fee_event"
            | "evt_partner_metadata_event"
            | "evt_partner_withdraw_migration_fee_event"
            | "evt_partner_withdraw_surplus_event"
            | "evt_update_pool_creator_event"
            | "evt_virtual_pool_metadata_event"
            | "evt_withdraw_leftover_event"
            | "evt_withdraw_migration_fee_event"
    );
}

fn infer_meteora_dbc_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    if entry_name == "anchor_self_cpi_log" || entry_name == "instruction_audit" {
        return Some("audit".to_string());
    }
    if entry_name == "swap"
        || entry_name == "swap2"
        || entry_name == "evt_swap_event"
        || entry_name == "evt_swap2_event"
    {
        return Some("swap".to_string());
    }
    if entry_name == "initialize_virtual_pool_with_spl_token"
        || entry_name == "initialize_virtual_pool_with_token2022"
        || entry_name == "evt_initialize_pool_event"
    {
        return Some("pool_create".to_string());
    }
    if entry_name == "create_locker" {
        return Some("pool_lifecycle".to_string());
    }
    if entry_name.contains("metadata") {
        return Some("admin_config".to_string());
    }
    if entry_name.contains("fee")
        || entry_name.contains("surplus")
        || entry_name.contains("leftover")
        || entry_name == "zap_protocol_fee"
    {
        return Some("fee".to_string());
    }
    if entry_name.contains("migrate")
        || entry_name.contains("migration")
        || entry_name == "evt_curve_complete_event"
    {
        return Some("migration".to_string());
    }
    if entry_name.contains("lp_token") {
        return Some("liquidity".to_string());
    }
    if entry_name.contains("config")
        || entry_name.contains("operator")
        || entry_name.contains("metadata")
        || entry_name == "transfer_pool_creator"
        || entry_name == "evt_update_pool_creator_event"
        || entry_name == "evt_partner_metadata_event"
        || entry_name == "evt_virtual_pool_metadata_event"
    {
        return Some("admin_config".to_string());
    }
    return infer_event_family(entry_name, entry_kind);
}

fn meteora_dbc_local_event_kind(entry_name: &str) -> std::option::Option<std::string::String> {
    if entry_name == "program" || entry_name == "state" {
        return None;
    }
    let normalized_event_name = match entry_name.strip_suffix("_local_idl") {
        Some(value) => value,
        None => entry_name,
    };
    if normalized_event_name == "anchor_self_cpi_log"
        || normalized_event_name == "instruction_audit"
    {
        return Some("meteora_dbc.instruction_audit".to_string());
    }
    if normalized_event_name == "initialize_virtual_pool_with_spl_token"
        || normalized_event_name == "initialize_virtual_pool_with_token2022"
    {
        return Some("meteora_dbc.create_pool".to_string());
    }
    if entry_name.starts_with("evt_") && !entry_name.ends_with("_local_idl") {
        return None;
    }
    if meteora_dbc_local_instruction_entry_is_known(entry_name)
        || meteora_dbc_local_event_entry_is_known(normalized_event_name)
    {
        return Some(format!("meteora_dbc.{}", normalized_event_name));
    }
    return None;
}

fn meteora_dbc_local_instruction_entry_is_known(entry_name: &str) -> bool {
    return matches!(
        entry_name,
        "anchor_self_cpi_log"
            | "instruction_audit"
            | "claim_creator_trading_fee"
            | "claim_partner_pool_creation_fee"
            | "claim_protocol_fee"
            | "claim_protocol_pool_creation_fee"
            | "claim_trading_fee"
            | "close_claim_protocol_fee_operator"
            | "close_operator_account"
            | "create_config"
            | "create_locker"
            | "create_operator_account"
            | "create_partner_metadata"
            | "create_virtual_pool_metadata"
            | "creator_withdraw_surplus"
            | "initialize_virtual_pool_with_spl_token"
            | "initialize_virtual_pool_with_token2022"
            | "migrate_meteora_damm"
            | "migrate_meteora_damm_claim_lp_token"
            | "migrate_meteora_damm_lock_lp_token"
            | "migration_damm_v2"
            | "migration_damm_v2_create_metadata"
            | "migration_meteora_damm_create_metadata"
            | "partner_withdraw_surplus"
            | "swap"
            | "swap2"
            | "transfer_pool_creator"
            | "withdraw_leftover"
            | "withdraw_migration_fee"
            | "zap_protocol_fee"
    );
}

fn meteora_dbc_local_event_entry_is_known(entry_name: &str) -> bool {
    return matches!(
        entry_name,
        "evt_claim_creator_trading_fee_event"
            | "evt_claim_pool_creation_fee_event"
            | "evt_claim_protocol_fee_event"
            | "evt_claim_protocol_liquidity_migration_fee_event"
            | "evt_claim_trading_fee_event"
            | "evt_close_claim_fee_operator_event"
            | "evt_create_claim_fee_operator_event"
            | "evt_create_config_event"
            | "evt_create_config_v2_event"
            | "evt_create_meteora_migration_metadata_event"
            | "evt_creator_withdraw_surplus_event"
            | "evt_curve_complete_event"
            | "evt_initialize_pool_event"
            | "evt_partner_claim_pool_creation_fee_event"
            | "evt_partner_metadata_event"
            | "evt_partner_withdraw_migration_fee_event"
            | "evt_partner_withdraw_surplus_event"
            | "evt_swap_event"
            | "evt_swap2_event"
            | "evt_update_pool_creator_event"
            | "evt_virtual_pool_metadata_event"
            | "evt_withdraw_leftover_event"
            | "evt_withdraw_migration_fee_event"
    );
}

fn infer_raydium_amm_v4_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    match entry_name {
        "swap_base_in" => return Some("swap".to_string()),
        "swap_base_out" => return Some("swap".to_string()),
        "swap_base_in_v2" => return Some("swap".to_string()),
        "swap_base_out_v2" => return Some("swap".to_string()),
        "initialize" => return Some("pool_create".to_string()),
        "initialize2" => return Some("pool_create".to_string()),
        "pre_initialize" => return Some("pool_create".to_string()),
        "deposit" => return Some("liquidity_add".to_string()),
        "withdraw" => return Some("liquidity_remove".to_string()),
        "withdraw_pnl" => return Some("fee".to_string()),
        "withdraw_srm" => return Some("fee".to_string()),
        "admin_cancel_orders" => return Some("order_cancel".to_string()),
        "migrate_to_open_book" => return Some("order_place".to_string()),
        "create_config_account" => return Some("admin_config".to_string()),
        "update_config_account" => return Some("admin_config".to_string()),
        "set_params" => return Some("admin_config".to_string()),
        "monitor_step" => return Some("order_place".to_string()),
        "simulate_info" => return Some("cpi_transport".to_string()),
        _ => return infer_event_family(entry_name, entry_kind),
    }
}

fn infer_raydium_cpmm_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    match entry_name {
        "anchor_idl_instruction" => return Some("idl_management".to_string()),
        "cpi_event" => return Some("cpi_transport".to_string()),
        _ => return infer_event_family(entry_name, entry_kind),
    }
}

fn infer_raydium_stable_swap_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    match entry_name {
        "initialize" => return Some("pool_create".to_string()),
        "pre_initialize" => return Some("pool_create".to_string()),
        "init_model_data" => return Some("model_setup".to_string()),
        "update_model_data" => return Some("admin_config".to_string()),
        "deposit" => return Some("liquidity_add".to_string()),
        "withdraw" => return Some("liquidity_remove".to_string()),
        "monitor_step" => return Some("order_place".to_string()),
        "set_params" => return Some("admin_config".to_string()),
        "withdraw_pnl" => return Some("fee".to_string()),
        "withdraw_srm" => return Some("fee".to_string()),
        "swap_base_in" => return Some("swap".to_string()),
        "swap_base_out" => return Some("swap".to_string()),
        "simulate_info" => return Some("cpi_transport".to_string()),
        "admin_cancel_orders" => return Some("orderbook_admin".to_string()),
        "swap_event" => return Some("cpi_transport".to_string()),
        _ => return infer_event_family(entry_name, entry_kind),
    }
}

fn infer_raydium_clmm_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    match entry_name {
        "cpi_event" => return Some("cpi_transport".to_string()),
        "create_personal_position_event" => return Some("position_open".to_string()),
        "liquidity_calculate_event" => return Some("liquidity_calculation".to_string()),
        "liquidity_change_event" => return Some("liquidity_change".to_string()),
        "pool_created_event" => return Some("pool_create".to_string()),
        "create_operation_account" => return Some("admin_config".to_string()),
        "update_operation_account" => return Some("admin_config".to_string()),
        "update_dynamic_fee_config" => return Some("admin_config".to_string()),
        _ => return infer_event_family(entry_name, entry_kind),
    }
}

fn infer_raydium_launchpad_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    match entry_name {
        "buy_exact_in" => return Some("swap".to_string()),
        "buy_exact_out" => return Some("swap".to_string()),
        "sell_exact_in" => return Some("swap".to_string()),
        "sell_exact_out" => return Some("swap".to_string()),
        "trade_event" => return Some("swap".to_string()),
        "pool_create_event" => return Some("pool_create".to_string()),
        "initialize" => return Some("pool_create".to_string()),
        "initialize_v2" => return Some("pool_create".to_string()),
        "initialize_with_token_2022" => return Some("pool_create".to_string()),
        "claim_creator_fee" => return Some("fee".to_string()),
        "claim_platform_fee" => return Some("fee".to_string()),
        "claim_platform_fee_from_vault" => return Some("fee".to_string()),
        "collect_fee" => return Some("fee".to_string()),
        "collect_migrate_fee" => return Some("fee".to_string()),
        "claim_vested_event" => return Some("vesting".to_string()),
        "claim_vested_token" => return Some("vesting".to_string()),
        "create_platform_vesting_account" => return Some("vesting".to_string()),
        "create_vesting_account" => return Some("vesting".to_string()),
        "create_vesting_event" => return Some("vesting".to_string()),
        "migrate_to_amm" => return Some("migration".to_string()),
        "migrate_to_cpswap" => return Some("migration".to_string()),
        "close_platform_global_access" => return Some("account_close".to_string()),
        "create_config" => return Some("admin_config".to_string()),
        "create_platform_config" => return Some("admin_config".to_string()),
        "create_platform_global_access" => return Some("admin_config".to_string()),
        "remove_platform_curve_param" => return Some("admin_config".to_string()),
        "update_config" => return Some("admin_config".to_string()),
        "update_platform_config" => return Some("admin_config".to_string()),
        "update_platform_curve_param" => return Some("admin_config".to_string()),
        "cpi_event" => return Some("cpi_transport".to_string()),
        _ => return infer_event_family(entry_name, entry_kind),
    }
}

fn infer_event_family(
    entry_name: &str,
    entry_kind: &str,
) -> std::option::Option<std::string::String> {
    if entry_kind == crate::ENTRY_KIND_PROGRAM {
        return None;
    }
    let normalized = entry_name.to_ascii_lowercase();
    if normalized == "lp_change_event" {
        return Some("liquidity".to_string());
    }
    if normalized == "cpi_event" {
        return Some("cpi_transport".to_string());
    }
    if contains_any(normalized.as_str(), &["migrate", "migration", "graduate"]) {
        return Some("migration".to_string());
    }
    if contains_any(normalized.as_str(), &["swap", "buy", "sell", "trade"]) {
        return Some("swap".to_string());
    }
    if contains_any(normalized.as_str(), &["create_pool", "initialize_pool", "initialize2"])
        || normalized == "create_customizable_pool"
        || normalized == "initialize"
        || normalized.starts_with("initialize_")
    {
        return Some("pool_create".to_string());
    }
    if contains_any(normalized.as_str(), &["add_liquidity", "increase_liquidity", "deposit"])
        || normalized.contains("bootstrap_liquidity")
    {
        return Some("liquidity_add".to_string());
    }
    if contains_any(normalized.as_str(), &["remove_liquidity", "decrease_liquidity", "withdraw"])
        && !normalized.contains("funds")
    {
        return Some("liquidity_remove".to_string());
    }
    if contains_any(
        normalized.as_str(),
        &["open_position", "initialize_position", "position_create"],
    ) {
        return Some("position_open".to_string());
    }
    if contains_any(normalized.as_str(), &["close_position", "position_close"])
        || normalized.contains("close_position_if_empty")
        || normalized == "close_protocol_position"
    {
        return Some("position_close".to_string());
    }
    if contains_any(normalized.as_str(), &["fee", "collect", "claim_fee"])
        && !normalized.contains("reward")
        && !normalized.contains("config")
    {
        return Some("fee".to_string());
    }
    if normalized.contains("reward") {
        return Some("reward".to_string());
    }
    if contains_any(
        normalized.as_str(),
        &["config", "admin", "authority", "permission", "pause", "status", "update_pool"],
    ) {
        return Some("admin_config".to_string());
    }
    if normalized == "create_support_mint_associated" {
        return Some("account_create".to_string());
    }
    if normalized.contains("mint") {
        return Some("mint".to_string());
    }
    if normalized.contains("burn") {
        return Some("burn".to_string());
    }
    if normalized.contains("transfer") {
        return Some("transfer".to_string());
    }
    if contains_any(normalized.as_str(), &["create_ata", "init_account", "open_orders_create"]) {
        return Some("account_create".to_string());
    }
    if normalized.contains("wrap_sol") {
        return Some("wrap_sol".to_string());
    }
    if normalized.contains("unwrap_sol") {
        return Some("unwrap_sol".to_string());
    }
    if normalized.contains("place_order")
        || normalized.contains("post_order")
        || normalized == "open_limit_order"
        || normalized == "increase_limit_order"
    {
        return Some("order_place".to_string());
    }
    if normalized.contains("cancel_order")
        || normalized.contains("cancel_all")
        || normalized == "close_limit_order"
        || normalized == "decrease_limit_order"
    {
        return Some("order_cancel".to_string());
    }
    if normalized.contains("fill") {
        return Some("order_fill".to_string());
    }
    if normalized.contains("consume_events") {
        return Some("consume_events".to_string());
    }
    if normalized.contains("settle_funds") || normalized == "settle_limit_order" {
        return Some("settle_funds".to_string());
    }
    if contains_any(normalized.as_str(), &["close_account", "close_open_orders"])
        || normalized.starts_with("close_")
    {
        return Some("account_close".to_string());
    }
    if normalized.contains("vault") && normalized.contains("deposit") {
        return Some("vault_deposit".to_string());
    }
    if normalized.contains("vault") && normalized.contains("withdraw") {
        return Some("vault_withdraw".to_string());
    }
    if contains_any(normalized.as_str(), &["lock_liquidity", "create_lock", "lock"])
        && !normalized.contains("unlock")
    {
        return Some("lock".to_string());
    }
    if normalized.contains("unlock") {
        return Some("unlock".to_string());
    }
    if contains_any(normalized.as_str(), &["launch", "create_bonding", "bonding_curve"]) {
        return Some("launch".to_string());
    }
    if contains_any(normalized.as_str(), &["migrate", "migration", "graduate"]) {
        return Some("migration".to_string());
    }
    if normalized.contains("unstake") {
        return Some("unstake".to_string());
    }
    if normalized.contains("stake") {
        return Some("stake".to_string());
    }
    return Some("audit".to_string());
}

fn contains_any(value: &str, needles: &[&str]) -> bool {
    for needle in needles {
        if value.contains(needle) {
            return true;
        }
    }
    return false;
}

fn raydium_launchpad_local_entry_is_known(entry_name: &str) -> bool {
    match entry_name {
        "buy_exact_in" => return true,
        "buy_exact_out" => return true,
        "close_platform_global_access" => return true,
        "claim_creator_fee" => return true,
        "claim_platform_fee" => return true,
        "claim_platform_fee_from_vault" => return true,
        "claim_vested_event" => return true,
        "claim_vested_token" => return true,
        "collect_fee" => return true,
        "collect_migrate_fee" => return true,
        "create_config" => return true,
        "create_platform_config" => return true,
        "cpi_event" => return true,
        "create_platform_vesting_account" => return true,
        "create_platform_global_access" => return true,
        "create_vesting_account" => return true,
        "create_vesting_event" => return true,
        "initialize" => return true,
        "initialize_v2" => return true,
        "initialize_with_token_2022" => return true,
        "migrate_to_amm" => return true,
        "migrate_to_cpswap" => return true,
        "pool_create_event" => return true,
        "remove_platform_curve_param" => return true,
        "sell_exact_in" => return true,
        "sell_exact_out" => return true,
        "trade_event" => return true,
        "update_config" => return true,
        "update_platform_config" => return true,
        "update_platform_curve_param" => return true,
        _ => return false,
    }
}

fn raydium_amm_v4_local_event_kind(entry_name: &str) -> std::option::Option<std::string::String> {
    match entry_name {
        "swap_base_in" => return Some("raydium_amm_v4.swap_base_in".to_string()),
        "swap_base_out" => return Some("raydium_amm_v4.swap_base_out".to_string()),
        "swap_base_in_v2" => return Some("raydium_amm_v4.swap_base_in_v2".to_string()),
        "swap_base_out_v2" => return Some("raydium_amm_v4.swap_base_out_v2".to_string()),
        "initialize" => return Some("raydium_amm_v4.initialize".to_string()),
        "initialize2" => return Some("raydium_amm_v4.initialize2_pool".to_string()),
        "pre_initialize" => return Some("raydium_amm_v4.pre_initialize".to_string()),
        "deposit" => return Some("raydium_amm_v4.deposit".to_string()),
        "withdraw" => return Some("raydium_amm_v4.withdraw".to_string()),
        "withdraw_pnl" => return Some("raydium_amm_v4.withdraw_pnl".to_string()),
        "withdraw_srm" => return Some("raydium_amm_v4.withdraw_srm".to_string()),
        "admin_cancel_orders" => return Some("raydium_amm_v4.admin_cancel_orders".to_string()),
        "migrate_to_open_book" => return Some("raydium_amm_v4.migrate_to_open_book".to_string()),
        "create_config_account" => return Some("raydium_amm_v4.create_config_account".to_string()),
        "update_config_account" => return Some("raydium_amm_v4.update_config_account".to_string()),
        "set_params" => return Some("raydium_amm_v4.set_params".to_string()),
        "monitor_step" => return Some("raydium_amm_v4.monitor_step".to_string()),
        "simulate_info" => return Some("raydium_amm_v4.simulate_info".to_string()),
        _ => return None,
    }
}

fn raydium_stable_swap_local_event_kind(
    entry_name: &str,
) -> std::option::Option<std::string::String> {
    match entry_name {
        "initialize" => return Some("raydium_stable_swap.initialize".to_string()),
        "init_model_data" => return Some("raydium_stable_swap.init_model_data".to_string()),
        "update_model_data" => return Some("raydium_stable_swap.update_model_data".to_string()),
        "pre_initialize" => return Some("raydium_stable_swap.pre_initialize".to_string()),
        "deposit" => return Some("raydium_stable_swap.deposit".to_string()),
        "withdraw" => return Some("raydium_stable_swap.withdraw".to_string()),
        "monitor_step" => return Some("raydium_stable_swap.monitor_step".to_string()),
        "set_params" => return Some("raydium_stable_swap.set_params".to_string()),
        "withdraw_pnl" => return Some("raydium_stable_swap.withdraw_pnl".to_string()),
        "withdraw_srm" => return Some("raydium_stable_swap.withdraw_srm".to_string()),
        "swap_base_in" => return Some("raydium_stable_swap.swap_base_in".to_string()),
        "swap_base_out" => return Some("raydium_stable_swap.swap_base_out".to_string()),
        "simulate_info" => return Some("raydium_stable_swap.simulate_info".to_string()),
        "admin_cancel_orders" => {
            return Some("raydium_stable_swap.admin_cancel_orders".to_string());
        },
        "swap_event" => return Some("raydium_stable_swap.swap_event".to_string()),
        _ => return None,
    }
}

pub(crate) fn known_local_event_kind(
    decoder_code: &str,
    entry_name: &str,
) -> std::option::Option<std::string::String> {
    if decoder_code == "pump_fun" {
        return pump_fun_local_event_kind(entry_name);
    }
    if decoder_code == "pump_fees" {
        return pump_fees_local_event_kind(entry_name);
    }
    if decoder_code == "pump_swap" {
        return pump_swap_local_event_kind(entry_name);
    }
    if decoder_code == "meteora_dbc" {
        return meteora_dbc_local_event_kind(entry_name);
    }
    if decoder_code == "meteora_dlmm" {
        return meteora_dlmm_local_event_kind(entry_name);
    }
    if decoder_code == "raydium_amm_v4" {
        return raydium_amm_v4_local_event_kind(entry_name);
    }
    if decoder_code == "raydium_stable_swap" {
        return raydium_stable_swap_local_event_kind(entry_name);
    }
    if decoder_code == "raydium_launchpad" && raydium_launchpad_local_entry_is_known(entry_name) {
        return Some(format!("raydium_launchpad.{}", entry_name));
    }
    match (decoder_code, entry_name) {
        ("raydium_cpmm", "swap_base_input") => {
            return Some("raydium_cpmm.swap_base_input".to_string());
        },
        ("raydium_cpmm", "swap_base_output") => {
            return Some("raydium_cpmm.swap_base_output".to_string());
        },
        ("raydium_cpmm", "close_permission_pda") => {
            return Some("raydium_cpmm.close_permission_pda".to_string());
        },
        ("raydium_cpmm", "collect_creator_fee") => {
            return Some("raydium_cpmm.collect_creator_fee".to_string());
        },
        ("raydium_cpmm", "collect_fund_fee") => {
            return Some("raydium_cpmm.collect_fund_fee".to_string());
        },
        ("raydium_cpmm", "collect_protocol_fee") => {
            return Some("raydium_cpmm.collect_protocol_fee".to_string());
        },
        ("raydium_cpmm", "create_amm_config") => {
            return Some("raydium_cpmm.create_amm_config".to_string());
        },
        ("raydium_cpmm", "create_permission_pda") => {
            return Some("raydium_cpmm.create_permission_pda".to_string());
        },
        ("raydium_cpmm", "deposit") => return Some("raydium_cpmm.deposit".to_string()),
        ("raydium_cpmm", "initialize") => return Some("raydium_cpmm.initialize".to_string()),
        ("raydium_cpmm", "initialize_with_permission") => {
            return Some("raydium_cpmm.initialize_with_permission".to_string());
        },
        ("raydium_cpmm", "lp_change_event") => {
            return Some("raydium_cpmm.lp_change_event".to_string());
        },
        ("raydium_cpmm", "cpi_event") => return Some("raydium_cpmm.cpi_event".to_string()),
        ("raydium_cpmm", "anchor_idl_instruction") => {
            return Some("raydium_cpmm.anchor_idl_instruction".to_string());
        },
        ("raydium_cpmm", "swap_event") => return Some("raydium_cpmm.swap_event".to_string()),
        ("raydium_cpmm", "update_amm_config") => {
            return Some("raydium_cpmm.update_amm_config".to_string());
        },
        ("raydium_cpmm", "update_pool_status") => {
            return Some("raydium_cpmm.update_pool_status".to_string());
        },
        ("raydium_cpmm", "withdraw") => return Some("raydium_cpmm.withdraw".to_string()),
        ("raydium_clmm", "cpi_event") => return Some("raydium_clmm.cpi_event".to_string()),
        ("raydium_clmm", "collect_personal_fee_event") => {
            return Some("raydium_clmm.collect_personal_fee_event".to_string());
        },
        ("raydium_clmm", "collect_protocol_fee_event") => {
            return Some("raydium_clmm.collect_protocol_fee_event".to_string());
        },
        ("raydium_clmm", "config_change_event") => {
            return Some("raydium_clmm.config_change_event".to_string());
        },
        ("raydium_clmm", "create_personal_position_event") => {
            return Some("raydium_clmm.create_personal_position_event".to_string());
        },
        ("raydium_clmm", "decrease_liquidity_event") => {
            return Some("raydium_clmm.decrease_liquidity_event".to_string());
        },
        ("raydium_clmm", "increase_liquidity_event") => {
            return Some("raydium_clmm.increase_liquidity_event".to_string());
        },
        ("raydium_clmm", "liquidity_calculate_event") => {
            return Some("raydium_clmm.liquidity_calculate_event".to_string());
        },
        ("raydium_clmm", "liquidity_change_event") => {
            return Some("raydium_clmm.liquidity_change_event".to_string());
        },
        ("raydium_clmm", "pool_created_event") => {
            return Some("raydium_clmm.pool_created_event".to_string());
        },
        ("raydium_clmm", "swap_event") => return Some("raydium_clmm.swap_event".to_string()),
        ("raydium_clmm", "update_reward_infos_event") => {
            return Some("raydium_clmm.update_reward_infos_event".to_string());
        },
        ("raydium_clmm", "close_limit_order") => {
            return Some("raydium_clmm.close_limit_order".to_string());
        },
        ("raydium_clmm", "open_limit_order") => {
            return Some("raydium_clmm.open_limit_order".to_string());
        },
        ("raydium_clmm", "increase_limit_order") => {
            return Some("raydium_clmm.increase_limit_order".to_string());
        },
        ("raydium_clmm", "decrease_limit_order") => {
            return Some("raydium_clmm.decrease_limit_order".to_string());
        },
        ("raydium_clmm", "close_position") => {
            return Some("raydium_clmm.close_position".to_string());
        },
        ("raydium_clmm", "close_protocol_position") => {
            return Some("raydium_clmm.close_protocol_position".to_string());
        },
        ("raydium_clmm", "collect_fund_fee") => {
            return Some("raydium_clmm.collect_fund_fee".to_string());
        },
        ("raydium_clmm", "collect_protocol_fee") => {
            return Some("raydium_clmm.collect_protocol_fee".to_string());
        },
        ("raydium_clmm", "collect_remaining_rewards") => {
            return Some("raydium_clmm.collect_remaining_rewards".to_string());
        },
        ("raydium_clmm", "create_amm_config") => {
            return Some("raydium_clmm.create_amm_config".to_string());
        },
        ("raydium_clmm", "create_customizable_pool") => {
            return Some("raydium_clmm.create_customizable_pool".to_string());
        },
        ("raydium_clmm", "create_dynamic_fee_config") => {
            return Some("raydium_clmm.create_dynamic_fee_config".to_string());
        },
        ("raydium_clmm", "create_operation_account") => {
            return Some("raydium_clmm.create_operation_account".to_string());
        },
        ("raydium_clmm", "create_pool") => return Some("raydium_clmm.create_pool".to_string()),
        ("raydium_clmm", "create_support_mint_associated") => {
            return Some("raydium_clmm.create_support_mint_associated".to_string());
        },
        ("raydium_clmm", "decrease_liquidity") => {
            return Some("raydium_clmm.decrease_liquidity".to_string());
        },
        ("raydium_clmm", "decrease_liquidity_v2") => {
            return Some("raydium_clmm.decrease_liquidity_v2".to_string());
        },
        ("raydium_clmm", "increase_liquidity") => {
            return Some("raydium_clmm.increase_liquidity".to_string());
        },
        ("raydium_clmm", "increase_liquidity_v2") => {
            return Some("raydium_clmm.increase_liquidity_v2".to_string());
        },
        ("raydium_clmm", "initialize_reward") => {
            return Some("raydium_clmm.initialize_reward".to_string());
        },
        ("raydium_clmm", "open_position") => {
            return Some("raydium_clmm.open_position".to_string());
        },
        ("raydium_clmm", "open_position_v2") => {
            return Some("raydium_clmm.open_position_v2".to_string());
        },
        ("raydium_clmm", "open_position_with_token22_nft") => {
            return Some("raydium_clmm.open_position_with_token22_nft".to_string());
        },
        ("raydium_clmm", "set_reward_params") => {
            return Some("raydium_clmm.set_reward_params".to_string());
        },
        ("raydium_clmm", "settle_limit_order") => {
            return Some("raydium_clmm.settle_limit_order".to_string());
        },
        ("raydium_clmm", "swap") => return Some("raydium_clmm.swap".to_string()),
        ("raydium_clmm", "swap_router_base_in") => {
            return Some("raydium_clmm.swap_router_base_in".to_string());
        },
        ("raydium_clmm", "swap_v2") => return Some("raydium_clmm.swap_v2".to_string()),
        ("raydium_clmm", "transfer_reward_owner") => {
            return Some("raydium_clmm.transfer_reward_owner".to_string());
        },
        ("raydium_clmm", "update_amm_config") => {
            return Some("raydium_clmm.update_amm_config".to_string());
        },
        ("raydium_clmm", "update_dynamic_fee_config") => {
            return Some("raydium_clmm.update_dynamic_fee_config".to_string());
        },
        ("raydium_clmm", "update_operation_account") => {
            return Some("raydium_clmm.update_operation_account".to_string());
        },
        ("raydium_clmm", "update_pool_status") => {
            return Some("raydium_clmm.update_pool_status".to_string());
        },
        ("raydium_clmm", "update_reward_infos") => {
            return Some("raydium_clmm.update_reward_infos".to_string());
        },
        _ => return None,
    }
}

#[cfg(test)]
mod tests {
    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("dex_event_coverage.sqlite3");
        let config = crate::DatabaseConfig {
            enabled: true,
            backend: crate::DatabaseBackend::Sqlite,
            sqlite: crate::SqliteDatabaseConfig {
                path: database_path.to_string_lossy().to_string(),
                create_if_missing: true,
                busy_timeout_ms: 5000,
                max_connections: 1,
                auto_initialize_schema: true,
                use_wal: true,
            },
        };
        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),
        };
        return std::sync::Arc::new(database);
    }

    #[test]
    fn generic_event_family_fallback_is_audit_not_unknown() {
        assert_eq!(
            super::infer_event_family("non_matching_entry", crate::ENTRY_KIND_INSTRUCTION),
            Some("audit".to_string())
        );
    }

    #[test]
    fn pump_fees_event_family_fallback_is_audit() {
        assert_eq!(
            super::infer_event_family_for_entry(
                "pump_fees",
                "non_matching_entry",
                crate::ENTRY_KIND_INSTRUCTION
            ),
            Some("audit".to_string())
        );
    }

    #[test]
    fn event_family_inference_covers_raydium_cpmm_core_entries() {
        assert_eq!(
            super::infer_event_family("swap_base_input", crate::ENTRY_KIND_INSTRUCTION),
            Some("swap".to_string())
        );
        assert_eq!(
            super::infer_event_family("initialize", crate::ENTRY_KIND_INSTRUCTION),
            Some("pool_create".to_string())
        );
        assert_eq!(
            super::infer_event_family("withdraw", crate::ENTRY_KIND_INSTRUCTION),
            Some("liquidity_remove".to_string())
        );
        assert_eq!(
            super::infer_event_family("collect_creator_fee", crate::ENTRY_KIND_INSTRUCTION),
            Some("fee".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "raydium_cpmm",
                "anchor_idl_instruction",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("idl_management".to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "raydium_cpmm",
                "anchor_idl_instruction",
                Some("idl_management"),
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string())
        );
    }

    #[test]
    fn event_family_inference_covers_raydium_clmm_idl_entries() {
        assert_eq!(
            super::infer_event_family("create_customizable_pool", crate::ENTRY_KIND_INSTRUCTION),
            Some("pool_create".to_string())
        );
        assert_eq!(
            super::infer_event_family("create_dynamic_fee_config", crate::ENTRY_KIND_INSTRUCTION),
            Some("admin_config".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "raydium_clmm",
                "update_dynamic_fee_config",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("admin_config".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "raydium_clmm",
                "create_personal_position_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("position_open".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "raydium_clmm",
                "liquidity_calculate_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("liquidity_calculation".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "raydium_clmm",
                "liquidity_change_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("liquidity_change".to_string())
        );
        assert_eq!(
            super::infer_event_family(
                "create_support_mint_associated",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("account_create".to_string())
        );
        assert_eq!(
            super::infer_event_family("close_limit_order", crate::ENTRY_KIND_INSTRUCTION),
            Some("order_cancel".to_string())
        );
        assert_eq!(
            super::infer_event_family("open_limit_order", crate::ENTRY_KIND_INSTRUCTION),
            Some("order_place".to_string())
        );
        assert_eq!(
            super::infer_event_family("increase_limit_order", crate::ENTRY_KIND_INSTRUCTION),
            Some("order_place".to_string())
        );
        assert_eq!(
            super::infer_event_family("decrease_limit_order", crate::ENTRY_KIND_INSTRUCTION),
            Some("order_cancel".to_string())
        );
        assert_eq!(
            super::infer_event_family("close_protocol_position", crate::ENTRY_KIND_INSTRUCTION),
            Some("position_close".to_string())
        );
        assert_eq!(
            super::infer_event_family("settle_limit_order", crate::ENTRY_KIND_INSTRUCTION),
            Some("settle_funds".to_string())
        );
        assert_eq!(
            super::infer_expected_db_target(Some("position_open"), crate::ENTRY_KIND_INSTRUCTION),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "raydium_clmm",
                "open_position",
                Some("position_open"),
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string())
        );
        assert_eq!(
            super::infer_expected_db_target(Some("position_close"), crate::ENTRY_KIND_INSTRUCTION),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS.to_string())
        );
        assert_eq!(
            super::known_local_event_kind("raydium_clmm", "create_pool"),
            Some("raydium_clmm.create_pool".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("raydium_clmm", "collect_protocol_fee"),
            Some("raydium_clmm.collect_protocol_fee".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("raydium_clmm", "open_limit_order"),
            Some("raydium_clmm.open_limit_order".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("raydium_clmm", "increase_limit_order"),
            Some("raydium_clmm.increase_limit_order".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("raydium_clmm", "decrease_limit_order"),
            Some("raydium_clmm.decrease_limit_order".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("raydium_clmm", "update_dynamic_fee_config"),
            Some("raydium_clmm.update_dynamic_fee_config".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("raydium_clmm", "cpi_event"),
            Some("raydium_clmm.cpi_event".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("raydium_clmm", "pool_created_event"),
            Some("raydium_clmm.pool_created_event".to_string())
        );
    }
    #[test]
    fn pump_fun_coverage_maps_local_idl_and_audit_entries() {
        assert_eq!(
            super::known_local_event_kind("pump_fun", "buy"),
            Some("pump_fun.buy".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("pump_fun", "create_v2"),
            Some("pump_fun.create_v2_token".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("pump_fun", "buy_v2"),
            Some("pump_fun.buy_v2".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("pump_fun", "collect_creator_fee_v2"),
            Some("pump_fun.collect_creator_fee_v2".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("pump_fun", "trade_event"),
            Some("pump_fun.trade_event".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("pump_fun", "claim_cashback_event"),
            Some("pump_fun.claim_cashback_event".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "pump_fun",
                "create_event",
                crate::ENTRY_KIND_EVENT
            ),
            Some("launch".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "pump_fun",
                "set_metaplex_creator_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("admin_config".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "pump_fun",
                "claim_token_incentives_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("reward".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "pump_fun",
                "buy_v2",
                crate::ENTRY_KIND_INSTRUCTION
            ),
            Some("swap".to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "pump_fun",
                "buy",
                Some("swap"),
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "pump_fun",
                "buy_v2",
                Some("swap"),
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_TRADE_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "pump_fun",
                "create_v2",
                Some("launch"),
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_LAUNCH_EVENTS.to_string())
        );
    }

    #[test]
    fn launchpad_swap_instructions_materialize_as_launch_events_without_duplicate_trades() {
        assert_eq!(
            super::known_local_event_kind("raydium_launchpad", "buy_exact_in"),
            Some("raydium_launchpad.buy_exact_in".to_string())
        );
        assert_eq!(
            super::known_local_event_kind("raydium_launchpad", "trade_event"),
            Some("raydium_launchpad.trade_event".to_string())
        );
        assert_eq!(super::known_local_event_kind("raydium_launchpad", "program"), None);
        assert_eq!(
            super::infer_event_family_for_entry(
                "raydium_launchpad",
                "pool_create_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("pool_create".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "raydium_launchpad",
                "claim_vested_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("vesting".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "raydium_launchpad",
                "create_platform_vesting_account",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("vesting".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "raydium_launchpad",
                "remove_platform_curve_param",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("admin_config".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "raydium_launchpad",
                "cpi_event",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("cpi_transport".to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "raydium_launchpad",
                "buy_exact_in",
                Some("swap"),
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_LAUNCH_EVENTS.to_string())
        );
    }

    #[tokio::test]
    async fn sync_upstream_registry_persists_raydium_cpmm_coverage_rows() {
        let database = make_database().await;
        let service = crate::DexEventCoverageService::new(database.clone());
        let result = service.sync_upstream_registry(Some("raydium_cpmm".to_string())).await;
        let result = match result {
            Ok(result) => result,
            Err(error) => panic!("coverage sync must succeed: {}", error),
        };
        assert!(result.upstream_entry_count > 0);
        assert_eq!(result.upstream_entry_count, result.upserted_entry_count);
        let rows_result = crate::query_dex_event_coverage_entries_list_by_decoder(
            database.as_ref(),
            "raydium_cpmm",
        )
        .await;
        let rows = match rows_result {
            Ok(rows) => rows,
            Err(error) => panic!("coverage rows must load: {}", error),
        };
        assert_eq!(rows.len(), result.upstream_entry_count);
        assert!(rows.iter().any(|row| return {
            row.entry_name == "swap_base_input"
                && row.event_family == Some("swap".to_string())
                && row.local_event_kind == Some("raydium_cpmm.swap_base_input".to_string())
        }));
        assert!(rows.iter().any(|row| return {
            row.entry_name == "deposit"
                && row.event_family == Some("liquidity_add".to_string())
                && row.local_event_kind == Some("raydium_cpmm.deposit".to_string())
        }));
        assert!(rows.iter().any(|row| return {
            row.entry_name == "lp_change_event"
                && row.event_family == Some("liquidity".to_string())
                && row.expected_db_target
                    == Some(crate::DexEventCoverageEntryDto::DB_TARGET_LIQUIDITY_EVENTS.to_string())
                && row.local_event_kind == Some("raydium_cpmm.lp_change_event".to_string())
        }));
        assert!(rows.iter().any(|row| return {
            row.entry_name == "cpi_event"
                && row.event_family == Some("cpi_transport".to_string())
                && row.expected_db_target
                    == Some(
                        crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string(),
                    )
                && row.local_event_kind == Some("raydium_cpmm.cpi_event".to_string())
        }));
        assert!(rows.iter().any(|row| return {
            row.entry_name == "anchor_idl_instruction"
                && row.event_family == Some("idl_management".to_string())
                && row.expected_db_target
                    == Some(
                        crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string(),
                    )
                && row.local_event_kind == Some("raydium_cpmm.anchor_idl_instruction".to_string())
        }));
        assert!(rows.iter().any(|row| return {
            row.entry_name == "swap_event"
                && row.event_family == Some("swap".to_string())
                && row.expected_db_target
                    == Some(
                        crate::DexEventCoverageEntryDto::DB_TARGET_DECODED_EVENTS_ONLY.to_string(),
                    )
                && row.local_event_kind == Some("raydium_cpmm.swap_event".to_string())
        }));
    }

    #[test]
    fn meteora_dlmm_coverage_targets_materialized_anchor_and_synthetic_events() {
        assert_eq!(
            super::infer_event_family_for_entry(
                "meteora_dlmm",
                "create_pool",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("pool_create".to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "meteora_dlmm",
                "create_pool",
                Some("pool_create"),
                crate::ENTRY_KIND_EVENT,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "meteora_dlmm",
                "composition_fee_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("fee".to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "meteora_dlmm",
                "composition_fee_event",
                Some("fee"),
                crate::ENTRY_KIND_EVENT,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "meteora_dlmm",
                "swap_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("swap_log".to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "meteora_dlmm",
                "swap_event",
                Some("swap_log"),
                crate::ENTRY_KIND_EVENT,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "meteora_dlmm",
                "swap2_evt",
                Some("swap_log"),
                crate::ENTRY_KIND_EVENT,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "meteora_dlmm",
                "update_fees_and_rewards",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("admin_config".to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "meteora_dlmm",
                "update_fees_and_rewards",
                Some("admin_config"),
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "meteora_dlmm",
                "update_reward_duration_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some("admin_config".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "meteora_dlmm",
                "close_claim_fee_operator_account",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("admin_config".to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "meteora_dlmm",
                "initialize_preset_parameter_v2",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("admin_config".to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "meteora_dlmm",
                "initialize_preset_parameter_v2",
                Some("admin_config"),
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "meteora_dlmm",
                "instruction_audit",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("admin_audit".to_string())
        );
        assert_eq!(
            super::infer_expected_db_target_for_entry(
                "meteora_dlmm",
                "instruction_audit",
                Some("admin_audit"),
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_event_family_for_entry(
                "meteora_dlmm",
                "migrate_position",
                crate::ENTRY_KIND_INSTRUCTION,
            ),
            Some("position_update".to_string())
        );
    }

    #[tokio::test]
    async fn meteora_dlmm_coverage_sync_removes_duplicate_logical_rows() {
        let database = make_database().await;
        let service = crate::DexEventCoverageService::new(database.clone());
        let result = service.sync_upstream_registry(Some("meteora_dlmm".to_string())).await;
        let result = match result {
            Ok(result) => result,
            Err(error) => panic!("coverage sync must succeed: {}", error),
        };
        assert!(result.upstream_entry_count > 0);
        let rows_result = crate::query_dex_event_coverage_entries_list_by_decoder(
            database.as_ref(),
            "meteora_dlmm",
        )
        .await;
        let rows = match rows_result {
            Ok(rows) => rows,
            Err(error) => panic!("coverage rows must load: {}", error),
        };
        let mut logical_keys = std::collections::HashSet::new();
        for row in &rows {
            let key = format!(
                "{}|{}|{}|{}|{}|{}",
                row.decoder_code,
                row.program_id.clone().unwrap_or_default(),
                row.entry_kind,
                row.entry_name,
                row.discriminator_hex.clone().unwrap_or_default(),
                row.local_event_kind.clone().unwrap_or_default()
            );
            assert!(logical_keys.insert(key));
        }
        assert!(rows.iter().any(|row| return {
            row.entry_name == "swap"
                && row.discriminator_hex == Some("f8c69e91e17587c8".to_string())
                && row.local_event_kind == Some("meteora_dlmm.swap".to_string())
        }));
        assert!(rows.iter().any(|row| return {
            row.entry_name == "update_fees_and_rewards"
                && row.event_family == Some("admin_config".to_string())
                && row.expected_db_target
                    == Some(
                        crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string(),
                    )
        }));
    }

    #[tokio::test]
    async fn refresh_local_counts_auto_syncs_empty_coverage_table() {
        let database = make_database().await;
        let service = crate::DexEventCoverageService::new(database.clone());
        let result = service.refresh_local_counts(Some("raydium_cpmm".to_string())).await;
        let result = match result {
            Ok(result) => result,
            Err(error) => panic!("coverage refresh must succeed: {}", error),
        };
        assert!(result.upstream_entry_count > 0);
        assert_eq!(result.upstream_entry_count, result.upserted_entry_count);
        let rows_result = crate::query_dex_event_coverage_entries_list_by_decoder(
            database.as_ref(),
            "raydium_cpmm",
        )
        .await;
        let rows = match rows_result {
            Ok(rows) => rows,
            Err(error) => panic!("coverage rows must load: {}", error),
        };
        assert!(!rows.is_empty());
    }

    #[test]
    fn pump_swap_anchor_events_have_materialized_targets() {
        assert_eq!(
            super::infer_pump_swap_expected_db_target("buy_event", crate::ENTRY_KIND_EVENT),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_pump_swap_expected_db_target("sell_event", crate::ENTRY_KIND_EVENT),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_LIFECYCLE_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_pump_swap_expected_db_target(
                "collect_coin_creator_fee_event",
                crate::ENTRY_KIND_EVENT,
            ),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_FEE_EVENTS.to_string())
        );
        assert_eq!(
            super::infer_pump_swap_expected_db_target("update_fee_config_event", crate::ENTRY_KIND_EVENT),
            Some(crate::DexEventCoverageEntryDto::DB_TARGET_POOL_ADMIN_EVENTS.to_string())
        );
    }

    #[test]
    fn pump_swap_anchor_swap_events_are_swap_logs() {
        assert_eq!(
            super::infer_pump_swap_event_family("buy_event", crate::ENTRY_KIND_EVENT),
            Some("swap_log".to_string())
        );
        assert_eq!(
            super::infer_pump_swap_event_family("sell_event", crate::ENTRY_KIND_EVENT),
            Some("swap_log".to_string())
        );
    }


}
