Files
sol-trade-sdk/src/trading/core/executor.rs
T

75 lines
2.5 KiB
Rust
Raw Normal View History

use anyhow::Result;
use solana_sdk::signature::Signature;
use std::{sync::Arc, time::Instant};
use crate::trading::core::parallel::{buy_parallel_execute, sell_parallel_execute};
use super::{
2025-09-17 11:14:15 +08:00
params::{BuyParams, SellParams},
traits::{InstructionBuilder, TradeExecutor},
};
2025-08-27 14:27:26 +08:00
/// Generic trade executor implementation
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 {
Self { instruction_builder, protocol_name }
}
}
#[async_trait::async_trait]
impl TradeExecutor for GenericTradeExecutor {
2025-09-17 11:14:15 +08:00
async fn buy_with_tip(&self, params: BuyParams) -> Result<Signature> {
let start = Instant::now();
// Build instructions directly from params to avoid unnecessary cloning
let instructions = self.instruction_builder.build_buy_instructions(&params).await?;
let final_instructions = match &params.middleware_manager {
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
println!("Building buy transaction instructions time cost: {:?}", start.elapsed());
2025-08-27 14:27:26 +08:00
// Execute transactions in parallel
buy_parallel_execute(params, final_instructions, self.protocol_name).await
}
2025-09-17 11:14:15 +08:00
async fn sell_with_tip(&self, params: SellParams) -> Result<Signature> {
let start = Instant::now();
// Build instructions directly from params to avoid unnecessary cloning
let instructions = self.instruction_builder.build_sell_instructions(&params).await?;
let final_instructions = match &params.middleware_manager {
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
println!("Building sell transaction instructions time cost: {:?}", start.elapsed());
2025-08-27 14:27:26 +08:00
// Execute transactions in parallel
sell_parallel_execute(params, final_instructions, self.protocol_name).await
}
fn protocol_name(&self) -> &'static str {
self.protocol_name
}
}