-- file: validation_sql/SQL_VALIDATION_RAYDIUM_STABLE_SWAP_0_7_52.sql

-- 0.7.52 raydium_stable_swap validation checklist.
-- Run on a dedicated fresh SQLite database after corpus construction and replay with:
-- skipDexDecode=no, forceDexDecode=yes, deferInstructionObservations=yes.

-- 01. Coverage stable swap.
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_stable_swap'
ORDER BY entry_kind, entry_name, discriminator_hex;

-- 02. Instruction observations.
SELECT
    instruction_name,
    discriminator_hex,
    COUNT(*) AS observed_count,
    COUNT(DISTINCT signature) AS tx_count
FROM k_sol_instruction_observations
WHERE decoder_code = 'raydium_stable_swap'
GROUP BY instruction_name, discriminator_hex
ORDER BY observed_count DESC, instruction_name, discriminator_hex;

-- 03. Residual local instruction audit.
SELECT
    json_extract(payload_json, '$.discriminatorHex') AS discriminator_hex,
    COUNT(*) AS audit_count,
    COUNT(DISTINCT transaction_id) AS tx_count
FROM k_sol_dex_decoded_events
WHERE protocol_name = 'raydium_stable_swap'
  AND event_kind = 'raydium_stable_swap.instruction_audit'
GROUP BY discriminator_hex
ORDER BY audit_count DESC, discriminator_hex;

-- 04. Residual upstream fallback for covered local entries.
SELECT
    json_extract(ug.payload_json, '$.upstreamDecoderCode') AS upstream_decoder_code,
    json_extract(ug.payload_json, '$.upstreamEntryName') AS entry_name,
    json_extract(ug.payload_json, '$.upstreamDiscriminatorHex') AS discriminator_hex,
    json_extract(ug.payload_json, '$.upstreamSourceRepo') AS source_repo,
    COUNT(*) AS fallback_count,
    COUNT(DISTINCT ug.transaction_id) AS tx_count
FROM k_sol_dex_decoded_events ug
JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = json_extract(ug.payload_json, '$.upstreamDecoderCode')
   AND ce.entry_name = json_extract(ug.payload_json, '$.upstreamEntryName')
   AND ce.discriminator_hex = json_extract(ug.payload_json, '$.upstreamDiscriminatorHex')
   AND ce.local_event_kind IS NOT NULL
   AND ce.local_event_kind <> ''
WHERE ug.protocol_name = 'upstream_git'
  AND ug.event_kind = 'upstream_git.instruction_match'
  AND json_extract(ug.payload_json, '$.upstreamDecoderCode') = 'raydium_stable_swap'
GROUP BY upstream_decoder_code, entry_name, discriminator_hex, source_repo
ORDER BY fallback_count DESC, entry_name;

-- 05. Non-swap safety: non-swap event must not materialize as trade.
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_stable_swap'
   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_stable_swap'
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;

-- 06. Failed transaction safety: failed tx must not materialize as business trade.
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_stable_swap'
  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;

-- 07. Decoded without coverage entry.
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_stable_swap'
   AND ce.local_event_kind = de.event_kind
WHERE de.protocol_name = 'raydium_stable_swap'
  AND ce.id IS NULL
GROUP BY de.event_kind
ORDER BY decoded_count DESC, de.event_kind;

-- 08. Multi-target materialization.
SELECT
    de.event_kind,
    COUNT(DISTINCT de.id) AS decoded_count,
    COUNT(DISTINCT te.id) AS trade_count,
    COUNT(DISTINCT le.id) AS liquidity_count,
    COUNT(DISTINCT pe.id) AS lifecycle_count,
    COUNT(DISTINCT fe.id) AS fee_count,
    COUNT(DISTINCT ae.id) AS admin_count,
    COUNT(DISTINCT oe.id) AS orderbook_count,
    (
        CASE WHEN COUNT(DISTINCT te.id) > 0 THEN 1 ELSE 0 END
      + CASE WHEN COUNT(DISTINCT le.id) > 0 THEN 1 ELSE 0 END
      + CASE WHEN COUNT(DISTINCT pe.id) > 0 THEN 1 ELSE 0 END
      + CASE WHEN COUNT(DISTINCT fe.id) > 0 THEN 1 ELSE 0 END
      + CASE WHEN COUNT(DISTINCT ae.id) > 0 THEN 1 ELSE 0 END
      + CASE WHEN COUNT(DISTINCT oe.id) > 0 THEN 1 ELSE 0 END
    ) AS materialized_target_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
LEFT JOIN k_sol_liquidity_events le
    ON le.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events pe
    ON pe.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 ae
    ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_orderbook_events oe
    ON oe.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_stable_swap'
GROUP BY de.event_kind
HAVING materialized_target_count > 1
ORDER BY materialized_target_count DESC, de.event_kind;

-- 09. Unexplained successful non-materialized events.
SELECT
    de.event_kind,
    COUNT(*) AS unexplained_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
LEFT JOIN k_sol_liquidity_events le
    ON le.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events pe
    ON pe.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 ae
    ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_orderbook_events oe
    ON oe.decoded_event_id = de.id
LEFT JOIN k_sol_token_account_events tae
    ON tae.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_stable_swap'
  AND (
      tx.err_json IS NULL
      OR tx.err_json = ''
      OR tx.err_json = 'null'
  )
  AND te.id IS NULL
  AND le.id IS NULL
  AND pe.id IS NULL
  AND fe.id IS NULL
  AND ae.id IS NULL
  AND oe.id IS NULL
  AND tae.id IS NULL
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipTradeReason')), '') = ''
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipLiquidityReason')), '') = ''
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipLifecycleReason')), '') = ''
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipCatalogReason')), '') = ''
GROUP BY de.event_kind
ORDER BY unexplained_count DESC, de.event_kind;

-- 10. Materialization summary.
SELECT
    de.event_kind,
    COUNT(DISTINCT de.id) AS decoded_count,
    COUNT(DISTINCT te.id) AS trade_count,
    COUNT(DISTINCT le.id) AS liquidity_count,
    COUNT(DISTINCT pe.id) AS lifecycle_count,
    COUNT(DISTINCT fe.id) AS fee_count,
    COUNT(DISTINCT ae.id) AS admin_count,
    COUNT(DISTINCT oe.id) AS orderbook_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
LEFT JOIN k_sol_liquidity_events le
    ON le.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events pe
    ON pe.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 ae
    ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_orderbook_events oe
    ON oe.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_stable_swap'
GROUP BY de.event_kind
ORDER BY de.event_kind;


-- 11. Stable Swap swap amount-source closure.
-- Successful swaps must materialize from exact vault deltas.
-- Failed swaps may remain instruction-bounds-only and must not materialize as trades.
SELECT
    de.event_kind,
    json_extract(de.payload_json, '$.amountSource') AS amount_source,
    CASE
        WHEN tx.err_json IS NOT NULL
         AND tx.err_json <> ''
         AND tx.err_json <> 'null'
        THEN 'failed'
        ELSE 'success'
    END AS tx_status,
    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_stable_swap'
  AND de.event_kind IN (
      'raydium_stable_swap.swap_base_in',
      'raydium_stable_swap.swap_base_out'
  )
GROUP BY de.event_kind, amount_source, tx_status
ORDER BY de.event_kind, amount_source, tx_status;

-- 12. Stable Swap successful swap without trade and without explanation.
-- Expected result: empty.
SELECT
    de.event_kind,
    COUNT(*) AS unexplained_success_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_stable_swap'
  AND de.event_kind IN (
      'raydium_stable_swap.swap_base_in',
      'raydium_stable_swap.swap_base_out'
  )
  AND (
      tx.err_json IS NULL
      OR tx.err_json = ''
      OR tx.err_json = 'null'
  )
  AND te.id IS NULL
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipTradeReason')), '') = ''
GROUP BY de.event_kind;

-- 13. Stable Swap failed swap safety.
-- Expected result: empty.
SELECT
    de.event_kind,
    json_extract(de.payload_json, '$.amountSource') AS amount_source,
    COUNT(*) AS failed_trade_count
FROM k_sol_dex_decoded_events de
JOIN k_sol_chain_transactions tx
    ON tx.id = de.transaction_id
JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_stable_swap'
  AND de.event_kind IN (
      'raydium_stable_swap.swap_base_in',
      'raydium_stable_swap.swap_base_out'
  )
  AND tx.err_json IS NOT NULL
  AND tx.err_json <> ''
  AND tx.err_json <> 'null'
GROUP BY de.event_kind, amount_source
ORDER BY de.event_kind, amount_source;
