mirror of
https://github.com/0xfnzero/solana-streamer.git
synced 2026-08-16 10:28:05 +00:00
refactor: restructure event handling from trait objects to enum pattern
- Convert UnifiedEvent from trait object (Box<dyn UnifiedEvent>) to concrete enum type - Remove match_event! macro in favor of native match expressions - Simplify event metadata access: event.event_type() -> event.metadata().event_type - Unify event callback signatures: Fn(Box<dyn UnifiedEvent>) -> Fn(UnifiedEvent) - Update all example code to use the new enum-based event system - Optimize type system to reduce runtime overhead and improve type safety Affected scope: - Core event parser and processor - All protocol event definitions (PumpFun, PumpSwap, Bonk, Raydium series, etc.) - gRPC and Shred streaming modules - All example code
This commit is contained in:
@@ -2,7 +2,6 @@ use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
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};
|
||||
|
||||
@@ -37,18 +36,18 @@ pub fn pumpfun_create_token_event_log_decode(data: &[u8]) -> Option<PumpFunCreat
|
||||
borsh::from_slice::<PumpFunCreateTokenEvent>(&data[..PUMPFUN_CREATE_TOKEN_EVENT_LOG_SIZE]).ok()
|
||||
}
|
||||
|
||||
impl_unified_event!(
|
||||
PumpFunCreateTokenEvent,
|
||||
mint,
|
||||
bonding_curve,
|
||||
user,
|
||||
creator,
|
||||
timestamp,
|
||||
virtual_token_reserves,
|
||||
virtual_sol_reserves,
|
||||
real_token_reserves,
|
||||
token_total_supply
|
||||
);
|
||||
// impl_unified_event!(
|
||||
// PumpFunCreateTokenEvent,
|
||||
// mint,
|
||||
// bonding_curve,
|
||||
// user,
|
||||
// creator,
|
||||
// timestamp,
|
||||
// virtual_token_reserves,
|
||||
// virtual_sol_reserves,
|
||||
// real_token_reserves,
|
||||
// token_total_supply
|
||||
// );
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct PumpFunTradeEvent {
|
||||
@@ -126,25 +125,25 @@ pub fn pumpfun_trade_event_log_decode(data: &[u8]) -> Option<PumpFunTradeEvent>
|
||||
borsh::from_slice::<PumpFunTradeEvent>(&data[..PUMPFUN_TRADE_EVENT_LOG_SIZE]).ok()
|
||||
}
|
||||
|
||||
impl_unified_event!(
|
||||
PumpFunTradeEvent,
|
||||
mint,
|
||||
sol_amount,
|
||||
token_amount,
|
||||
is_buy,
|
||||
user,
|
||||
timestamp,
|
||||
virtual_sol_reserves,
|
||||
virtual_token_reserves,
|
||||
real_sol_reserves,
|
||||
real_token_reserves,
|
||||
fee_recipient,
|
||||
fee_basis_points,
|
||||
fee,
|
||||
creator,
|
||||
creator_fee_basis_points,
|
||||
creator_fee
|
||||
);
|
||||
// impl_unified_event!(
|
||||
// PumpFunTradeEvent,
|
||||
// mint,
|
||||
// sol_amount,
|
||||
// token_amount,
|
||||
// is_buy,
|
||||
// user,
|
||||
// timestamp,
|
||||
// virtual_sol_reserves,
|
||||
// virtual_token_reserves,
|
||||
// real_sol_reserves,
|
||||
// real_token_reserves,
|
||||
// fee_recipient,
|
||||
// fee_basis_points,
|
||||
// fee,
|
||||
// creator,
|
||||
// creator_fee_basis_points,
|
||||
// creator_fee
|
||||
// );
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct PumpFunMigrateEvent {
|
||||
@@ -211,17 +210,17 @@ pub fn pumpfun_migrate_event_log_decode(data: &[u8]) -> Option<PumpFunMigrateEve
|
||||
borsh::from_slice::<PumpFunMigrateEvent>(&data[..PUMPFUN_MIGRATE_EVENT_LOG_SIZE]).ok()
|
||||
}
|
||||
|
||||
impl_unified_event!(
|
||||
PumpFunMigrateEvent,
|
||||
user,
|
||||
mint,
|
||||
mint_amount,
|
||||
sol_amount,
|
||||
pool_migration_fee,
|
||||
bonding_curve,
|
||||
timestamp,
|
||||
pool
|
||||
);
|
||||
// impl_unified_event!(
|
||||
// PumpFunMigrateEvent,
|
||||
// user,
|
||||
// mint,
|
||||
// mint_amount,
|
||||
// sol_amount,
|
||||
// pool_migration_fee,
|
||||
// bonding_curve,
|
||||
// timestamp,
|
||||
// pool
|
||||
// );
|
||||
|
||||
/// 铸币曲线
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
@@ -236,8 +235,6 @@ pub struct PumpFunBondingCurveAccountEvent {
|
||||
pub bonding_curve: BondingCurve,
|
||||
}
|
||||
|
||||
impl_unified_event!(PumpFunBondingCurveAccountEvent,);
|
||||
|
||||
/// 全局配置
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct PumpFunGlobalAccountEvent {
|
||||
@@ -250,7 +247,6 @@ pub struct PumpFunGlobalAccountEvent {
|
||||
pub rent_epoch: u64,
|
||||
pub global: Global,
|
||||
}
|
||||
impl_unified_event!(PumpFunGlobalAccountEvent,);
|
||||
|
||||
/// 事件鉴别器常量
|
||||
pub mod discriminators {
|
||||
|
||||
@@ -60,12 +60,9 @@ pub const CONFIGS: &[GenericEventParseConfig] = &[
|
||||
];
|
||||
|
||||
/// 解析迁移事件
|
||||
fn parse_migrate_inner_instruction(
|
||||
data: &[u8],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
fn parse_migrate_inner_instruction(data: &[u8], metadata: EventMetadata) -> Option<UnifiedEvent> {
|
||||
if let Some(event) = pumpfun_migrate_event_log_decode(data) {
|
||||
Some(Box::new(PumpFunMigrateEvent { metadata, ..event }))
|
||||
Some(UnifiedEvent::PumpFunMigrateEvent(PumpFunMigrateEvent { metadata, ..event }))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -75,21 +72,18 @@ fn parse_migrate_inner_instruction(
|
||||
fn parse_create_token_inner_instruction(
|
||||
data: &[u8],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
) -> Option<UnifiedEvent> {
|
||||
if let Some(event) = pumpfun_create_token_event_log_decode(data) {
|
||||
Some(Box::new(PumpFunCreateTokenEvent { metadata, ..event }))
|
||||
Some(UnifiedEvent::PumpFunCreateTokenEvent(PumpFunCreateTokenEvent { metadata, ..event }))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// 解析交易事件
|
||||
fn parse_trade_inner_instruction(
|
||||
data: &[u8],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
fn parse_trade_inner_instruction(data: &[u8], metadata: EventMetadata) -> Option<UnifiedEvent> {
|
||||
if let Some(event) = pumpfun_trade_event_log_decode(data) {
|
||||
Some(Box::new(PumpFunTradeEvent { metadata, ..event }))
|
||||
Some(UnifiedEvent::PumpFunTradeEvent(PumpFunTradeEvent { metadata, ..event }))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -100,7 +94,7 @@ fn parse_create_token_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
) -> Option<UnifiedEvent> {
|
||||
if data.len() < 16 || accounts.len() < 11 {
|
||||
return None;
|
||||
}
|
||||
@@ -141,7 +135,7 @@ fn parse_create_token_instruction(
|
||||
Pubkey::default()
|
||||
};
|
||||
|
||||
Some(Box::new(PumpFunCreateTokenEvent {
|
||||
Some(UnifiedEvent::PumpFunCreateTokenEvent(PumpFunCreateTokenEvent {
|
||||
metadata,
|
||||
name: name.to_string(),
|
||||
symbol: symbol.to_string(),
|
||||
@@ -161,13 +155,13 @@ fn parse_buy_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
) -> Option<UnifiedEvent> {
|
||||
if data.len() < 16 || accounts.len() < 13 {
|
||||
return None;
|
||||
}
|
||||
let amount = u64::from_le_bytes(data[0..8].try_into().unwrap());
|
||||
let max_sol_cost = u64::from_le_bytes(data[8..16].try_into().unwrap());
|
||||
Some(Box::new(PumpFunTradeEvent {
|
||||
Some(UnifiedEvent::PumpFunTradeEvent(PumpFunTradeEvent {
|
||||
metadata,
|
||||
global: accounts[0],
|
||||
fee_recipient: accounts[1],
|
||||
@@ -195,13 +189,13 @@ fn parse_sell_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
) -> Option<UnifiedEvent> {
|
||||
if data.len() < 16 || accounts.len() < 11 {
|
||||
return None;
|
||||
}
|
||||
let amount = u64::from_le_bytes(data[0..8].try_into().unwrap());
|
||||
let min_sol_output = u64::from_le_bytes(data[8..16].try_into().unwrap());
|
||||
Some(Box::new(PumpFunTradeEvent {
|
||||
Some(UnifiedEvent::PumpFunTradeEvent(PumpFunTradeEvent {
|
||||
metadata,
|
||||
global: accounts[0],
|
||||
fee_recipient: accounts[1],
|
||||
@@ -229,11 +223,11 @@ fn parse_migrate_instruction(
|
||||
_data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
) -> Option<UnifiedEvent> {
|
||||
if accounts.len() < 24 {
|
||||
return None;
|
||||
}
|
||||
Some(Box::new(PumpFunMigrateEvent {
|
||||
Some(UnifiedEvent::PumpFunMigrateEvent(PumpFunMigrateEvent {
|
||||
metadata,
|
||||
global: accounts[0],
|
||||
withdraw_authority: accounts[1],
|
||||
|
||||
@@ -34,12 +34,12 @@ pub fn bonding_curve_decode(data: &[u8]) -> Option<BondingCurve> {
|
||||
pub fn bonding_curve_parser(
|
||||
account: &AccountPretty,
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
) -> Option<UnifiedEvent> {
|
||||
if account.data.len() < BONDING_CURVE_SIZE + 8 {
|
||||
return None;
|
||||
}
|
||||
if let Some(bonding_curve) = bonding_curve_decode(&account.data[8..BONDING_CURVE_SIZE + 8]) {
|
||||
Some(Box::new(PumpFunBondingCurveAccountEvent {
|
||||
Some(UnifiedEvent::PumpFunBondingCurveAccountEvent(PumpFunBondingCurveAccountEvent {
|
||||
metadata,
|
||||
pubkey: account.pubkey,
|
||||
executable: account.executable,
|
||||
@@ -81,15 +81,12 @@ pub fn global_decode(data: &[u8]) -> Option<Global> {
|
||||
borsh::from_slice::<Global>(&data[..GLOBAL_SIZE]).ok()
|
||||
}
|
||||
|
||||
pub fn global_parser(
|
||||
account: &AccountPretty,
|
||||
metadata: EventMetadata,
|
||||
) -> Option<Box<dyn UnifiedEvent>> {
|
||||
pub fn global_parser(account: &AccountPretty, metadata: EventMetadata) -> Option<UnifiedEvent> {
|
||||
if account.data.len() < GLOBAL_SIZE + 8 {
|
||||
return None;
|
||||
}
|
||||
if let Some(global) = global_decode(&account.data[8..GLOBAL_SIZE + 8]) {
|
||||
Some(Box::new(PumpFunGlobalAccountEvent {
|
||||
Some(UnifiedEvent::PumpFunGlobalAccountEvent(PumpFunGlobalAccountEvent {
|
||||
metadata,
|
||||
pubkey: account.pubkey,
|
||||
executable: account.executable,
|
||||
|
||||
Reference in New Issue
Block a user