From b281a4be52d550273ed0c9ce91110198e6e30fbc Mon Sep 17 00:00:00 2001 From: Wood Date: Sat, 7 Mar 2026 11:45:40 +0800 Subject: [PATCH] idl: sync pump_amm; parser account comments; low-latency: sequential inner parse (no thread::scope) Made-with: Cursor --- idl/pump_amm.json | 2 +- .../event_parser/core/event_parser.rs | 78 +++++++------------ .../event_parser/protocols/pumpfun/parser.rs | 33 ++++++-- .../event_parser/protocols/pumpswap/parser.rs | 18 ++++- 4 files changed, 74 insertions(+), 57 deletions(-) diff --git a/idl/pump_amm.json b/idl/pump_amm.json index 47d7b95..372cf2a 100644 --- a/idl/pump_amm.json +++ b/idl/pump_amm.json @@ -4835,7 +4835,7 @@ }, { "code": 6052, - "name": "CashbackEarnedDoesNotMatchTokenInVault" + "name": "TokensInVaultLessThanCashbackEarned" } ], "types": [ diff --git a/src/streaming/event_parser/core/event_parser.rs b/src/streaming/event_parser/core/event_parser.rs index 848c20a..93a8d4e 100644 --- a/src/streaming/event_parser/core/event_parser.rs +++ b/src/streaming/event_parser/core/event_parser.rs @@ -390,59 +390,41 @@ impl EventParser { // 处理 inner instructions - 查找对应的 CPI log 进行 merge // 当 inner_index 有值时,只查找索引大于当前 inner_index 的 CPI log + // 超低延迟:顺序执行,避免 thread::scope 的 spawn/join 开销 let mut inner_instruction_event: Option = None; if let Some(inner_instructions_ref) = inner_instructions { let current_inner_idx = inner_index.unwrap_or(-1) as i32; - - // 并行执行两个任务: 解析 inner event 和提取 swap_data - let (inner_event_result, swap_data_result) = std::thread::scope(|s| { - let inner_event_handle = s.spawn(|| { - for (idx, inner_instruction) in inner_instructions_ref.instructions.iter().enumerate() { - // 只查找索引大于当前 inner_index 的 CPI log - if (idx as i32) <= current_inner_idx { - continue; - } - - let inner_data = &inner_instruction.data; - // 检查长度(需要 16 字节的 discriminator) - if inner_data.len() < 16 { - continue; - } - let inner_discriminator = &inner_data[..16]; - let inner_instruction_data = &inner_data[16..]; - if let Some(inner_event) = EventDispatcher::dispatch_inner_instruction( - protocol.clone(), - inner_discriminator, - inner_instruction_data, - metadata.clone(), - ) { - return Some(inner_event); - } - } - None - }); + for (idx, inner_instruction) in inner_instructions_ref.instructions.iter().enumerate() { + if (idx as i32) <= current_inner_idx { + continue; + } + let inner_data = &inner_instruction.data; + if inner_data.len() < 16 { + continue; + } + let inner_discriminator = &inner_data[..16]; + let inner_instruction_data = &inner_data[16..]; + if let Some(inner_event) = EventDispatcher::dispatch_inner_instruction( + protocol.clone(), + inner_discriminator, + inner_instruction_data, + metadata.clone(), + ) { + inner_instruction_event = Some(inner_event); + break; + } + } - let swap_data_handle = s.spawn(|| { - if event.metadata().swap_data.is_none() { - parse_swap_data_from_next_grpc_instructions( - &event, - inner_instructions_ref, - current_inner_idx as i8, - accounts, - ) - } else { - None - } - }); - - // 等待两个任务完成 - (inner_event_handle.join().unwrap(), swap_data_handle.join().unwrap()) - }); - - inner_instruction_event = inner_event_result; - if let Some(swap_data) = swap_data_result { - event.metadata_mut().set_swap_data(swap_data); + if event.metadata().swap_data.is_none() { + if let Some(swap_data) = parse_swap_data_from_next_grpc_instructions( + &event, + inner_instructions_ref, + current_inner_idx as i8, + accounts, + ) { + event.metadata_mut().set_swap_data(swap_data); + } } } diff --git a/src/streaming/event_parser/protocols/pumpfun/parser.rs b/src/streaming/event_parser/protocols/pumpfun/parser.rs index 78058bb..8f4adaa 100755 --- a/src/streaming/event_parser/protocols/pumpfun/parser.rs +++ b/src/streaming/event_parser/protocols/pumpfun/parser.rs @@ -111,6 +111,9 @@ fn parse_trade_inner_instruction(data: &[u8], metadata: EventMetadata) -> Option } /// 解析创建代币指令事件 +/// 账户: 0: mint, 1: mint_authority, 2: bonding_curve, 3: associated_bonding_curve, 4: global, +/// 5: mpl_token_metadata, 6: metadata_account, 7: user, 8: system_program, 9: token_program, +/// 10: associated_token_program, 11: rent, 12: event_authority, 13: program. fn parse_create_token_instruction( data: &[u8], accounts: &[Pubkey], @@ -183,6 +186,9 @@ fn parse_create_token_instruction( } /// 解析创建 V2 代币指令事件 (SPL-22 Token, Mayhem Mode) +/// 账户: 0: mint, 1: mint_authority, 2: bonding_curve, 3: associated_bonding_curve, 4: global, +/// 5: user, 6: system_program, 7: token_program, 8: associated_token_program, 9: mayhem_program_id, +/// 10: global_params, 11: sol_vault, 12: mayhem_state, 13: mayhem_token_vault, 14: event_authority, 15: program. fn parse_create_v2_token_instruction( data: &[u8], accounts: &[Pubkey], @@ -256,7 +262,12 @@ fn parse_create_v2_token_instruction( })) } -// 解析买入指令事件 +/// 解析买入指令事件 +/// Buy 指令共 16 个固定账户(与 idl/pumpfun.json 一致): +/// 0: global, 1: fee_recipient, 2: mint, 3: bonding_curve, 4: associated_bonding_curve, +/// 5: associated_user, 6: user, 7: system_program, 8: token_program, 9: creator_vault, +/// 10: event_authority, 11: program, 12: global_volume_accumulator, 13: user_volume_accumulator, +/// 14: fee_config, 15: fee_program. fn parse_buy_instruction( data: &[u8], accounts: &[Pubkey], @@ -295,9 +306,11 @@ fn parse_buy_instruction( } /// 解析 buy_exact_sol_in 指令事件 -/// 注意:参数顺序与 buy 指令不同 -/// buy_exact_sol_in: spendable_sol_in (SOL), min_tokens_out (token) -/// buy: amount (token), max_sol_cost (SOL) +/// 账户布局与 buy 相同,共 16 个固定账户: 0: global, 1: fee_recipient, 2: mint, 3: bonding_curve, +/// 4: associated_bonding_curve, 5: associated_user, 6: user, 7: system_program, 8: token_program, +/// 9: creator_vault, 10: event_authority, 11: program, 12: global_volume_accumulator, +/// 13: user_volume_accumulator, 14: fee_config, 15: fee_program. +/// 参数顺序与 buy 不同: spendable_sol_in (SOL), min_tokens_out (token). fn parse_buy_exact_sol_in_instruction( data: &[u8], accounts: &[Pubkey], mut metadata: EventMetadata, @@ -337,7 +350,12 @@ fn parse_buy_exact_sol_in_instruction( })) } -// 解析卖出指令事件 +/// 解析卖出指令事件 +/// Sell 指令共 14 个固定账户(与 idl/pumpfun.json 一致): +/// 0: global, 1: fee_recipient, 2: mint, 3: bonding_curve, 4: associated_bonding_curve, +/// 5: associated_user, 6: user, 7: system_program, 8: creator_vault, 9: token_program, +/// 10: event_authority, 11: program, 12: fee_config, 13: fee_program. +/// remaining_accounts 可能含 user_volume_accumulator(返现)等。 fn parse_sell_instruction( data: &[u8], accounts: &[Pubkey], @@ -376,6 +394,11 @@ fn parse_sell_instruction( } /// 解析迁移指令事件 +/// 共 24 个固定账户: 0: global, 1: withdraw_authority, 2: mint, 3: bonding_curve, 4: associated_bonding_curve, +/// 5: user, 6: system_program, 7: token_program, 8: pump_amm, 9: pool, 10: pool_authority, +/// 11: pool_authority_mint_account, 12: pool_authority_wsol_account, 13: amm_global_config, 14: wsol_mint, +/// 15: lp_mint, 16: user_pool_token_account, 17: pool_base_token_account, 18: pool_quote_token_account, +/// 19: token_2022_program, 20: associated_token_program, 21: pump_amm_event_authority, 22: event_authority, 23: program. fn parse_migrate_instruction( _data: &[u8], accounts: &[Pubkey], diff --git a/src/streaming/event_parser/protocols/pumpswap/parser.rs b/src/streaming/event_parser/protocols/pumpswap/parser.rs index d330475..21400b0 100755 --- a/src/streaming/event_parser/protocols/pumpswap/parser.rs +++ b/src/streaming/event_parser/protocols/pumpswap/parser.rs @@ -130,6 +130,13 @@ fn parse_withdraw_inner_instruction(data: &[u8], metadata: EventMetadata) -> Opt } /// 解析买入指令事件 +/// Buy 指令共 23 个固定账户(与 idl/pump_amm.json 一致): +/// 0: pool, 1: user, 2: global_config, 3: base_mint, 4: quote_mint, 5: user_base_token_account, +/// 6: user_quote_token_account, 7: pool_base_token_account, 8: pool_quote_token_account, +/// 9: protocol_fee_recipient, 10: protocol_fee_recipient_token_account, 11: base_token_program, +/// 12: quote_token_program, 13: system_program, 14: associated_token_program, 15: event_authority, +/// 16: program, 17: coin_creator_vault_ata, 18: coin_creator_vault_authority, +/// 19: global_volume_accumulator, 20: user_volume_accumulator, 21: fee_config, 22: fee_program. fn parse_buy_instruction( data: &[u8], accounts: &[Pubkey], @@ -167,9 +174,8 @@ fn parse_buy_instruction( } /// 解析 buy_exact_quote_in 指令事件 -/// 注意:参数顺序与 buy 指令不同 -/// buy_exact_quote_in: spendable_quote_in (SOL), min_base_amount_out (token) -/// buy: base_amount_out (token), max_quote_amount_in (SOL) +/// 账户布局与 buy 相同,共 23 个固定账户(0–22,17/18 为 coin_creator_vault_ata / coin_creator_vault_authority)。 +/// 参数顺序与 buy 不同: spendable_quote_in (SOL), min_base_amount_out (token). fn parse_buy_exact_quote_in_instruction( data: &[u8], accounts: &[Pubkey], @@ -208,6 +214,12 @@ fn parse_buy_exact_quote_in_instruction( } /// 解析卖出指令事件 +/// Sell 指令共 21 个固定账户(与 idl/pump_amm.json 一致): +/// 0: pool, 1: user, 2: global_config, 3: base_mint, 4: quote_mint, 5: user_base_token_account, +/// 6: user_quote_token_account, 7: pool_base_token_account, 8: pool_quote_token_account, +/// 9: protocol_fee_recipient, 10: protocol_fee_recipient_token_account, 11: base_token_program, +/// 12: quote_token_program, 13: system_program, 14: associated_token_program, 15: event_authority, +/// 16: program, 17: coin_creator_vault_ata, 18: coin_creator_vault_authority, 19: fee_config, 20: fee_program. fn parse_sell_instruction( data: &[u8], accounts: &[Pubkey],