feat(pump): add buy_v2, sell_v2, buy_exact_quote_in_v2 instruction builders

Support Pump.fun Bonding Curve v2 unified trading interface with quote_mint
parameter for SOL-paired (WSOL) and USDC-paired coins.

- Add BUY_V2, SELL_V2, BUY_EXACT_QUOTE_IN_V2 discriminators
- Implement 27/26-account v2 instruction layouts matching official IDL
- Add PumpFunParams::quote_mint and use_v2_ix fields (default legacy)
- Add with_quote_mint() builder, buyback fee recipient pool, and PDA helpers
- Add v2 ix data encoders in pumpfun_ix_data.rs
- Legacy instructions remain default for backward compatibility

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
0xfnzero
2026-05-08 20:48:11 +08:00
co-authored by Claude Opus 4.7
parent 7a8d2a9c17
commit 60d1db4755
7 changed files with 379 additions and 9 deletions
+203 -6
View File
@@ -10,12 +10,14 @@ use crate::{
};
use crate::{
instruction::pumpfun_ix_data::{
encode_pumpfun_buy_exact_sol_in_ix_data, encode_pumpfun_buy_ix_data,
encode_pumpfun_sell_ix_data, TRACK_VOLUME_TRUE,
encode_pumpfun_buy_exact_quote_in_v2_ix_data, encode_pumpfun_buy_exact_sol_in_ix_data,
encode_pumpfun_buy_ix_data, encode_pumpfun_buy_v2_ix_data,
encode_pumpfun_sell_ix_data, encode_pumpfun_sell_v2_ix_data, TRACK_VOLUME_TRUE,
},
instruction::utils::pumpfun::{
accounts, get_bonding_curve_pda, get_bonding_curve_v2_pda,
get_protocol_extra_fee_recipient_random, get_user_volume_accumulator_pda,
get_buyback_fee_recipient_random, get_protocol_extra_fee_recipient_random,
get_quote_token_program, get_user_volume_accumulator_pda,
pump_fun_fee_recipient_meta, resolve_creator_vault_for_ix_with_fee_sharing,
global_constants::{self},
},
@@ -128,6 +130,106 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
);
}
let fee_recipient_meta =
pump_fun_fee_recipient_meta(protocol_params.fee_recipient, is_mayhem_mode);
// ── V2 instruction path (buy_v2 / buy_exact_quote_in_v2, 27 fixed accounts) ──
if protocol_params.use_v2_ix {
let quote_mint = if protocol_params.quote_mint != Pubkey::default() {
protocol_params.quote_mint
} else {
crate::constants::WSOL_TOKEN_ACCOUNT
};
let quote_token_program = get_quote_token_program(&quote_mint);
let quote_token_program_meta = if quote_token_program == crate::constants::TOKEN_PROGRAM {
crate::constants::TOKEN_PROGRAM_META
} else {
crate::constants::TOKEN_PROGRAM_2022_META
};
let associated_quote_bonding_curve =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&bonding_curve_addr, &quote_mint, &quote_token_program,
);
let associated_quote_user =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(), &quote_mint, &quote_token_program,
params.open_seed_optimize,
);
let associated_quote_fee_recipient =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&fee_recipient_meta.pubkey, &quote_mint, &quote_token_program,
);
let buyback_fee_recipient =
get_buyback_fee_recipient_random();
let associated_quote_buyback_fee_recipient =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&buyback_fee_recipient, &quote_mint, &quote_token_program,
);
let associated_creator_vault =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&creator_vault_account, &quote_mint, &quote_token_program,
);
let associated_user_volume_accumulator =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&user_volume_accumulator, &quote_mint, &quote_token_program,
);
let sharing_config =
crate::instruction::utils::pumpfun::get_fee_sharing_config_pda(&params.output_mint)
.ok_or_else(|| anyhow!("sharing_config PDA derivation failed"))?;
let v2_data = if params.use_exact_sol_amount.unwrap_or(true) {
let min_tokens_out = calculate_with_slippage_sell(buy_token_amount, slippage_bp);
encode_pumpfun_buy_exact_quote_in_v2_ix_data(lamports_in, min_tokens_out)
} else {
encode_pumpfun_buy_v2_ix_data(buy_token_amount, max_sol_cost)
};
let v2_metas: Vec<AccountMeta> = vec![
global_constants::GLOBAL_ACCOUNT_META,
AccountMeta::new_readonly(params.output_mint, false),
AccountMeta::new_readonly(quote_mint, false),
token_program_meta,
quote_token_program_meta,
crate::constants::ASSOCIATED_TOKEN_PROGRAM_META,
fee_recipient_meta,
AccountMeta::new(associated_quote_fee_recipient, false),
AccountMeta::new_readonly(buyback_fee_recipient, false),
AccountMeta::new(associated_quote_buyback_fee_recipient, false),
AccountMeta::new(bonding_curve_addr, false),
AccountMeta::new(associated_bonding_curve, false),
AccountMeta::new(associated_quote_bonding_curve, false),
AccountMeta::new(params.payer.pubkey(), true),
AccountMeta::new(user_token_account, false),
AccountMeta::new(associated_quote_user, false),
AccountMeta::new(creator_vault_account, false),
AccountMeta::new(associated_creator_vault, false),
AccountMeta::new_readonly(sharing_config, false),
accounts::GLOBAL_VOLUME_ACCUMULATOR_META,
AccountMeta::new(user_volume_accumulator, false),
AccountMeta::new(associated_user_volume_accumulator, false),
accounts::FEE_CONFIG_META,
accounts::FEE_PROGRAM_META,
crate::constants::SYSTEM_PROGRAM_META,
accounts::EVENT_AUTHORITY_META,
accounts::PUMPFUN_META,
];
if quote_mint != crate::constants::WSOL_TOKEN_ACCOUNT {
let create_quote_ata = crate::common::fast_fn::create_associated_token_account_idempotent_fast_use_seed(
&params.payer.pubkey(), &params.payer.pubkey(),
&quote_mint, &quote_token_program, params.open_seed_optimize,
);
if !create_quote_ata.is_empty() {
instructions.extend(create_quote_ata);
}
}
instructions.push(Instruction::new_with_bytes(accounts::PUMPFUN, &v2_data, v2_metas));
return Ok(instructions);
}
// ── Legacy buy path ──
let buy_data = if params.use_exact_sol_amount.unwrap_or(true) {
let min_tokens_out = calculate_with_slippage_sell(buy_token_amount, slippage_bp);
encode_pumpfun_buy_exact_sol_in_ix_data(
@@ -139,9 +241,6 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
encode_pumpfun_buy_ix_data(buy_token_amount, max_sol_cost, TRACK_VOLUME_TRUE)
};
let fee_recipient_meta =
pump_fun_fee_recipient_meta(protocol_params.fee_recipient, is_mayhem_mode);
let bonding_curve_v2 = get_bonding_curve_v2_pda(&params.output_mint).ok_or_else(|| {
anyhow!("bonding_curve_v2 PDA derivation failed for mint {}", params.output_mint)
})?;
@@ -251,6 +350,104 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
);
let mut instructions = Vec::with_capacity(2);
// ── V2 sell path (sell_v2, 26 fixed accounts) ──
if protocol_params.use_v2_ix {
let quote_mint = if protocol_params.quote_mint != Pubkey::default() {
protocol_params.quote_mint
} else {
crate::constants::WSOL_TOKEN_ACCOUNT
};
let quote_token_program = get_quote_token_program(&quote_mint);
let quote_token_program_meta = if quote_token_program == crate::constants::TOKEN_PROGRAM {
crate::constants::TOKEN_PROGRAM_META
} else {
crate::constants::TOKEN_PROGRAM_2022_META
};
let associated_quote_bonding_curve =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&bonding_curve_addr, &quote_mint, &quote_token_program,
);
let associated_quote_user =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(), &quote_mint, &quote_token_program,
params.open_seed_optimize,
);
let sell_fee_recipient_meta =
pump_fun_fee_recipient_meta(protocol_params.fee_recipient, is_mayhem_mode);
let associated_quote_fee_recipient =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&sell_fee_recipient_meta.pubkey, &quote_mint, &quote_token_program,
);
let buyback_fee_recipient =
get_buyback_fee_recipient_random();
let associated_quote_buyback_fee_recipient =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&buyback_fee_recipient, &quote_mint, &quote_token_program,
);
let associated_creator_vault =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&creator_vault_account, &quote_mint, &quote_token_program,
);
let user_volume_accumulator = get_user_volume_accumulator_pda(&params.payer.pubkey())
.ok_or_else(|| anyhow!("user_volume_accumulator PDA derivation failed"))?;
let associated_user_volume_accumulator =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&user_volume_accumulator, &quote_mint, &quote_token_program,
);
let sharing_config =
crate::instruction::utils::pumpfun::get_fee_sharing_config_pda(&params.input_mint)
.ok_or_else(|| anyhow!("sharing_config PDA derivation failed"))?;
let v2_sell_data = encode_pumpfun_sell_v2_ix_data(token_amount, min_sol_output);
let v2_sell_metas: Vec<AccountMeta> = vec![
global_constants::GLOBAL_ACCOUNT_META,
AccountMeta::new_readonly(params.input_mint, false),
AccountMeta::new_readonly(quote_mint, false),
token_program_meta,
quote_token_program_meta,
crate::constants::ASSOCIATED_TOKEN_PROGRAM_META,
sell_fee_recipient_meta,
AccountMeta::new(associated_quote_fee_recipient, false),
AccountMeta::new_readonly(buyback_fee_recipient, false),
AccountMeta::new(associated_quote_buyback_fee_recipient, false),
AccountMeta::new(bonding_curve_addr, false),
AccountMeta::new(associated_bonding_curve, false),
AccountMeta::new(associated_quote_bonding_curve, false),
AccountMeta::new(params.payer.pubkey(), true),
AccountMeta::new(user_token_account, false),
AccountMeta::new(associated_quote_user, false),
AccountMeta::new(creator_vault_account, false),
AccountMeta::new(associated_creator_vault, false),
AccountMeta::new_readonly(sharing_config, false),
AccountMeta::new(user_volume_accumulator, false),
AccountMeta::new(associated_user_volume_accumulator, false),
accounts::FEE_CONFIG_META,
accounts::FEE_PROGRAM_META,
crate::constants::SYSTEM_PROGRAM_META,
accounts::EVENT_AUTHORITY_META,
accounts::PUMPFUN_META,
];
instructions.push(Instruction::new_with_bytes(accounts::PUMPFUN, &v2_sell_data, v2_sell_metas));
if protocol_params.close_token_account_when_sell.unwrap_or(false)
|| params.close_input_mint_ata
{
instructions.push(close_account(
&token_program, &user_token_account,
&params.payer.pubkey(), &params.payer.pubkey(),
&[&params.payer.pubkey()],
)?);
}
return Ok(instructions);
}
// ── Legacy sell path ──
let sell_data = encode_pumpfun_sell_ix_data(token_amount, min_sol_output);
let fee_recipient_meta =
pump_fun_fee_recipient_meta(protocol_params.fee_recipient, is_mayhem_mode);