feat: add transaction confirmation wait option and parallel execution optimization
- Add wait_transaction_confirmed parameter to all trading methods - Support async transaction sending without waiting for confirmation for better performance - Optimize parallel execution logic to return on first successful transaction - Fix direct priority_fee modification issue - Improve error handling and resource management - Update all example code to support new parameter
This commit is contained in:
@@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
|
||||
use solana_hash::Hash;
|
||||
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signature::Keypair};
|
||||
use std::{str::FromStr, sync::Arc};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
use crate::{
|
||||
@@ -30,6 +31,7 @@ pub async fn parallel_execute_with_tips(
|
||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||
protocol_name: String,
|
||||
is_buy: bool,
|
||||
wait_transaction_confirmed: bool,
|
||||
) -> Result<()> {
|
||||
let cores = core_affinity::get_core_ids().unwrap();
|
||||
let mut handles: Vec<JoinHandle<Result<()>>> = vec![];
|
||||
@@ -97,7 +99,7 @@ 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_fee = priority_fee.buy_tip_fees[i % priority_fee.buy_tip_fees.len()];
|
||||
|
||||
build_tip_transaction_with_priority_fee(
|
||||
payer,
|
||||
@@ -125,22 +127,36 @@ pub async fn parallel_execute_with_tips(
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
// 等待所有任务完成
|
||||
let mut errors = Vec::new();
|
||||
// 任意一个成功即返回
|
||||
let (tx, mut rx) = mpsc::channel(swqos_clients.len());
|
||||
|
||||
// 启动监听任务
|
||||
for handle in handles {
|
||||
match handle.await {
|
||||
Ok(Ok(_)) => (),
|
||||
let tx = tx.clone();
|
||||
tokio::spawn(async move {
|
||||
let result = handle.await;
|
||||
let _ = tx.send(result).await;
|
||||
});
|
||||
}
|
||||
drop(tx); // 关闭发送端
|
||||
|
||||
// 等待第一个成功的结果
|
||||
let mut errors = Vec::new();
|
||||
|
||||
if !wait_transaction_confirmed {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
while let Some(result) = rx.recv().await {
|
||||
match result {
|
||||
Ok(Ok(_)) => {
|
||||
return Ok(());
|
||||
}
|
||||
Ok(Err(e)) => errors.push(format!("Task error: {}", e)),
|
||||
Err(e) => errors.push(format!("Join error: {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
if !errors.is_empty() {
|
||||
for error in &errors {
|
||||
println!("{}", error);
|
||||
}
|
||||
return Err(anyhow!("Some tasks failed: {:?}", errors));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
// 如果没有成功的,返回错误
|
||||
return Err(anyhow!("所有交易都失败了: {:?}", errors));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user