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_u128_le, read_u64_le, read_u8_le, EventMetadata, EventType, ProtocolType},
|
|
|
|
|
core::traits::{EventParser, GenericEventParseConfig, GenericEventParser, UnifiedEvent},
|
|
|
|
|
protocols::raydium_clmm::{discriminators, RaydiumClmmSwapEvent, RaydiumClmmSwapV2Event},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Raydium CLMM程序ID
|
|
|
|
|
pub const RAYDIUM_CLMM_PROGRAM_ID: Pubkey =
|
|
|
|
|
solana_sdk::pubkey!("CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK");
|
|
|
|
|
|
|
|
|
|
/// Raydium CLMM事件解析器
|
|
|
|
|
pub struct RaydiumClmmEventParser {
|
|
|
|
|
inner: GenericEventParser,
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 05:37:04 +08:00
|
|
|
impl Default for RaydiumClmmEventParser {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 23:46:42 +08:00
|
|
|
impl RaydiumClmmEventParser {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
// 配置所有事件类型
|
|
|
|
|
let configs = vec![
|
|
|
|
|
GenericEventParseConfig {
|
2025-08-11 01:04:16 +08:00
|
|
|
program_id: RAYDIUM_CLMM_PROGRAM_ID,
|
|
|
|
|
protocol_type: ProtocolType::RaydiumClmm,
|
2025-07-19 23:46:42 +08:00
|
|
|
inner_instruction_discriminator: "",
|
|
|
|
|
instruction_discriminator: discriminators::SWAP,
|
|
|
|
|
event_type: EventType::RaydiumClmmSwap,
|
|
|
|
|
inner_instruction_parser: Self::parse_trade_inner_instruction,
|
|
|
|
|
instruction_parser: Self::parse_swap_instruction,
|
|
|
|
|
},
|
|
|
|
|
GenericEventParseConfig {
|
2025-08-11 01:04:16 +08:00
|
|
|
program_id: RAYDIUM_CLMM_PROGRAM_ID,
|
|
|
|
|
protocol_type: ProtocolType::RaydiumClmm,
|
2025-07-19 23:46:42 +08:00
|
|
|
inner_instruction_discriminator: "",
|
|
|
|
|
instruction_discriminator: discriminators::SWAP_V2,
|
|
|
|
|
event_type: EventType::RaydiumClmmSwapV2,
|
|
|
|
|
inner_instruction_parser: Self::parse_trade_inner_instruction,
|
|
|
|
|
instruction_parser: Self::parse_swap_v2_instruction,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2025-08-11 01:04:16 +08:00
|
|
|
let inner = GenericEventParser::new(vec![RAYDIUM_CLMM_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_instruction(
|
|
|
|
|
data: &[u8],
|
|
|
|
|
accounts: &[Pubkey],
|
|
|
|
|
metadata: EventMetadata,
|
|
|
|
|
) -> Option<Box<dyn UnifiedEvent>> {
|
|
|
|
|
if data.len() < 16 || accounts.len() < 10 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let amount = read_u64_le(data, 0)?;
|
|
|
|
|
let other_amount_threshold = read_u64_le(data, 8)?;
|
|
|
|
|
let sqrt_price_limit_x64 = read_u128_le(data, 16)?;
|
|
|
|
|
let is_base_input = read_u8_le(data, 32)?;
|
|
|
|
|
|
|
|
|
|
let mut metadata = metadata;
|
|
|
|
|
metadata.set_id(format!(
|
|
|
|
|
"{}-{}-{}-{}",
|
|
|
|
|
metadata.signature, accounts[2], accounts[3], accounts[4]
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
Some(Box::new(RaydiumClmmSwapEvent {
|
|
|
|
|
metadata,
|
|
|
|
|
amount,
|
|
|
|
|
other_amount_threshold,
|
|
|
|
|
sqrt_price_limit_x64,
|
|
|
|
|
is_base_input: is_base_input == 1,
|
|
|
|
|
payer: accounts[0],
|
|
|
|
|
amm_config: accounts[1],
|
|
|
|
|
pool_state: accounts[2],
|
|
|
|
|
input_token_account: accounts[3],
|
|
|
|
|
output_token_account: accounts[4],
|
|
|
|
|
input_vault: accounts[5],
|
|
|
|
|
output_vault: accounts[6],
|
|
|
|
|
observation_state: accounts[7],
|
|
|
|
|
token_program: accounts[8],
|
|
|
|
|
tick_array: accounts[9],
|
|
|
|
|
remaining_accounts: accounts[10..].to_vec(),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_swap_v2_instruction(
|
|
|
|
|
data: &[u8],
|
|
|
|
|
accounts: &[Pubkey],
|
|
|
|
|
metadata: EventMetadata,
|
|
|
|
|
) -> Option<Box<dyn UnifiedEvent>> {
|
|
|
|
|
if data.len() < 16 || accounts.len() < 13 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let amount = read_u64_le(data, 0)?;
|
|
|
|
|
let other_amount_threshold = read_u64_le(data, 8)?;
|
|
|
|
|
let sqrt_price_limit_x64 = read_u128_le(data, 16)?;
|
|
|
|
|
let is_base_input = read_u8_le(data, 32)?;
|
|
|
|
|
|
|
|
|
|
let mut metadata = metadata;
|
|
|
|
|
metadata.set_id(format!(
|
|
|
|
|
"{}-{}-{}-{}",
|
|
|
|
|
metadata.signature, accounts[2], accounts[3], accounts[4]
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
Some(Box::new(RaydiumClmmSwapV2Event {
|
|
|
|
|
metadata,
|
|
|
|
|
amount,
|
|
|
|
|
other_amount_threshold,
|
|
|
|
|
sqrt_price_limit_x64,
|
|
|
|
|
is_base_input: is_base_input == 1,
|
|
|
|
|
payer: accounts[0],
|
|
|
|
|
amm_config: accounts[1],
|
|
|
|
|
pool_state: accounts[2],
|
|
|
|
|
input_token_account: accounts[3],
|
|
|
|
|
output_token_account: accounts[4],
|
|
|
|
|
input_vault: accounts[5],
|
|
|
|
|
output_vault: accounts[6],
|
|
|
|
|
observation_state: accounts[7],
|
|
|
|
|
token_program: accounts[8],
|
|
|
|
|
token_program2022: accounts[9],
|
|
|
|
|
memo_program: accounts[10],
|
|
|
|
|
input_vault_mint: accounts[11],
|
|
|
|
|
output_vault_mint: accounts[12],
|
|
|
|
|
remaining_accounts: accounts[13..].to_vec(),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
|
impl EventParser for RaydiumClmmEventParser {
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|