use super::program_ids;
use super::utils::*;
use crate::core::events::*;
use solana_sdk::{pubkey::Pubkey, signature::Signature};
pub mod discriminators {
pub const BUY: [u8; 8] = [102, 6, 61, 18, 1, 218, 235, 234];
pub const SELL: [u8; 8] = [51, 230, 133, 164, 1, 127, 131, 173];
pub const CREATE: [u8; 8] = [24, 30, 200, 40, 5, 28, 7, 119];
pub const CREATE_V2: [u8; 8] = [214, 144, 76, 236, 95, 139, 49, 180];
pub const BUY_EXACT_SOL_IN: [u8; 8] = [56, 252, 116, 8, 158, 223, 205, 95];
pub const MIGRATE_EVENT_LOG: [u8; 8] = [189, 233, 93, 185, 92, 148, 234, 148];
pub const MIGRATE_BONDING_CURVE_CREATOR: [u8; 8] = [87, 124, 52, 191, 52, 38, 214, 232];
pub const BUY_V2: [u8; 8] = [184, 23, 238, 97, 103, 197, 211, 61];
pub const SELL_V2: [u8; 8] = [93, 246, 130, 60, 231, 233, 64, 178];
pub const BUY_EXACT_QUOTE_IN_V2: [u8; 8] = [194, 171, 28, 70, 104, 77, 91, 47];
}
pub const PROGRAM_ID_PUBKEY: Pubkey = program_ids::PUMPFUN_PROGRAM_ID;
pub fn parse_instruction(
instruction_data: &[u8],
accounts: &[Pubkey],
signature: Signature,
slot: u64,
tx_index: u64,
block_time_us: Option<i64>,
grpc_recv_us: i64,
) -> Option<DexEvent> {
if instruction_data.len() < 8 {
return None;
}
let outer_disc: [u8; 8] = instruction_data[0..8].try_into().ok()?;
let data = &instruction_data[8..];
if outer_disc == discriminators::CREATE_V2 {
return parse_create_v2_instruction(
data,
accounts,
signature,
slot,
tx_index,
block_time_us,
grpc_recv_us,
);
}
if outer_disc == discriminators::CREATE {
return parse_create_instruction(
data,
accounts,
signature,
slot,
tx_index,
block_time_us,
grpc_recv_us,
);
}
if outer_disc == discriminators::BUY {
return parse_buy_instruction(
data,
accounts,
signature,
slot,
tx_index,
block_time_us,
grpc_recv_us,
"buy",
false,
);
}
if outer_disc == discriminators::BUY_EXACT_SOL_IN {
return parse_buy_instruction(
data,
accounts,
signature,
slot,
tx_index,
block_time_us,
grpc_recv_us,
"buy_exact_sol_in",
true,
);
}
if outer_disc == discriminators::SELL {
return parse_sell_instruction(
data,
accounts,
signature,
slot,
tx_index,
block_time_us,
grpc_recv_us,
"sell",
false,
);
}
if outer_disc == discriminators::BUY_V2 {
return parse_buy_v2_instruction(
data,
accounts,
signature,
slot,
tx_index,
block_time_us,
grpc_recv_us,
"buy_v2",
false,
);
}
if outer_disc == discriminators::BUY_EXACT_QUOTE_IN_V2 {
return parse_buy_v2_instruction(
data,
accounts,
signature,
slot,
tx_index,
block_time_us,
grpc_recv_us,
"buy_exact_quote_in_v2",
true,
);
}
if outer_disc == discriminators::SELL_V2 {
return parse_sell_v2_instruction(
data,
accounts,
signature,
slot,
tx_index,
block_time_us,
grpc_recv_us,
"sell_v2",
);
}
if instruction_data.len() >= 16 {
let cpi_disc: [u8; 8] = instruction_data[8..16].try_into().ok()?;
if cpi_disc == discriminators::MIGRATE_EVENT_LOG {
return parse_migrate_log_instruction(
&instruction_data[16..],
accounts,
signature,
slot,
tx_index,
block_time_us,
grpc_recv_us,
);
}
}
None
}
fn parse_buy_instruction(
data: &[u8],
accounts: &[Pubkey],
signature: Signature,
slot: u64,
tx_index: u64,
block_time_us: Option<i64>,
grpc_recv_us: i64,
ix_name: &'static str,
exact_quote_in: bool,
) -> Option<DexEvent> {
if accounts.len() < 7 {
return None;
}
let (first_arg, second_arg) = if data.len() >= 16 {
(read_u64_le(data, 0).unwrap_or(0), read_u64_le(data, 8).unwrap_or(0))
} else {
(0, 0)
};
let (token_amount, sol_amount, amount, max_sol_cost) = if exact_quote_in {
(second_arg, first_arg, 0, 0)
} else {
(first_arg, second_arg, first_arg, second_arg)
};
let mint = get_account(accounts, 2)?;
let metadata =
create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), grpc_recv_us);
let trade_event = PumpFunTradeEvent {
metadata,
mint,
is_buy: true,
bonding_curve: get_account(accounts, 3).unwrap_or_default(),
user: get_account(accounts, 6).unwrap_or_default(),
sol_amount,
token_amount,
amount,
max_sol_cost,
fee_recipient: get_account(accounts, 1).unwrap_or_default(),
associated_bonding_curve: get_account(accounts, 4).unwrap_or_default(),
token_program: get_account(accounts, 8).unwrap_or_default(),
creator_vault: get_account(accounts, 9).unwrap_or_default(),
ix_name: ix_name.to_string(),
..Default::default()
};
if exact_quote_in {
Some(DexEvent::PumpFunBuyExactSolIn(trade_event))
} else {
Some(DexEvent::PumpFunBuy(trade_event))
}
}
fn parse_sell_instruction(
data: &[u8],
accounts: &[Pubkey],
signature: Signature,
slot: u64,
tx_index: u64,
block_time_us: Option<i64>,
grpc_recv_us: i64,
ix_name: &'static str,
v2_accounts: bool,
) -> Option<DexEvent> {
let min_accounts = if v2_accounts { 26 } else { 7 };
if accounts.len() < min_accounts {
return None;
}
let (amount, min_sol_output) = if data.len() >= 16 {
(read_u64_le(data, 0).unwrap_or(0), read_u64_le(data, 8).unwrap_or(0))
} else {
(0, 0)
};
let token_amount = amount;
let sol_amount = min_sol_output;
let (
mint_idx,
bonding_curve_idx,
associated_bonding_curve_idx,
user_idx,
fee_recipient_idx,
token_program_idx,
creator_vault_idx,
) = if v2_accounts { (1, 10, 11, 13, 6, 3, 16) } else { (2, 3, 4, 6, 1, 9, 8) };
let mint = get_account(accounts, mint_idx)?;
let metadata =
create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), grpc_recv_us);
Some(DexEvent::PumpFunSell(PumpFunTradeEvent {
metadata,
mint,
is_buy: false,
bonding_curve: get_account(accounts, bonding_curve_idx).unwrap_or_default(),
user: get_account(accounts, user_idx).unwrap_or_default(),
sol_amount,
token_amount,
amount,
min_sol_output,
fee_recipient: get_account(accounts, fee_recipient_idx).unwrap_or_default(),
associated_bonding_curve: get_account(accounts, associated_bonding_curve_idx)
.unwrap_or_default(),
token_program: get_account(accounts, token_program_idx).unwrap_or_default(),
creator_vault: get_account(accounts, creator_vault_idx).unwrap_or_default(),
ix_name: ix_name.to_string(),
..Default::default()
}))
}
fn parse_buy_v2_instruction(
data: &[u8],
accounts: &[Pubkey],
signature: Signature,
slot: u64,
tx_index: u64,
block_time_us: Option<i64>,
grpc_recv_us: i64,
ix_name: &'static str,
exact_quote_in: bool,
) -> Option<DexEvent> {
const MIN_ACC: usize = 27;
if accounts.len() < MIN_ACC {
return None;
}
let (first_arg, second_arg) = if data.len() >= 16 {
(read_u64_le(data, 0).unwrap_or(0), read_u64_le(data, 8).unwrap_or(0))
} else {
(0, 0)
};
let (token_amount, sol_amount, amount, max_sol_cost) = if exact_quote_in {
(second_arg, first_arg, 0, 0)
} else {
(first_arg, second_arg, first_arg, second_arg)
};
let metadata =
create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), grpc_recv_us);
let trade_event = PumpFunTradeEvent {
metadata,
mint: accounts[1],
is_buy: true,
bonding_curve: accounts[10],
associated_bonding_curve: accounts[11],
user: accounts[13],
sol_amount,
token_amount,
amount,
max_sol_cost,
fee_recipient: accounts[6],
token_program: accounts[3],
creator_vault: accounts[16],
ix_name: ix_name.to_string(),
..Default::default()
};
if exact_quote_in {
Some(DexEvent::PumpFunBuyExactSolIn(trade_event))
} else {
Some(DexEvent::PumpFunBuy(trade_event))
}
}
fn parse_sell_v2_instruction(
data: &[u8],
accounts: &[Pubkey],
signature: Signature,
slot: u64,
tx_index: u64,
block_time_us: Option<i64>,
grpc_recv_us: i64,
ix_name: &'static str,
) -> Option<DexEvent> {
parse_sell_instruction(
data,
accounts,
signature,
slot,
tx_index,
block_time_us,
grpc_recv_us,
ix_name,
true,
)
}
fn parse_create_instruction(
data: &[u8],
accounts: &[Pubkey],
signature: Signature,
slot: u64,
tx_index: u64,
block_time_us: Option<i64>,
grpc_recv_us: i64,
) -> Option<DexEvent> {
if accounts.len() < 8 {
return None;
}
let mut offset = 0;
let name = if let Some((s, len)) = read_str_unchecked(data, offset) {
offset += len;
s.to_string()
} else {
String::new()
};
let symbol = if let Some((s, len)) = read_str_unchecked(data, offset) {
offset += len;
s.to_string()
} else {
String::new()
};
let uri = if let Some((s, len)) = read_str_unchecked(data, offset) {
offset += len;
s.to_string()
} else {
String::new()
};
if data.len() < offset + 32 + 32 + 32 + 32 {
return None;
}
let mint = read_pubkey(data, offset).unwrap_or_default();
offset += 32;
let bonding_curve = read_pubkey(data, offset).unwrap_or_default();
offset += 32;
let user = read_pubkey(data, offset).unwrap_or_default();
offset += 32;
let creator = read_pubkey(data, offset).unwrap_or_default();
let metadata =
create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), grpc_recv_us);
Some(DexEvent::PumpFunCreate(PumpFunCreateTokenEvent {
metadata,
name,
symbol,
uri,
mint,
bonding_curve,
user,
creator,
..Default::default()
}))
}
fn parse_create_v2_instruction(
data: &[u8],
accounts: &[Pubkey],
signature: Signature,
slot: u64,
tx_index: u64,
block_time_us: Option<i64>,
grpc_recv_us: i64,
) -> Option<DexEvent> {
const CREATE_V2_MIN_ACCOUNTS: usize = 16;
if accounts.len() < CREATE_V2_MIN_ACCOUNTS {
return None;
}
let acc = &accounts[0..CREATE_V2_MIN_ACCOUNTS];
let mut offset = 0usize;
let name = if let Some((s, len)) = read_str_unchecked(data, offset) {
offset += len;
s.to_string()
} else {
String::new()
};
let symbol = if let Some((s, len)) = read_str_unchecked(data, offset) {
offset += len;
s.to_string()
} else {
String::new()
};
let uri = if let Some((s, len)) = read_str_unchecked(data, offset) {
offset += len;
s.to_string()
} else {
String::new()
};
if data.len() < offset + 32 + 1 {
return None;
}
let creator = read_pubkey(data, offset)?;
offset += 32;
let is_mayhem_mode = read_bool(data, offset)?;
offset += 1;
let is_cashback_enabled = read_option_bool_idl(data, offset).unwrap_or(false);
let mint = acc[0];
let bonding_curve = acc[2];
let user = acc[5];
let metadata =
create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), grpc_recv_us);
Some(DexEvent::PumpFunCreateV2(PumpFunCreateV2TokenEvent {
metadata,
name,
symbol,
uri,
mint,
bonding_curve,
user,
creator,
mint_authority: acc[1],
associated_bonding_curve: acc[3],
global: acc[4],
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],
is_mayhem_mode,
is_cashback_enabled,
..Default::default()
}))
}
#[allow(unused_variables)]
fn parse_migrate_log_instruction(
data: &[u8],
accounts: &[Pubkey],
signature: Signature,
slot: u64,
tx_index: u64,
block_time_us: Option<i64>,
rpc_recv_us: i64,
) -> Option<DexEvent> {
let mut offset = 0;
let user = read_pubkey(data, offset)?;
offset += 32;
let mint = read_pubkey(data, offset)?;
offset += 32;
let mint_amount = read_u64_le(data, offset)?;
offset += 8;
let sol_amount = read_u64_le(data, offset)?;
offset += 8;
let pool_migration_fee = read_u64_le(data, offset)?;
offset += 8;
let bonding_curve = read_pubkey(data, offset)?;
offset += 32;
let timestamp = read_u64_le(data, offset)? as i64;
offset += 8;
let pool = read_pubkey(data, offset)?;
let metadata =
create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), rpc_recv_us);
Some(DexEvent::PumpFunMigrate(PumpFunMigrateEvent {
metadata,
user,
mint,
mint_amount,
sol_amount,
pool_migration_fee,
bonding_curve,
timestamp,
pool,
}))
}
#[cfg(test)]
mod tests {
use super::*;
fn instruction_data(discriminator: [u8; 8], first: u64, second: u64) -> Vec<u8> {
let mut data = Vec::with_capacity(24);
data.extend_from_slice(&discriminator);
data.extend_from_slice(&first.to_le_bytes());
data.extend_from_slice(&second.to_le_bytes());
data
}
fn accounts(n: usize) -> Vec<Pubkey> {
(0..n).map(|_| Pubkey::new_unique()).collect()
}
#[test]
fn pumpfun_buy_instruction_exposes_raw_args() {
let data = instruction_data(discriminators::BUY, 123, 456);
let acc = accounts(15);
let event =
parse_instruction(&data, &acc, Signature::default(), 1, 0, None, 99).expect("event");
match event {
DexEvent::PumpFunBuy(t) => {
assert_eq!(t.amount, 123);
assert_eq!(t.max_sol_cost, 456);
assert_eq!(t.min_sol_output, 0);
assert_eq!(t.token_amount, 123);
assert_eq!(t.sol_amount, 456);
assert_eq!(t.ix_name, "buy");
}
other => panic!("expected PumpFunBuy, got {other:?}"),
}
}
#[test]
fn pumpfun_sell_instruction_exposes_raw_args() {
let data = instruction_data(discriminators::SELL, 321, 654);
let acc = accounts(14);
let event =
parse_instruction(&data, &acc, Signature::default(), 1, 0, None, 99).expect("event");
match event {
DexEvent::PumpFunSell(t) => {
assert_eq!(t.amount, 321);
assert_eq!(t.max_sol_cost, 0);
assert_eq!(t.min_sol_output, 654);
assert_eq!(t.token_amount, 321);
assert_eq!(t.sol_amount, 654);
assert_eq!(t.ix_name, "sell");
}
other => panic!("expected PumpFunSell, got {other:?}"),
}
}
#[test]
fn pumpfun_v2_instruction_args_use_v2_account_layout() {
let data = instruction_data(discriminators::BUY_V2, 777, 888);
let acc = accounts(27);
let event =
parse_instruction(&data, &acc, Signature::default(), 1, 0, None, 99).expect("event");
match event {
DexEvent::PumpFunBuy(t) => {
assert_eq!(t.amount, 777);
assert_eq!(t.max_sol_cost, 888);
assert_eq!(t.mint, acc[1]);
assert_eq!(t.bonding_curve, acc[10]);
assert_eq!(t.associated_bonding_curve, acc[11]);
assert_eq!(t.user, acc[13]);
assert_eq!(t.ix_name, "buy_v2");
}
other => panic!("expected PumpFunBuy, got {other:?}"),
}
}
}