mirror of
https://github.com/0xfnzero/solana-streamer.git
synced 2026-08-17 02:48:05 +00:00
Release solana-streamer-sdk v1.4.3
Route gRPC, ShredStream, and account parsing through sol-parser-sdk. Remove local protocol parsing implementations. Depend on sol-parser-sdk v0.4.8 from crates.io.
This commit is contained in:
@@ -97,136 +97,6 @@ pub struct PumpFunCreateV2TokenEvent {
|
||||
pub program: Pubkey,
|
||||
}
|
||||
|
||||
pub fn pumpfun_create_v2_token_event_log_decode(data: &[u8]) -> Option<PumpFunCreateV2TokenEvent> {
|
||||
let mut offset = 0;
|
||||
|
||||
// Parse name string: [length (4 bytes u32)][string bytes]
|
||||
if data.len() < offset + 4 {
|
||||
return None;
|
||||
}
|
||||
let name_len = u32::from_le_bytes(data[offset..offset + 4].try_into().ok()?) as usize;
|
||||
offset += 4;
|
||||
if data.len() < offset + name_len {
|
||||
return None;
|
||||
}
|
||||
let name = String::from_utf8(data[offset..offset + name_len].to_vec()).ok()?;
|
||||
offset += name_len;
|
||||
|
||||
// Parse symbol string
|
||||
if data.len() < offset + 4 {
|
||||
return None;
|
||||
}
|
||||
let symbol_len = u32::from_le_bytes(data[offset..offset + 4].try_into().ok()?) as usize;
|
||||
offset += 4;
|
||||
if data.len() < offset + symbol_len {
|
||||
return None;
|
||||
}
|
||||
let symbol = String::from_utf8(data[offset..offset + symbol_len].to_vec()).ok()?;
|
||||
offset += symbol_len;
|
||||
|
||||
// Parse uri string
|
||||
if data.len() < offset + 4 {
|
||||
return None;
|
||||
}
|
||||
let uri_len = u32::from_le_bytes(data[offset..offset + 4].try_into().ok()?) as usize;
|
||||
offset += 4;
|
||||
if data.len() < offset + uri_len {
|
||||
return None;
|
||||
}
|
||||
let uri = String::from_utf8(data[offset..offset + uri_len].to_vec()).ok()?;
|
||||
offset += uri_len;
|
||||
|
||||
// Parse Pubkey fields (32 bytes each)
|
||||
if data.len() < offset + 32 {
|
||||
return None;
|
||||
}
|
||||
let mint = Pubkey::new_from_array(data[offset..offset + 32].try_into().ok()?);
|
||||
offset += 32;
|
||||
|
||||
if data.len() < offset + 32 {
|
||||
return None;
|
||||
}
|
||||
let bonding_curve = Pubkey::new_from_array(data[offset..offset + 32].try_into().ok()?);
|
||||
offset += 32;
|
||||
|
||||
if data.len() < offset + 32 {
|
||||
return None;
|
||||
}
|
||||
let user = Pubkey::new_from_array(data[offset..offset + 32].try_into().ok()?);
|
||||
offset += 32;
|
||||
|
||||
if data.len() < offset + 32 {
|
||||
return None;
|
||||
}
|
||||
let creator = Pubkey::new_from_array(data[offset..offset + 32].try_into().ok()?);
|
||||
offset += 32;
|
||||
|
||||
// Parse numeric fields
|
||||
if data.len() < offset + 8 {
|
||||
return None;
|
||||
}
|
||||
let timestamp = i64::from_le_bytes(data[offset..offset + 8].try_into().ok()?);
|
||||
offset += 8;
|
||||
|
||||
if data.len() < offset + 8 {
|
||||
return None;
|
||||
}
|
||||
let virtual_token_reserves = u64::from_le_bytes(data[offset..offset + 8].try_into().ok()?);
|
||||
offset += 8;
|
||||
|
||||
if data.len() < offset + 8 {
|
||||
return None;
|
||||
}
|
||||
let virtual_sol_reserves = u64::from_le_bytes(data[offset..offset + 8].try_into().ok()?);
|
||||
offset += 8;
|
||||
|
||||
if data.len() < offset + 8 {
|
||||
return None;
|
||||
}
|
||||
let real_token_reserves = u64::from_le_bytes(data[offset..offset + 8].try_into().ok()?);
|
||||
offset += 8;
|
||||
|
||||
if data.len() < offset + 8 {
|
||||
return None;
|
||||
}
|
||||
let token_total_supply = u64::from_le_bytes(data[offset..offset + 8].try_into().ok()?);
|
||||
offset += 8;
|
||||
|
||||
// If data length allows, parse V2 extra fields: token_program (32 bytes) + is_mayhem_mode (1 byte) + is_cashback_enabled (1 byte)
|
||||
let (token_program, is_mayhem_mode, is_cashback_enabled) = if data.len() >= offset + 34 {
|
||||
let token_program = Pubkey::new_from_array(data[offset..offset + 32].try_into().ok()?);
|
||||
let is_mayhem_mode = data[offset + 32] != 0;
|
||||
let is_cashback_enabled = data[offset + 33] != 0;
|
||||
(token_program, is_mayhem_mode, is_cashback_enabled)
|
||||
} else if data.len() >= offset + 33 {
|
||||
// Backward compat: only token_program + is_mayhem_mode, no is_cashback_enabled
|
||||
let token_program = Pubkey::new_from_array(data[offset..offset + 32].try_into().ok()?);
|
||||
let is_mayhem_mode = data[offset + 32] != 0;
|
||||
(token_program, is_mayhem_mode, false)
|
||||
} else {
|
||||
(Pubkey::default(), false, false)
|
||||
};
|
||||
|
||||
Some(PumpFunCreateV2TokenEvent {
|
||||
name,
|
||||
symbol,
|
||||
uri,
|
||||
mint,
|
||||
bonding_curve,
|
||||
user,
|
||||
creator,
|
||||
timestamp,
|
||||
virtual_token_reserves,
|
||||
virtual_sol_reserves,
|
||||
real_token_reserves,
|
||||
token_total_supply,
|
||||
token_program,
|
||||
is_mayhem_mode,
|
||||
is_cashback_enabled,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct PumpFunTradeEvent {
|
||||
#[borsh(skip)]
|
||||
@@ -318,49 +188,6 @@ pub struct PumpFunTradeEvent {
|
||||
/// Layout: mint(32)+sol_amount(8)+token_amount(8)+is_buy(1)+user(32)+timestamp(8)+virtual_sol(8)+virtual_token(8)+real_sol(8)+real_token(8)+fee_recipient(32)+fee_basis_points(8)+fee(8)+creator(32)+creator_fee_bps(8)+creator_fee(8)+track_volume(1)+total_unclaimed(8)+total_claimed(8)+current_sol_volume(8)+last_update_timestamp(8) = 250
|
||||
pub const PUMPFUN_TRADE_EVENT_LOG_SIZE: usize = 250;
|
||||
|
||||
/// Decode TradeEvent log; if data.len() > 250 then parse ix_name, mayhem_mode, cashback (IDL-aligned).
|
||||
pub fn pumpfun_trade_event_log_decode(data: &[u8]) -> Option<PumpFunTradeEvent> {
|
||||
if data.len() < PUMPFUN_TRADE_EVENT_LOG_SIZE {
|
||||
return None;
|
||||
}
|
||||
let mut event =
|
||||
borsh::from_slice::<PumpFunTradeEvent>(&data[..PUMPFUN_TRADE_EVENT_LOG_SIZE]).ok()?;
|
||||
let mut offset = PUMPFUN_TRADE_EVENT_LOG_SIZE;
|
||||
if offset < data.len() {
|
||||
let (ix_name, inc) = read_borsh_string(data, offset).unwrap_or((String::new(), 0));
|
||||
offset += inc;
|
||||
event.ix_name = ix_name;
|
||||
}
|
||||
if offset + 1 <= data.len() {
|
||||
event.mayhem_mode = data[offset] != 0;
|
||||
offset += 1;
|
||||
}
|
||||
if offset + 8 <= data.len() {
|
||||
event.cashback_fee_basis_points =
|
||||
u64::from_le_bytes(data[offset..offset + 8].try_into().ok()?);
|
||||
offset += 8;
|
||||
}
|
||||
if offset + 8 <= data.len() {
|
||||
event.cashback = u64::from_le_bytes(data[offset..offset + 8].try_into().ok()?);
|
||||
}
|
||||
event.is_cashback_coin = event.cashback_fee_basis_points > 0;
|
||||
Some(event)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_borsh_string(data: &[u8], start: usize) -> Option<(String, usize)> {
|
||||
if start + 4 > data.len() {
|
||||
return None;
|
||||
}
|
||||
let len = u32::from_le_bytes(data[start..start + 4].try_into().ok()?) as usize;
|
||||
let start = start + 4;
|
||||
if start + len > data.len() {
|
||||
return None;
|
||||
}
|
||||
let s = String::from_utf8_lossy(&data[start..start + len]).to_string();
|
||||
Some((s, 4 + len))
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct PumpFunMigrateEvent {
|
||||
#[borsh(skip)]
|
||||
@@ -551,13 +378,6 @@ pub struct PumpFunMigrateBondingCurveCreatorEvent {
|
||||
|
||||
pub const PUMPFUN_MIGRATE_EVENT_LOG_SIZE: usize = 160;
|
||||
|
||||
pub fn pumpfun_migrate_event_log_decode(data: &[u8]) -> Option<PumpFunMigrateEvent> {
|
||||
if data.len() < PUMPFUN_MIGRATE_EVENT_LOG_SIZE {
|
||||
return None;
|
||||
}
|
||||
borsh::from_slice::<PumpFunMigrateEvent>(&data[..PUMPFUN_MIGRATE_EVENT_LOG_SIZE]).ok()
|
||||
}
|
||||
|
||||
/// Bonding curve
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct PumpFunBondingCurveAccountEvent {
|
||||
|
||||
Executable → Regular
+12
-423
@@ -1,448 +1,37 @@
|
||||
use crate::streaming::event_parser::{
|
||||
common::{EventMetadata, EventType},
|
||||
protocols::pumpfun::{
|
||||
discriminators, pumpfun_create_v2_token_event_log_decode, pumpfun_migrate_event_log_decode,
|
||||
pumpfun_trade_event_log_decode, PumpFunCreateTokenEvent, PumpFunCreateV2TokenEvent,
|
||||
PumpFunMigrateEvent, PumpFunTradeEvent,
|
||||
},
|
||||
DexEvent,
|
||||
common::EventMetadata, core::EventDispatcher, DexEvent, Protocol,
|
||||
};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
/// PumpFun程序ID
|
||||
pub const PUMPFUN_PROGRAM_ID: Pubkey =
|
||||
solana_sdk::pubkey!("6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P");
|
||||
pub use sol_parser_sdk::instr::program_ids::PUMPFUN_PROGRAM_ID;
|
||||
|
||||
/// 解析 PumpFun instruction data
|
||||
///
|
||||
/// 根据判别器路由到具体的 instruction 解析函数
|
||||
pub fn parse_pumpfun_instruction_data(
|
||||
discriminator: &[u8],
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<DexEvent> {
|
||||
match discriminator {
|
||||
discriminators::CREATE_TOKEN_IX => parse_create_token_instruction(data, accounts, metadata),
|
||||
discriminators::CREATE_V2_TOKEN_IX => {
|
||||
parse_create_v2_token_instruction(data, accounts, metadata)
|
||||
}
|
||||
discriminators::BUY_IX => parse_buy_instruction(data, accounts, metadata),
|
||||
discriminators::BUY_EXACT_SOL_IN_IX => {
|
||||
parse_buy_exact_sol_in_instruction(data, accounts, metadata)
|
||||
}
|
||||
discriminators::SELL_IX => parse_sell_instruction(data, accounts, metadata),
|
||||
discriminators::MIGRATE_IX => parse_migrate_instruction(data, accounts, metadata),
|
||||
_ => None,
|
||||
}
|
||||
EventDispatcher::dispatch_instruction(
|
||||
Protocol::PumpFun,
|
||||
discriminator,
|
||||
data,
|
||||
accounts,
|
||||
metadata,
|
||||
)
|
||||
}
|
||||
|
||||
/// 解析 PumpFun inner instruction data
|
||||
///
|
||||
/// 根据判别器路由到具体的 inner instruction 解析函数
|
||||
pub fn parse_pumpfun_inner_instruction_data(
|
||||
discriminator: &[u8],
|
||||
data: &[u8],
|
||||
metadata: EventMetadata,
|
||||
) -> Option<DexEvent> {
|
||||
match discriminator {
|
||||
discriminators::CREATE_TOKEN_EVENT => parse_create_token_inner_instruction(data, metadata),
|
||||
discriminators::TRADE_EVENT => parse_trade_inner_instruction(data, metadata),
|
||||
discriminators::COMPLETE_PUMP_AMM_MIGRATION_EVENT => {
|
||||
parse_migrate_inner_instruction(data, metadata)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
EventDispatcher::dispatch_inner_instruction(Protocol::PumpFun, discriminator, data, metadata)
|
||||
}
|
||||
|
||||
/// 解析 PumpFun 账户数据
|
||||
///
|
||||
/// 根据判别器路由到具体的账户解析函数
|
||||
pub fn parse_pumpfun_account_data(
|
||||
discriminator: &[u8],
|
||||
account: &crate::streaming::grpc::AccountPretty,
|
||||
metadata: crate::streaming::event_parser::common::EventMetadata,
|
||||
) -> Option<crate::streaming::event_parser::DexEvent> {
|
||||
match discriminator {
|
||||
discriminators::BONDING_CURVE_ACCOUNT => {
|
||||
crate::streaming::event_parser::protocols::pumpfun::types::bonding_curve_parser(
|
||||
account, metadata,
|
||||
)
|
||||
}
|
||||
discriminators::GLOBAL_ACCOUNT => {
|
||||
crate::streaming::event_parser::protocols::pumpfun::types::global_parser(
|
||||
account, metadata,
|
||||
)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// 解析迁移事件
|
||||
fn parse_migrate_inner_instruction(data: &[u8], mut metadata: EventMetadata) -> Option<DexEvent> {
|
||||
metadata.event_type = EventType::PumpFunMigrate;
|
||||
if let Some(event) = pumpfun_migrate_event_log_decode(data) {
|
||||
Some(DexEvent::PumpFunMigrateEvent(PumpFunMigrateEvent { metadata, ..event }))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// 解析创建代币日志事件
|
||||
fn parse_create_token_inner_instruction(
|
||||
data: &[u8],
|
||||
mut metadata: EventMetadata,
|
||||
metadata: EventMetadata,
|
||||
) -> Option<DexEvent> {
|
||||
metadata.event_type = EventType::PumpFunCreateToken;
|
||||
if let Some(event) = pumpfun_create_v2_token_event_log_decode(data) {
|
||||
Some(DexEvent::PumpFunCreateV2TokenEvent(PumpFunCreateV2TokenEvent { metadata, ..event }))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// 解析交易事件 (inner instruction 不设置 event_type,因为不知道是 Buy 还是 Sell)
|
||||
fn parse_trade_inner_instruction(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
|
||||
// 注意:inner instruction 的 trade event 不设置 event_type
|
||||
// 因为它会被合并到 instruction event 中,而 instruction event 已经设置了正确的 event_type
|
||||
if let Some(event) = pumpfun_trade_event_log_decode(data) {
|
||||
Some(DexEvent::PumpFunTradeEvent(PumpFunTradeEvent { metadata, ..event }))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// 解析创建代币指令事件
|
||||
/// 账户: 0: mint, 1: mint_authority, 2: bonding_curve, 3: associated_bonding_curve, 4: global,
|
||||
/// 5: mpl_token_metadata, 6: metadata_account, 7: user, 8: system_program, 9: token_program,
|
||||
/// 10: associated_token_program, 11: rent, 12: event_authority, 13: program.
|
||||
/// 共 14 个固定账户,不足时返回 None 避免越界。
|
||||
fn parse_create_token_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
mut metadata: EventMetadata,
|
||||
) -> Option<DexEvent> {
|
||||
metadata.event_type = EventType::PumpFunCreateToken;
|
||||
|
||||
const CREATE_TOKEN_MIN_ACCOUNTS: usize = 14;
|
||||
if data.len() < 16 || accounts.len() < CREATE_TOKEN_MIN_ACCOUNTS {
|
||||
return None;
|
||||
}
|
||||
let mut offset = 0;
|
||||
if offset + 4 > data.len() {
|
||||
return None;
|
||||
}
|
||||
let name_len = u32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()) as usize;
|
||||
offset += 4;
|
||||
if offset + name_len > data.len() {
|
||||
return None;
|
||||
}
|
||||
let name = String::from_utf8_lossy(&data[offset..offset + name_len]);
|
||||
offset += name_len;
|
||||
if offset + 4 > data.len() {
|
||||
return None;
|
||||
}
|
||||
let symbol_len = u32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()) as usize;
|
||||
offset += 4;
|
||||
if offset + symbol_len > data.len() {
|
||||
return None;
|
||||
}
|
||||
let symbol = String::from_utf8_lossy(&data[offset..offset + symbol_len]);
|
||||
offset += symbol_len;
|
||||
if offset + 4 > data.len() {
|
||||
return None;
|
||||
}
|
||||
let uri_len = u32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()) as usize;
|
||||
offset += 4;
|
||||
if offset + uri_len > data.len() {
|
||||
return None;
|
||||
}
|
||||
let uri = String::from_utf8_lossy(&data[offset..offset + uri_len]);
|
||||
offset += uri_len;
|
||||
let creator = if offset + 32 <= data.len() {
|
||||
Pubkey::new_from_array(data[offset..offset + 32].try_into().ok()?)
|
||||
} else {
|
||||
Pubkey::default()
|
||||
};
|
||||
|
||||
Some(DexEvent::PumpFunCreateTokenEvent(PumpFunCreateTokenEvent {
|
||||
metadata,
|
||||
name: name.to_string(),
|
||||
symbol: symbol.to_string(),
|
||||
uri: uri.to_string(),
|
||||
creator,
|
||||
mint: accounts[0],
|
||||
mint_authority: accounts[1],
|
||||
bonding_curve: accounts[2],
|
||||
associated_bonding_curve: accounts[3],
|
||||
global: accounts[4],
|
||||
mpl_token_metadata: accounts[5],
|
||||
metadata_account: accounts[6],
|
||||
user: accounts[7],
|
||||
system_program: accounts[8],
|
||||
token_program: accounts[9],
|
||||
associated_token_program: accounts[10],
|
||||
rent: accounts[11],
|
||||
event_authority: accounts[12],
|
||||
program: accounts[13],
|
||||
..Default::default()
|
||||
}))
|
||||
}
|
||||
|
||||
/// 解析创建 V2 代币指令事件 (SPL-22 Token, Mayhem Mode)
|
||||
/// 与 IDL create_v2 及区块浏览器一致,共 16 个固定账户:
|
||||
/// 0: mint, 1: mint_authority, 2: bonding_curve, 3: associated_bonding_curve, 4: global,
|
||||
/// 5: user, 6: system_program, 7: token_program, 8: associated_token_program, 9: mayhem_program_id,
|
||||
/// 10: global_params, 11: sol_vault, 12: mayhem_state, 13: mayhem_token_vault, 14: event_authority, 15: program.
|
||||
/// 不足 16 个账户时返回 None 避免越界。
|
||||
/// 注意:shredstream 路径仅传入 static_account_keys,若交易使用 Address Lookup Tables,
|
||||
/// 无法解析 loaded_addresses,部分账户会以 default 填充,导致 token_program/global 等错误。
|
||||
fn parse_create_v2_token_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
mut metadata: EventMetadata,
|
||||
) -> Option<DexEvent> {
|
||||
const CREATE_V2_MIN_ACCOUNTS: usize = 16;
|
||||
// Guard: avoid index out of bounds (e.g. ALT-loaded tx with fewer static accounts). See issue #63.
|
||||
if accounts.len() < CREATE_V2_MIN_ACCOUNTS || data.len() < 16 {
|
||||
return None;
|
||||
}
|
||||
metadata.event_type = EventType::PumpFunCreateV2Token;
|
||||
|
||||
let mut offset = 0;
|
||||
if offset + 4 > data.len() {
|
||||
return None;
|
||||
}
|
||||
let name_len = u32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()) as usize;
|
||||
offset += 4;
|
||||
if offset + name_len > data.len() {
|
||||
return None;
|
||||
}
|
||||
let name = String::from_utf8_lossy(&data[offset..offset + name_len]);
|
||||
offset += name_len;
|
||||
if offset + 4 > data.len() {
|
||||
return None;
|
||||
}
|
||||
let symbol_len = u32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()) as usize;
|
||||
offset += 4;
|
||||
if offset + symbol_len > data.len() {
|
||||
return None;
|
||||
}
|
||||
let symbol = String::from_utf8_lossy(&data[offset..offset + symbol_len]);
|
||||
offset += symbol_len;
|
||||
if offset + 4 > data.len() {
|
||||
return None;
|
||||
}
|
||||
let uri_len = u32::from_le_bytes(data[offset..offset + 4].try_into().unwrap()) as usize;
|
||||
offset += 4;
|
||||
if offset + uri_len > data.len() {
|
||||
return None;
|
||||
}
|
||||
let uri = String::from_utf8_lossy(&data[offset..offset + uri_len]);
|
||||
offset += uri_len;
|
||||
let creator = if offset + 32 <= data.len() {
|
||||
Pubkey::new_from_array(data[offset..offset + 32].try_into().ok()?)
|
||||
} else {
|
||||
Pubkey::default()
|
||||
};
|
||||
|
||||
// Safe slice: already guaranteed accounts.len() >= 16 above; avoid any index panic (issue #63).
|
||||
let acc = &accounts[0..CREATE_V2_MIN_ACCOUNTS];
|
||||
Some(DexEvent::PumpFunCreateV2TokenEvent(PumpFunCreateV2TokenEvent {
|
||||
metadata,
|
||||
name: name.to_string(),
|
||||
symbol: symbol.to_string(),
|
||||
uri: uri.to_string(),
|
||||
creator,
|
||||
mint: acc[0],
|
||||
mint_authority: acc[1],
|
||||
bonding_curve: acc[2],
|
||||
associated_bonding_curve: acc[3],
|
||||
global: acc[4],
|
||||
user: acc[5],
|
||||
system_program: acc[6],
|
||||
token_program: acc[7],
|
||||
associated_token_program: acc[8],
|
||||
mayhem_program_id: acc[9],
|
||||
global_params: acc[10],
|
||||
sol_vault: acc[11],
|
||||
mayhem_state: acc[12],
|
||||
mayhem_token_vault: acc[13],
|
||||
event_authority: acc[14],
|
||||
program: acc[15],
|
||||
..Default::default()
|
||||
}))
|
||||
}
|
||||
|
||||
/// Parse buy instruction event.
|
||||
/// Buy has 16 fixed accounts + optional 17th (index 16, "Account" on block explorers):
|
||||
/// 0: global, 1: fee_recipient, 2: mint, 3: bonding_curve, 4: associated_bonding_curve,
|
||||
/// 5: associated_user, 6: user, 7: system_program, 8: token_program, 9: creator_vault,
|
||||
/// 10: event_authority, 11: program, 12: global_volume_accumulator, 13: user_volume_accumulator,
|
||||
/// 14: fee_config, 15: fee_program, 16 (optional): account.
|
||||
fn parse_buy_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
mut metadata: EventMetadata,
|
||||
) -> Option<DexEvent> {
|
||||
metadata.event_type = EventType::PumpFunBuy;
|
||||
|
||||
if data.len() < 16 || accounts.len() < 16 {
|
||||
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(DexEvent::PumpFunTradeEvent(PumpFunTradeEvent {
|
||||
metadata,
|
||||
global: accounts[0],
|
||||
fee_recipient: accounts[1],
|
||||
mint: accounts[2],
|
||||
bonding_curve: accounts[3],
|
||||
associated_bonding_curve: accounts[4],
|
||||
associated_user: accounts[5],
|
||||
user: accounts[6],
|
||||
system_program: accounts[7],
|
||||
token_program: accounts[8],
|
||||
creator_vault: accounts[9],
|
||||
event_authority: accounts[10],
|
||||
program: accounts[11],
|
||||
global_volume_accumulator: accounts[12],
|
||||
user_volume_accumulator: accounts[13],
|
||||
fee_config: accounts[14],
|
||||
fee_program: accounts[15],
|
||||
account: accounts.get(16).copied(),
|
||||
max_sol_cost,
|
||||
amount,
|
||||
is_buy: true,
|
||||
..Default::default()
|
||||
}))
|
||||
}
|
||||
|
||||
/// Parse buy_exact_sol_in instruction event.
|
||||
/// Same account layout as buy: 16 fixed + optional 17th (index 16).
|
||||
/// Args: spendable_sol_in (SOL), min_tokens_out (token).
|
||||
fn parse_buy_exact_sol_in_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
mut metadata: EventMetadata,
|
||||
) -> Option<DexEvent> {
|
||||
metadata.event_type = EventType::PumpFunBuyExactSolIn;
|
||||
|
||||
if data.len() < 16 || accounts.len() < 16 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let spendable_sol_in = u64::from_le_bytes(data[0..8].try_into().unwrap());
|
||||
let min_tokens_out = u64::from_le_bytes(data[8..16].try_into().unwrap());
|
||||
|
||||
Some(DexEvent::PumpFunTradeEvent(PumpFunTradeEvent {
|
||||
metadata,
|
||||
global: accounts[0],
|
||||
fee_recipient: accounts[1],
|
||||
mint: accounts[2],
|
||||
bonding_curve: accounts[3],
|
||||
associated_bonding_curve: accounts[4],
|
||||
associated_user: accounts[5],
|
||||
user: accounts[6],
|
||||
system_program: accounts[7],
|
||||
token_program: accounts[8],
|
||||
creator_vault: accounts[9],
|
||||
event_authority: accounts[10],
|
||||
program: accounts[11],
|
||||
global_volume_accumulator: accounts[12],
|
||||
user_volume_accumulator: accounts[13],
|
||||
fee_config: accounts[14],
|
||||
fee_program: accounts[15],
|
||||
account: accounts.get(16).copied(),
|
||||
max_sol_cost: spendable_sol_in,
|
||||
amount: min_tokens_out,
|
||||
is_buy: true,
|
||||
..Default::default()
|
||||
}))
|
||||
}
|
||||
|
||||
/// Parse sell instruction event.
|
||||
/// Sell has 14 fixed accounts; some versions pass 17 accounts, index 16 = "Account" on block explorers.
|
||||
fn parse_sell_instruction(
|
||||
data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
mut metadata: EventMetadata,
|
||||
) -> Option<DexEvent> {
|
||||
metadata.event_type = EventType::PumpFunSell;
|
||||
|
||||
if data.len() < 16 || accounts.len() < 14 {
|
||||
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(DexEvent::PumpFunTradeEvent(PumpFunTradeEvent {
|
||||
metadata,
|
||||
global: accounts[0],
|
||||
fee_recipient: accounts[1],
|
||||
mint: accounts[2],
|
||||
bonding_curve: accounts[3],
|
||||
associated_bonding_curve: accounts[4],
|
||||
associated_user: accounts[5],
|
||||
user: accounts[6],
|
||||
system_program: accounts[7],
|
||||
creator_vault: accounts[8],
|
||||
token_program: accounts[9],
|
||||
event_authority: accounts[10],
|
||||
program: accounts[11],
|
||||
global_volume_accumulator: Pubkey::default(),
|
||||
user_volume_accumulator: Pubkey::default(),
|
||||
fee_config: accounts[12],
|
||||
fee_program: accounts[13],
|
||||
account: accounts.get(16).copied(),
|
||||
min_sol_output,
|
||||
amount,
|
||||
is_buy: false,
|
||||
..Default::default()
|
||||
}))
|
||||
}
|
||||
|
||||
/// 解析迁移指令事件
|
||||
/// 共 24 个固定账户: 0: global, 1: withdraw_authority, 2: mint, 3: bonding_curve, 4: associated_bonding_curve,
|
||||
/// 5: user, 6: system_program, 7: token_program, 8: pump_amm, 9: pool, 10: pool_authority,
|
||||
/// 11: pool_authority_mint_account, 12: pool_authority_wsol_account, 13: amm_global_config, 14: wsol_mint,
|
||||
/// 15: lp_mint, 16: user_pool_token_account, 17: pool_base_token_account, 18: pool_quote_token_account,
|
||||
/// 19: token_2022_program, 20: associated_token_program, 21: pump_amm_event_authority, 22: event_authority, 23: program.
|
||||
fn parse_migrate_instruction(
|
||||
_data: &[u8],
|
||||
accounts: &[Pubkey],
|
||||
mut metadata: EventMetadata,
|
||||
) -> Option<DexEvent> {
|
||||
metadata.event_type = EventType::PumpFunMigrate;
|
||||
|
||||
if accounts.len() < 24 {
|
||||
return None;
|
||||
}
|
||||
Some(DexEvent::PumpFunMigrateEvent(PumpFunMigrateEvent {
|
||||
metadata,
|
||||
global: accounts[0],
|
||||
withdraw_authority: accounts[1],
|
||||
mint: accounts[2],
|
||||
bonding_curve: accounts[3],
|
||||
associated_bonding_curve: accounts[4],
|
||||
user: accounts[5],
|
||||
system_program: accounts[6],
|
||||
token_program: accounts[7],
|
||||
pump_amm: accounts[8],
|
||||
pool: accounts[9],
|
||||
pool_authority: accounts[10],
|
||||
pool_authority_mint_account: accounts[11],
|
||||
pool_authority_wsol_account: accounts[12],
|
||||
amm_global_config: accounts[13],
|
||||
wsol_mint: accounts[14],
|
||||
lp_mint: accounts[15],
|
||||
user_pool_token_account: accounts[16],
|
||||
pool_base_token_account: accounts[17],
|
||||
pool_quote_token_account: accounts[18],
|
||||
token_2022_program: accounts[19],
|
||||
associated_token_program: accounts[20],
|
||||
pump_amm_event_authority: accounts[21],
|
||||
event_authority: accounts[22],
|
||||
program: accounts[23],
|
||||
..Default::default()
|
||||
}))
|
||||
EventDispatcher::dispatch_account(Protocol::PumpFun, discriminator, account, metadata)
|
||||
}
|
||||
|
||||
@@ -2,15 +2,6 @@ use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
use crate::streaming::{
|
||||
event_parser::{
|
||||
common::{EventMetadata, EventType},
|
||||
protocols::pumpfun::{PumpFunBondingCurveAccountEvent, PumpFunGlobalAccountEvent},
|
||||
DexEvent,
|
||||
},
|
||||
grpc::AccountPretty,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct BondingCurve {
|
||||
pub virtual_token_reserves: u64,
|
||||
@@ -26,37 +17,6 @@ pub struct BondingCurve {
|
||||
|
||||
pub const BONDING_CURVE_SIZE: usize = 8 * 5 + 1 + 32 + 1 + 1;
|
||||
|
||||
pub fn bonding_curve_decode(data: &[u8]) -> Option<BondingCurve> {
|
||||
if data.len() < BONDING_CURVE_SIZE {
|
||||
return None;
|
||||
}
|
||||
borsh::from_slice::<BondingCurve>(&data[..BONDING_CURVE_SIZE]).ok()
|
||||
}
|
||||
|
||||
pub fn bonding_curve_parser(
|
||||
account: &AccountPretty,
|
||||
mut metadata: EventMetadata,
|
||||
) -> Option<DexEvent> {
|
||||
metadata.event_type = EventType::AccountPumpFunBondingCurve;
|
||||
|
||||
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(DexEvent::PumpFunBondingCurveAccountEvent(PumpFunBondingCurveAccountEvent {
|
||||
metadata,
|
||||
pubkey: account.pubkey,
|
||||
executable: account.executable,
|
||||
lamports: account.lamports,
|
||||
owner: account.owner,
|
||||
rent_epoch: account.rent_epoch,
|
||||
bonding_curve,
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct Global {
|
||||
pub initialized: bool,
|
||||
@@ -84,31 +44,3 @@ pub struct Global {
|
||||
|
||||
pub const GLOBAL_SIZE: usize =
|
||||
1 + 32 * 2 + 8 * 5 + 32 + 1 + 8 * 2 + 32 * 7 + 32 * 2 + 1 + 32 * 2 + 1 + 32 * 7 + 1;
|
||||
|
||||
pub fn global_decode(data: &[u8]) -> Option<Global> {
|
||||
if data.len() < GLOBAL_SIZE {
|
||||
return None;
|
||||
}
|
||||
borsh::from_slice::<Global>(&data[..GLOBAL_SIZE]).ok()
|
||||
}
|
||||
|
||||
pub fn global_parser(account: &AccountPretty, mut metadata: EventMetadata) -> Option<DexEvent> {
|
||||
metadata.event_type = EventType::AccountPumpFunGlobal;
|
||||
|
||||
if account.data.len() < GLOBAL_SIZE + 8 {
|
||||
return None;
|
||||
}
|
||||
if let Some(global) = global_decode(&account.data[8..GLOBAL_SIZE + 8]) {
|
||||
Some(DexEvent::PumpFunGlobalAccountEvent(PumpFunGlobalAccountEvent {
|
||||
metadata,
|
||||
pubkey: account.pubkey,
|
||||
executable: account.executable,
|
||||
lamports: account.lamports,
|
||||
owner: account.owner,
|
||||
rent_epoch: account.rent_epoch,
|
||||
global,
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user