2025-09-07 17:22:58 +08:00
|
|
|
use anyhow::Result;
|
2025-09-09 12:03:42 +08:00
|
|
|
use solana_sdk::signature::Signature;
|
2025-09-07 18:07:45 +08:00
|
|
|
use std::{sync::Arc, time::Instant};
|
2025-06-17 23:32:20 +08:00
|
|
|
|
2025-09-09 17:02:24 +08:00
|
|
|
use crate::trading::core::parallel::{buy_parallel_execute, sell_parallel_execute};
|
|
|
|
|
|
2025-06-17 23:32:20 +08:00
|
|
|
use super::{
|
2025-09-17 00:10:33 +08:00
|
|
|
params::{InternalBuyParams, InternalSellParams},
|
2025-06-17 23:32:20 +08:00
|
|
|
traits::{InstructionBuilder, TradeExecutor},
|
|
|
|
|
};
|
2025-07-10 18:14:21 +08:00
|
|
|
|
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-09-17 00:10:33 +08:00
|
|
|
async fn buy_with_tip(&self, params: InternalBuyParams) -> Result<Signature> {
|
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_buy_instructions(¶ms).await?;
|
2025-09-09 17:02:24 +08:00
|
|
|
let final_instructions = match ¶ms.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-09-09 17:09:35 +08:00
|
|
|
buy_parallel_execute(params, final_instructions, self.protocol_name).await
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 00:10:33 +08:00
|
|
|
async fn sell_with_tip(&self, params: InternalSellParams) -> Result<Signature> {
|
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?;
|
2025-09-09 17:02:24 +08:00
|
|
|
let final_instructions = match ¶ms.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-09-09 17:09:35 +08:00
|
|
|
sell_parallel_execute(params, final_instructions, self.protocol_name).await
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn protocol_name(&self) -> &'static str {
|
|
|
|
|
self.protocol_name
|
|
|
|
|
}
|
|
|
|
|
}
|