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:
@@ -1,6 +1,7 @@
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use solana_sdk::{instruction::Instruction, signer::Signer};
|
use solana_sdk::{instruction::Instruction, signer::Signer};
|
||||||
use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
|
use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
|
||||||
|
use spl_token::instruction::close_account;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
constants::bonk::{
|
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![
|
let accounts = vec![
|
||||||
solana_sdk::instruction::AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
|
solana_sdk::instruction::AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
|
||||||
@@ -281,6 +274,8 @@ impl BonkInstructionBuilder {
|
|||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
instructions.push(close_account(&accounts::TOKEN_PROGRAM, &user_quote_token_account, ¶ms.payer.pubkey(), ¶ms.payer.pubkey(), &[¶ms.payer.pubkey()]).unwrap());
|
||||||
|
|
||||||
Ok(instructions)
|
Ok(instructions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use solana_sdk::pubkey::Pubkey;
|
|
||||||
use crate::constants;
|
use crate::constants;
|
||||||
|
use solana_sdk::pubkey::Pubkey;
|
||||||
|
|
||||||
pub fn get_amount_out(
|
pub fn get_amount_out(
|
||||||
amount_in: u64,
|
amount_in: u64,
|
||||||
@@ -54,3 +54,29 @@ pub fn get_vault_pda(pool_state: &Pubkey, mint: &Pubkey) -> Option<Pubkey> {
|
|||||||
let pda: Option<(Pubkey, u8)> = Pubkey::try_find_program_address(seeds, program_id);
|
let pda: Option<(Pubkey, u8)> = Pubkey::try_find_program_address(seeds, program_id);
|
||||||
pda.map(|pubkey| pubkey.0)
|
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
@@ -22,10 +22,6 @@ impl SolanaTrade {
|
|||||||
payer: &Pubkey,
|
payer: &Pubkey,
|
||||||
mint: &Pubkey,
|
mint: &Pubkey,
|
||||||
) -> Result<u64, anyhow::Error> {
|
) -> 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
|
trading::common::utils::get_token_balance(&self.rpc, payer, mint).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,4 +158,26 @@ impl SolanaTrade {
|
|||||||
|
|
||||||
Ok(base_amount)
|
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,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user