refactor: optimize instruction builders structure and improve code documentation

- Add structured comment sections with clear code organization
- Improve variable naming and account address calculation logic
- Update comments for better readability across all trading protocols
- Enhance code structure for PumpFun, PumpSwap, and Raydium protocols
This commit is contained in:
ysq
2025-09-09 00:50:24 +08:00
parent ccf6330260
commit b9fa2f2f0f
16 changed files with 333 additions and 307 deletions
+2 -2
View File
@@ -2,8 +2,8 @@ use solana_sdk::{message::AddressLookupTableAccount, pubkey::Pubkey};
use crate::common::address_lookup_cache::get_address_lookup_table_account;
/// 获取地址查找表账户列表
/// 如果提供了lookup_table_key,则获取对应的账户,否则返回空列表
/// Get address lookup table account list
/// If lookup_table_key is provided, get the corresponding account, otherwise return empty list
pub async fn get_address_lookup_table_accounts(
lookup_table_key: Option<Pubkey>,
) -> Vec<AddressLookupTableAccount> {
+7 -7
View File
@@ -4,7 +4,7 @@ use once_cell::sync::Lazy;
use smallvec::SmallVec;
use solana_sdk::{compute_budget::ComputeBudgetInstruction, instruction::Instruction};
/// 缓存键,包含计算预算指令的所有参数
/// Cache key containing all parameters for compute budget instructions
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct ComputeBudgetCacheKey {
data_size_limit: u32,
@@ -13,8 +13,8 @@ struct ComputeBudgetCacheKey {
is_buy: bool,
}
/// 全局缓存,存储计算预算指令
/// 使用 DashMap 提供高性能的无锁并发访问
/// Global cache storing compute budget instructions
/// Uses DashMap for high-performance lock-free concurrent access
static COMPUTE_BUDGET_CACHE: Lazy<DashMap<ComputeBudgetCacheKey, SmallVec<[Instruction; 3]>>> =
Lazy::new(|| DashMap::new());
@@ -31,15 +31,15 @@ pub fn compute_budget_instructions(
(priority_fee.tip_unit_price, priority_fee.tip_unit_limit)
};
// 创建缓存键
// Create cache key
let cache_key = ComputeBudgetCacheKey { data_size_limit, unit_price, unit_limit, is_buy };
// 先尝试从缓存中获取
// Try to get from cache first
if let Some(cached_insts) = COMPUTE_BUDGET_CACHE.get(&cache_key) {
return cached_insts.clone();
}
// 缓存未命中,生成新的指令
// Cache miss, generate new instructions
let mut insts = SmallVec::<[Instruction; 3]>::new();
if is_buy {
@@ -51,7 +51,7 @@ pub fn compute_budget_instructions(
ComputeBudgetInstruction::set_compute_unit_limit(unit_limit),
]);
// 将结果存入缓存
// Store result in cache
let insts_clone = insts.clone();
COMPUTE_BUDGET_CACHE.insert(cache_key, insts_clone);
+9 -9
View File
@@ -18,7 +18,7 @@ use super::{
};
use crate::{common::PriorityFee, trading::MiddlewareManager};
/// 构建标准的RPC交易
/// Build standard RPC transaction
pub async fn build_transaction(
payer: Arc<Keypair>,
priority_fee: &PriorityFee,
@@ -35,14 +35,14 @@ pub async fn build_transaction(
) -> Result<VersionedTransaction, anyhow::Error> {
let mut instructions = Vec::with_capacity(business_instructions.len() + 5);
// 添加nonce指令
// Add nonce instruction
if is_buy {
if let Err(e) = add_nonce_instruction(&mut instructions, payer.as_ref()) {
return Err(e);
}
}
// 添加计算预算指令
// Add compute budget instructions
instructions.extend(compute_budget_instructions(
priority_fee,
data_size_limit,
@@ -50,10 +50,10 @@ pub async fn build_transaction(
is_buy,
));
// 添加业务指令
// Add business instructions
instructions.extend(business_instructions);
// 添加小费转账指令
// Add tip transfer instruction
if with_tip {
instructions.push(transfer(
&payer.pubkey(),
@@ -62,14 +62,14 @@ pub async fn build_transaction(
));
}
// 获取交易使用的blockhash
// Get blockhash for transaction
let blockhash =
if is_buy { get_transaction_blockhash(recent_blockhash) } else { recent_blockhash };
// 获取地址查找表账户
// Get address lookup table accounts
let address_lookup_table_accounts = get_address_lookup_table_accounts(lookup_table_key).await;
// 构建交易
// Build transaction
build_versioned_transaction(
payer,
instructions,
@@ -82,7 +82,7 @@ pub async fn build_transaction(
.await
}
/// 构建版本化交易的底层函数
/// Low-level function for building versioned transactions
async fn build_versioned_transaction(
payer: Arc<Keypair>,
instructions: Vec<Instruction>,