-- file: validation_sql/SQL_VALIDATION_RAYDIUM_CLMM_0_7_49_PRE11.sql
-- Raydium CLMM validation after 0.7.49-pre.11.

SELECT
    entry_name,
    entry_kind,
    event_family,
    expected_db_target,
    proof_status,
    local_event_kind,
    discriminator_hex,
    observed_count,
    materialized_count,
    trade_count
FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'raydium_clmm'
ORDER BY entry_kind, entry_name, discriminator_hex;

SELECT
    decoder_code,
    COUNT(*) AS listed_entry_count,
    SUM(CASE WHEN local_event_kind IS NOT NULL AND local_event_kind <> '' THEN 1 ELSE 0 END) AS decoded_entry_count,
    SUM(CASE WHEN observed_count > 0 THEN 1 ELSE 0 END) AS observed_entry_count,
    SUM(CASE WHEN materialized_count > 0 THEN 1 ELSE 0 END) AS materialized_entry_count,
    COALESCE(SUM(observed_count), 0) AS total_observed_count,
    COALESCE(SUM(materialized_count), 0) AS total_materialized_count,
    COALESCE(SUM(trade_count), 0) AS trade_count
FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'raydium_clmm'
GROUP BY decoder_code;

SELECT
    de.protocol_name,
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(DISTINCT de.transaction_id) AS transaction_count
FROM k_sol_dex_decoded_events de
WHERE de.protocol_name = 'raydium_clmm'
GROUP BY de.protocol_name, de.event_kind
ORDER BY decoded_count DESC, de.event_kind;

SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(le.id) AS liquidity_count,
    COUNT(fe.id) AS fee_count,
    COUNT(re.id) AS reward_count,
    COUNT(pa.id) AS admin_count,
    COUNT(ple.id) AS lifecycle_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_liquidity_events le ON le.decoded_event_id = de.id
LEFT JOIN k_sol_fee_events fe ON fe.decoded_event_id = de.id
LEFT JOIN k_sol_reward_events re ON re.decoded_event_id = de.id
LEFT JOIN k_sol_pool_admin_events pa ON pa.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events ple ON ple.decoded_event_id = de.id
LEFT JOIN k_sol_trade_events te ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_clmm'
GROUP BY de.event_kind
ORDER BY de.event_kind;

-- Residual duplicated audits: should only return unknown/unmapped discriminators
-- or known discriminators for which no same-transaction named event exists.
SELECT
    json_extract(audit.payload_json, '$.discriminatorHex') AS discriminator_hex,
    COUNT(*) AS residual_audit_count,
    COUNT(DISTINCT audit.transaction_id) AS transaction_count
FROM k_sol_dex_decoded_events audit
WHERE audit.protocol_name = 'raydium_clmm'
  AND audit.event_kind = 'raydium_clmm.instruction_audit'
GROUP BY discriminator_hex
ORDER BY residual_audit_count DESC, discriminator_hex;

-- Duplicated known audits that still have a named CLMM event in the same transaction.
-- Expected: zero rows.
SELECT
    json_extract(audit.payload_json, '$.discriminatorHex') AS discriminator_hex,
    audit.event_kind AS audit_event_kind,
    named.event_kind AS named_event_kind,
    COUNT(*) AS duplicate_count
FROM k_sol_dex_decoded_events audit
JOIN k_sol_dex_decoded_events named
    ON named.transaction_id = audit.transaction_id
   AND named.protocol_name = 'raydium_clmm'
   AND named.event_kind <> 'raydium_clmm.instruction_audit'
   AND COALESCE(
        json_extract(named.payload_json, '$.instructionDiscriminatorHex'),
        json_extract(named.payload_json, '$.instruction_discriminator_hex'),
        json_extract(named.payload_json, '$.discriminatorHex'),
        json_extract(named.payload_json, '$.discriminator_hex')
   ) = COALESCE(
        json_extract(audit.payload_json, '$.discriminatorHex'),
        json_extract(audit.payload_json, '$.discriminator_hex'),
        json_extract(audit.payload_json, '$.instructionDiscriminatorHex'),
        json_extract(audit.payload_json, '$.instruction_discriminator_hex')
   )
WHERE audit.protocol_name = 'raydium_clmm'
  AND audit.event_kind = 'raydium_clmm.instruction_audit'
GROUP BY discriminator_hex, audit_event_kind, named_event_kind
ORDER BY duplicate_count DESC;
