mirror of
https://github.com/0xfnzero/solana-streamer.git
synced 2026-08-22 05:18:06 +00:00
feat: Add account event parser and protocol type definitions
- Add account event parser (account_event_parser.rs) for account-level event handling - Introduce type definitions for pumpfun, pumpswap, raydium_amm_v4, raydium_clmm, raydium_cpmm protocols - Enhance event processor to support account event processing alongside transactions - Improve subscription system with separate transaction and account filters - Optimize parser configuration using Option types for cleaner code structure - Update examples and documentation to reflect new capabilities Changes include: - 6 new types.rs files for protocol-specific data structures - Updated event processor for account, transaction, and block meta events - Refactored subscription manager with account filtering support - Enhanced metrics and batch processing logic
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
use crate::impl_unified_event;
|
||||
use crate::streaming::event_parser::common::EventMetadata;
|
||||
use crate::streaming::event_parser::protocols::raydium_cpmm::types::PoolState;
|
||||
use crate::{
|
||||
impl_unified_event, streaming::event_parser::protocols::raydium_cpmm::types::AmmConfig,
|
||||
};
|
||||
use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
@@ -107,6 +110,32 @@ pub struct RaydiumCpmmWithdrawEvent {
|
||||
}
|
||||
impl_unified_event!(RaydiumCpmmWithdrawEvent,);
|
||||
|
||||
/// 池配置
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct RaydiumCpmmAmmConfigAccountEvent {
|
||||
pub metadata: EventMetadata,
|
||||
pub pubkey: String,
|
||||
pub executable: bool,
|
||||
pub lamports: u64,
|
||||
pub owner: String,
|
||||
pub rent_epoch: u64,
|
||||
pub amm_config: AmmConfig,
|
||||
}
|
||||
impl_unified_event!(RaydiumCpmmAmmConfigAccountEvent,);
|
||||
|
||||
/// 池状态
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct RaydiumCpmmPoolStateAccountEvent {
|
||||
pub metadata: EventMetadata,
|
||||
pub pubkey: String,
|
||||
pub executable: bool,
|
||||
pub lamports: u64,
|
||||
pub owner: String,
|
||||
pub rent_epoch: u64,
|
||||
pub pool_state: PoolState,
|
||||
}
|
||||
impl_unified_event!(RaydiumCpmmPoolStateAccountEvent,);
|
||||
|
||||
/// 事件鉴别器常量
|
||||
pub mod discriminators {
|
||||
// 指令鉴别器
|
||||
@@ -115,4 +144,8 @@ pub mod discriminators {
|
||||
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];
|
||||
|
||||
// 账号鉴别器
|
||||
pub const AMM_CONFIG: &[u8] = &[218, 244, 33, 104, 203, 203, 43, 111];
|
||||
pub const POOL_STATE: &[u8] = &[247, 237, 227, 245, 215, 195, 222, 70];
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod events;
|
||||
pub mod parser;
|
||||
pub mod types;
|
||||
|
||||
pub use events::*;
|
||||
pub use parser::RaydiumCpmmEventParser;
|
||||
|
||||
@@ -38,8 +38,8 @@ impl RaydiumCpmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::SWAP_BASE_IN,
|
||||
event_type: EventType::RaydiumCpmmSwapBaseInput,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_swap_base_input_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_swap_base_input_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CPMM_PROGRAM_ID,
|
||||
@@ -47,8 +47,8 @@ impl RaydiumCpmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::SWAP_BASE_OUT,
|
||||
event_type: EventType::RaydiumCpmmSwapBaseOutput,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_swap_base_output_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_swap_base_output_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CPMM_PROGRAM_ID,
|
||||
@@ -56,8 +56,8 @@ impl RaydiumCpmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::DEPOSIT,
|
||||
event_type: EventType::RaydiumCpmmDeposit,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_deposit_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_deposit_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CPMM_PROGRAM_ID,
|
||||
@@ -65,8 +65,8 @@ impl RaydiumCpmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::INITIALIZE,
|
||||
event_type: EventType::RaydiumCpmmInitialize,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_initialize_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_initialize_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CPMM_PROGRAM_ID,
|
||||
@@ -74,8 +74,8 @@ impl RaydiumCpmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::WITHDRAW,
|
||||
event_type: EventType::RaydiumCpmmWithdraw,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_withdraw_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_withdraw_instruction),
|
||||
},
|
||||
];
|
||||
|
||||
@@ -84,10 +84,6 @@ impl RaydiumCpmmEventParser {
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
fn empty_parse(_data: &[u8], _metadata: EventMetadata) -> Option<Box<dyn UnifiedEvent>> {
|
||||
None
|
||||
}
|
||||
|
||||
/// 解析提款指令事件
|
||||
fn parse_withdraw_instruction(
|
||||
data: &[u8],
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
use crate::streaming::{
|
||||
event_parser::{
|
||||
common::EventMetadata,
|
||||
protocols::raydium_cpmm::{
|
||||
RaydiumCpmmAmmConfigAccountEvent, RaydiumCpmmPoolStateAccountEvent,
|
||||
},
|
||||
UnifiedEvent,
|
||||
},
|
||||
grpc::AccountPretty,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct AmmConfig {
|
||||
pub bump: u8,
|
||||
pub disable_create_pool: bool,
|
||||
pub index: u16,
|
||||
pub trade_fee_rate: u64,
|
||||
pub protocol_fee_rate: u64,
|
||||
pub fund_fee_rate: u64,
|
||||
pub create_pool_fee: u64,
|
||||
pub protocol_owner: Pubkey,
|
||||
pub fund_owner: Pubkey,
|
||||
pub padding: [u64; 16],
|
||||
}
|
||||
|
||||
pub const AMM_CONFIG_SIZE: usize = 228;
|
||||
|
||||
pub fn amm_config_decode(data: &[u8]) -> Option<AmmConfig> {
|
||||
borsh::from_slice::<AmmConfig>(&data).ok()
|
||||
}
|
||||
|
||||
pub fn amm_config_parser(
|
||||
account: &AccountPretty,
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
if account.data.len() < AMM_CONFIG_SIZE {
|
||||
return None;
|
||||
}
|
||||
if let Some(amm_config) = amm_config_decode(&account.data[8..AMM_CONFIG_SIZE + 8]) {
|
||||
Some(Box::new(RaydiumCpmmAmmConfigAccountEvent {
|
||||
metadata,
|
||||
pubkey: account.pubkey.to_string(),
|
||||
executable: account.executable,
|
||||
lamports: account.lamports,
|
||||
owner: account.owner.to_string(),
|
||||
rent_epoch: account.rent_epoch,
|
||||
amm_config: amm_config,
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct PoolState {
|
||||
pub amm_config: Pubkey,
|
||||
pub pool_creator: Pubkey,
|
||||
pub token0_vault: Pubkey,
|
||||
pub token1_vault: Pubkey,
|
||||
pub lp_mint: Pubkey,
|
||||
pub token0_mint: Pubkey,
|
||||
pub token1_mint: Pubkey,
|
||||
pub token0_program: Pubkey,
|
||||
pub token1_program: Pubkey,
|
||||
pub observation_key: Pubkey,
|
||||
pub auth_bump: u8,
|
||||
pub status: u8,
|
||||
pub lp_mint_decimals: u8,
|
||||
pub mint0_decimals: u8,
|
||||
pub mint1_decimals: u8,
|
||||
pub lp_supply: u64,
|
||||
pub protocol_fees_token0: u64,
|
||||
pub protocol_fees_token1: u64,
|
||||
pub fund_fees_token0: u64,
|
||||
pub fund_fees_token1: u64,
|
||||
pub open_time: u64,
|
||||
pub recent_epoch: u64,
|
||||
pub padding: [u64; 31],
|
||||
}
|
||||
|
||||
pub const POOL_STATE_SIZE: usize = 629;
|
||||
|
||||
pub fn pool_state_decode(data: &[u8]) -> Option<PoolState> {
|
||||
borsh::from_slice::<PoolState>(&data).ok()
|
||||
}
|
||||
|
||||
pub fn pool_state_parser(
|
||||
account: &AccountPretty,
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
if account.data.len() < POOL_STATE_SIZE {
|
||||
return None;
|
||||
}
|
||||
if let Some(pool_state) = pool_state_decode(&account.data[8..POOL_STATE_SIZE + 8]) {
|
||||
Some(Box::new(RaydiumCpmmPoolStateAccountEvent {
|
||||
metadata,
|
||||
pubkey: account.pubkey.to_string(),
|
||||
executable: account.executable,
|
||||
lamports: account.lamports,
|
||||
owner: account.owner.to_string(),
|
||||
rent_epoch: account.rent_epoch,
|
||||
pool_state: pool_state,
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user