-- file: validation_sql/SQL_VALIDATION_RAYDIUM_CPMM_0_7_48.sql

-- 1. Coverage rows must exist after diagnostics/validation/backfill refresh.
SELECT *
FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'raydium_cpmm'
ORDER BY entry_kind, entry_name, discriminator_hex;

-- 2. Aggregated coverage summary for Raydium CPMM.
SELECT
    decoder_code,
    listed_entry_count,
    decoded_entry_count,
    observed_entry_count,
    materialized_entry_count,
    total_observed_count,
    total_materialized_count,
    trade_count,
    audit_only_entry_count,
    upstream_git_mapped_unverified_entry_count,
    upstream_git_local_corpus_observed_entry_count,
    upstream_git_local_corpus_materialized_entry_count
FROM (
    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,
        SUM(CASE WHEN expected_db_target = 'k_sol_dex_decoded_events_only' THEN 1 ELSE 0 END) AS audit_only_entry_count,
        SUM(CASE WHEN proof_status = 'upstream_git_mapped_unverified' THEN 1 ELSE 0 END) AS upstream_git_mapped_unverified_entry_count,
        SUM(CASE WHEN proof_status = 'upstream_git_local_corpus_observed' THEN 1 ELSE 0 END) AS upstream_git_local_corpus_observed_entry_count,
        SUM(CASE WHEN proof_status = 'upstream_git_local_corpus_materialized' THEN 1 ELSE 0 END) AS upstream_git_local_corpus_materialized_entry_count
    FROM k_sol_dex_event_coverage_entries
    GROUP BY decoder_code
)
WHERE decoder_code = 'raydium_cpmm';

-- 3. Decoded event distribution.
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_cpmm'
GROUP BY de.protocol_name, de.event_kind
ORDER BY decoded_count DESC, de.event_kind;

-- 4. Audit/non-trade safety: no audit/non-trade CPMM event may produce trades.
SELECT
    de.protocol_name,
    de.event_kind,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_cpmm'
  AND (
      de.event_kind LIKE '%audit%'
      OR de.event_kind IN (
          'raydium_cpmm.lp_change_event',
          'raydium_cpmm.swap_event',
          'raydium_cpmm.collect_creator_fee',
          'raydium_cpmm.collect_fund_fee',
          'raydium_cpmm.collect_protocol_fee',
          'raydium_cpmm.create_amm_config',
          'raydium_cpmm.update_amm_config',
          'raydium_cpmm.update_pool_status',
          'raydium_cpmm.create_permission_pda',
          'raydium_cpmm.close_permission_pda'
      )
      OR json_extract(de.payload_json, '$.eventActionability') IN ('non_trade_useful', 'informational', 'non_actionable_trade')
  )
GROUP BY de.protocol_name, de.event_kind
ORDER BY trade_count DESC, de.event_kind;

-- 5. Failed transactions must not materialize trades.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
JOIN k_sol_chain_transactions tx
    ON tx.id = de.transaction_id
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_cpmm'
  AND tx.err_json IS NOT NULL
  AND tx.err_json <> ''
GROUP BY de.event_kind
ORDER BY trade_count DESC, decoded_count DESC;

-- 6. Local specialized decoder must replace upstream fallback for CPMM entries.
SELECT
    json_extract(payload_json, '$.upstreamDecoderCode') AS upstream_decoder_code,
    json_extract(payload_json, '$.entryName') AS entry_name,
    json_extract(payload_json, '$.discriminatorHex') AS discriminator_hex,
    COUNT(*) AS fallback_count
FROM k_sol_dex_decoded_events
WHERE protocol_name = 'upstream_git'
  AND event_kind = 'upstream_git.instruction_match'
  AND json_extract(payload_json, '$.upstreamDecoderCode') = 'raydium_cpmm'
GROUP BY upstream_decoder_code, entry_name, discriminator_hex
ORDER BY fallback_count DESC, entry_name;

-- 7. Program data events are decoded but must remain non-trade/non-candle candidates.
SELECT
    event_kind,
    COUNT(*) AS decoded_count,
    SUM(CASE WHEN json_extract(payload_json, '$.tradeCandidate') = 1 THEN 1 ELSE 0 END) AS trade_candidate_count,
    SUM(CASE WHEN json_extract(payload_json, '$.candleCandidate') = 1 THEN 1 ELSE 0 END) AS candle_candidate_count,
    SUM(CASE WHEN json_extract(payload_json, '$.eventActionability') IN ('non_trade_useful', 'non_actionable_trade') THEN 1 ELSE 0 END) AS safe_actionability_count
FROM k_sol_dex_decoded_events
WHERE protocol_name = 'raydium_cpmm'
  AND event_kind IN ('raydium_cpmm.lp_change_event', 'raydium_cpmm.swap_event')
GROUP BY event_kind
ORDER BY event_kind;

-- 8. Liquidity materialization for lp_change_event, if pool/pair exists in local corpus.
SELECT
    de.event_kind,
    json_extract(de.payload_json, '$.changeType') AS change_type,
    COUNT(de.id) AS decoded_count,
    COUNT(le.id) AS liquidity_materialized_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_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_cpmm'
  AND de.event_kind = 'raydium_cpmm.lp_change_event'
GROUP BY de.event_kind, change_type
ORDER BY change_type;


-- 9. Replay coverage hook check: capture this value before and after local replay.
SELECT MAX(updated_at) AS latest_coverage_refresh_at
FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'raydium_cpmm';

-- 10. lp_change_event is a bidirectional liquidity event in coverage.
SELECT
    entry_name,
    event_family,
    expected_db_target,
    proof_status,
    observed_count,
    materialized_count,
    trade_count
FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'raydium_cpmm'
  AND entry_name = 'lp_change_event';

-- 0.7.48 final — materialization safety summary.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(le.id) AS liquidity_count,
    COUNT(fe.id) AS fee_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_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_cpmm'
GROUP BY de.event_kind
ORDER BY de.event_kind;

-- 0.7.48 final — lp_change_event add/remove split.
SELECT
    de.event_kind,
    json_extract(de.payload_json, '$.changeType') AS change_type,
    COUNT(*) AS decoded_count,
    COUNT(le.id) AS liquidity_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_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_cpmm'
  AND de.event_kind = 'raydium_cpmm.lp_change_event'
GROUP BY de.event_kind, change_type
ORDER BY change_type;

-- 0.7.48 final — initialize_with_permission must remain lifecycle-only.
SELECT
    de.event_kind,
    COUNT(DISTINCT de.id) AS decoded_count,
    COUNT(DISTINCT ple.id) AS lifecycle_count,
    COUNT(DISTINCT pa.id) AS admin_count,
    COUNT(DISTINCT te.id) AS trade_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_pool_lifecycle_events ple
    ON ple.decoded_event_id = de.id
LEFT JOIN k_sol_pool_admin_events pa
    ON pa.decoded_event_id = de.id
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_cpmm'
  AND de.event_kind IN (
      'raydium_cpmm.initialize',
      'raydium_cpmm.initialize_with_permission'
  )
GROUP BY de.event_kind
ORDER BY de.event_kind;

-- 0.7.48 final — instruction observation index.
SELECT
    instruction_name,
    discriminator_hex,
    COUNT(*) AS observed_count,
    COUNT(DISTINCT signature) AS tx_count
FROM k_sol_instruction_observations
WHERE decoder_code = 'raydium_cpmm'
GROUP BY instruction_name, discriminator_hex
ORDER BY observed_count DESC;
