2025-09-22 00:05:20 +08:00
|
|
|
use crate::trading::SwapParams;
|
2025-06-17 23:32:20 +08:00
|
|
|
use anyhow::Result;
|
2025-09-09 12:03:42 +08:00
|
|
|
use solana_sdk::{instruction::Instruction, signature::Signature};
|
2025-08-19 18:07:21 +08:00
|
|
|
|
2025-06-17 23:32:20 +08:00
|
|
|
/// 交易执行器trait - 定义了所有交易协议都需要实现的核心方法
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
|
pub trait TradeExecutor: Send + Sync {
|
2025-11-16 23:46:58 +08:00
|
|
|
async fn swap(&self, params: SwapParams) -> Result<(bool, Signature, Option<anyhow::Error>)>;
|
2025-06-17 23:32:20 +08:00
|
|
|
/// 获取协议名称
|
|
|
|
|
fn protocol_name(&self) -> &'static str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 指令构建器trait - 负责构建协议特定的交易指令
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
|
pub trait InstructionBuilder: Send + Sync {
|
|
|
|
|
/// 构建买入指令
|
2025-09-22 00:05:20 +08:00
|
|
|
async fn build_buy_instructions(&self, params: &SwapParams) -> Result<Vec<Instruction>>;
|
2025-06-17 23:32:20 +08:00
|
|
|
|
|
|
|
|
/// 构建卖出指令
|
2025-09-22 00:05:20 +08:00
|
|
|
async fn build_sell_instructions(&self, params: &SwapParams) -> Result<Vec<Instruction>>;
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 协议特定参数trait - 允许每个协议定义自己的参数
|
|
|
|
|
pub trait ProtocolParams: Send + Sync {
|
|
|
|
|
/// 将参数转换为Any以便向下转型
|
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any;
|
|
|
|
|
|
|
|
|
|
/// 克隆参数
|
|
|
|
|
fn clone_box(&self) -> Box<dyn ProtocolParams>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Clone for Box<dyn ProtocolParams> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
self.clone_box()
|
|
|
|
|
}
|
|
|
|
|
}
|