2025-08-11 01:04:16 +08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
2025-07-25 15:32:05 +08:00
|
|
|
use prost_types::Timestamp;
|
2025-07-19 23:46:42 +08:00
|
|
|
use solana_sdk::{instruction::CompiledInstruction, pubkey::Pubkey};
|
|
|
|
|
use solana_transaction_status::UiCompiledInstruction;
|
|
|
|
|
|
|
|
|
|
use crate::streaming::event_parser::{
|
|
|
|
|
common::{read_u64_le, EventMetadata, EventType, ProtocolType},
|
|
|
|
|
core::traits::{EventParser, GenericEventParseConfig, GenericEventParser, UnifiedEvent},
|
|
|
|
|
protocols::raydium_cpmm::{discriminators, RaydiumCpmmSwapEvent},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Raydium CPMM程序ID
|
|
|
|
|
pub const RAYDIUM_CPMM_PROGRAM_ID: Pubkey =
|
|
|
|
|
solana_sdk::pubkey!("CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C");
|
|
|
|
|
|
|
|
|
|
/// Raydium CPMM事件解析器
|
|
|
|
|
pub struct RaydiumCpmmEventParser {
|
|
|
|
|
inner: GenericEventParser,
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 05:37:04 +08:00
|
|
|
impl Default for RaydiumCpmmEventParser {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 23:46:42 +08:00
|
|
|
impl RaydiumCpmmEventParser {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
// 配置所有事件类型
|
|
|
|
|
let configs = vec![
|
|
|
|
|
GenericEventParseConfig {
|
2025-08-11 01:04:16 +08:00
|
|
|
program_id: RAYDIUM_CPMM_PROGRAM_ID,
|
|
|
|
|
protocol_type: ProtocolType::RaydiumCpmm,
|
2025-07-19 23:46:42 +08:00
|
|
|
inner_instruction_discriminator: "",
|
|
|
|
|
instruction_discriminator: discriminators::SWAP_BASE_IN,
|
|
|
|
|
event_type: EventType::RaydiumCpmmSwapBaseInput,
|
|
|
|
|
inner_instruction_parser: Self::parse_trade_inner_instruction,
|
|
|
|
|
instruction_parser: Self::parse_swap_base_input_instruction,
|
|
|
|
|
},
|
|
|
|
|
GenericEventParseConfig {
|
2025-08-11 01:04:16 +08:00
|
|
|
program_id: RAYDIUM_CPMM_PROGRAM_ID,
|
|
|
|
|
protocol_type: ProtocolType::RaydiumCpmm,
|
2025-07-19 23:46:42 +08:00
|
|
|
inner_instruction_discriminator: "",
|
|
|
|
|
instruction_discriminator: discriminators::SWAP_BASE_OUT,
|
|
|
|
|
event_type: EventType::RaydiumCpmmSwapBaseOutput,
|
|
|
|
|
inner_instruction_parser: Self::parse_trade_inner_instruction,
|
|
|
|
|
instruction_parser: Self::parse_swap_base_output_instruction,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2025-08-11 01:04:16 +08:00
|
|
|
let inner = GenericEventParser::new(vec![RAYDIUM_CPMM_PROGRAM_ID], configs);
|
2025-07-19 23:46:42 +08:00
|
|
|
|
|
|
|
|
Self { inner }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 解析交易事件
|
|
|
|
|
fn parse_trade_inner_instruction(
|
|
|
|
|
_data: &[u8],
|
|
|
|
|
_metadata: EventMetadata,
|
|
|
|
|
) -> Option<Box<dyn UnifiedEvent>> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 解析买入指令事件
|
|
|
|
|
fn parse_swap_base_input_instruction(
|
|
|
|
|
data: &[u8],
|
|
|
|
|
accounts: &[Pubkey],
|
|
|
|
|
metadata: EventMetadata,
|
|
|
|
|
) -> Option<Box<dyn UnifiedEvent>> {
|
|
|
|
|
if data.len() < 16 || accounts.len() < 13 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let amount_in = read_u64_le(data, 0)?;
|
|
|
|
|
let minimum_amount_out = read_u64_le(data, 8)?;
|
|
|
|
|
|
|
|
|
|
let mut metadata = metadata;
|
|
|
|
|
metadata.set_id(format!(
|
|
|
|
|
"{}-{}-{}-{}",
|
|
|
|
|
metadata.signature, accounts[3], accounts[10], accounts[11]
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
Some(Box::new(RaydiumCpmmSwapEvent {
|
|
|
|
|
metadata,
|
|
|
|
|
amount_in,
|
|
|
|
|
minimum_amount_out,
|
|
|
|
|
payer: accounts[0],
|
|
|
|
|
authority: accounts[1],
|
|
|
|
|
amm_config: accounts[2],
|
|
|
|
|
pool_state: accounts[3],
|
|
|
|
|
input_token_account: accounts[4],
|
|
|
|
|
output_token_account: accounts[5],
|
|
|
|
|
input_vault: accounts[6],
|
|
|
|
|
output_vault: accounts[7],
|
|
|
|
|
input_token_mint: accounts[10],
|
|
|
|
|
output_token_mint: accounts[11],
|
|
|
|
|
observation_state: accounts[12],
|
|
|
|
|
..Default::default()
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_swap_base_output_instruction(
|
|
|
|
|
data: &[u8],
|
|
|
|
|
accounts: &[Pubkey],
|
|
|
|
|
metadata: EventMetadata,
|
|
|
|
|
) -> Option<Box<dyn UnifiedEvent>> {
|
|
|
|
|
if data.len() < 16 || accounts.len() < 13 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let max_amount_in = read_u64_le(data, 0)?;
|
|
|
|
|
let amount_out = read_u64_le(data, 8)?;
|
|
|
|
|
|
|
|
|
|
let mut metadata = metadata;
|
|
|
|
|
metadata.set_id(format!(
|
|
|
|
|
"{}-{}-{}-{}",
|
|
|
|
|
metadata.signature, accounts[3], accounts[10], accounts[11]
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
Some(Box::new(RaydiumCpmmSwapEvent {
|
|
|
|
|
metadata,
|
|
|
|
|
max_amount_in,
|
|
|
|
|
amount_out,
|
|
|
|
|
payer: accounts[0],
|
|
|
|
|
authority: accounts[1],
|
|
|
|
|
amm_config: accounts[2],
|
|
|
|
|
pool_state: accounts[3],
|
|
|
|
|
input_token_account: accounts[4],
|
|
|
|
|
output_token_account: accounts[5],
|
|
|
|
|
input_vault: accounts[6],
|
|
|
|
|
output_vault: accounts[7],
|
|
|
|
|
input_token_mint: accounts[10],
|
|
|
|
|
output_token_mint: accounts[11],
|
|
|
|
|
observation_state: accounts[12],
|
|
|
|
|
..Default::default()
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
|
impl EventParser for RaydiumCpmmEventParser {
|
2025-08-11 01:04:16 +08:00
|
|
|
fn inner_instruction_configs(&self) -> HashMap<&'static str, Vec<GenericEventParseConfig>> {
|
|
|
|
|
self.inner.inner_instruction_configs()
|
|
|
|
|
}
|
|
|
|
|
fn instruction_configs(&self) -> HashMap<Vec<u8>, Vec<GenericEventParseConfig>> {
|
|
|
|
|
self.inner.instruction_configs()
|
|
|
|
|
}
|
2025-07-19 23:46:42 +08:00
|
|
|
fn parse_events_from_inner_instruction(
|
|
|
|
|
&self,
|
|
|
|
|
inner_instruction: &UiCompiledInstruction,
|
|
|
|
|
signature: &str,
|
|
|
|
|
slot: u64,
|
2025-07-25 15:32:05 +08:00
|
|
|
block_time: Option<Timestamp>,
|
2025-07-29 14:48:43 +08:00
|
|
|
program_received_time_ms: i64,
|
2025-07-26 17:11:28 +08:00
|
|
|
index: String,
|
2025-07-19 23:46:42 +08:00
|
|
|
) -> Vec<Box<dyn UnifiedEvent>> {
|
2025-07-26 17:11:28 +08:00
|
|
|
self.inner.parse_events_from_inner_instruction(
|
|
|
|
|
inner_instruction,
|
|
|
|
|
signature,
|
|
|
|
|
slot,
|
|
|
|
|
block_time,
|
2025-07-29 14:48:43 +08:00
|
|
|
program_received_time_ms,
|
2025-07-26 17:11:28 +08:00
|
|
|
index,
|
|
|
|
|
)
|
2025-07-19 23:46:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_events_from_instruction(
|
|
|
|
|
&self,
|
|
|
|
|
instruction: &CompiledInstruction,
|
|
|
|
|
accounts: &[Pubkey],
|
|
|
|
|
signature: &str,
|
|
|
|
|
slot: u64,
|
2025-07-25 15:32:05 +08:00
|
|
|
block_time: Option<Timestamp>,
|
2025-07-29 14:48:43 +08:00
|
|
|
program_received_time_ms: i64,
|
2025-07-26 17:11:28 +08:00
|
|
|
index: String,
|
2025-07-19 23:46:42 +08:00
|
|
|
) -> Vec<Box<dyn UnifiedEvent>> {
|
2025-07-26 17:11:28 +08:00
|
|
|
self.inner.parse_events_from_instruction(
|
|
|
|
|
instruction,
|
|
|
|
|
accounts,
|
|
|
|
|
signature,
|
|
|
|
|
slot,
|
|
|
|
|
block_time,
|
2025-07-29 14:48:43 +08:00
|
|
|
program_received_time_ms,
|
2025-07-26 17:11:28 +08:00
|
|
|
index,
|
|
|
|
|
)
|
2025-07-19 23:46:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn should_handle(&self, program_id: &Pubkey) -> bool {
|
|
|
|
|
self.inner.should_handle(program_id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn supported_program_ids(&self) -> Vec<Pubkey> {
|
|
|
|
|
self.inner.supported_program_ids()
|
|
|
|
|
}
|
|
|
|
|
}
|