-- file: validation_sql/SQL_VALIDATION_RAYDIUM_CLMM_0_7_50_PRE_R2.sql

-- Raydium CLMM 0.7.50-pre-r2 re-verification pack.
-- Goal: repeat the Launchpad-style closure checks on a dedicated CLMM
-- 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.

-- 01. Coverage detail.
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;
->
collect_personal_fee_event  event   fee k_sol_fee_events    upstream_git_mapped_unverified  raydium_clmm.collect_personal_fee_event a6ae69c051a15369    0   0   0
collect_protocol_fee_event  event   fee k_sol_fee_events    upstream_git_local_corpus_materialized  raydium_clmm.collect_protocol_fee_event ce57114f2d29d53d    4   4   0
config_change_event event   admin_config    k_sol_pool_admin_events upstream_git_local_corpus_materialized  raydium_clmm.config_change_event    f7bd07776a705f97    4   4   0
create_personal_position_event  event   position_open   k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.create_personal_position_event 641e57f9c4df9ace    26  23  0
decrease_liquidity_event    event   liquidity_remove    k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.decrease_liquidity_event   3ade563a44325538    16  16  0
increase_liquidity_event    event   liquidity_add   k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.increase_liquidity_event   314f69d420221e54    9   8   0
liquidity_calculate_event   event   liquidity_calculation   k_sol_dex_decoded_events_only   upstream_git_local_corpus_observed  raydium_clmm.liquidity_calculate_event  ed7094e63954b4a2    58  0   0
liquidity_change_event  event   liquidity_change    k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.liquidity_change_event 7ef0afce9e58996b    50  45  0
pool_created_event  event   pool_create k_sol_pool_lifecycle_events upstream_git_local_corpus_materialized  raydium_clmm.pool_created_event 195e4b2f7063353f    14  13  0
swap_event  event   swap    k_sol_dex_decoded_events_only   upstream_git_local_corpus_observed  raydium_clmm.swap_event 40c6cde8260871e2    1456    0   0
update_reward_infos_event   event   reward  k_sol_reward_events upstream_git_local_corpus_materialized  raydium_clmm.update_reward_infos_event  6d7fba4e724125ec    7   7   0
close_limit_order   instruction order_cancel    k_sol_orderbook_events  upstream_git_local_corpus_materialized  raydium_clmm.close_limit_order  4c7c800fd55725fa    24  22  0
close_position  instruction position_close  k_sol_dex_decoded_events_only   upstream_git_local_corpus_observed  raydium_clmm.close_position 7b86510031446262    16  0   0
close_protocol_position instruction position_close  k_sol_dex_decoded_events_only   upstream_git_local_corpus_observed  raydium_clmm.close_protocol_position    c975989055556cb2    3   0   0
collect_fund_fee    instruction fee k_sol_fee_events    upstream_git_local_corpus_materialized  raydium_clmm.collect_fund_fee   a78a4e95dfc2067e    5   3   0
collect_protocol_fee    instruction fee k_sol_fee_events    upstream_git_local_corpus_materialized  raydium_clmm.collect_protocol_fee   8888fcddc2427e59    11  11  0
collect_remaining_rewards   instruction reward  k_sol_reward_events upstream_git_local_corpus_materialized  raydium_clmm.collect_remaining_rewards  12eda6c52210d590    10  6   0
cpi_event   instruction cpi_transport   k_sol_dex_decoded_events_only   upstream_git_mapped_unverified  raydium_clmm.cpi_event  e445a52e51cb9a1d    0   0   0
create_amm_config   instruction admin_config    k_sol_pool_admin_events upstream_git_local_corpus_materialized  raydium_clmm.create_amm_config  8934edd4d7756c68    9   7   0
create_customizable_pool    instruction pool_create k_sol_pool_lifecycle_events upstream_git_local_corpus_materialized  raydium_clmm.create_customizable_pool   2b44d4a7592fa401    5   3   0
create_dynamic_fee_config   instruction admin_config    k_sol_pool_admin_events upstream_git_local_corpus_materialized  raydium_clmm.create_dynamic_fee_config  bd0eb5785576e33e    1   1   0
create_operation_account    instruction admin_config    k_sol_pool_admin_events upstream_git_local_corpus_materialized  raydium_clmm.create_operation_account   3f5794216d230868    4   1   0
create_pool instruction pool_create k_sol_pool_lifecycle_events upstream_git_local_corpus_materialized  raydium_clmm.create_pool    e992d18ecf6840bc    8   8   0
create_support_mint_associated  instruction account_create  k_sol_token_account_events  upstream_git_local_corpus_observed  raydium_clmm.create_support_mint_associated 11fb415c88f20ea9    5   0   0
decrease_limit_order    instruction order_cancel    k_sol_orderbook_events  upstream_git_local_corpus_materialized  raydium_clmm.decrease_limit_order   759d3c674231a300    24  22  0
decrease_liquidity  instruction liquidity_remove    k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.decrease_liquidity a026d06f685b2c01    16  1   0
decrease_liquidity_v2   instruction liquidity_remove    k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.decrease_liquidity_v2  3a7fbc3e4f52c460    25  23  0
increase_limit_order    instruction order_place k_sol_orderbook_events  upstream_git_local_corpus_materialized  raydium_clmm.increase_limit_order   b19059ecfaba7d63    6   6   0
increase_liquidity  instruction liquidity_add   k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.increase_liquidity 2e9cf3760dcdfbb2    6   2   0
increase_liquidity_v2   instruction liquidity_add   k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.increase_liquidity_v2  851d59df45eeb00a    7   6   0
initialize_reward   instruction pool_create k_sol_reward_events upstream_git_local_corpus_materialized  raydium_clmm.initialize_reward  5f87c0c4f281e644    7   4   0
open_limit_order    instruction order_place k_sol_orderbook_events  upstream_git_local_corpus_materialized  raydium_clmm.open_limit_order   9d20dab7471d1293    10  5   0
open_position   instruction position_open   k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.open_position  87802f4d0f98f031    9   1   0
open_position_v2    instruction position_open   k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.open_position_v2   4db84ad67056f1c7    11  7   0
open_position_with_token22_nft  instruction position_open   k_sol_liquidity_events  upstream_git_local_corpus_materialized  raydium_clmm.open_position_with_token22_nft 4dffae527d1dc92e    19  15  0
set_reward_params   instruction reward  k_sol_reward_events upstream_git_local_corpus_materialized  raydium_clmm.set_reward_params  7034a74b20c9d389    6   4   0
settle_limit_order  instruction settle_funds    k_sol_orderbook_events  upstream_git_local_corpus_materialized  raydium_clmm.settle_limit_order cd4e74215c691a60    9   9   0
swap    instruction swap    k_sol_trade_events  upstream_git_local_corpus_materialized  raydium_clmm.swap   f8c69e91e17587c8    1464    910 910
swap_router_base_in instruction swap    k_sol_dex_decoded_events_only   upstream_git_local_corpus_observed  raydium_clmm.swap_router_base_in    457d73daf5baf2c4    90  0   0
swap_v2 instruction swap    k_sol_trade_events  upstream_git_local_corpus_materialized  raydium_clmm.swap_v2    2b04ed0b1ac91e62    715 276 276
transfer_reward_owner   instruction reward  k_sol_reward_events upstream_git_local_corpus_materialized  raydium_clmm.transfer_reward_owner  07160c53f22b3079    7   5   0
update_amm_config   instruction admin_config    k_sol_pool_admin_events upstream_git_local_corpus_materialized  raydium_clmm.update_amm_config  313cae889a1c74c8    11  8   0
update_dynamic_fee_config   instruction admin_config    k_sol_pool_admin_events upstream_git_mapped_unverified  raydium_clmm.update_dynamic_fee_config  0707500802c784f0    0   0   0
update_operation_account    instruction admin_config    k_sol_pool_admin_events upstream_git_local_corpus_materialized  raydium_clmm.update_operation_account   7f467728bce33d07    4   3   0
update_pool_status  instruction admin_config    k_sol_pool_admin_events upstream_git_local_corpus_materialized  raydium_clmm.update_pool_status 82576c062ee0757b    6   4   0
update_reward_infos instruction reward  k_sol_reward_events upstream_git_local_corpus_materialized  raydium_clmm.update_reward_infos    a3ace0340b9a6adf    7   7   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_clmm'
GROUP BY decoder_code;
->
raydium_clmm    47  46  43  37  4204    1500    1186

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'
  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_clmm'
GROUP BY instruction_name, discriminator_hex
ORDER BY observed_count DESC, instruction_name, discriminator_hex;
->
raydium_clmm.swap   f8c69e91e17587c8    1464    1327
raydium_clmm.swap_v2    2b04ed0b1ac91e62    715 459
raydium_clmm.swap_router_base_in    457d73daf5baf2c4    90  88
raydium_clmm.close_limit_order  4c7c800fd55725fa    28  24
raydium_clmm.decrease_limit_order   759d3c674231a300    28  24
raydium_clmm.decrease_liquidity_v2  3a7fbc3e4f52c460    25  18
raydium_clmm.create_support_mint_associated 11fb415c88f20ea9    22  5
raydium_clmm.update_amm_config  313cae889a1c74c8    20  11
raydium_clmm.open_position_with_token22_nft 4dffae527d1dc92e    19  19
raydium_clmm.collect_fund_fee   a78a4e95dfc2067e    18  5
raydium_clmm.close_position 7b86510031446262    16  16
raydium_clmm.decrease_liquidity a026d06f685b2c01    16  16
raydium_clmm.create_amm_config  8934edd4d7756c68    12  9
raydium_clmm.collect_protocol_fee   8888fcddc2427e59    11  4
raydium_clmm.open_position_v2   4db84ad67056f1c7    11  11
raydium_clmm.collect_remaining_rewards  12eda6c52210d590    10  10
raydium_clmm.create_pool    e992d18ecf6840bc    10  10
raydium_clmm.open_limit_order   9d20dab7471d1293    10  10
raydium_clmm.open_position  87802f4d0f98f031    9   9
raydium_clmm.set_reward_params  7034a74b20c9d389    9   6
raydium_clmm.settle_limit_order cd4e74215c691a60    9   9
raydium_clmm.transfer_reward_owner  07160c53f22b3079    9   7
raydium_clmm.increase_liquidity_v2  851d59df45eeb00a    7   7
raydium_clmm.initialize_reward  5f87c0c4f281e644    7   7
raydium_clmm.update_reward_infos    a3ace0340b9a6adf    7   7
raydium_clmm.increase_limit_order   b19059ecfaba7d63    6   6
raydium_clmm.increase_liquidity 2e9cf3760dcdfbb2    6   6
raydium_clmm.update_pool_status 82576c062ee0757b    6   6
raydium_clmm.close_protocol_position    c975989055556cb2    5   3
raydium_clmm.create_customizable_pool   2b44d4a7592fa401    5   5
raydium_clmm.update_operation_account   7f467728bce33d07    5   4
raydium_clmm.create_dynamic_fee_config  bd0eb5785576e33e    4   1
raydium_clmm.create_operation_account   3f5794216d230868    4   4

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_clmm'
   AND ce.discriminator_hex = io.discriminator_hex
WHERE io.decoder_code = 'raydium_clmm'
  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_clmm'
GROUP BY de.event_kind
ORDER BY decoded_count DESC, de.event_kind;
->
raydium_clmm.swap   1464    0   0   0   0   0   0   0   0   910
raydium_clmm.swap_event 1456    0   0   0   0   0   0   0   0   0
raydium_clmm.swap_v2    715 0   0   0   0   0   0   0   0   276
raydium_clmm.swap_router_base_in    90  0   0   0   0   0   0   0   0   0
raydium_clmm.liquidity_calculate_event  58  0   0   0   0   0   0   0   0   0
raydium_clmm.liquidity_change_event 50  45  0   0   0   0   0   0   0   0
raydium_clmm.create_personal_position_event 26  23  0   0   0   0   0   0   0   0
raydium_clmm.decrease_liquidity_v2  25  23  0   0   0   0   0   0   0   0
raydium_clmm.close_limit_order  24  0   0   0   0   0   0   22  0   0
raydium_clmm.decrease_limit_order   24  0   0   0   0   0   0   22  0   0
raydium_clmm.open_position_with_token22_nft 19  15  0   0   0   0   0   0   0   0
raydium_clmm.close_position 16  7   0   0   0   0   0   0   0   0
raydium_clmm.decrease_liquidity 16  1   0   0   0   0   0   0   0   0
raydium_clmm.decrease_liquidity_event   16  16  0   0   0   0   0   0   0   0
raydium_clmm.pool_created_event 14  0   0   0   0   0   13  0   0   0
raydium_clmm.collect_protocol_fee   11  0   11  0   0   0   0   0   0   0
raydium_clmm.open_position_v2   11  7   0   0   0   0   0   0   0   0
raydium_clmm.update_amm_config  11  0   0   0   8   0   0   0   0   0
raydium_clmm.collect_remaining_rewards  10  0   0   6   0   0   0   0   0   0
raydium_clmm.open_limit_order   10  0   0   0   0   0   0   5   0   0
raydium_clmm.create_amm_config  9   0   0   0   7   0   0   0   0   0
raydium_clmm.increase_liquidity_event   9   8   0   0   0   0   0   0   0   0
raydium_clmm.open_position  9   1   0   0   0   0   0   0   0   0
raydium_clmm.settle_limit_order 9   0   0   0   0   0   0   9   0   0
raydium_clmm.create_pool    8   0   0   0   0   0   8   0   0   0
raydium_clmm.increase_liquidity_v2  7   6   0   0   0   0   0   0   0   0
raydium_clmm.initialize_reward  7   0   0   4   0   0   4   0   0   0
raydium_clmm.transfer_reward_owner  7   0   0   5   0   0   0   0   0   0
raydium_clmm.update_reward_infos    7   0   0   7   7   0   0   0   0   0
raydium_clmm.update_reward_infos_event  7   0   0   7   7   0   0   0   0   0
raydium_clmm.increase_limit_order   6   0   0   0   0   0   0   6   0   0
raydium_clmm.increase_liquidity 6   2   0   0   0   0   0   0   0   0
raydium_clmm.set_reward_params  6   0   0   4   4   0   0   0   0   0
raydium_clmm.update_pool_status 6   0   0   0   4   0   0   0   0   0
raydium_clmm.collect_fund_fee   5   0   3   0   0   0   0   0   0   0
raydium_clmm.create_customizable_pool   5   0   0   0   0   0   3   0   0   0
raydium_clmm.create_support_mint_associated 5   0   0   0   0   0   0   0   5   0
raydium_clmm.collect_protocol_fee_event 4   0   4   0   0   0   0   0   0   0
raydium_clmm.config_change_event    4   0   0   0   4   0   0   0   0   0
raydium_clmm.create_operation_account   4   0   0   0   1   0   0   0   0   0
raydium_clmm.update_operation_account   4   0   0   0   3   0   0   0   0   0
raydium_clmm.close_protocol_position    3   0   0   0   0   0   0   0   0   0
raydium_clmm.create_dynamic_fee_config  1   0   0   0   1   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_clmm'
   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_clmm'
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;
->
raydium_clmm.liquidity_change_event liquidity_change_event  event   liquidity_change    k_sol_liquidity_events  50  1   45  0   0   0   0   0   0   0   0
raydium_clmm.create_personal_position_event create_personal_position_event  event   position_open   k_sol_liquidity_events  26  0   23  0   0   0   0   0   0   0   0
raydium_clmm.decrease_liquidity decrease_liquidity  instruction liquidity_remove    k_sol_liquidity_events  16  5   1   0   0   0   0   0   0   0   0
raydium_clmm.open_position_v2   open_position_v2    instruction position_open   k_sol_liquidity_events  11  3   7   0   0   0   0   0   0   0   0
raydium_clmm.increase_liquidity_event   increase_liquidity_event    event   liquidity_add   k_sol_liquidity_events  9   0   8   0   0   0   0   0   0   0   0
raydium_clmm.open_position  open_position   instruction position_open   k_sol_liquidity_events  9   5   1   0   0   0   0   0   0   0   0
raydium_clmm.increase_liquidity increase_liquidity  instruction liquidity_add   k_sol_liquidity_events  6   3   2   0   0   0   0   0   0   0   0

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_clmm'
  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
-- 09. Residual instruction audit rows.
-- Expected: zero rows after full direct mapping.
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_clmm'
  AND event_kind = 'raydium_clmm.instruction_audit'
GROUP BY discriminator_hex
ORDER BY audit_count DESC, discriminator_hex;

-- 10. Redundant upstream fallback matches for locally covered entries.
-- Expected: zero rows.
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_clmm'
GROUP BY upstream_decoder_code, entry_name, discriminator_hex, source_repo
ORDER BY fallback_count DESC, entry_name;

-- 11. Decoded event kinds without a coverage entry.
-- Expected: zero rows, excluding explicit `.instruction_audit` if it remains by design.
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_clmm'
   AND ce.local_event_kind = de.event_kind
WHERE de.protocol_name = 'raydium_clmm'
  AND ce.id IS NULL
GROUP BY de.event_kind
ORDER BY decoded_count DESC, de.event_kind;

-- 12. Non-swap decoded events that created trades.
-- Expected: zero rows.
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_clmm'
   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_clmm'
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;
