refactor: unify transaction building and execution architecture
- Remove redundant transaction builder functions and merge into single build_transaction() - Simplify compute budget manager with unified add_compute_budget_instructions() - Consolidate trade executor interface by removing separate buy/sell methods - Unify BuyParams/SellParams usage, remove *WithTipParams structs - Streamline parallel execution logic and remove TradeType parameter - Delete obsolete files: address_lookup.rs, tip_cache.rs - Clean up nonce manager by removing unused is_using_nonce() function This refactoring reduces code duplication and provides a cleaner, more maintainable API for transaction building and execution across all trading protocols.
This commit is contained in:
@@ -2,72 +2,27 @@ use solana_sdk::{compute_budget::ComputeBudgetInstruction, instruction::Instruct
|
||||
|
||||
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.tip_unit_price,
|
||||
));
|
||||
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
|
||||
priority_fee.tip_unit_limit,
|
||||
));
|
||||
}
|
||||
|
||||
/// 通用的计算预算指令添加函数
|
||||
/// 为交易添加计算预算指令
|
||||
pub fn add_compute_budget_instructions(
|
||||
instructions: &mut Vec<Instruction>,
|
||||
unit_price: u64,
|
||||
unit_limit: u32,
|
||||
priority_fee: &PriorityFee,
|
||||
data_size_limit: u32,
|
||||
is_rpc: bool,
|
||||
is_buy: bool,
|
||||
) {
|
||||
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.tip_unit_price,
|
||||
));
|
||||
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
|
||||
priority_fee.tip_unit_limit,
|
||||
));
|
||||
if is_buy {
|
||||
instructions
|
||||
.push(ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(data_size_limit));
|
||||
}
|
||||
if is_rpc {
|
||||
instructions
|
||||
.push(ComputeBudgetInstruction::set_compute_unit_price(priority_fee.rpc_unit_price));
|
||||
instructions
|
||||
.push(ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.rpc_unit_limit));
|
||||
} else {
|
||||
instructions
|
||||
.push(ComputeBudgetInstruction::set_compute_unit_price(priority_fee.tip_unit_price));
|
||||
instructions
|
||||
.push(ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.tip_unit_limit));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,10 +47,3 @@ pub fn get_transaction_blockhash(recent_blockhash: Hash) -> Hash {
|
||||
recent_blockhash
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if using nonce account
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -13,21 +13,13 @@ 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,
|
||||
},
|
||||
compute_budget_manager::add_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},
|
||||
MiddlewareManager,
|
||||
},
|
||||
};
|
||||
use crate::{common::PriorityFee, trading::MiddlewareManager};
|
||||
|
||||
/// 构建标准的RPC交易
|
||||
pub async fn build_rpc_transaction(
|
||||
pub async fn build_transaction(
|
||||
payer: Arc<Keypair>,
|
||||
priority_fee: &PriorityFee,
|
||||
business_instructions: Vec<Instruction>,
|
||||
@@ -37,75 +29,37 @@ pub async fn build_rpc_transaction(
|
||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||
protocol_name: String,
|
||||
is_buy: bool,
|
||||
) -> 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,
|
||||
middleware_manager,
|
||||
protocol_name,
|
||||
is_buy,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// 构建带小费的交易
|
||||
pub async fn build_tip_transaction(
|
||||
payer: Arc<Keypair>,
|
||||
priority_fee: &PriorityFee,
|
||||
business_instructions: Vec<Instruction>,
|
||||
with_tip: bool,
|
||||
tip_account: &Pubkey,
|
||||
tip_amount: f64,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
recent_blockhash: Hash,
|
||||
data_size_limit: u32,
|
||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||
protocol_name: String,
|
||||
is_buy: bool,
|
||||
) -> Result<VersionedTransaction, anyhow::Error> {
|
||||
let mut instructions = vec![];
|
||||
|
||||
// 添加nonce指令
|
||||
if let Err(e) = add_nonce_instruction(&mut instructions, payer.as_ref()) {
|
||||
return Err(e);
|
||||
if is_buy {
|
||||
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);
|
||||
add_compute_budget_instructions(&mut instructions, priority_fee, data_size_limit, true, is_buy);
|
||||
|
||||
// 添加业务指令
|
||||
instructions.extend(business_instructions);
|
||||
|
||||
// 添加小费转账指令
|
||||
instructions.push(transfer(
|
||||
&payer.pubkey(),
|
||||
tip_account,
|
||||
sol_str_to_lamports(tip_amount.to_string().as_str()).unwrap_or(0),
|
||||
));
|
||||
if with_tip {
|
||||
instructions.push(transfer(
|
||||
&payer.pubkey(),
|
||||
tip_account,
|
||||
sol_str_to_lamports(tip_amount.to_string().as_str()).unwrap_or(0),
|
||||
));
|
||||
}
|
||||
|
||||
// 获取交易使用的blockhash
|
||||
let blockhash = get_transaction_blockhash(recent_blockhash);
|
||||
let blockhash =
|
||||
if is_buy { get_transaction_blockhash(recent_blockhash) } else { recent_blockhash };
|
||||
|
||||
// 获取地址查找表账户
|
||||
let address_lookup_table_accounts = get_address_lookup_table_accounts(lookup_table_key).await;
|
||||
@@ -150,136 +104,3 @@ async fn build_versioned_transaction(
|
||||
|
||||
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,
|
||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||
protocol_name: String,
|
||||
is_buy: bool,
|
||||
) -> 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,
|
||||
middleware_manager,
|
||||
protocol_name,
|
||||
is_buy,
|
||||
)
|
||||
.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,
|
||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||
protocol_name: String,
|
||||
is_buy: bool,
|
||||
) -> 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,
|
||||
middleware_manager,
|
||||
protocol_name,
|
||||
is_buy,
|
||||
)
|
||||
.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,
|
||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||
protocol_name: String,
|
||||
is_buy: bool,
|
||||
) -> Result<VersionedTransaction, anyhow::Error> {
|
||||
let mut instructions = vec![];
|
||||
|
||||
// 添加计算预算指令
|
||||
add_sell_tip_compute_budget_instructions(&mut instructions, priority_fee);
|
||||
|
||||
// 添加业务指令
|
||||
instructions.extend(business_instructions);
|
||||
|
||||
// 添加小费转账指令
|
||||
instructions.push(transfer(
|
||||
&payer.pubkey(),
|
||||
tip_account,
|
||||
sol_str_to_lamports(tip_amount.to_string().as_str()).unwrap_or(0),
|
||||
));
|
||||
|
||||
// 获取地址查找表账户
|
||||
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,
|
||||
middleware_manager,
|
||||
protocol_name,
|
||||
is_buy,
|
||||
)
|
||||
.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,
|
||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||
protocol_name: String,
|
||||
is_buy: bool,
|
||||
) -> 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,
|
||||
middleware_manager,
|
||||
protocol_name,
|
||||
is_buy,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user