2025-06-17 23:32:20 +08:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2025-07-10 02:38:06 +08:00
|
|
|
use crate::instruction::{bonk::BonkInstructionBuilder, pumpfun::PumpFunInstructionBuilder, pumpswap::PumpSwapInstructionBuilder};
|
2025-07-09 22:29:29 +08:00
|
|
|
|
2025-06-17 23:32:20 +08:00
|
|
|
use super::{
|
|
|
|
|
core::{executor::GenericTradeExecutor, traits::TradeExecutor},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// 支持的交易协议
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2025-07-10 22:15:53 +08:00
|
|
|
pub enum DexType {
|
2025-06-17 23:32:20 +08:00
|
|
|
PumpFun,
|
|
|
|
|
PumpSwap,
|
2025-07-10 00:23:29 +08:00
|
|
|
Bonk,
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-07-10 22:15:53 +08:00
|
|
|
impl std::fmt::Display for DexType {
|
2025-06-17 23:32:20 +08:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
match self {
|
2025-07-10 22:15:53 +08:00
|
|
|
DexType::PumpFun => write!(f, "PumpFun"),
|
|
|
|
|
DexType::PumpSwap => write!(f, "PumpSwap"),
|
|
|
|
|
DexType::Bonk => write!(f, "Bonk"),
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-10 22:15:53 +08:00
|
|
|
impl std::str::FromStr for DexType {
|
2025-06-17 23:32:20 +08:00
|
|
|
type Err = anyhow::Error;
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
|
match s.to_lowercase().as_str() {
|
2025-07-10 22:15:53 +08:00
|
|
|
"pumpfun" => Ok(DexType::PumpFun),
|
|
|
|
|
"pumpswap" => Ok(DexType::PumpSwap),
|
|
|
|
|
"bonk" => Ok(DexType::Bonk),
|
2025-06-17 23:32:20 +08:00
|
|
|
_ => Err(anyhow!("Unsupported protocol: {}", s)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 交易工厂 - 用于创建不同协议的交易执行器
|
|
|
|
|
pub struct TradeFactory;
|
|
|
|
|
|
|
|
|
|
impl TradeFactory {
|
|
|
|
|
/// 创建指定协议的交易执行器
|
2025-07-11 00:02:51 +08:00
|
|
|
pub fn create_executor(dex_type: DexType) -> Arc<dyn TradeExecutor> {
|
|
|
|
|
match dex_type {
|
2025-07-10 22:15:53 +08:00
|
|
|
DexType::PumpFun => {
|
2025-06-17 23:32:20 +08:00
|
|
|
let instruction_builder = Arc::new(PumpFunInstructionBuilder);
|
|
|
|
|
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpFun"))
|
|
|
|
|
}
|
2025-07-10 22:15:53 +08:00
|
|
|
DexType::PumpSwap => {
|
2025-06-17 23:32:20 +08:00
|
|
|
let instruction_builder = Arc::new(PumpSwapInstructionBuilder);
|
|
|
|
|
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpSwap"))
|
|
|
|
|
}
|
2025-07-10 22:15:53 +08:00
|
|
|
DexType::Bonk => {
|
2025-07-10 00:23:29 +08:00
|
|
|
let instruction_builder = Arc::new(BonkInstructionBuilder);
|
2025-07-09 22:29:29 +08:00
|
|
|
Arc::new(GenericTradeExecutor::new(
|
|
|
|
|
instruction_builder,
|
2025-07-10 00:23:29 +08:00
|
|
|
"Bonk",
|
2025-07-09 22:29:29 +08:00
|
|
|
))
|
|
|
|
|
}
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 获取所有支持的协议
|
2025-07-11 00:02:51 +08:00
|
|
|
pub fn supported_dex_types() -> Vec<DexType> {
|
2025-07-10 22:15:53 +08:00
|
|
|
vec![DexType::PumpFun, DexType::PumpSwap, DexType::Bonk]
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 检查协议是否支持
|
2025-07-11 00:02:51 +08:00
|
|
|
pub fn is_supported(dex_type: &DexType) -> bool {
|
|
|
|
|
Self::supported_dex_types().contains(dex_type)
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
}
|