-- file: validation_sql/SQL_VALIDATION_RAYDIUM_CPMM_0_7_50_PRE_R2.sql

-- Raydium CPMM 0.7.50-pre-r2 re-verification pack.
-- Goal: repeat the Launchpad-style closure checks on a dedicated CPMM
-- database, after replay with skipDexDecode='no' and forceDexDecode='yes'.
-- Expected invariants:
-- - no `unknown` or empty event_family except the synthetic `program` row;
-- - no observed discriminator missing from coverage;
-- - no residual upstream fallback for locally-covered entries;
-- - no successful, actionable decoded event left without its materialized row;
-- - no failed transaction materialized into business tables;
-- - decoded-only entries such as CPMM anchor IDL management and CLMM router/program-data
--   corroboration events are excluded from materialization shortfall checks.

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_cpmm'
ORDER BY entry_kind, entry_name, discriminator_hex;
->
lp_change_event event   liquidity   k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_cpmm.lp_change_event    79a3cdc939da753c    25  25  0
swap_event  event   swap    k_sol_dex_decoded_events_only   upstream_git_local_corpus_observed  raydium_cpmm.swap_event 40c6cde8260871e2    529 0   0
anchor_idl_instruction  instruction idl_management  k_sol_dex_decoded_events_only   upstream_git_local_corpus_observed  raydium_cpmm.anchor_idl_instruction 40f4bc78a7e9690a    6   0   0
close_permission_pda    instruction admin_config    k_sol_pool_admin_events upstream_git_mapped_unverified  raydium_cpmm.close_permission_pda   9c5420764587467b    0   0   0
collect_creator_fee instruction fee k_sol_fee_events    upstream_git_local_corpus_materialized  raydium_cpmm.collect_creator_fee    1416567bc61cdb84    4   4   0
collect_fund_fee    instruction fee k_sol_fee_events    upstream_git_local_corpus_materialized  raydium_cpmm.collect_fund_fee   a78a4e95dfc2067e    7   7   0
collect_protocol_fee    instruction fee k_sol_fee_events    upstream_git_local_corpus_materialized  raydium_cpmm.collect_protocol_fee   8888fcddc2427e59    15  15  0
cpi_event   instruction cpi_transport   k_sol_dex_decoded_events_only   upstream_git_mapped_unverified  raydium_cpmm.cpi_event  e445a52e51cb9a1d    0   0   0
create_amm_config   instruction admin_config    k_sol_pool_admin_events upstream_git_local_corpus_materialized  raydium_cpmm.create_amm_config  8934edd4d7756c68    6   6   0
create_permission_pda   instruction admin_config    k_sol_pool_admin_events upstream_git_local_corpus_materialized  raydium_cpmm.create_permission_pda  878802d889a9b5ca    4   4   0
deposit instruction liquidity_add   k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_cpmm.deposit    f223c68952e1f2b6    11  11  0
initialize  instruction pool_create k_sol_pool_lifecycle_events upstream_git_local_corpus_materialized  raydium_cpmm.initialize afaf6d1f0d989bed    5   5   0
initialize_with_permission  instruction pool_create k_sol_pool_lifecycle_events upstream_git_local_corpus_materialized  raydium_cpmm.initialize_with_permission 3f37fe4131b25979    4   4   0
swap_base_input instruction swap    k_sol_trade_events  upstream_git_local_corpus_materialized  raydium_cpmm.swap_base_input    8fbe5adac41e33de    750 482 482
swap_base_output    instruction swap    k_sol_trade_events  upstream_git_local_corpus_materialized  raydium_cpmm.swap_base_output   37d96256a34ab4ad    25  17  17
update_amm_config   instruction admin_config    k_sol_pool_admin_events upstream_git_local_corpus_materialized  raydium_cpmm.update_amm_config  313cae889a1c74c8    13  13  0
update_pool_status  instruction admin_config    k_sol_pool_admin_events upstream_git_mapped_unverified  raydium_cpmm.update_pool_status 82576c062ee0757b    0   0   0
withdraw    instruction liquidity_remove    k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_cpmm.withdraw   b712469c946da122    14  14  0
program program     k_sol_dex_decoded_events_only   upstream_git_unverified         0   0   0

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_cpmm'
GROUP BY decoder_code;
->
raydium_cpmm    19  18  15  13  1418    607 499

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_cpmm'
  AND (
       event_family IS NULL
    OR event_family = ''
    OR event_family = 'unknown'
  )
  AND entry_kind <> 'program'
ORDER BY entry_kind, entry_name;
-> vide

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, instruction_name, discriminator_hex;
->
raydium_cpmm.swap_base_input    8fbe5adac41e33de    750 741
raydium_cpmm.swap_base_output   37d96256a34ab4ad    25  25
raydium_cpmm.collect_protocol_fee   8888fcddc2427e59    15  15
raydium_cpmm.withdraw   b712469c946da122    14  13
raydium_cpmm.update_amm_config  313cae889a1c74c8    13  5
raydium_cpmm.deposit    f223c68952e1f2b6    11  11
raydium_cpmm.collect_fund_fee   a78a4e95dfc2067e    7   7
raydium_cpmm.create_amm_config  8934edd4d7756c68    6   4
raydium_cpmm.initialize afaf6d1f0d989bed    5   5
raydium_cpmm.collect_creator_fee    1416567bc61cdb84    4   4
raydium_cpmm.create_permission_pda  878802d889a9b5ca    4   4
raydium_cpmm.initialize_with_permission 3f37fe4131b25979    4   4
    40f4bc78a7e9690a    3   3

SELECT
    io.discriminator_hex,
    MIN(io.instruction_name) AS sample_instruction_name,
    MIN(io.decoded_event_kind) AS sample_decoded_event_kind,
    COUNT(*) AS observed_count,
    COUNT(DISTINCT io.signature) AS tx_count
FROM k_sol_instruction_observations io
LEFT JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = 'raydium_cpmm'
   AND ce.discriminator_hex = io.discriminator_hex
WHERE io.decoder_code = 'raydium_cpmm'
  AND io.discriminator_hex IS NOT NULL
  AND io.discriminator_hex <> ''
  AND ce.id IS NULL
GROUP BY io.discriminator_hex
ORDER BY observed_count DESC, io.discriminator_hex;
-> vide

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(ae.id) AS admin_count,
    COUNT(la.id) AS launch_count,
    COUNT(pe.id) AS lifecycle_count,
    COUNT(oe.id) AS orderbook_count,
    COUNT(ta.id) AS token_account_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 ae ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_launch_events la ON la.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events pe ON pe.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 ta ON ta.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 decoded_count DESC, de.event_kind;
->
raydium_cpmm.swap_base_input    750 0   0   0   0   0   0   0   0   482
raydium_cpmm.swap_event 529 0   0   0   0   0   0   0   0   0
raydium_cpmm.lp_change_event    25  25  0   0   0   0   0   0   0   0
raydium_cpmm.swap_base_output   25  0   0   0   0   0   0   0   0   17
raydium_cpmm.collect_protocol_fee   15  0   15  0   0   0   0   0   0   0
raydium_cpmm.withdraw   14  14  0   0   0   0   0   0   0   0
raydium_cpmm.update_amm_config  13  0   0   0   13  0   0   0   0   0
raydium_cpmm.deposit    11  11  0   0   0   0   0   0   0   0
raydium_cpmm.collect_fund_fee   7   0   7   0   0   0   0   0   0   0
raydium_cpmm.create_amm_config  6   0   0   0   6   0   0   0   0   0
raydium_cpmm.initialize 5   0   0   0   0   0   5   0   0   0
raydium_cpmm.collect_creator_fee    4   0   4   0   0   0   0   0   0   0
raydium_cpmm.create_permission_pda  4   0   0   0   4   0   0   0   0   0
raydium_cpmm.initialize_with_permission 4   0   0   0   0   0   4   0   0   0
raydium_cpmm.anchor_idl_instruction 3   0   0   0   0   0   0   0   0   0
raydium_cpmm.instruction_audit  3   0   0   0   0   0   0   0   0   0

SELECT
    de.event_kind,
    ce.entry_name,
    ce.entry_kind,
    ce.event_family,
    ce.expected_db_target,
    COUNT(*) AS decoded_count,
    COUNT(CASE WHEN tx.err_json IS NOT NULL AND tx.err_json <> '' AND tx.err_json <> 'null' THEN de.id END) AS failed_tx_count,
    COUNT(le.id) AS liquidity_count,
    COUNT(fe.id) AS fee_count,
    COUNT(re.id) AS reward_count,
    COUNT(ae.id) AS admin_count,
    COUNT(la.id) AS launch_count,
    COUNT(pe.id) AS lifecycle_count,
    COUNT(oe.id) AS orderbook_count,
    COUNT(ta.id) AS token_account_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_dex_event_coverage_entries ce
    ON ce.decoder_code = 'raydium_cpmm'
   AND ce.local_event_kind = de.event_kind
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 ae ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_launch_events la ON la.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events pe ON pe.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 ta ON ta.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, ce.entry_name, ce.entry_kind, ce.event_family, ce.expected_db_target
HAVING
    COALESCE(ce.expected_db_target, '') <> 'k_sol_dex_decoded_events_only'
    AND (liquidity_count + fee_count + reward_count + admin_count + launch_count + lifecycle_count + orderbook_count + token_account_count + trade_count + failed_tx_count) < decoded_count
ORDER BY decoded_count DESC, de.event_kind;
-> vide

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(ae.id) AS admin_count,
    COUNT(la.id) AS launch_count,
    COUNT(pe.id) AS lifecycle_count,
    COUNT(oe.id) AS orderbook_count,
    COUNT(ta.id) AS token_account_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_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 ae ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_launch_events la ON la.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events pe ON pe.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 ta ON ta.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 tx.err_json IS NOT NULL
  AND tx.err_json <> ''
  AND tx.err_json <> 'null'
GROUP BY de.event_kind
HAVING
       COUNT(le.id) > 0
    OR COUNT(fe.id) > 0
    OR COUNT(re.id) > 0
    OR COUNT(ae.id) > 0
    OR COUNT(la.id) > 0
    OR COUNT(pe.id) > 0
    OR COUNT(oe.id) > 0
    OR COUNT(ta.id) > 0
    OR COUNT(te.id) > 0
ORDER BY de.event_kind;
-> vide

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_cpmm'
  AND event_kind = 'raydium_cpmm.instruction_audit'
GROUP BY discriminator_hex
ORDER BY audit_count DESC, discriminator_hex;
->
40f4bc78a7e9690a    3   3

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_cpmm'
GROUP BY upstream_decoder_code, entry_name, discriminator_hex, source_repo
ORDER BY fallback_count DESC, entry_name;
-> vide

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_cpmm'
   AND ce.local_event_kind = de.event_kind
WHERE de.protocol_name = 'raydium_cpmm'
  AND ce.id IS NULL
GROUP BY de.event_kind
ORDER BY decoded_count DESC, de.event_kind;
->
raydium_cpmm.instruction_audit  3

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_cpmm'
   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_cpmm'
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;
-> vide