mirror of
https://github.com/0xfnzero/solana-streamer.git
synced 2026-08-21 21:08:07 +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,6 +1,8 @@
|
||||
use crate::impl_unified_event;
|
||||
use crate::streaming::event_parser::common::EventMetadata;
|
||||
// use borsh::BorshDeserialize;
|
||||
use crate::streaming::event_parser::protocols::raydium_clmm::types::{PoolState, TickArrayState};
|
||||
use crate::{
|
||||
impl_unified_event, streaming::event_parser::protocols::raydium_clmm::types::AmmConfig,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
@@ -217,6 +219,45 @@ pub struct RaydiumClmmOpenPositionV2Event {
|
||||
}
|
||||
impl_unified_event!(RaydiumClmmOpenPositionV2Event,);
|
||||
|
||||
/// 池配置
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RaydiumClmmAmmConfigAccountEvent {
|
||||
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!(RaydiumClmmAmmConfigAccountEvent,);
|
||||
|
||||
/// 池状态
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RaydiumClmmPoolStateAccountEvent {
|
||||
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!(RaydiumClmmPoolStateAccountEvent,);
|
||||
|
||||
/// 池状态
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RaydiumClmmTickArrayStateAccountEvent {
|
||||
pub metadata: EventMetadata,
|
||||
pub pubkey: String,
|
||||
pub executable: bool,
|
||||
pub lamports: u64,
|
||||
pub owner: String,
|
||||
pub rent_epoch: u64,
|
||||
pub tick_array_state: TickArrayState,
|
||||
}
|
||||
impl_unified_event!(RaydiumClmmTickArrayStateAccountEvent,);
|
||||
|
||||
/// 事件鉴别器常量
|
||||
pub mod discriminators {
|
||||
// 指令鉴别器
|
||||
@@ -228,4 +269,9 @@ pub mod discriminators {
|
||||
pub const CREATE_POOL: &[u8] = &[233, 146, 209, 142, 207, 104, 64, 188];
|
||||
pub const OPEN_POSITION_WITH_TOKEN_22_NFT: &[u8] = &[77, 255, 174, 82, 125, 29, 201, 46];
|
||||
pub const OPEN_POSITION_V2: &[u8] = &[77, 184, 74, 214, 112, 86, 241, 199];
|
||||
|
||||
// 账号鉴别器
|
||||
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];
|
||||
pub const TICK_ARRAY_STATE: &[u8] = &[192, 155, 85, 205, 49, 249, 129, 42];
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod events;
|
||||
pub mod parser;
|
||||
pub mod types;
|
||||
|
||||
pub use events::*;
|
||||
pub use parser::RaydiumClmmEventParser;
|
||||
|
||||
@@ -43,8 +43,8 @@ impl RaydiumClmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::SWAP,
|
||||
event_type: EventType::RaydiumClmmSwap,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_swap_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_swap_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CLMM_PROGRAM_ID,
|
||||
@@ -52,8 +52,8 @@ impl RaydiumClmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::SWAP_V2,
|
||||
event_type: EventType::RaydiumClmmSwapV2,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_swap_v2_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_swap_v2_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CLMM_PROGRAM_ID,
|
||||
@@ -61,8 +61,8 @@ impl RaydiumClmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::CLOSE_POSITION,
|
||||
event_type: EventType::RaydiumClmmClosePosition,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_close_position_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_close_position_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CLMM_PROGRAM_ID,
|
||||
@@ -70,8 +70,8 @@ impl RaydiumClmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::DECREASE_LIQUIDITY_V2,
|
||||
event_type: EventType::RaydiumClmmDecreaseLiquidityV2,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_decrease_liquidity_v2_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_decrease_liquidity_v2_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CLMM_PROGRAM_ID,
|
||||
@@ -79,8 +79,8 @@ impl RaydiumClmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::CREATE_POOL,
|
||||
event_type: EventType::RaydiumClmmCreatePool,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_create_pool_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_create_pool_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CLMM_PROGRAM_ID,
|
||||
@@ -88,8 +88,8 @@ impl RaydiumClmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::INCREASE_LIQUIDITY_V2,
|
||||
event_type: EventType::RaydiumClmmIncreaseLiquidityV2,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_increase_liquidity_v2_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_increase_liquidity_v2_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CLMM_PROGRAM_ID,
|
||||
@@ -97,8 +97,8 @@ impl RaydiumClmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::OPEN_POSITION_WITH_TOKEN_22_NFT,
|
||||
event_type: EventType::RaydiumClmmOpenPositionWithToken22Nft,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_open_position_with_token_22_nft_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_open_position_with_token_22_nft_instruction),
|
||||
},
|
||||
GenericEventParseConfig {
|
||||
program_id: RAYDIUM_CLMM_PROGRAM_ID,
|
||||
@@ -106,8 +106,8 @@ impl RaydiumClmmEventParser {
|
||||
inner_instruction_discriminator: "",
|
||||
instruction_discriminator: discriminators::OPEN_POSITION_V2,
|
||||
event_type: EventType::RaydiumClmmOpenPositionV2,
|
||||
inner_instruction_parser: Self::empty_parse,
|
||||
instruction_parser: Self::parse_open_position_v2_instruction,
|
||||
inner_instruction_parser: None,
|
||||
instruction_parser: Some(Self::parse_open_position_v2_instruction),
|
||||
},
|
||||
];
|
||||
|
||||
@@ -116,10 +116,6 @@ impl RaydiumClmmEventParser {
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
fn empty_parse(_data: &[u8], _metadata: EventMetadata) -> Option<Box<dyn UnifiedEvent>> {
|
||||
None
|
||||
}
|
||||
|
||||
/// 解析打开仓位V2指令事件
|
||||
fn parse_open_position_v2_instruction(
|
||||
data: &[u8],
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
use crate::streaming::{
|
||||
event_parser::{
|
||||
common::EventMetadata,
|
||||
protocols::raydium_clmm::{
|
||||
RaydiumClmmAmmConfigAccountEvent, RaydiumClmmPoolStateAccountEvent,
|
||||
RaydiumClmmTickArrayStateAccountEvent,
|
||||
},
|
||||
UnifiedEvent,
|
||||
},
|
||||
grpc::AccountPretty,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct AmmConfig {
|
||||
pub bump: u8,
|
||||
pub index: u16,
|
||||
pub owner: Pubkey,
|
||||
pub protocol_fee_rate: u32,
|
||||
pub trade_fee_rate: u32,
|
||||
pub tick_spacing: u16,
|
||||
pub fund_fee_rate: u32,
|
||||
pub padding_u32: u32,
|
||||
pub fund_owner: Pubkey,
|
||||
pub padding: [u64; 3],
|
||||
}
|
||||
|
||||
pub const AMM_CONFIG_SIZE: usize = 1 + 2 + 32 + 4 * 2 + 2 + 4 * 2 + 32 + 8 * 3;
|
||||
|
||||
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(RaydiumClmmAmmConfigAccountEvent {
|
||||
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 RewardInfo {
|
||||
pub reward_state: u8,
|
||||
pub open_time: u64,
|
||||
pub end_time: u64,
|
||||
pub last_update_time: u64,
|
||||
pub emissions_per_second_x64: u128,
|
||||
pub reward_total_emissioned: u64,
|
||||
pub reward_claimed: u64,
|
||||
pub token_mint: Pubkey,
|
||||
pub token_vault: Pubkey,
|
||||
pub authority: Pubkey,
|
||||
pub reward_growth_global_x64: u128,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct PoolState {
|
||||
pub bump: [u8; 1],
|
||||
pub amm_config: Pubkey,
|
||||
pub owner: Pubkey,
|
||||
pub token_mint0: Pubkey,
|
||||
pub token_mint1: Pubkey,
|
||||
pub token_vault0: Pubkey,
|
||||
pub token_vault1: Pubkey,
|
||||
pub observation_key: Pubkey,
|
||||
pub mint_decimals0: u8,
|
||||
pub mint_decimals1: u8,
|
||||
pub tick_spacing: u16,
|
||||
pub liquidity: u128,
|
||||
pub sqrt_price_x64: u128,
|
||||
pub tick_current: i32,
|
||||
pub padding3: u16,
|
||||
pub padding4: u16,
|
||||
pub fee_growth_global0_x64: u128,
|
||||
pub fee_growth_global1_x64: u128,
|
||||
pub protocol_fees_token0: u64,
|
||||
pub protocol_fees_token1: u64,
|
||||
pub swap_in_amount_token0: u128,
|
||||
pub swap_out_amount_token1: u128,
|
||||
pub swap_in_amount_token1: u128,
|
||||
pub swap_out_amount_token0: u128,
|
||||
pub status: u8,
|
||||
pub padding: [u8; 7],
|
||||
pub reward_infos: [RewardInfo; 3],
|
||||
pub tick_array_bitmap: [u64; 16],
|
||||
pub total_fees_token0: u64,
|
||||
pub total_fees_claimed_token0: u64,
|
||||
pub total_fees_token1: u64,
|
||||
pub total_fees_claimed_token1: u64,
|
||||
pub fund_fees_token0: u64,
|
||||
pub fund_fees_token1: u64,
|
||||
pub open_time: u64,
|
||||
pub recent_epoch: u64,
|
||||
pub padding1: [u64; 24],
|
||||
pub padding2: [u64; 32],
|
||||
}
|
||||
|
||||
pub const POOL_STATE_SIZE: usize = 1536;
|
||||
|
||||
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(RaydiumClmmPoolStateAccountEvent {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct TickState {
|
||||
pub tick: i32,
|
||||
pub liquidity_net: i128,
|
||||
pub liquidity_gross: u128,
|
||||
pub fee_growth_outside0_x64: u128,
|
||||
pub fee_growth_outside1_x64: u128,
|
||||
pub reward_growths_outside_x64: [u128; 3],
|
||||
pub padding: [u32; 13],
|
||||
}
|
||||
|
||||
impl Default for TickState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
tick: 0,
|
||||
liquidity_net: 0,
|
||||
liquidity_gross: 0,
|
||||
fee_growth_outside0_x64: 0,
|
||||
fee_growth_outside1_x64: 0,
|
||||
reward_growths_outside_x64: [0; 3],
|
||||
padding: [0; 13],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct TickArrayState {
|
||||
pub pool_id: Pubkey,
|
||||
pub start_tick_index: i32,
|
||||
#[serde(with = "serde_big_array::BigArray")]
|
||||
pub ticks: [TickState; 60],
|
||||
pub initialized_tick_count: u8,
|
||||
pub recent_epoch: u64,
|
||||
#[serde(with = "serde_big_array::BigArray")]
|
||||
pub padding: [u8; 107],
|
||||
}
|
||||
|
||||
impl Default for TickArrayState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
pool_id: Pubkey::default(),
|
||||
start_tick_index: 0,
|
||||
ticks: core::array::from_fn(|_| TickState::default()),
|
||||
initialized_tick_count: 0,
|
||||
recent_epoch: 0,
|
||||
padding: [0u8; 107],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const TICK_ARRAY_STATE_SIZE: usize = 10232;
|
||||
|
||||
pub fn tick_array_state_decode(data: &[u8]) -> Option<TickArrayState> {
|
||||
borsh::from_slice::<TickArrayState>(&data).ok()
|
||||
}
|
||||
|
||||
pub fn tick_array_state_parser(
|
||||
account: &AccountPretty,
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
if account.data.len() < TICK_ARRAY_STATE_SIZE {
|
||||
return None;
|
||||
}
|
||||
if let Some(tick_array_state) =
|
||||
tick_array_state_decode(&account.data[8..TICK_ARRAY_STATE_SIZE + 8])
|
||||
{
|
||||
Some(Box::new(RaydiumClmmTickArrayStateAccountEvent {
|
||||
metadata,
|
||||
pubkey: account.pubkey.to_string(),
|
||||
executable: account.executable,
|
||||
lamports: account.lamports,
|
||||
owner: account.owner.to_string(),
|
||||
rent_epoch: account.rent_epoch,
|
||||
tick_array_state: tick_array_state,
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user