feat: 为 buy 和 sell 方法添加 Signature 返回值

This commit is contained in:
liam
2025-09-09 12:03:42 +08:00
parent b9fa2f2f0f
commit 5092561091
6 changed files with 59 additions and 37 deletions
+7 -6
View File
@@ -1,4 +1,5 @@
use anyhow::Result;
use solana_sdk::signature::Signature;
use std::{sync::Arc, time::Instant};
use super::{
@@ -32,7 +33,7 @@ impl TradeExecutor for GenericTradeExecutor {
params: BuyParams,
swqos_clients: Vec<Arc<SwqosClient>>,
middleware_manager: Option<Arc<MiddlewareManager>>,
) -> Result<()> {
) -> Result<Signature> {
let mut data_size_limit = params.data_size_limit;
if data_size_limit == 0 {
data_size_limit = MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT;
@@ -55,7 +56,7 @@ impl TradeExecutor for GenericTradeExecutor {
println!("Building buy transaction instructions time cost: {:?}", start.elapsed());
// Execute transactions in parallel
parallel_execute_with_tips(
let sig = parallel_execute_with_tips(
swqos_clients,
params.payer,
final_instructions,
@@ -71,7 +72,7 @@ impl TradeExecutor for GenericTradeExecutor {
)
.await?;
Ok(())
Ok(sig)
}
async fn sell_with_tip(
@@ -79,7 +80,7 @@ impl TradeExecutor for GenericTradeExecutor {
params: SellParams,
swqos_clients: Vec<Arc<SwqosClient>>,
middleware_manager: Option<Arc<MiddlewareManager>>,
) -> Result<()> {
) -> Result<Signature> {
let start = Instant::now();
// Build instructions directly from params to avoid unnecessary cloning
@@ -97,7 +98,7 @@ impl TradeExecutor for GenericTradeExecutor {
println!("Building sell transaction instructions time cost: {:?}", start.elapsed());
// Execute transactions in parallel
parallel_execute_with_tips(
let sig = parallel_execute_with_tips(
swqos_clients,
params.payer,
final_instructions,
@@ -113,7 +114,7 @@ impl TradeExecutor for GenericTradeExecutor {
)
.await?;
Ok(())
Ok(sig)
}
fn protocol_name(&self) -> &'static str {
+21 -7
View File
@@ -1,6 +1,9 @@
use anyhow::{anyhow, Result};
use solana_hash::Hash;
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signature::Keypair};
// use solana_program::example_mocks::solana_signature::Signature;
use solana_sdk::{
instruction::Instruction, pubkey::Pubkey, signature::Keypair, signature::Signature,
};
use std::{str::FromStr, sync::Arc, time::Instant};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
@@ -25,9 +28,9 @@ pub async fn parallel_execute_with_tips(
is_buy: bool,
wait_transaction_confirmed: bool,
with_tip: bool,
) -> Result<()> {
) -> Result<Signature> {
let cores = core_affinity::get_core_ids().unwrap();
let mut handles: Vec<JoinHandle<Result<()>>> = Vec::with_capacity(swqos_clients.len());
let mut handles: Vec<JoinHandle<Result<Signature>>> = Vec::with_capacity(swqos_clients.len());
if is_buy
&& (swqos_clients.len() > priority_fee.buy_tip_fees.len()
|| priority_fee.buy_tip_fees.is_empty())
@@ -103,7 +106,11 @@ pub async fn parallel_execute_with_tips(
start.elapsed()
);
Ok::<(), anyhow::Error>(())
transaction
.signatures
.first()
.ok_or_else(|| anyhow!("Transaction has no signatures"))
.cloned()
});
handles.push(handle);
@@ -125,13 +132,20 @@ pub async fn parallel_execute_with_tips(
let mut errors = Vec::new();
if !wait_transaction_confirmed {
return Ok(());
if let Some(result) = rx.recv().await {
match result {
Ok(Ok(sig)) => return Ok(sig),
Ok(Err(e)) => errors.push(format!("Task error: {}", e)),
Err(e) => errors.push(format!("Join error: {}", e)),
}
}
return Err(anyhow!("No transaction signature available"));
}
while let Some(result) = rx.recv().await {
match result {
Ok(Ok(_)) => {
return Ok(());
Ok(Ok(sig)) => {
return Ok(sig);
}
Ok(Err(e)) => errors.push(format!("Task error: {}", e)),
Err(e) => errors.push(format!("Join error: {}", e)),
+3 -3
View File
@@ -2,7 +2,7 @@ use std::sync::Arc;
use crate::{swqos::SwqosClient, trading::MiddlewareManager};
use anyhow::Result;
use solana_sdk::instruction::Instruction;
use solana_sdk::{instruction::Instruction, signature::Signature};
use super::params::{BuyParams, SellParams};
@@ -15,14 +15,14 @@ pub trait TradeExecutor: Send + Sync {
params: BuyParams,
swqos_clients: Vec<Arc<SwqosClient>>,
middleware_manager: Option<Arc<MiddlewareManager>>,
) -> Result<()>;
) -> Result<Signature>;
/// 使用MEV服务执行卖出交易
async fn sell_with_tip(
&self,
params: SellParams,
swqos_clients: Vec<Arc<SwqosClient>>,
middleware_manager: Option<Arc<MiddlewareManager>>,
) -> Result<()>;
) -> Result<Signature>;
/// 获取协议名称
fn protocol_name(&self) -> &'static str;
}