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-22 00:05:20 +08:00
|
|
|
use crate::trading::core::{
|
|
|
|
|
parallel::{buy_parallel_execute, sell_parallel_execute},
|
|
|
|
|
traits::TradeExecutor,
|
2025-06-17 23:32:20 +08:00
|
|
|
};
|
2025-07-10 18:14:21 +08:00
|
|
|
|
2025-09-22 00:05:20 +08:00
|
|
|
use super::{params::SwapParams, traits::InstructionBuilder};
|
|
|
|
|
|
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-22 00:05:20 +08:00
|
|
|
async fn swap(&self, params: SwapParams) -> Result<Signature> {
|
2025-09-07 18:07:45 +08:00
|
|
|
let start = Instant::now();
|
2025-09-22 00:05:20 +08:00
|
|
|
// 暂时支持这三种。后续重构扩展builder 支持所有的 swap
|
|
|
|
|
let is_buy = params.input_mint == crate::constants::SOL_TOKEN_ACCOUNT
|
|
|
|
|
|| params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
|
|
|
|
|| (params.input_mint == crate::constants::USD1_TOKEN_ACCOUNT
|
|
|
|
|
&& params.output_mint != crate::constants::WSOL_TOKEN_ACCOUNT);
|
2025-09-07 18:07:45 +08:00
|
|
|
// Build instructions directly from params to avoid unnecessary cloning
|
2025-09-22 00:05:20 +08:00
|
|
|
let instructions = if is_buy {
|
|
|
|
|
self.instruction_builder.build_buy_instructions(¶ms).await?
|
|
|
|
|
} else {
|
|
|
|
|
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(),
|
2025-09-22 00:05:20 +08:00
|
|
|
is_buy,
|
2025-08-19 18:07:21 +08:00
|
|
|
)?,
|
|
|
|
|
None => instructions,
|
|
|
|
|
};
|
2025-09-22 00:05:20 +08:00
|
|
|
println!("Building swap transaction instructions time cost: {:?}", start.elapsed());
|
2025-08-27 14:27:26 +08:00
|
|
|
// Execute transactions in parallel
|
2025-09-22 00:05:20 +08:00
|
|
|
if is_buy {
|
|
|
|
|
buy_parallel_execute(params, final_instructions, self.protocol_name).await
|
|
|
|
|
} else {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|