mirror of
https://github.com/0xfnzero/solana-streamer.git
synced 2026-08-19 03:48:04 +00:00
feat: Add Raydium AMM V4 support and enhance protocol parsers
- Add complete Raydium AMM V4 protocol integration - Enhance PumpFun, CLMM, CPMM event parsing - Refactor parser architecture for better extensibility - Update documentation and examples
This commit is contained in:
@@ -27,9 +27,92 @@ pub struct RaydiumCpmmSwapEvent {
|
||||
|
||||
impl_unified_event!(RaydiumCpmmSwapEvent,);
|
||||
|
||||
/// 存款
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct RaydiumCpmmDepositEvent {
|
||||
pub metadata: EventMetadata,
|
||||
pub lp_token_amount: u64,
|
||||
pub maximum_token0_amount: u64,
|
||||
pub maximum_token1_amount: u64,
|
||||
|
||||
pub owner: Pubkey,
|
||||
pub authority: Pubkey,
|
||||
pub pool_state: Pubkey,
|
||||
pub owner_lp_token: Pubkey,
|
||||
pub token0_account: Pubkey,
|
||||
pub token1_account: Pubkey,
|
||||
pub token0_vault: Pubkey,
|
||||
pub token1_vault: Pubkey,
|
||||
pub token_program: Pubkey,
|
||||
pub token_program2022: Pubkey,
|
||||
pub vault0_mint: Pubkey,
|
||||
pub vault1_mint: Pubkey,
|
||||
pub lp_mint: Pubkey,
|
||||
}
|
||||
impl_unified_event!(RaydiumCpmmDepositEvent,);
|
||||
|
||||
/// 初始化
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct RaydiumCpmmInitializeEvent {
|
||||
pub metadata: EventMetadata,
|
||||
pub init_amount0: u64,
|
||||
pub init_amount1: u64,
|
||||
pub open_time: u64,
|
||||
|
||||
pub creator: Pubkey,
|
||||
pub amm_config: Pubkey,
|
||||
pub authority: Pubkey,
|
||||
pub pool_state: Pubkey,
|
||||
pub token0_mint: Pubkey,
|
||||
pub token1_mint: Pubkey,
|
||||
pub lp_mint: Pubkey,
|
||||
pub creator_token0: Pubkey,
|
||||
pub creator_token1: Pubkey,
|
||||
pub creator_lp_token: Pubkey,
|
||||
pub token0_vault: Pubkey,
|
||||
pub token1_vault: Pubkey,
|
||||
pub create_pool_fee: Pubkey,
|
||||
pub observation_state: Pubkey,
|
||||
pub token_program: Pubkey,
|
||||
pub token0_program: Pubkey,
|
||||
pub token1_program: Pubkey,
|
||||
pub associated_token_program: Pubkey,
|
||||
pub system_program: Pubkey,
|
||||
pub rent: Pubkey,
|
||||
}
|
||||
impl_unified_event!(RaydiumCpmmInitializeEvent,);
|
||||
|
||||
/// 提款
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct RaydiumCpmmWithdrawEvent {
|
||||
pub metadata: EventMetadata,
|
||||
pub lp_token_amount: u64,
|
||||
pub minimum_token0_amount: u64,
|
||||
pub minimum_token1_amount: u64,
|
||||
|
||||
pub owner: Pubkey,
|
||||
pub authority: Pubkey,
|
||||
pub pool_state: Pubkey,
|
||||
pub owner_lp_token: Pubkey,
|
||||
pub token0_account: Pubkey,
|
||||
pub token1_account: Pubkey,
|
||||
pub token0_vault: Pubkey,
|
||||
pub token1_vault: Pubkey,
|
||||
pub token_program: Pubkey,
|
||||
pub token_program2022: Pubkey,
|
||||
pub vault0_mint: Pubkey,
|
||||
pub vault1_mint: Pubkey,
|
||||
pub lp_mint: Pubkey,
|
||||
pub memo_program: Pubkey,
|
||||
}
|
||||
impl_unified_event!(RaydiumCpmmWithdrawEvent,);
|
||||
|
||||
/// 事件鉴别器常量
|
||||
pub mod discriminators {
|
||||
// 指令鉴别器
|
||||
pub const SWAP_BASE_IN: &[u8] = &[143, 190, 90, 218, 196, 30, 51, 222];
|
||||
pub const SWAP_BASE_OUT: &[u8] = &[55, 217, 98, 86, 163, 74, 180, 173];
|
||||
pub const DEPOSIT: &[u8] = &[242, 35, 198, 137, 82, 225, 242, 182];
|
||||
pub const INITIALIZE: &[u8] = &[175, 175, 109, 31, 13, 152, 155, 237];
|
||||
pub const WITHDRAW: &[u8] = &[183, 18, 70, 156, 148, 109, 161, 34];
|
||||
}
|
||||
|
||||
@@ -7,7 +7,10 @@ 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},
|
||||
protocols::raydium_cpmm::{
|
||||
discriminators, RaydiumCpmmDepositEvent, RaydiumCpmmInitializeEvent, RaydiumCpmmSwapEvent,
|
||||
RaydiumCpmmWithdrawEvent,
|
||||
},
|
||||
};
|
||||
|
||||
/// Raydium CPMM程序ID
|
||||
@@ -35,7 +38,7 @@ impl RaydiumCpmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::SWAP_BASE_IN,
|
||||
event_type: EventType::RaydiumCpmmSwapBaseInput,
|
||||
inner_instruction_parser: Self::parse_trade_inner_instruction,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_swap_base_input_instruction,
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
@@ -44,9 +47,36 @@ impl RaydiumCpmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::SWAP_BASE_OUT,
|
||||
event_type: EventType::RaydiumCpmmSwapBaseOutput,
|
||||
inner_instruction_parser: Self::parse_trade_inner_instruction,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_swap_base_output_instruction,
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CPMM_PROGRAM_ID,
|
||||
protocol_type: ProtocolType::RaydiumCpmm,
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::DEPOSIT,
|
||||
event_type: EventType::RaydiumCpmmDeposit,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_deposit_instruction,
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CPMM_PROGRAM_ID,
|
||||
protocol_type: ProtocolType::RaydiumCpmm,
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::INITIALIZE,
|
||||
event_type: EventType::RaydiumCpmmInitialize,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_initialize_instruction,
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CPMM_PROGRAM_ID,
|
||||
protocol_type: ProtocolType::RaydiumCpmm,
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::WITHDRAW,
|
||||
event_type: EventType::RaydiumCpmmWithdraw,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_withdraw_instruction,
|
||||
},
|
||||
];
|
||||
|
||||
let inner = GenericEventParser::new(vec![RAYDIUM_CPMM_PROGRAM_ID], configs);
|
||||
@@ -54,14 +84,114 @@ impl RaydiumCpmmEventParser {
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
/// 解析交易事件
|
||||
fn parse_trade_inner_instruction(
|
||||
_data: &[u8],
|
||||
_metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
fn empty_parse(_data: &[u8], _metadata: EventMetadata) -> Option<Box<dyn UnifiedEvent>> {
|
||||
None
|
||||
}
|
||||
|
||||
/// 解析提款指令事件
|
||||
fn parse_withdraw_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
if data.len() < 24 || accounts.len() < 14 {
|
||||
return None;
|
||||
}
|
||||
let mut metadata = metadata;
|
||||
metadata.set_id(format!("{}-{}-{}", metadata.signature, accounts[0], accounts[1]));
|
||||
Some(Box::new(RaydiumCpmmWithdrawEvent {
|
||||
metadata,
|
||||
lp_token_amount: read_u64_le(data, 0)?,
|
||||
minimum_token0_amount: read_u64_le(data, 8)?,
|
||||
minimum_token1_amount: read_u64_le(data, 16)?,
|
||||
owner: accounts[0],
|
||||
authority: accounts[1],
|
||||
pool_state: accounts[2],
|
||||
owner_lp_token: accounts[3],
|
||||
token0_account: accounts[4],
|
||||
token1_account: accounts[5],
|
||||
token0_vault: accounts[6],
|
||||
token1_vault: accounts[7],
|
||||
token_program: accounts[8],
|
||||
token_program2022: accounts[9],
|
||||
vault0_mint: accounts[10],
|
||||
vault1_mint: accounts[11],
|
||||
lp_mint: accounts[12],
|
||||
memo_program: accounts[13],
|
||||
}))
|
||||
}
|
||||
|
||||
/// 解析初始化指令事件
|
||||
fn parse_initialize_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
if data.len() < 24 || accounts.len() < 20 {
|
||||
return None;
|
||||
}
|
||||
let mut metadata = metadata;
|
||||
metadata.set_id(format!("{}-{}-{}", metadata.signature, accounts[0], accounts[1]));
|
||||
Some(Box::new(RaydiumCpmmInitializeEvent {
|
||||
metadata,
|
||||
init_amount0: read_u64_le(data, 0)?,
|
||||
init_amount1: read_u64_le(data, 8)?,
|
||||
open_time: read_u64_le(data, 16)?,
|
||||
creator: accounts[0],
|
||||
amm_config: accounts[1],
|
||||
authority: accounts[2],
|
||||
pool_state: accounts[3],
|
||||
token0_mint: accounts[4],
|
||||
token1_mint: accounts[5],
|
||||
lp_mint: accounts[6],
|
||||
creator_token0: accounts[7],
|
||||
creator_token1: accounts[8],
|
||||
creator_lp_token: accounts[9],
|
||||
token0_vault: accounts[10],
|
||||
token1_vault: accounts[11],
|
||||
create_pool_fee: accounts[12],
|
||||
observation_state: accounts[13],
|
||||
token_program: accounts[14],
|
||||
token0_program: accounts[15],
|
||||
token1_program: accounts[16],
|
||||
associated_token_program: accounts[17],
|
||||
system_program: accounts[18],
|
||||
rent: accounts[19],
|
||||
}))
|
||||
}
|
||||
|
||||
/// 解析存款指令事件
|
||||
fn parse_deposit_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
if data.len() < 24 || accounts.len() < 13 {
|
||||
return None;
|
||||
}
|
||||
let mut metadata = metadata;
|
||||
metadata.set_id(format!("{}-{}-{}", metadata.signature, accounts[0], accounts[1]));
|
||||
Some(Box::new(RaydiumCpmmDepositEvent {
|
||||
metadata,
|
||||
lp_token_amount: read_u64_le(data, 0)?,
|
||||
maximum_token0_amount: read_u64_le(data, 8)?,
|
||||
maximum_token1_amount: read_u64_le(data, 16)?,
|
||||
owner: accounts[0],
|
||||
authority: accounts[1],
|
||||
pool_state: accounts[2],
|
||||
owner_lp_token: accounts[3],
|
||||
token0_account: accounts[4],
|
||||
token1_account: accounts[5],
|
||||
token0_vault: accounts[6],
|
||||
token1_vault: accounts[7],
|
||||
token_program: accounts[8],
|
||||
token_program2022: accounts[9],
|
||||
vault0_mint: accounts[10],
|
||||
vault1_mint: accounts[11],
|
||||
lp_mint: accounts[12],
|
||||
}))
|
||||
}
|
||||
|
||||
/// 解析买入指令事件
|
||||
fn parse_swap_base_input_instruction(
|
||||
data: &[u8],
|
||||
|
||||
Reference in New Issue
Block a user