feat: optimize Bonk protocol with caching and wSOL management

- Add instruction/PDA caching for Bonk operations
- Implement reusable wSOL management module
- Support pre-computed pool addresses in BonkParams
- Optimize memory allocation with SmallVec and fixed arrays
- Reduce code duplication across protocols
This commit is contained in:
ysq
2025-09-08 18:29:05 +08:00
parent 47bc81f025
commit ea6c3e9989
6 changed files with 227 additions and 154 deletions
+3 -1
View File
@@ -3,10 +3,12 @@ pub mod transaction_builder;
pub mod compute_budget_manager;
pub mod address_lookup_manager;
pub mod utils;
pub mod wsol_manager;
// Re-export commonly used functions
pub use nonce_manager::*;
pub use transaction_builder::*;
pub use compute_budget_manager::*;
pub use address_lookup_manager::*;
pub use utils::*;
pub use utils::*;
pub use wsol_manager::*;
+65
View File
@@ -0,0 +1,65 @@
use crate::common::fast_fn::create_associated_token_account_idempotent_fast;
use smallvec::SmallVec;
use solana_sdk::{instruction::Instruction, pubkey::Pubkey};
use solana_system_interface::instruction::transfer;
use spl_token::instruction::close_account;
#[inline]
pub fn handle_wsol(payer: &Pubkey, amount_in: u64) -> SmallVec<[Instruction; 3]> {
let wsol_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
);
let mut insts = SmallVec::<[Instruction; 3]>::new();
insts.extend([
create_associated_token_account_idempotent_fast(
&payer,
&payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
),
transfer(&payer, &wsol_token_account, amount_in),
spl_token::instruction::sync_native(&crate::constants::TOKEN_PROGRAM, &wsol_token_account)
.unwrap(),
]);
insts
}
pub fn close_wsol(payer: &Pubkey) -> Instruction {
let wsol_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
);
crate::common::fast_fn::get_cached_instruction(
crate::common::fast_fn::InstructionCacheKey::CloseWsolAccount {
payer: *payer,
wsol_token_account,
},
|| {
close_account(
&crate::constants::TOKEN_PROGRAM,
&wsol_token_account,
&payer,
&payer,
&[],
)
.unwrap()
},
)
}
#[inline]
pub fn create_wsol_ata(payer: &Pubkey) -> Instruction {
create_associated_token_account_idempotent_fast(
&payer,
&payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
)
}
+12
View File
@@ -243,6 +243,9 @@ pub struct BonkParams {
pub virtual_quote: u128,
pub real_base: u128,
pub real_quote: u128,
pub pool_state: Pubkey,
pub base_vault: Pubkey,
pub quote_vault: Pubkey,
/// Token program ID
/// Specifies the program used by the token, usually spl_token::ID or spl_token_2022::ID
pub mint_token_program: Pubkey,
@@ -274,6 +277,9 @@ impl BonkParams {
virtual_quote: trade_info.virtual_quote as u128,
real_base: trade_info.real_base_after as u128,
real_quote: trade_info.real_quote_after as u128,
pool_state: trade_info.pool_state,
base_vault: trade_info.base_vault,
quote_vault: trade_info.quote_vault,
mint_token_program: trade_info.base_token_program,
platform_config: trade_info.platform_config,
platform_associated_account: trade_info.platform_associated_account,
@@ -327,6 +333,9 @@ impl BonkParams {
virtual_quote: DEFAULT_VIRTUAL_QUOTE,
real_base: real_base,
real_quote: real_quote,
pool_state: trade_info.pool_state,
base_vault: trade_info.base_vault,
quote_vault: trade_info.quote_vault,
mint_token_program: trade_info.base_token_program,
platform_config: trade_info.platform_config,
platform_associated_account: trade_info.platform_associated_account,
@@ -360,6 +369,9 @@ impl BonkParams {
virtual_quote: pool_data.virtual_quote as u128,
real_base: pool_data.real_base as u128,
real_quote: pool_data.real_quote as u128,
pool_state: pool_address,
base_vault: pool_data.base_vault,
quote_vault: pool_data.quote_vault,
mint_token_program: token_account.owner,
platform_config: pool_data.platform_config,
platform_associated_account,