Files
sol-trade-sdk/src/trading/factory.rs
T
ysq 44ae51747b feat: refactor event parsing architecture and add Raydium Launchpad support
Major changes:
- Refactor event parsing system: migrate scattered logs_* modules to unified event_parser architecture
- Add unified UnifiedEvent trait and EventParser trait for standardized event parsing interface
- Introduce GenericEventParser and EventParserFactory for plugin-style protocol extension
- Add match_event! macro to simplify event type matching and handling
- Add Raydium Launchpad (Bonk.fun) protocol support:
  - Complete buy/sell trading functionality
  - Pool state management and querying
  - Event parsing and subscription
- Update trading system to support new protocol architecture
- Refactor constants organization, rename raydium to raydium_launchpad
- Update documentation and example code

Technical improvements:
- Unified event interface design for better code maintainability
- Factory pattern implementation for dynamic protocol loading
- Generic event parser to reduce code duplication
- Improved error handling and type safety

Breaking Changes:
- Remove old logs_* modules, use new event_parser system
- Protocol constants path change: raydium -> raydium_launchpad
- Event subscription API updated to unified interface
2025-07-09 22:29:29 +08:00

81 lines
2.5 KiB
Rust
Executable File

use anyhow::{anyhow, Result};
use std::sync::Arc;
use crate::trading::protocols::raydium_launchpad::RaydiumLaunchpadInstructionBuilder;
use super::{
core::{executor::GenericTradeExecutor, traits::TradeExecutor},
protocols::{pumpfun::PumpFunInstructionBuilder, pumpswap::PumpSwapInstructionBuilder},
};
/// 支持的交易协议
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Protocol {
PumpFun,
PumpSwap,
RaydiumLaunchpad,
}
impl std::fmt::Display for Protocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Protocol::PumpFun => write!(f, "PumpFun"),
Protocol::PumpSwap => write!(f, "PumpSwap"),
Protocol::RaydiumLaunchpad => write!(f, "RaydiumLaunchpad"),
}
}
}
impl std::str::FromStr for Protocol {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"pumpfun" => Ok(Protocol::PumpFun),
"pumpswap" => Ok(Protocol::PumpSwap),
"raydiumlaunchpad" => Ok(Protocol::RaydiumLaunchpad),
_ => Err(anyhow!("Unsupported protocol: {}", s)),
}
}
}
/// 交易工厂 - 用于创建不同协议的交易执行器
pub struct TradeFactory;
impl TradeFactory {
/// 创建指定协议的交易执行器
pub fn create_executor(protocol: Protocol) -> Arc<dyn TradeExecutor> {
match protocol {
Protocol::PumpFun => {
let instruction_builder = Arc::new(PumpFunInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpFun"))
}
Protocol::PumpSwap => {
let instruction_builder = Arc::new(PumpSwapInstructionBuilder);
Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpSwap"))
}
Protocol::RaydiumLaunchpad => {
let instruction_builder = Arc::new(RaydiumLaunchpadInstructionBuilder);
Arc::new(GenericTradeExecutor::new(
instruction_builder,
"RaydiumLaunchpad",
))
}
}
}
/// 获取所有支持的协议
pub fn supported_protocols() -> Vec<Protocol> {
vec![
Protocol::PumpFun,
Protocol::PumpSwap,
Protocol::RaydiumLaunchpad,
]
}
/// 检查协议是否支持
pub fn is_supported(protocol: &Protocol) -> bool {
Self::supported_protocols().contains(protocol)
}
}