feat: optimize Bonk trading instructions and add price calculation

- Optimize sell instruction building, remove redundant code and add account closing
- Add token price calculation function supporting different decimal places
- Clean up debug logs and refactor code structure
This commit is contained in:
ysq
2025-07-12 01:14:05 +08:00
parent 9946637c62
commit c4bf62be7d
3 changed files with 53 additions and 14 deletions
+3 -8
View File
@@ -1,6 +1,7 @@
use anyhow::{anyhow, Result};
use solana_sdk::{instruction::Instruction, signer::Signer};
use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
use spl_token::instruction::close_account;
use crate::{
constants::bonk::{
@@ -241,14 +242,6 @@ impl BonkInstructionBuilder {
),
);
// 创建用户的代币账户
instructions.push(create_associated_token_account_idempotent(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&accounts::TOKEN_PROGRAM,
));
// 创建卖出指令
let accounts = vec![
solana_sdk::instruction::AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
@@ -281,6 +274,8 @@ impl BonkInstructionBuilder {
data,
});
instructions.push(close_account(&accounts::TOKEN_PROGRAM, &user_quote_token_account, &params.payer.pubkey(), &params.payer.pubkey(), &[&params.payer.pubkey()]).unwrap());
Ok(instructions)
}
}