diff --git a/src/instruction/bonk.rs b/src/instruction/bonk.rs index 84d0b81..5ee4741 100755 --- a/src/instruction/bonk.rs +++ b/src/instruction/bonk.rs @@ -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( - ¶ms.payer.pubkey(), - ¶ms.payer.pubkey(), - ¶ms.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, ¶ms.payer.pubkey(), ¶ms.payer.pubkey(), &[¶ms.payer.pubkey()]).unwrap()); + Ok(instructions) } } diff --git a/src/trading/bonk/common.rs b/src/trading/bonk/common.rs index ede49d6..ddacf8c 100755 --- a/src/trading/bonk/common.rs +++ b/src/trading/bonk/common.rs @@ -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 { 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) -} \ No newline at end of file +} + +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 +} diff --git a/src/utils.rs b/src/utils.rs index bdc9f12..75dfe21 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -22,10 +22,6 @@ impl SolanaTrade { payer: &Pubkey, mint: &Pubkey, ) -> Result { - 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, + ) + } }