2025-06-17 23:32:20 +08:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
|
use solana_hash::Hash;
|
|
|
|
|
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signature::Keypair};
|
|
|
|
|
use std::{str::FromStr, sync::Arc};
|
2025-08-21 21:32:53 +08:00
|
|
|
use tokio::sync::mpsc;
|
2025-06-17 23:32:20 +08:00
|
|
|
use tokio::task::JoinHandle;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
common::PriorityFee,
|
2025-08-19 18:07:21 +08:00
|
|
|
swqos::{SwqosClient, SwqosType, TradeType},
|
|
|
|
|
trading::{
|
|
|
|
|
common::{
|
|
|
|
|
build_rpc_transaction, build_sell_tip_transaction_with_priority_fee,
|
|
|
|
|
build_sell_transaction, build_tip_transaction_with_priority_fee,
|
|
|
|
|
},
|
|
|
|
|
core::timer::TradeTimer,
|
|
|
|
|
MiddlewareManager,
|
2025-06-17 23:32:20 +08:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// 并行执行交易的通用函数
|
|
|
|
|
pub async fn parallel_execute_with_tips(
|
2025-07-06 22:06:44 +08:00
|
|
|
swqos_clients: Vec<Arc<SwqosClient>>,
|
2025-06-17 23:32:20 +08:00
|
|
|
payer: Arc<Keypair>,
|
|
|
|
|
instructions: Vec<Instruction>,
|
|
|
|
|
priority_fee: PriorityFee,
|
|
|
|
|
lookup_table_key: Option<Pubkey>,
|
|
|
|
|
recent_blockhash: Hash,
|
|
|
|
|
data_size_limit: u32,
|
|
|
|
|
trade_type: TradeType,
|
2025-08-19 18:07:21 +08:00
|
|
|
middleware_manager: Option<Arc<MiddlewareManager>>,
|
|
|
|
|
protocol_name: String,
|
|
|
|
|
is_buy: bool,
|
2025-08-21 21:32:53 +08:00
|
|
|
wait_transaction_confirmed: bool,
|
2025-06-17 23:32:20 +08:00
|
|
|
) -> Result<()> {
|
|
|
|
|
let cores = core_affinity::get_core_ids().unwrap();
|
|
|
|
|
let mut handles: Vec<JoinHandle<Result<()>>> = vec![];
|
|
|
|
|
|
2025-07-06 22:06:44 +08:00
|
|
|
for i in 0..swqos_clients.len() {
|
|
|
|
|
let swqos_client = swqos_clients[i].clone();
|
2025-06-17 23:32:20 +08:00
|
|
|
let payer = payer.clone();
|
|
|
|
|
let instructions = instructions.clone();
|
|
|
|
|
let mut priority_fee = priority_fee.clone();
|
|
|
|
|
let core_id = cores[i % cores.len()];
|
|
|
|
|
|
2025-08-19 18:07:21 +08:00
|
|
|
let middleware_manager = middleware_manager.clone();
|
|
|
|
|
let protocol_name = protocol_name.clone();
|
|
|
|
|
|
2025-06-17 23:32:20 +08:00
|
|
|
let handle = tokio::spawn(async move {
|
|
|
|
|
core_affinity::set_for_current(core_id);
|
2025-07-11 10:40:04 +08:00
|
|
|
|
2025-08-19 18:07:21 +08:00
|
|
|
let mut timer =
|
|
|
|
|
TradeTimer::new(format!("构建交易指令: {:?}", swqos_client.get_swqos_type()));
|
2025-07-11 10:40:04 +08:00
|
|
|
|
2025-06-17 23:32:20 +08:00
|
|
|
let transaction = if matches!(trade_type, TradeType::Sell)
|
2025-07-07 03:28:02 +08:00
|
|
|
&& swqos_client.get_swqos_type() == SwqosType::Default
|
2025-06-17 23:32:20 +08:00
|
|
|
{
|
|
|
|
|
build_sell_transaction(
|
|
|
|
|
payer,
|
|
|
|
|
&priority_fee,
|
|
|
|
|
instructions,
|
|
|
|
|
lookup_table_key,
|
|
|
|
|
recent_blockhash,
|
2025-08-19 18:07:21 +08:00
|
|
|
middleware_manager,
|
|
|
|
|
protocol_name,
|
|
|
|
|
is_buy,
|
2025-06-17 23:32:20 +08:00
|
|
|
)
|
|
|
|
|
.await?
|
|
|
|
|
} else if matches!(trade_type, TradeType::Sell)
|
2025-07-07 03:28:02 +08:00
|
|
|
&& swqos_client.get_swqos_type() != SwqosType::Default
|
2025-06-17 23:32:20 +08:00
|
|
|
{
|
2025-07-06 22:06:44 +08:00
|
|
|
let tip_account = swqos_client.get_tip_account()?;
|
2025-06-17 23:32:20 +08:00
|
|
|
let tip_account = Arc::new(Pubkey::from_str(&tip_account).map_err(|e| anyhow!(e))?);
|
|
|
|
|
build_sell_tip_transaction_with_priority_fee(
|
|
|
|
|
payer,
|
|
|
|
|
&priority_fee,
|
|
|
|
|
instructions,
|
|
|
|
|
&tip_account,
|
|
|
|
|
lookup_table_key,
|
|
|
|
|
recent_blockhash,
|
2025-08-19 18:07:21 +08:00
|
|
|
middleware_manager,
|
|
|
|
|
protocol_name,
|
|
|
|
|
is_buy,
|
2025-06-17 23:32:20 +08:00
|
|
|
)
|
|
|
|
|
.await?
|
2025-07-07 03:28:02 +08:00
|
|
|
} else if swqos_client.get_swqos_type() == SwqosType::Default {
|
2025-06-17 23:32:20 +08:00
|
|
|
build_rpc_transaction(
|
|
|
|
|
payer,
|
|
|
|
|
&priority_fee,
|
|
|
|
|
instructions,
|
|
|
|
|
lookup_table_key,
|
|
|
|
|
recent_blockhash,
|
|
|
|
|
data_size_limit,
|
2025-08-19 18:07:21 +08:00
|
|
|
middleware_manager,
|
|
|
|
|
protocol_name,
|
|
|
|
|
is_buy,
|
2025-06-17 23:32:20 +08:00
|
|
|
)
|
|
|
|
|
.await?
|
|
|
|
|
} else {
|
2025-07-06 22:06:44 +08:00
|
|
|
let tip_account = swqos_client.get_tip_account()?;
|
2025-06-17 23:32:20 +08:00
|
|
|
let tip_account = Arc::new(Pubkey::from_str(&tip_account).map_err(|e| anyhow!(e))?);
|
2025-08-21 21:32:53 +08:00
|
|
|
priority_fee.buy_tip_fee = priority_fee.buy_tip_fees[i % priority_fee.buy_tip_fees.len()];
|
2025-06-17 23:32:20 +08:00
|
|
|
|
|
|
|
|
build_tip_transaction_with_priority_fee(
|
|
|
|
|
payer,
|
|
|
|
|
&priority_fee,
|
|
|
|
|
instructions,
|
|
|
|
|
&tip_account,
|
|
|
|
|
lookup_table_key,
|
|
|
|
|
recent_blockhash,
|
|
|
|
|
data_size_limit,
|
2025-08-19 18:07:21 +08:00
|
|
|
middleware_manager,
|
|
|
|
|
protocol_name,
|
|
|
|
|
is_buy,
|
2025-06-17 23:32:20 +08:00
|
|
|
)
|
|
|
|
|
.await?
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-11 10:40:04 +08:00
|
|
|
timer.stage(format!("提交交易指令: {:?}", swqos_client.get_swqos_type()));
|
|
|
|
|
|
2025-08-19 18:07:21 +08:00
|
|
|
swqos_client.send_transaction(trade_type, &transaction).await?;
|
2025-07-11 10:40:04 +08:00
|
|
|
|
|
|
|
|
timer.finish();
|
2025-06-17 23:32:20 +08:00
|
|
|
Ok::<(), anyhow::Error>(())
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
handles.push(handle);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 21:32:53 +08:00
|
|
|
// 任意一个成功即返回
|
|
|
|
|
let (tx, mut rx) = mpsc::channel(swqos_clients.len());
|
|
|
|
|
|
|
|
|
|
// 启动监听任务
|
2025-06-17 23:32:20 +08:00
|
|
|
for handle in handles {
|
2025-08-21 21:32:53 +08:00
|
|
|
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(());
|
|
|
|
|
}
|
2025-06-17 23:32:20 +08:00
|
|
|
Ok(Err(e)) => errors.push(format!("Task error: {}", e)),
|
|
|
|
|
Err(e) => errors.push(format!("Join error: {}", e)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 21:32:53 +08:00
|
|
|
// 如果没有成功的,返回错误
|
|
|
|
|
return Err(anyhow!("所有交易都失败了: {:?}", errors));
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|