2025-09-07 17:22:58 +08:00
|
|
|
use anyhow::Result;
|
2025-09-07 18:07:45 +08:00
|
|
|
use std::{sync::Arc, time::Instant};
|
2025-06-17 23:32:20 +08:00
|
|
|
|
|
|
|
|
use super::{
|
|
|
|
|
parallel::parallel_execute_with_tips,
|
2025-09-07 17:22:58 +08:00
|
|
|
params::{BuyParams, SellParams},
|
2025-06-17 23:32:20 +08:00
|
|
|
traits::{InstructionBuilder, TradeExecutor},
|
|
|
|
|
};
|
2025-09-07 17:22:58 +08:00
|
|
|
use crate::{swqos::SwqosClient, trading::middleware::MiddlewareManager};
|
2025-06-17 23:32:20 +08:00
|
|
|
|
2025-07-10 18:14:21 +08:00
|
|
|
const MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT: u32 = 256 * 1024;
|
|
|
|
|
|
2025-08-27 14:27:26 +08:00
|
|
|
/// Generic trade executor implementation
|
2025-06-17 23:32:20 +08:00
|
|
|
pub struct GenericTradeExecutor {
|
|
|
|
|
instruction_builder: Arc<dyn InstructionBuilder>,
|
|
|
|
|
protocol_name: &'static str,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl GenericTradeExecutor {
|
|
|
|
|
pub fn new(
|
|
|
|
|
instruction_builder: Arc<dyn InstructionBuilder>,
|
|
|
|
|
protocol_name: &'static str,
|
|
|
|
|
) -> Self {
|
2025-08-19 18:07:21 +08:00
|
|
|
Self { instruction_builder, protocol_name }
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
|
impl TradeExecutor for GenericTradeExecutor {
|
2025-08-19 18:07:21 +08:00
|
|
|
async fn buy_with_tip(
|
|
|
|
|
&self,
|
2025-09-07 17:22:58 +08:00
|
|
|
params: BuyParams,
|
|
|
|
|
swqos_clients: Vec<Arc<SwqosClient>>,
|
2025-08-19 18:07:21 +08:00
|
|
|
middleware_manager: Option<Arc<MiddlewareManager>>,
|
|
|
|
|
) -> Result<()> {
|
2025-09-07 17:22:58 +08:00
|
|
|
let mut data_size_limit = params.data_size_limit;
|
|
|
|
|
if data_size_limit == 0 {
|
|
|
|
|
data_size_limit = MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT;
|
2025-07-10 18:14:21 +08:00
|
|
|
}
|
2025-06-17 23:32:20 +08:00
|
|
|
|
2025-09-07 18:07:45 +08:00
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
|
|
// Build instructions directly from params to avoid unnecessary cloning
|
|
|
|
|
let instructions = self.instruction_builder.build_buy_instructions(¶ms).await?;
|
|
|
|
|
let final_instructions = match &middleware_manager {
|
2025-08-19 18:07:21 +08:00
|
|
|
Some(middleware_manager) => middleware_manager
|
|
|
|
|
.apply_middlewares_process_protocol_instructions(
|
|
|
|
|
instructions,
|
|
|
|
|
self.protocol_name.to_string(),
|
|
|
|
|
true,
|
|
|
|
|
)?,
|
|
|
|
|
None => instructions,
|
|
|
|
|
};
|
2025-07-11 10:40:04 +08:00
|
|
|
|
2025-09-07 18:07:45 +08:00
|
|
|
println!("Building buy transaction instructions time cost: {:?}", start.elapsed());
|
2025-06-17 23:32:20 +08:00
|
|
|
|
2025-08-27 14:27:26 +08:00
|
|
|
// Execute transactions in parallel
|
2025-06-17 23:32:20 +08:00
|
|
|
parallel_execute_with_tips(
|
2025-09-07 17:22:58 +08:00
|
|
|
swqos_clients,
|
2025-06-17 23:32:20 +08:00
|
|
|
params.payer,
|
2025-08-19 18:07:21 +08:00
|
|
|
final_instructions,
|
2025-09-07 18:07:45 +08:00
|
|
|
Arc::new(params.priority_fee),
|
2025-06-17 23:32:20 +08:00
|
|
|
params.lookup_table_key,
|
|
|
|
|
params.recent_blockhash,
|
2025-09-07 17:22:58 +08:00
|
|
|
data_size_limit,
|
2025-08-19 18:07:21 +08:00
|
|
|
middleware_manager,
|
2025-09-07 18:07:45 +08:00
|
|
|
self.protocol_name,
|
2025-08-19 18:07:21 +08:00
|
|
|
true,
|
2025-08-21 21:32:53 +08:00
|
|
|
params.wait_transaction_confirmed,
|
2025-09-07 17:22:58 +08:00
|
|
|
true,
|
2025-06-17 23:32:20 +08:00
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 18:07:21 +08:00
|
|
|
async fn sell_with_tip(
|
|
|
|
|
&self,
|
2025-09-07 17:22:58 +08:00
|
|
|
params: SellParams,
|
|
|
|
|
swqos_clients: Vec<Arc<SwqosClient>>,
|
2025-08-19 18:07:21 +08:00
|
|
|
middleware_manager: Option<Arc<MiddlewareManager>>,
|
|
|
|
|
) -> Result<()> {
|
2025-09-07 18:07:45 +08:00
|
|
|
let start = Instant::now();
|
2025-06-17 23:32:20 +08:00
|
|
|
|
2025-09-07 18:07:45 +08:00
|
|
|
// Build instructions directly from params to avoid unnecessary cloning
|
|
|
|
|
let instructions = self.instruction_builder.build_sell_instructions(¶ms).await?;
|
|
|
|
|
let final_instructions = match &middleware_manager {
|
2025-08-19 18:07:21 +08:00
|
|
|
Some(middleware_manager) => middleware_manager
|
|
|
|
|
.apply_middlewares_process_protocol_instructions(
|
|
|
|
|
instructions,
|
|
|
|
|
self.protocol_name.to_string(),
|
|
|
|
|
false,
|
|
|
|
|
)?,
|
|
|
|
|
None => instructions,
|
|
|
|
|
};
|
2025-07-11 10:40:04 +08:00
|
|
|
|
2025-09-07 18:07:45 +08:00
|
|
|
println!("Building sell transaction instructions time cost: {:?}", start.elapsed());
|
2025-06-17 23:32:20 +08:00
|
|
|
|
2025-08-27 14:27:26 +08:00
|
|
|
// Execute transactions in parallel
|
2025-06-17 23:32:20 +08:00
|
|
|
parallel_execute_with_tips(
|
2025-09-07 17:22:58 +08:00
|
|
|
swqos_clients,
|
2025-06-17 23:32:20 +08:00
|
|
|
params.payer,
|
2025-08-19 18:07:21 +08:00
|
|
|
final_instructions,
|
2025-09-07 18:07:45 +08:00
|
|
|
Arc::new(params.priority_fee),
|
2025-06-17 23:32:20 +08:00
|
|
|
params.lookup_table_key,
|
|
|
|
|
params.recent_blockhash,
|
|
|
|
|
0,
|
2025-08-19 18:07:21 +08:00
|
|
|
middleware_manager,
|
2025-09-07 18:07:45 +08:00
|
|
|
self.protocol_name,
|
2025-08-19 18:07:21 +08:00
|
|
|
false,
|
2025-08-21 21:32:53 +08:00
|
|
|
params.wait_transaction_confirmed,
|
2025-09-07 17:22:58 +08:00
|
|
|
params.with_tip,
|
2025-06-17 23:32:20 +08:00
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn protocol_name(&self) -> &'static str {
|
|
|
|
|
self.protocol_name
|
|
|
|
|
}
|
|
|
|
|
}
|