Files
sol-trade-sdk/src/trading/factory.rs
T

76 lines
2.3 KiB
Rust
Raw Normal View History

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};
use super::{
core::{executor::GenericTradeExecutor, traits::TradeExecutor},
};
/// 支持的交易协议
#[derive(Debug, Clone, PartialEq, Eq)]
2025-07-10 22:15:53 +08:00
pub enum DexType {
PumpFun,
PumpSwap,
Bonk,
}
2025-07-10 22:15:53 +08:00
impl std::fmt::Display for DexType {
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-07-10 22:15:53 +08:00
impl std::str::FromStr for DexType {
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),
_ => 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 => {
let instruction_builder = Arc::new(PumpFunInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpFun"))
}
2025-07-10 22:15:53 +08:00
DexType::PumpSwap => {
let instruction_builder = Arc::new(PumpSwapInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpSwap"))
}
2025-07-10 22:15:53 +08:00
DexType::Bonk => {
let instruction_builder = Arc::new(BonkInstructionBuilder);
Arc::new(GenericTradeExecutor::new(
instruction_builder,
"Bonk",
))
}
}
}
/// 获取所有支持的协议
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-07-11 00:02:51 +08:00
pub fn is_supported(dex_type: &DexType) -> bool {
Self::supported_dex_types().contains(dex_type)
}
}