2025-09-07 17:22:58 +08:00
|
|
|
use anyhow::Result;
|
2025-06-17 23:32:20 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
timer::TradeTimer,
|
|
|
|
|
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-08-27 14:27:26 +08:00
|
|
|
let timer = TradeTimer::new("Building buy transaction instructions");
|
2025-06-17 23:32:20 +08:00
|
|
|
|
2025-08-27 14:27:26 +08:00
|
|
|
// Validate parameters - convert to BuyParams for validation
|
2025-06-17 23:32:20 +08:00
|
|
|
let buy_params = BuyParams {
|
|
|
|
|
rpc: params.rpc,
|
|
|
|
|
payer: params.payer.clone(),
|
|
|
|
|
mint: params.mint,
|
2025-07-10 23:54:49 +08:00
|
|
|
sol_amount: params.sol_amount,
|
2025-06-17 23:32:20 +08:00
|
|
|
slippage_basis_points: params.slippage_basis_points,
|
|
|
|
|
priority_fee: params.priority_fee.clone(),
|
|
|
|
|
lookup_table_key: params.lookup_table_key,
|
|
|
|
|
recent_blockhash: params.recent_blockhash,
|
2025-09-07 17:22:58 +08:00
|
|
|
data_size_limit: data_size_limit,
|
2025-08-21 21:32:53 +08:00
|
|
|
wait_transaction_confirmed: params.wait_transaction_confirmed,
|
2025-06-17 23:32:20 +08:00
|
|
|
protocol_params: params.protocol_params.clone(),
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-27 14:27:26 +08:00
|
|
|
// Build instructions
|
2025-08-19 18:07:21 +08:00
|
|
|
let instructions = self.instruction_builder.build_buy_instructions(&buy_params).await?;
|
|
|
|
|
let final_instructions = match middleware_manager.clone() {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
timer.finish();
|
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-06-17 23:32:20 +08:00
|
|
|
params.priority_fee,
|
|
|
|
|
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,
|
|
|
|
|
self.protocol_name.to_string(),
|
|
|
|
|
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-08-27 14:27:26 +08:00
|
|
|
let timer = TradeTimer::new("Building sell transaction instructions");
|
2025-06-17 23:32:20 +08:00
|
|
|
|
2025-08-27 14:27:26 +08:00
|
|
|
// Convert to SellParams for instruction building
|
2025-06-17 23:32:20 +08:00
|
|
|
let sell_params = SellParams {
|
|
|
|
|
rpc: params.rpc,
|
|
|
|
|
payer: params.payer.clone(),
|
|
|
|
|
mint: params.mint,
|
2025-07-10 23:54:49 +08:00
|
|
|
token_amount: params.token_amount,
|
2025-06-17 23:32:20 +08:00
|
|
|
slippage_basis_points: params.slippage_basis_points,
|
|
|
|
|
priority_fee: params.priority_fee.clone(),
|
|
|
|
|
lookup_table_key: params.lookup_table_key,
|
|
|
|
|
recent_blockhash: params.recent_blockhash,
|
2025-08-21 21:32:53 +08:00
|
|
|
wait_transaction_confirmed: params.wait_transaction_confirmed,
|
2025-06-17 23:32:20 +08:00
|
|
|
protocol_params: params.protocol_params.clone(),
|
2025-09-07 17:22:58 +08:00
|
|
|
with_tip: params.with_tip,
|
2025-06-17 23:32:20 +08:00
|
|
|
};
|
|
|
|
|
|
2025-08-27 14:27:26 +08:00
|
|
|
// Build instructions
|
2025-08-19 18:07:21 +08:00
|
|
|
let instructions = self.instruction_builder.build_sell_instructions(&sell_params).await?;
|
|
|
|
|
let final_instructions = match middleware_manager.clone() {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
timer.finish();
|
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-06-17 23:32:20 +08:00
|
|
|
params.priority_fee,
|
|
|
|
|
params.lookup_table_key,
|
|
|
|
|
params.recent_blockhash,
|
|
|
|
|
0,
|
2025-08-19 18:07:21 +08:00
|
|
|
middleware_manager,
|
|
|
|
|
self.protocol_name.to_string(),
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|