-- file: validation_sql/SQL_VALIDATION_RAYDIUM_AMM_V4_0_7_51_PRE2_REPLAY_CLEANUP.sql
-- Raydium AMM v4 replay cleanup checks after 0.7.51-pre.2.

-- 1. Legacy generic AMM v4 swap must be gone.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    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_amm_v4'
  AND de.event_kind = 'raydium_amm_v4.swap'
GROUP BY de.event_kind;

-- 2. All AMM v4 decoded event kinds must have coverage entries.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = 'raydium_amm_v4'
   AND ce.local_event_kind = de.event_kind
WHERE de.protocol_name = 'raydium_amm_v4'
  AND ce.id IS NULL
GROUP BY de.event_kind
ORDER BY decoded_count DESC, de.event_kind;

-- 3. AMM v4 instruction observations must use 1-byte discriminators.
SELECT
    discriminator_hex,
    COUNT(*) AS observation_count,
    COUNT(DISTINCT signature) AS tx_count
FROM k_sol_instruction_observations
WHERE decoder_code = 'raydium_amm_v4'
  AND discriminator_hex IS NOT NULL
  AND length(discriminator_hex) > 2
GROUP BY discriminator_hex
ORDER BY observation_count DESC, discriminator_hex;

-- 4. Non-swap AMM v4 events must never materialize trades.
SELECT
    de.event_kind,
    ce.event_family,
    COUNT(*) AS decoded_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = 'raydium_amm_v4'
   AND ce.local_event_kind = de.event_kind
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_amm_v4'
GROUP BY de.event_kind, ce.event_family
HAVING ce.event_family <> 'swap'
   AND COUNT(te.id) > 0
ORDER BY trade_count DESC, de.event_kind;

-- 5. Failed AMM v4 transactions must never materialize trades.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_failed_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_amm_v4'
  AND tx.err_json IS NOT NULL
  AND tx.err_json <> ''
  AND tx.err_json <> 'null'
GROUP BY de.event_kind
HAVING COUNT(te.id) > 0
ORDER BY trade_count DESC, de.event_kind;

-- 6. AMM v4 successful swaps missing trade materialization.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_success_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_amm_v4'
  AND de.event_kind IN (
      'raydium_amm_v4.swap_base_in',
      'raydium_amm_v4.swap_base_in_v2',
      'raydium_amm_v4.swap_base_out',
      'raydium_amm_v4.swap_base_out_v2'
  )
  AND (
      tx.err_json IS NULL
      OR tx.err_json = ''
      OR tx.err_json = 'null'
  )
GROUP BY de.event_kind
HAVING COUNT(te.id) < COUNT(*)
ORDER BY de.event_kind;

-- 7. AMM v4 coverage table after refresh.
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_amm_v4'
ORDER BY entry_kind, entry_name, discriminator_hex;
