feat: refactor trading architecture with unified framework

- Add unified TradeExecutor interface and protocol abstraction
- Refactor PumpFun/PumpSwap into adapter pattern
- Introduce TradeFactory for multi-protocol support
- Add parallel execution and unified parameter system
- Include Raydium protocol support and log parsing
- Simplify codebase structure and improve maintainability
This commit is contained in:
sgxiang
2025-06-17 23:32:20 +08:00
parent 768dc92156
commit 428ece5d6a
23 changed files with 1908 additions and 1505 deletions
@@ -0,0 +1,21 @@
use solana_sdk::{
message::AddressLookupTableAccount,
pubkey::Pubkey,
};
use crate::common::address_lookup_cache::get_address_lookup_table_account;
/// 获取地址查找表账户列表
/// 如果提供了lookup_table_key,则获取对应的账户,否则返回空列表
pub async fn get_address_lookup_table_accounts(
lookup_table_key: Option<Pubkey>,
) -> Vec<AddressLookupTableAccount> {
let mut address_lookup_table_accounts = vec![];
if let Some(lookup_table_key) = lookup_table_key {
let account = get_address_lookup_table_account(&lookup_table_key).await;
address_lookup_table_accounts.push(account);
}
address_lookup_table_accounts
}
@@ -0,0 +1,73 @@
use solana_sdk::{compute_budget::ComputeBudgetInstruction, instruction::Instruction};
use crate::common::PriorityFee;
/// 为RPC交易添加计算预算指令
pub fn add_rpc_compute_budget_instructions(
instructions: &mut Vec<Instruction>,
priority_fee: &PriorityFee,
data_size_limit: u32,
) {
instructions
.push(ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(data_size_limit));
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(
priority_fee.rpc_unit_price,
));
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
priority_fee.rpc_unit_limit,
));
}
/// 为带小费的交易添加计算预算指令
pub fn add_tip_compute_budget_instructions(
instructions: &mut Vec<Instruction>,
priority_fee: &PriorityFee,
data_size_limit: u32,
) {
instructions
.push(ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(data_size_limit));
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(
priority_fee.unit_price,
));
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
priority_fee.unit_limit,
));
}
/// 通用的计算预算指令添加函数
pub fn add_compute_budget_instructions(
instructions: &mut Vec<Instruction>,
unit_price: u64,
unit_limit: u32,
data_size_limit: u32,
) {
instructions
.push(ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(data_size_limit));
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(unit_price));
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(unit_limit));
}
pub fn add_sell_compute_budget_instructions(
instructions: &mut Vec<Instruction>,
priority_fee: &PriorityFee,
) {
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(
priority_fee.rpc_unit_price,
));
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
priority_fee.rpc_unit_limit,
));
}
/// 为带小费的交易添加计算预算指令
pub fn add_sell_tip_compute_budget_instructions(
instructions: &mut Vec<Instruction>,
priority_fee: &PriorityFee,
) {
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(
priority_fee.unit_price,
));
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
priority_fee.unit_limit,
));
}
+10
View File
@@ -0,0 +1,10 @@
pub mod nonce_manager;
pub mod transaction_builder;
pub mod compute_budget_manager;
pub mod address_lookup_manager;
// Re-export commonly used functions
pub use nonce_manager::*;
pub use transaction_builder::*;
pub use compute_budget_manager::*;
pub use address_lookup_manager::*;
+72
View File
@@ -0,0 +1,72 @@
use anyhow::anyhow;
use solana_sdk::{
instruction::Instruction,
signature::Keypair,
signer::Signer,
system_instruction,
};
use solana_hash::Hash;
use crate::common::nonce_cache::NonceCache;
/// 添加nonce消费指令到指令集合中
///
/// 只有提供了nonce_pubkey时才使用nonce功能
/// 如果nonce被锁定、已使用或未准备好,将返回错误
/// 成功时会锁定并标记nonce为已使用
pub fn add_nonce_instruction(
instructions: &mut Vec<Instruction>,
payer: &Keypair
) -> Result<(), anyhow::Error> {
let nonce_cache = NonceCache::get_instance();
let nonce_info = nonce_cache.get_nonce_info();
// 只检查nonce_account是否存在
if let Some(nonce_pubkey) = nonce_info.nonce_account {
// 暂不加锁
// if nonce_info.lock {
// return Err(anyhow!("Nonce is locked"));
// }
if nonce_info.used {
return Err(anyhow!("Nonce is used"));
}
if nonce_info.current_nonce == Hash::default() {
return Err(anyhow!("Nonce is not ready"));
}
// if nonce_info.next_buy_time == 0 || chrono::Utc::now().timestamp() < nonce_info.next_buy_time {
// return Err(anyhow!("Nonce is not ready"));
// }
// 加锁 - 暂不加锁
// nonce_cache.lock();
// 创建Solana系统nonce推进指令 - 使用系统程序ID
let nonce_advance_ix = system_instruction::advance_nonce_account(
&nonce_pubkey,
&payer.pubkey(),
);
instructions.push(nonce_advance_ix);
}
Ok(())
}
/// 获取用于交易的blockhash
/// 如果使用了nonce账户,返回nonce中的blockhash,否则返回传入的recent_blockhash
pub fn get_transaction_blockhash(recent_blockhash: Hash) -> Hash {
let nonce_cache = NonceCache::get_instance();
let nonce_info = nonce_cache.get_nonce_info();
if nonce_info.nonce_account.is_some() {
nonce_info.current_nonce
} else {
recent_blockhash
}
}
/// 检查是否使用nonce账户
pub fn is_using_nonce() -> bool {
let nonce_cache = NonceCache::get_instance();
let nonce_info = nonce_cache.get_nonce_info();
nonce_info.nonce_account.is_some()
}
+241
View File
@@ -0,0 +1,241 @@
use anyhow::anyhow;
use solana_hash::Hash;
use solana_sdk::{
instruction::Instruction,
message::{v0, VersionedMessage},
native_token::sol_to_lamports,
pubkey::Pubkey,
signature::Keypair,
signer::Signer,
system_instruction,
transaction::VersionedTransaction,
};
use std::sync::Arc;
use super::{
address_lookup_manager::get_address_lookup_table_accounts,
compute_budget_manager::{
add_rpc_compute_budget_instructions, add_tip_compute_budget_instructions,
},
nonce_manager::{add_nonce_instruction, get_transaction_blockhash},
};
use crate::{
common::PriorityFee,
trading::common::{
add_sell_compute_budget_instructions, add_sell_tip_compute_budget_instructions,
},
};
/// 构建标准的RPC交易
pub async fn build_rpc_transaction(
payer: Arc<Keypair>,
priority_fee: &PriorityFee,
business_instructions: Vec<Instruction>,
lookup_table_key: Option<Pubkey>,
recent_blockhash: Hash,
data_size_limit: u32,
) -> Result<VersionedTransaction, anyhow::Error> {
let mut instructions = vec![];
// 添加nonce指令
if let Err(e) = add_nonce_instruction(&mut instructions, payer.as_ref()) {
return Err(e);
}
// 添加计算预算指令
add_rpc_compute_budget_instructions(&mut instructions, priority_fee, data_size_limit);
// 添加业务指令
instructions.extend(business_instructions);
// 获取交易使用的blockhash
let blockhash = get_transaction_blockhash(recent_blockhash);
// 获取地址查找表账户
let address_lookup_table_accounts = get_address_lookup_table_accounts(lookup_table_key).await;
// 构建交易
build_versioned_transaction(
payer,
instructions,
address_lookup_table_accounts,
blockhash,
)
.await
}
/// 构建带小费的交易
pub async fn build_tip_transaction(
payer: Arc<Keypair>,
priority_fee: &PriorityFee,
business_instructions: Vec<Instruction>,
tip_account: &Pubkey,
tip_amount: f64,
lookup_table_key: Option<Pubkey>,
recent_blockhash: Hash,
data_size_limit: u32,
) -> Result<VersionedTransaction, anyhow::Error> {
let mut instructions = vec![];
// 添加nonce指令
if let Err(e) = add_nonce_instruction(&mut instructions, payer.as_ref()) {
return Err(e);
}
// 添加计算预算指令
add_tip_compute_budget_instructions(&mut instructions, priority_fee, data_size_limit);
// 添加业务指令
instructions.extend(business_instructions);
// 添加小费转账指令
instructions.push(system_instruction::transfer(
&payer.pubkey(),
tip_account,
sol_to_lamports(tip_amount),
));
// 获取交易使用的blockhash
let blockhash = get_transaction_blockhash(recent_blockhash);
// 获取地址查找表账户
let address_lookup_table_accounts = get_address_lookup_table_accounts(lookup_table_key).await;
// 构建交易
build_versioned_transaction(
payer,
instructions,
address_lookup_table_accounts,
blockhash,
)
.await
}
/// 构建版本化交易的底层函数
async fn build_versioned_transaction(
payer: Arc<Keypair>,
instructions: Vec<Instruction>,
address_lookup_table_accounts: Vec<solana_sdk::message::AddressLookupTableAccount>,
blockhash: Hash,
) -> Result<VersionedTransaction, anyhow::Error> {
let v0_message: v0::Message = v0::Message::try_compile(
&payer.pubkey(),
&instructions,
&address_lookup_table_accounts,
blockhash,
)?;
let versioned_message: VersionedMessage = VersionedMessage::V0(v0_message.clone());
let transaction = VersionedTransaction::try_new(versioned_message, &[payer.as_ref()])?;
Ok(transaction)
}
/// 构建带小费的交易(使用PriorityFee中的tip_fee
pub async fn build_tip_transaction_with_priority_fee(
payer: Arc<Keypair>,
priority_fee: &PriorityFee,
business_instructions: Vec<Instruction>,
tip_account: &Pubkey,
lookup_table_key: Option<Pubkey>,
recent_blockhash: Hash,
data_size_limit: u32,
) -> Result<VersionedTransaction, anyhow::Error> {
build_tip_transaction(
payer,
priority_fee,
business_instructions,
tip_account,
priority_fee.buy_tip_fee,
lookup_table_key,
recent_blockhash,
data_size_limit,
)
.await
}
/// 构建标准的RPC交易
pub async fn build_sell_transaction(
payer: Arc<Keypair>,
priority_fee: &PriorityFee,
business_instructions: Vec<Instruction>,
lookup_table_key: Option<Pubkey>,
recent_blockhash: Hash,
) -> Result<VersionedTransaction, anyhow::Error> {
let mut instructions = vec![];
// 添加计算预算指令
add_sell_compute_budget_instructions(&mut instructions, priority_fee);
// 添加业务指令
instructions.extend(business_instructions);
// 获取地址查找表账户
let address_lookup_table_accounts = get_address_lookup_table_accounts(lookup_table_key).await;
// 构建交易
build_versioned_transaction(
payer,
instructions,
address_lookup_table_accounts,
recent_blockhash,
)
.await
}
pub async fn build_sell_tip_transaction(
payer: Arc<Keypair>,
priority_fee: &PriorityFee,
business_instructions: Vec<Instruction>,
tip_account: &Pubkey,
tip_amount: f64,
lookup_table_key: Option<Pubkey>,
recent_blockhash: Hash,
) -> Result<VersionedTransaction, anyhow::Error> {
let mut instructions = vec![];
// 添加计算预算指令
add_sell_tip_compute_budget_instructions(&mut instructions, priority_fee);
// 添加业务指令
instructions.extend(business_instructions);
// 添加小费转账指令
instructions.push(system_instruction::transfer(
&payer.pubkey(),
tip_account,
sol_to_lamports(tip_amount),
));
// 获取地址查找表账户
let address_lookup_table_accounts = get_address_lookup_table_accounts(lookup_table_key).await;
// 构建交易
build_versioned_transaction(
payer,
instructions,
address_lookup_table_accounts,
recent_blockhash,
)
.await
}
pub async fn build_sell_tip_transaction_with_priority_fee(
payer: Arc<Keypair>,
priority_fee: &PriorityFee,
business_instructions: Vec<Instruction>,
tip_account: &Pubkey,
lookup_table_key: Option<Pubkey>,
recent_blockhash: Hash,
) -> Result<VersionedTransaction, anyhow::Error> {
build_sell_tip_transaction(
payer,
priority_fee,
business_instructions,
tip_account,
priority_fee.sell_tip_fee,
lookup_table_key,
recent_blockhash,
)
.await
}