Fix trade execution correctness and release 4.0.11

This commit is contained in:
0xfnzero
2026-05-21 17:50:06 +08:00
parent 0cf0064e80
commit 8ed2aef931
10 changed files with 186 additions and 132 deletions
+18 -23
View File
@@ -30,24 +30,20 @@ use crate::{
},
};
use anyhow::{anyhow, Result};
use solana_sdk::instruction::AccountMeta;
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signer::Signer};
use solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
signer::Signer,
};
#[inline]
fn is_pump_suffix_mint(mint: &Pubkey) -> bool {
mint.to_string().ends_with("pump")
}
#[inline]
fn effective_pump_mint_token_program(mint: &Pubkey, protocol_params: &PumpFunParams) -> Pubkey {
fn effective_pump_mint_token_program(protocol_params: &PumpFunParams) -> Pubkey {
let tp = protocol_params.token_program;
if tp == Pubkey::default() || tp == TOKEN_PROGRAM_2022 {
return TOKEN_PROGRAM_2022;
if tp == Pubkey::default() {
TOKEN_PROGRAM_2022
} else {
tp
}
if is_pump_suffix_mint(mint) {
return TOKEN_PROGRAM_2022;
}
tp
}
/// Resolve quote mint and its token program from PumpFunParams.
@@ -123,7 +119,7 @@ fn build_buy_v1(params: &SwapParams) -> Result<Vec<Instruction>> {
})?;
let is_mayhem_mode = bonding_curve.is_mayhem_mode;
let token_program = effective_pump_mint_token_program(&params.output_mint, protocol_params);
let token_program = effective_pump_mint_token_program(protocol_params);
let token_program_meta = if token_program == TOKEN_PROGRAM_2022 {
crate::constants::TOKEN_PROGRAM_2022_META
} else {
@@ -277,7 +273,7 @@ fn build_sell_v1(params: &SwapParams) -> Result<Vec<Instruction>> {
})?;
let is_mayhem_mode = bonding_curve.is_mayhem_mode;
let token_program = effective_pump_mint_token_program(&params.input_mint, protocol_params);
let token_program = effective_pump_mint_token_program(protocol_params);
let token_program_meta = if token_program == TOKEN_PROGRAM_2022 {
crate::constants::TOKEN_PROGRAM_2022_META
} else {
@@ -391,8 +387,7 @@ fn build_buy_v2(params: &SwapParams) -> Result<Vec<Instruction>> {
})?;
let is_mayhem_mode = bonding_curve.is_mayhem_mode;
let base_token_program =
effective_pump_mint_token_program(&params.output_mint, protocol_params);
let base_token_program = effective_pump_mint_token_program(protocol_params);
let base_token_program_meta = if base_token_program == TOKEN_PROGRAM_2022 {
crate::constants::TOKEN_PROGRAM_2022_META
} else {
@@ -612,7 +607,7 @@ fn build_sell_v2(params: &SwapParams) -> Result<Vec<Instruction>> {
})?;
let is_mayhem_mode = bonding_curve.is_mayhem_mode;
let base_token_program = effective_pump_mint_token_program(&params.input_mint, protocol_params);
let base_token_program = effective_pump_mint_token_program(protocol_params);
let base_token_program_meta = if base_token_program == TOKEN_PROGRAM_2022 {
crate::constants::TOKEN_PROGRAM_2022_META
} else {
@@ -773,7 +768,7 @@ mod tests {
use super::*;
use crate::{
common::{bonding_curve::BondingCurveAccount, GasFeeStrategy},
constants::{TOKEN_PROGRAM, TOKEN_PROGRAM_2022},
constants::TOKEN_PROGRAM,
trading::core::params::{DexParamEnum, PumpFunParams, SwapParams},
};
use solana_sdk::signature::Keypair;
@@ -857,14 +852,14 @@ mod tests {
}
#[test]
fn pump_suffix_buy_forces_token_2022_even_when_params_legacy() {
fn pump_suffix_buy_respects_explicit_legacy_token_program() {
crate::common::seed::set_default_rents();
let params = swap_params_for_buy(pump_mint(), TOKEN_PROGRAM);
let instructions = build_buy_v1(&params).unwrap();
assert_eq!(instructions.len(), 3);
assert_eq!(instructions[2].accounts[8].pubkey, TOKEN_PROGRAM_2022);
assert_eq!(instructions[1].program_id, TOKEN_PROGRAM_2022);
assert_eq!(instructions[2].accounts[8].pubkey, TOKEN_PROGRAM);
assert_eq!(instructions[1].program_id, TOKEN_PROGRAM);
assert_eq!(instructions[2].accounts.len(), 18);
assert_eq!(instructions[2].data.len(), 25);
}