feat: upgrade to v0.5.2

This commit is contained in:
ysq
2025-08-27 14:27:26 +08:00
parent c10406915c
commit 9438172029
16 changed files with 189 additions and 159 deletions
+22 -22
View File
@@ -17,7 +17,7 @@ use crate::{
const MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT: u32 = 256 * 1024;
/// 通用交易执行器实现
/// Generic trade executor implementation
pub struct GenericTradeExecutor {
instruction_builder: Arc<dyn InstructionBuilder>,
protocol_name: &'static str,
@@ -46,8 +46,8 @@ impl TradeExecutor for GenericTradeExecutor {
return Err(anyhow!("RPC is not set"));
}
let rpc = params.rpc.as_ref().unwrap().clone();
let mut timer = TradeTimer::new("构建买入交易指令");
// 构建指令
let mut timer = TradeTimer::new("Building buy transaction instructions");
// Build instructions
let instructions = self.instruction_builder.build_buy_instructions(&params).await?;
let final_instructions = match middleware_manager.clone() {
Some(middleware_manager) => middleware_manager
@@ -58,9 +58,9 @@ impl TradeExecutor for GenericTradeExecutor {
)?,
None => instructions,
};
timer.stage("构建rpc交易指令");
timer.stage("Building RPC transaction instructions");
// 构建交易
// Build transaction
let transaction = build_rpc_transaction(
params.payer.clone(),
&params.priority_fee,
@@ -73,13 +73,13 @@ impl TradeExecutor for GenericTradeExecutor {
true,
)
.await?;
timer.stage("rpc提交确认");
timer.stage("RPC submission confirmation");
// 发送交易
// Send transaction
if params.wait_transaction_confirmed {
rpc.send_and_confirm_transaction(&transaction).await?;
} else {
// 异步发送交易
// Send transaction asynchronously
rpc.send_transaction(&transaction).await?;
}
timer.finish();
@@ -95,9 +95,9 @@ impl TradeExecutor for GenericTradeExecutor {
if params.data_size_limit == 0 {
params.data_size_limit = MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT;
}
let timer = TradeTimer::new("构建买入交易指令");
let timer = TradeTimer::new("Building buy transaction instructions");
// 验证参数 - 转换为BuyParams进行验证
// Validate parameters - convert to BuyParams for validation
let buy_params = BuyParams {
rpc: params.rpc,
payer: params.payer.clone(),
@@ -112,7 +112,7 @@ impl TradeExecutor for GenericTradeExecutor {
protocol_params: params.protocol_params.clone(),
};
// 构建指令
// Build instructions
let instructions = self.instruction_builder.build_buy_instructions(&buy_params).await?;
let final_instructions = match middleware_manager.clone() {
Some(middleware_manager) => middleware_manager
@@ -126,7 +126,7 @@ impl TradeExecutor for GenericTradeExecutor {
timer.finish();
// 并行执行交易
// Execute transactions in parallel
parallel_execute_with_tips(
params.swqos_clients,
params.payer,
@@ -155,9 +155,9 @@ impl TradeExecutor for GenericTradeExecutor {
return Err(anyhow!("RPC is not set"));
}
let rpc = params.rpc.as_ref().unwrap().clone();
let mut timer = TradeTimer::new("构建卖出交易指令");
let mut timer = TradeTimer::new("Building sell transaction instructions");
// 构建指令
// Build instructions
let instructions = self.instruction_builder.build_sell_instructions(&params).await?;
let final_instructions = match middleware_manager.clone() {
Some(middleware_manager) => middleware_manager
@@ -168,9 +168,9 @@ impl TradeExecutor for GenericTradeExecutor {
)?,
None => instructions,
};
timer.stage("卖出交易指令");
timer.stage("Sell transaction instructions");
// 构建交易
// Build transaction
let transaction = build_sell_transaction(
params.payer.clone(),
&params.priority_fee,
@@ -182,9 +182,9 @@ impl TradeExecutor for GenericTradeExecutor {
false,
)
.await?;
timer.stage("卖出交易签名");
timer.stage("Sell transaction signing");
// 发送交易
// Send transaction
if params.wait_transaction_confirmed {
rpc.send_and_confirm_transaction(&transaction).await?;
} else {
@@ -200,9 +200,9 @@ impl TradeExecutor for GenericTradeExecutor {
params: SellWithTipParams,
middleware_manager: Option<Arc<MiddlewareManager>>,
) -> Result<()> {
let timer = TradeTimer::new("构建卖出交易指令");
let timer = TradeTimer::new("Building sell transaction instructions");
// 转换为SellParams进行指令构建
// Convert to SellParams for instruction building
let sell_params = SellParams {
rpc: params.rpc,
payer: params.payer.clone(),
@@ -216,7 +216,7 @@ impl TradeExecutor for GenericTradeExecutor {
protocol_params: params.protocol_params.clone(),
};
// 构建指令
// Build instructions
let instructions = self.instruction_builder.build_sell_instructions(&sell_params).await?;
let final_instructions = match middleware_manager.clone() {
Some(middleware_manager) => middleware_manager
@@ -230,7 +230,7 @@ impl TradeExecutor for GenericTradeExecutor {
timer.finish();
// 并行执行交易
// Execute transactions in parallel
parallel_execute_with_tips(
params.swqos_clients,
params.payer,
+17 -12
View File
@@ -18,7 +18,7 @@ use crate::{
},
};
/// 并行执行交易的通用函数
/// Generic function for parallel transaction execution
pub async fn parallel_execute_with_tips(
swqos_clients: Vec<Arc<SwqosClient>>,
payer: Arc<Keypair>,
@@ -49,8 +49,10 @@ pub async fn parallel_execute_with_tips(
let handle = tokio::spawn(async move {
core_affinity::set_for_current(core_id);
let mut timer =
TradeTimer::new(format!("构建交易指令: {:?}", swqos_client.get_swqos_type()));
let mut timer = TradeTimer::new(format!(
"Building transaction instructions: {:?}",
swqos_client.get_swqos_type()
));
let transaction = if matches!(trade_type, TradeType::Sell)
&& swqos_client.get_swqos_type() == SwqosType::Default
@@ -99,7 +101,8 @@ pub async fn parallel_execute_with_tips(
} else {
let tip_account = swqos_client.get_tip_account()?;
let tip_account = Arc::new(Pubkey::from_str(&tip_account).map_err(|e| anyhow!(e))?);
priority_fee.buy_tip_fee = priority_fee.buy_tip_fees[i % priority_fee.buy_tip_fees.len()];
priority_fee.buy_tip_fee =
priority_fee.buy_tip_fees[i % priority_fee.buy_tip_fees.len()];
build_tip_transaction_with_priority_fee(
payer,
@@ -116,7 +119,10 @@ pub async fn parallel_execute_with_tips(
.await?
};
timer.stage(format!("提交交易指令: {:?}", swqos_client.get_swqos_type()));
timer.stage(format!(
"Submitting transaction instructions: {:?}",
swqos_client.get_swqos_type()
));
swqos_client.send_transaction(trade_type, &transaction).await?;
@@ -126,11 +132,10 @@ pub async fn parallel_execute_with_tips(
handles.push(handle);
}
// 任意一个成功即返回
// Return as soon as any one succeeds
let (tx, mut rx) = mpsc::channel(swqos_clients.len());
// 启动监听任务
// Start monitoring tasks
for handle in handles {
let tx = tx.clone();
tokio::spawn(async move {
@@ -138,9 +143,9 @@ pub async fn parallel_execute_with_tips(
let _ = tx.send(result).await;
});
}
drop(tx); // 关闭发送端
drop(tx); // Close the sender
// 等待第一个成功的结果
// Wait for the first successful result
let mut errors = Vec::new();
if !wait_transaction_confirmed {
@@ -157,6 +162,6 @@ pub async fn parallel_execute_with_tips(
}
}
// 如果没有成功的,返回错误
return Err(anyhow!("所有交易都失败了: {:?}", errors));
// If no success, return error
return Err(anyhow!("All transactions failed: {:?}", errors));
}
+15 -18
View File
@@ -1,6 +1,6 @@
use std::time::Instant;
/// 交易时间测量器
/// Trade time measurement tool
#[derive(Clone)]
pub struct TradeTimer {
start_time: Instant,
@@ -8,31 +8,28 @@ pub struct TradeTimer {
}
impl TradeTimer {
/// 创建新的计时器
/// Create a new timer
pub fn new(stage: impl Into<String>) -> Self {
Self {
start_time: Instant::now(),
stage: stage.into(),
}
Self { start_time: Instant::now(), stage: stage.into() }
}
/// 记录当前阶段耗时并开始新阶段
/// Record current stage time and start a new stage
pub fn stage(&mut self, new_stage: impl Into<String>) {
let elapsed = self.start_time.elapsed();
println!(" {} 耗时: {:?}", self.stage, elapsed);
println!(" {} time cost: {:?}", self.stage, elapsed);
self.start_time = Instant::now();
self.stage = new_stage.into();
}
/// 完成计时并输出最终耗时
/// Complete timing and output final time cost
pub fn finish(mut self) {
let elapsed = self.start_time.elapsed();
println!(" {} 耗时: {:?}", self.stage, elapsed);
self.stage.clear(); // 清空stage,避免Drop时重复打印
println!(" {} time cost: {:?}", self.stage, elapsed);
self.stage.clear(); // Clear stage to avoid duplicate printing in Drop
}
/// 获取当前阶段的耗时(不重置计时器)
/// Get the elapsed time of current stage (without resetting the timer)
pub fn elapsed(&self) -> std::time::Duration {
self.start_time.elapsed()
}
@@ -42,7 +39,7 @@ impl Drop for TradeTimer {
fn drop(&mut self) {
if !self.stage.is_empty() {
let elapsed = self.start_time.elapsed();
println!(" {} 耗时: {:?}", self.stage, elapsed);
println!(" {} time cost: {:?}", self.stage, elapsed);
}
}
}
}