feat: upgrade to v0.2.12 and refactor system instructions

This commit is contained in:
ysq
2025-08-03 22:51:11 +08:00
parent 45140ae789
commit e976af6935
11 changed files with 62 additions and 126 deletions
+4 -15
View File
@@ -1,5 +1,6 @@
use anyhow::{anyhow, Result};
use solana_sdk::{instruction::Instruction, signer::Signer};
use solana_system_interface::instruction::transfer;
use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
use spl_token::instruction::close_account;
@@ -105,11 +106,7 @@ impl BonkInstructionBuilder {
);
instructions.push(
// 将SOL转入wSOL ATA账户
solana_sdk::system_instruction::transfer(
&params.payer.pubkey(),
&user_quote_token_account,
amount_in,
),
transfer(&params.payer.pubkey(), &user_quote_token_account, amount_in),
);
// 同步wSOL余额
@@ -155,11 +152,7 @@ impl BonkInstructionBuilder {
data.extend_from_slice(&minimum_amount_out.to_le_bytes());
data.extend_from_slice(&share_fee_rate.to_le_bytes());
instructions.push(Instruction {
program_id: accounts::BONK,
accounts,
data,
});
instructions.push(Instruction { program_id: accounts::BONK, accounts, data });
if protocol_params.auto_handle_wsol {
// 关闭wSOL ATA账户,回收租金
@@ -262,11 +255,7 @@ impl BonkInstructionBuilder {
data.extend_from_slice(&minimum_amount_out.to_le_bytes());
data.extend_from_slice(&share_fee_rate.to_le_bytes());
instructions.push(Instruction {
program_id: accounts::BONK,
accounts,
data,
});
instructions.push(Instruction { program_id: accounts::BONK, accounts, data });
let protocol_params = params
.protocol_params
+3 -3
View File
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Result};
use solana_sdk::{instruction::Instruction, native_token::sol_to_lamports};
use solana_sdk::{instruction::Instruction, native_token::sol_str_to_lamports};
use spl_associated_token_account::{
get_associated_token_address, instruction::create_associated_token_account,
};
@@ -8,7 +8,7 @@ use spl_token::instruction::close_account;
use crate::{
constants,
trading::pumpfun::common::{
get_bonding_curve_pda, get_global_pda, get_global_volume_accumulator_pda, get_metadata_pda, get_mint_authority_pda, get_user_volume_accumulator_pda
get_bonding_curve_pda, get_global_volume_accumulator_pda, get_user_volume_accumulator_pda,
},
};
@@ -57,7 +57,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
let mut buy_token_amount =
get_buy_token_amount_from_sol_amount(&bonding_curve, params.sol_amount);
if buy_token_amount <= 100 * 1_000_000_u64 {
buy_token_amount = if max_sol_cost > sol_to_lamports(0.01) {
buy_token_amount = if max_sol_cost > sol_str_to_lamports("0.01").unwrap_or(0) {
25547619 * 1_000_000_u64
} else {
255476 * 1_000_000_u64
+9 -22
View File
@@ -1,5 +1,6 @@
use anyhow::{anyhow, Result};
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signer::Signer};
use solana_system_interface::instruction::transfer;
use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
use spl_token::instruction::close_account;
@@ -19,7 +20,9 @@ use crate::{
pumpswap::{
self,
common::{
coin_creator_vault_ata, coin_creator_vault_authority, fee_recipient_ata, find_pool, get_global_volume_accumulator_pda, get_token_amount, get_user_volume_accumulator_pda, get_wsol_amount
coin_creator_vault_ata, coin_creator_vault_authority, fee_recipient_ata, find_pool,
get_global_volume_accumulator_pda, get_token_amount,
get_user_volume_accumulator_pda, get_wsol_amount,
},
},
},
@@ -273,7 +276,7 @@ impl PumpSwapInstructionBuilder {
);
instructions.push(
// Transfer SOL to wSOL ATA account
solana_sdk::system_instruction::transfer(
transfer(
&params.payer.pubkey(),
if quote_mint_is_wsol {
&user_quote_token_account
@@ -302,11 +305,7 @@ impl PumpSwapInstructionBuilder {
instructions.push(create_associated_token_account_idempotent(
&params.payer.pubkey(),
&params.payer.pubkey(),
if quote_mint_is_wsol {
&base_mint
} else {
&quote_mint
},
if quote_mint_is_wsol { &base_mint } else { &quote_mint },
&accounts::TOKEN_PROGRAM,
));
@@ -362,11 +361,7 @@ impl PumpSwapInstructionBuilder {
data.extend_from_slice(&token_amount.to_le_bytes());
}
instructions.push(Instruction {
program_id: accounts::AMM_PROGRAM,
accounts,
data,
});
instructions.push(Instruction { program_id: accounts::AMM_PROGRAM, accounts, data });
if auto_handle_wsol {
// Close wSOL ATA account, reclaim rent
instructions.push(
@@ -465,11 +460,7 @@ impl PumpSwapInstructionBuilder {
instructions.push(create_associated_token_account_idempotent(
&params.payer.pubkey(),
&params.payer.pubkey(),
if quote_mint_is_wsol {
&base_mint
} else {
&quote_mint
},
if quote_mint_is_wsol { &base_mint } else { &quote_mint },
&accounts::TOKEN_PROGRAM,
));
@@ -521,11 +512,7 @@ impl PumpSwapInstructionBuilder {
data.extend_from_slice(&token_amount.to_le_bytes());
}
instructions.push(Instruction {
program_id: accounts::AMM_PROGRAM,
accounts,
data,
});
instructions.push(Instruction { program_id: accounts::AMM_PROGRAM, accounts, data });
if auto_handle_wsol {
instructions.push(
+11 -30
View File
@@ -1,5 +1,6 @@
use anyhow::{anyhow, Result};
use solana_sdk::{instruction::Instruction, signer::Signer};
use solana_system_interface::instruction::transfer;
use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
use spl_token::instruction::close_account;
@@ -49,9 +50,8 @@ impl RaydiumCpmmInstructionBuilder {
let pool_state = if protocol_params.pool_state.is_some() {
protocol_params.pool_state.unwrap()
} else {
let mint_token_in_pool_state_index = protocol_params
.mint_token_in_pool_state_index
.unwrap_or(1);
let mint_token_in_pool_state_index =
protocol_params.mint_token_in_pool_state_index.unwrap_or(1);
get_pool_pda(
&accounts::AMM_CONFIG,
if mint_token_in_pool_state_index == 1 {
@@ -116,11 +116,7 @@ impl RaydiumCpmmInstructionBuilder {
);
instructions.push(
// 将SOL转入wSOL ATA账户
solana_sdk::system_instruction::transfer(
&params.payer.pubkey(),
&wsol_token_account,
amount_in,
),
transfer(&params.payer.pubkey(), &wsol_token_account, amount_in),
);
// 同步wSOL余额
@@ -135,9 +131,7 @@ impl RaydiumCpmmInstructionBuilder {
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&protocol_params
.mint_token_program
.unwrap_or(accounts::TOKEN_PROGRAM),
&protocol_params.mint_token_program.unwrap_or(accounts::TOKEN_PROGRAM),
));
// 创建买入指令
@@ -152,9 +146,7 @@ impl RaydiumCpmmInstructionBuilder {
solana_sdk::instruction::AccountMeta::new(mint_vault_account, false), // Output Vault Account
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Input Token Program (readonly)
solana_sdk::instruction::AccountMeta::new_readonly(
protocol_params
.mint_token_program
.unwrap_or(accounts::TOKEN_PROGRAM),
protocol_params.mint_token_program.unwrap_or(accounts::TOKEN_PROGRAM),
false,
), // Output Token Program (readonly)
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Input token mint (readonly)
@@ -167,11 +159,7 @@ impl RaydiumCpmmInstructionBuilder {
data.extend_from_slice(&amount_in.to_le_bytes());
data.extend_from_slice(&minimum_amount_out.to_le_bytes());
instructions.push(Instruction {
program_id: accounts::RAYDIUM_CPMM,
accounts,
data,
});
instructions.push(Instruction { program_id: accounts::RAYDIUM_CPMM, accounts, data });
if protocol_params.auto_handle_wsol {
// 关闭wSOL ATA账户,回收租金
@@ -239,9 +227,8 @@ impl RaydiumCpmmInstructionBuilder {
let pool_state = if protocol_params.pool_state.is_some() {
protocol_params.pool_state.unwrap()
} else {
let mint_token_in_pool_state_index = protocol_params
.mint_token_in_pool_state_index
.unwrap_or(1);
let mint_token_in_pool_state_index =
protocol_params.mint_token_in_pool_state_index.unwrap_or(1);
get_pool_pda(
&accounts::AMM_CONFIG,
if mint_token_in_pool_state_index == 1 {
@@ -297,9 +284,7 @@ impl RaydiumCpmmInstructionBuilder {
solana_sdk::instruction::AccountMeta::new(mint_vault_account, false), // Input Vault Account
solana_sdk::instruction::AccountMeta::new(wsol_vault_account, false), // Output Vault Account
solana_sdk::instruction::AccountMeta::new_readonly(
protocol_params
.mint_token_program
.unwrap_or(accounts::TOKEN_PROGRAM),
protocol_params.mint_token_program.unwrap_or(accounts::TOKEN_PROGRAM),
false,
), // Input Token Program (readonly)
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Output Token Program (readonly)
@@ -313,11 +298,7 @@ impl RaydiumCpmmInstructionBuilder {
data.extend_from_slice(&amount.to_le_bytes());
data.extend_from_slice(&minimum_amount_out.to_le_bytes());
instructions.push(Instruction {
program_id: accounts::RAYDIUM_CPMM,
accounts,
data,
});
instructions.push(Instruction { program_id: accounts::RAYDIUM_CPMM, accounts, data });
if protocol_params.auto_handle_wsol {
instructions.push(