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)
}
}
+28 -2
View File
@@ -1,5 +1,5 @@
use solana_sdk::pubkey::Pubkey;
use crate::constants;
use solana_sdk::pubkey::Pubkey;
pub fn get_amount_out(
amount_in: u64,
@@ -53,4 +53,30 @@ pub fn get_vault_pda(pool_state: &Pubkey, mint: &Pubkey) -> Option<Pubkey> {
let program_id: &Pubkey = &constants::bonk::accounts::BONK;
let pda: Option<(Pubkey, u8)> = Pubkey::try_find_program_address(seeds, program_id);
pda.map(|pubkey| pubkey.0)
}
}
pub fn get_token_price(
virtual_base: u128,
virtual_quote: u128,
real_base: u128,
real_quote: u128,
decimal_base: u64,
decimal_quote: u64,
) -> f64 {
// 计算小数位数差异
let decimal_diff = decimal_quote as i32 - decimal_base as i32;
let decimal_factor = if decimal_diff >= 0 {
10_f64.powi(decimal_diff)
} else {
1.0 / 10_f64.powi(-decimal_diff)
};
// 计算价格前的状态
let quote_reserves = virtual_quote.checked_add(real_quote).unwrap();
let base_reserves = virtual_base.checked_sub(real_base).unwrap();
// 使用浮点数计算价格,避免整数除法的精度丢失
let price = (quote_reserves as f64) / (base_reserves as f64) / decimal_factor;
price
}
+22 -4
View File
@@ -22,10 +22,6 @@ impl SolanaTrade {
payer: &Pubkey,
mint: &Pubkey,
) -> Result<u64, anyhow::Error> {
println!(
"get_token_balance payer: {}, mint: {}, rpc_url: {}",
payer, mint, self.trade_config.rpc_url
);
trading::common::utils::get_token_balance(&self.rpc, payer, mint).await
}
@@ -162,4 +158,26 @@ impl SolanaTrade {
Ok(base_amount)
}
// -------------------------------- Bonk --------------------------------
#[inline]
pub fn get_bonk_token_price(
&self,
virtual_base: u128,
virtual_quote: u128,
real_base: u128,
real_quote: u128,
decimal_base: u64,
decimal_quote: u64,
) -> f64 {
trading::bonk::common::get_token_price(
virtual_base,
virtual_quote,
real_base,
real_quote,
decimal_base,
decimal_quote,
)
}
}