2025-06-17 23:32:20 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2025-07-16 22:54:23 +08:00
|
|
|
use crate::instruction::{
|
2025-10-05 22:27:04 +08:00
|
|
|
bonk::BonkInstructionBuilder, meteora_damm_v2::MeteoraDammV2InstructionBuilder,
|
|
|
|
|
pumpfun::PumpFunInstructionBuilder, pumpswap::PumpSwapInstructionBuilder,
|
|
|
|
|
raydium_amm_v4::RaydiumAmmV4InstructionBuilder, raydium_cpmm::RaydiumCpmmInstructionBuilder,
|
2025-06-17 23:32:20 +08:00
|
|
|
};
|
|
|
|
|
|
2025-07-16 22:54:23 +08:00
|
|
|
use super::core::{executor::GenericTradeExecutor, traits::TradeExecutor};
|
|
|
|
|
|
2025-06-17 23:32:20 +08:00
|
|
|
/// 支持的交易协议
|
2025-12-08 01:35:40 +08:00
|
|
|
#[derive(Debug, Clone, Copy, 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-07-16 22:54:23 +08:00
|
|
|
RaydiumCpmm,
|
2025-08-19 18:07:21 +08:00
|
|
|
RaydiumAmmV4,
|
2025-10-05 22:27:04 +08:00
|
|
|
MeteoraDammV2,
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 交易工厂 - 用于创建不同协议的交易执行器
|
|
|
|
|
pub struct TradeFactory;
|
|
|
|
|
|
|
|
|
|
impl TradeFactory {
|
2025-09-07 18:07:45 +08:00
|
|
|
/// 创建指定协议的交易执行器(零开销单例)
|
2025-07-11 00:02:51 +08:00
|
|
|
pub fn create_executor(dex_type: DexType) -> Arc<dyn TradeExecutor> {
|
|
|
|
|
match dex_type {
|
2025-09-07 18:07:45 +08:00
|
|
|
DexType::PumpFun => Self::pumpfun_executor(),
|
|
|
|
|
DexType::PumpSwap => Self::pumpswap_executor(),
|
|
|
|
|
DexType::Bonk => Self::bonk_executor(),
|
|
|
|
|
DexType::RaydiumCpmm => Self::raydium_cpmm_executor(),
|
|
|
|
|
DexType::RaydiumAmmV4 => Self::raydium_amm_v4_executor(),
|
2025-10-05 22:27:04 +08:00
|
|
|
DexType::MeteoraDammV2 => Self::meteora_damm_v2_executor(),
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-07 18:07:45 +08:00
|
|
|
// Static instances created at compile time - zero runtime overhead
|
|
|
|
|
#[inline]
|
|
|
|
|
fn pumpfun_executor() -> Arc<dyn TradeExecutor> {
|
|
|
|
|
static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
|
|
|
|
|
std::sync::LazyLock::new(|| {
|
|
|
|
|
let instruction_builder = Arc::new(PumpFunInstructionBuilder);
|
|
|
|
|
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpFun"))
|
|
|
|
|
});
|
|
|
|
|
INSTANCE.clone()
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 18:07:45 +08:00
|
|
|
#[inline]
|
|
|
|
|
fn pumpswap_executor() -> Arc<dyn TradeExecutor> {
|
|
|
|
|
static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
|
|
|
|
|
std::sync::LazyLock::new(|| {
|
|
|
|
|
let instruction_builder = Arc::new(PumpSwapInstructionBuilder);
|
|
|
|
|
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpSwap"))
|
|
|
|
|
});
|
|
|
|
|
INSTANCE.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn bonk_executor() -> Arc<dyn TradeExecutor> {
|
|
|
|
|
static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
|
|
|
|
|
std::sync::LazyLock::new(|| {
|
|
|
|
|
let instruction_builder = Arc::new(BonkInstructionBuilder);
|
|
|
|
|
Arc::new(GenericTradeExecutor::new(instruction_builder, "Bonk"))
|
|
|
|
|
});
|
|
|
|
|
INSTANCE.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn raydium_cpmm_executor() -> Arc<dyn TradeExecutor> {
|
|
|
|
|
static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
|
|
|
|
|
std::sync::LazyLock::new(|| {
|
|
|
|
|
let instruction_builder = Arc::new(RaydiumCpmmInstructionBuilder);
|
|
|
|
|
Arc::new(GenericTradeExecutor::new(instruction_builder, "RaydiumCpmm"))
|
|
|
|
|
});
|
|
|
|
|
INSTANCE.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn raydium_amm_v4_executor() -> Arc<dyn TradeExecutor> {
|
|
|
|
|
static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
|
|
|
|
|
std::sync::LazyLock::new(|| {
|
|
|
|
|
let instruction_builder = Arc::new(RaydiumAmmV4InstructionBuilder);
|
|
|
|
|
Arc::new(GenericTradeExecutor::new(instruction_builder, "RaydiumAmmV4"))
|
|
|
|
|
});
|
|
|
|
|
INSTANCE.clone()
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|
2025-10-05 22:27:04 +08:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn meteora_damm_v2_executor() -> Arc<dyn TradeExecutor> {
|
|
|
|
|
static INSTANCE: std::sync::LazyLock<Arc<dyn TradeExecutor>> =
|
|
|
|
|
std::sync::LazyLock::new(|| {
|
|
|
|
|
let instruction_builder = Arc::new(MeteoraDammV2InstructionBuilder);
|
|
|
|
|
Arc::new(GenericTradeExecutor::new(instruction_builder, "MeteoraDammV2"))
|
|
|
|
|
});
|
|
|
|
|
INSTANCE.clone()
|
|
|
|
|
}
|
2025-06-17 23:32:20 +08:00
|
|
|
}
|