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:
ysq
2025-08-13 23:30:59 +08:00
parent c753c8418d
commit aee8921ad5
40 changed files with 1845 additions and 349 deletions
@@ -4,6 +4,7 @@ use solana_sdk::pubkey::Pubkey;
use crate::impl_unified_event;
use crate::streaming::event_parser::common::EventMetadata;
use crate::streaming::event_parser::protocols::pumpfun::types::{BondingCurve, Global};
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
pub struct PumpFunCreateTokenEvent {
@@ -177,6 +178,35 @@ impl_unified_event!(
pool
);
/// 铸币曲线
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
pub struct PumpFunBondingCurveAccountEvent {
#[borsh(skip)]
pub metadata: EventMetadata,
pub pubkey: String,
pub executable: bool,
pub lamports: u64,
pub owner: String,
pub rent_epoch: u64,
pub bonding_curve: BondingCurve,
}
impl_unified_event!(PumpFunBondingCurveAccountEvent,);
/// 全局配置
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
pub struct PumpFunGlobalAccountEvent {
#[borsh(skip)]
pub metadata: EventMetadata,
pub pubkey: String,
pub executable: bool,
pub lamports: u64,
pub owner: String,
pub rent_epoch: u64,
pub global: Global,
}
impl_unified_event!(PumpFunGlobalAccountEvent,);
/// 事件鉴别器常量
pub mod discriminators {
// 事件鉴别器
@@ -189,4 +219,8 @@ pub mod discriminators {
pub const BUY_IX: &[u8] = &[102, 6, 61, 18, 1, 218, 235, 234];
pub const SELL_IX: &[u8] = &[51, 230, 133, 164, 1, 127, 131, 173];
pub const MIGRATE_IX: &[u8] = &[155, 234, 231, 146, 236, 158, 162, 30];
// 账户鉴别器
pub const BONDING_CURVE_ACCOUNT: &[u8] = &[23, 183, 248, 55, 96, 216, 172, 96];
pub const GLOBAL_ACCOUNT: &[u8] = &[167, 232, 232, 177, 200, 108, 114, 127];
}
@@ -1,5 +1,6 @@
pub mod events;
pub mod parser;
pub mod types;
pub use events::*;
pub use parser::PumpFunEventParser;
@@ -37,8 +37,8 @@ impl PumpFunEventParser {
inner_instruction_discriminator: discriminators::CREATE_TOKEN_EVENT,
instruction_discriminator: discriminators::CREATE_TOKEN_IX,
event_type: EventType::PumpFunCreateToken,
inner_instruction_parser: Self::parse_create_token_inner_instruction,
instruction_parser: Self::parse_create_token_instruction,
inner_instruction_parser: Some(Self::parse_create_token_inner_instruction),
instruction_parser: Some(Self::parse_create_token_instruction),
},
GenericEventParseConfig {
program_id: PUMPFUN_PROGRAM_ID,
@@ -46,8 +46,8 @@ impl PumpFunEventParser {
inner_instruction_discriminator: discriminators::TRADE_EVENT,
instruction_discriminator: discriminators::BUY_IX,
event_type: EventType::PumpFunBuy,
inner_instruction_parser: Self::parse_trade_inner_instruction,
instruction_parser: Self::parse_buy_instruction,
inner_instruction_parser: Some(Self::parse_trade_inner_instruction),
instruction_parser: Some(Self::parse_buy_instruction),
},
GenericEventParseConfig {
program_id: PUMPFUN_PROGRAM_ID,
@@ -55,8 +55,8 @@ impl PumpFunEventParser {
inner_instruction_discriminator: discriminators::TRADE_EVENT,
instruction_discriminator: discriminators::SELL_IX,
event_type: EventType::PumpFunSell,
inner_instruction_parser: Self::parse_trade_inner_instruction,
instruction_parser: Self::parse_sell_instruction,
inner_instruction_parser: Some(Self::parse_trade_inner_instruction),
instruction_parser: Some(Self::parse_sell_instruction),
},
GenericEventParseConfig {
program_id: PUMPFUN_PROGRAM_ID,
@@ -64,8 +64,8 @@ impl PumpFunEventParser {
inner_instruction_discriminator: discriminators::COMPLETE_PUMP_AMM_MIGRATION_EVENT,
instruction_discriminator: discriminators::MIGRATE_IX,
event_type: EventType::PumpFunMigrate,
inner_instruction_parser: Self::parse_migrate_inner_instruction,
instruction_parser: Self::parse_migrate_instruction,
inner_instruction_parser: Some(Self::parse_migrate_inner_instruction),
instruction_parser: Some(Self::parse_migrate_instruction),
},
];
@@ -0,0 +1,98 @@
use borsh::BorshDeserialize;
use serde::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey;
use crate::streaming::{
event_parser::{
common::EventMetadata,
protocols::pumpfun::{PumpFunBondingCurveAccountEvent, PumpFunGlobalAccountEvent},
UnifiedEvent,
},
grpc::AccountPretty,
};
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
pub struct BondingCurve {
pub virtual_token_reserves: u64,
pub virtual_sol_reserves: u64,
pub real_token_reserves: u64,
pub real_sol_reserves: u64,
pub token_total_supply: u64,
pub complete: bool,
pub creator: Pubkey,
}
pub const BONDING_CURVE_SIZE: usize = 8 * 5 + 1 + 32;
pub fn bonding_curve_decode(data: &[u8]) -> Option<BondingCurve> {
if data.len() < BONDING_CURVE_SIZE {
return None;
}
borsh::from_slice::<BondingCurve>(&data).ok()
}
pub fn bonding_curve_parser(
account: &AccountPretty,
metadata: EventMetadata,
) -> Option<Box<dyn UnifiedEvent>> {
if let Some(bonding_curve) = bonding_curve_decode(&account.data[8..BONDING_CURVE_SIZE + 8]) {
Some(Box::new(PumpFunBondingCurveAccountEvent {
metadata,
pubkey: account.pubkey.to_string(),
executable: account.executable,
lamports: account.lamports,
owner: account.owner.to_string(),
rent_epoch: account.rent_epoch,
bonding_curve,
}))
} else {
None
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
pub struct Global {
pub initialized: bool,
pub authority: Pubkey,
pub fee_recipient: Pubkey,
pub initial_virtual_token_reserves: u64,
pub initial_virtual_sol_reserves: u64,
pub initial_real_token_reserves: u64,
pub token_total_supply: u64,
pub fee_basis_points: u64,
pub withdraw_authority: Pubkey,
pub enable_migrate: bool,
pub pool_migration_fee: u64,
pub creator_fee_basis_points: u64,
pub fee_recipients: [Pubkey; 7],
pub set_creator_authority: Pubkey,
pub admin_set_creator_authority: Pubkey,
}
pub const GLOBAL_SIZE: usize = 1 + 32 * 2 + 8 * 5 + 32 + 1 + 8 * 2 + 32 * 7 + 32 * 2;
pub fn global_decode(data: &[u8]) -> Option<Global> {
if data.len() < GLOBAL_SIZE {
return None;
}
borsh::from_slice::<Global>(&data).ok()
}
pub fn global_parser(
account: &AccountPretty,
metadata: EventMetadata,
) -> Option<Box<dyn UnifiedEvent>> {
if let Some(global) = global_decode(&account.data[8..GLOBAL_SIZE + 8]) {
Some(Box::new(PumpFunGlobalAccountEvent {
metadata,
pubkey: account.pubkey.to_string(),
executable: account.executable,
lamports: account.lamports,
owner: account.owner.to_string(),
rent_epoch: account.rent_epoch,
global,
}))
} else {
None
}
}