From 42952dfc8bfc5703cc732fac4e8119dcf7b0a767 Mon Sep 17 00:00:00 2001 From: wood Date: Thu, 10 Jul 2025 22:15:53 +0800 Subject: [PATCH] rename protocol to dex_type --- README.md | 12 ++++---- README_CN.md | 12 ++++---- src/lib.rs | 66 +++++++++++++++++++++--------------------- src/main.rs | 14 ++++----- src/trading/factory.rs | 36 ++++++++++------------- 5 files changed, 68 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index 6b24e65..4289e42 100755 --- a/README.md +++ b/README.md @@ -255,6 +255,7 @@ async fn test_pumpfun() -> AnyResult<()> { // buy solana_trade_client .buy( + DexType::PumpFun, mint_pubkey, Some(creator), buy_sol_cost, @@ -262,7 +263,6 @@ async fn test_pumpfun() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpFun, Some(Box::new(PumpFunParams { bonding_curve: Some(Arc::new(bonding_curve.clone())), })), @@ -274,6 +274,7 @@ async fn test_pumpfun() -> AnyResult<()> { let amount_token = 0; // Enter the actual amount_token solana_trade_client .sell( + DexType::PumpFun, mint_pubkey, Some(creator), amount_token, @@ -281,7 +282,6 @@ async fn test_pumpfun() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpFun, None, ) .await?; @@ -306,6 +306,7 @@ async fn test_pumpswap() -> AnyResult<()> { // buy solana_trade_client .buy( + DexType::PumpSwap, mint_pubkey, Some(creator), buy_sol_cost, @@ -313,7 +314,6 @@ async fn test_pumpswap() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpSwap, None, ) .await?; @@ -323,6 +323,7 @@ async fn test_pumpswap() -> AnyResult<()> { let amount_token = 0; // Enter the actual amount_token solana_trade_client .sell( + DexType::PumpSwap, mint_pubkey, Some(creator), amount_token, @@ -330,7 +331,6 @@ async fn test_pumpswap() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpSwap, None, ) .await?; @@ -354,6 +354,7 @@ async fn test_bonk() -> Result<(), Box> { // buy solana_trade_client .buy( + DexType::Bonk, mint_pubkey, None, buy_sol_cost, @@ -361,7 +362,6 @@ async fn test_bonk() -> Result<(), Box> { recent_blockhash, None, false, - TradingProtocol::Bonk, None, ) .await?; @@ -371,6 +371,7 @@ async fn test_bonk() -> Result<(), Box> { let amount_token = 0; // Enter the actual amount_token solana_trade_client .sell( + DexType::Bonk, mint_pubkey, None, amount_token, @@ -378,7 +379,6 @@ async fn test_bonk() -> Result<(), Box> { recent_blockhash, None, false, - TradingProtocol::Bonk, None, ) .await?; diff --git a/README_CN.md b/README_CN.md index 0d29b91..9424610 100755 --- a/README_CN.md +++ b/README_CN.md @@ -255,6 +255,7 @@ async fn test_pumpfun() -> AnyResult<()> { // buy solana_trade_client .buy( + DexType::PumpFun, mint_pubkey, Some(creator), buy_sol_cost, @@ -262,7 +263,6 @@ async fn test_pumpfun() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpFun, Some(Box::new(PumpFunParams { bonding_curve: Some(Arc::new(bonding_curve.clone())), })), @@ -274,6 +274,7 @@ async fn test_pumpfun() -> AnyResult<()> { let amount_token = 0; // 写上真实的amount_token solana_trade_client .sell( + DexType::PumpFun, mint_pubkey, Some(creator), amount_token, @@ -281,7 +282,6 @@ async fn test_pumpfun() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpFun, None, ) .await?; @@ -306,6 +306,7 @@ async fn test_pumpswap() -> AnyResult<()> { // buy solana_trade_client .buy( + DexType::PumpSwap, mint_pubkey, Some(creator), buy_sol_cost, @@ -313,7 +314,6 @@ async fn test_pumpswap() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpSwap, None, ) .await?; @@ -323,6 +323,7 @@ async fn test_pumpswap() -> AnyResult<()> { let amount_token = 0; // 写上真实的amount_token solana_trade_client .sell( + DexType::PumpSwap, mint_pubkey, Some(creator), amount_token, @@ -330,7 +331,6 @@ async fn test_pumpswap() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpSwap, None, ) .await?; @@ -354,6 +354,7 @@ async fn test_bonk() -> Result<(), Box> { // buy solana_trade_client .buy( + DexType::Bonk, mint_pubkey, None, buy_sol_cost, @@ -361,7 +362,6 @@ async fn test_bonk() -> Result<(), Box> { recent_blockhash, None, false, - TradingProtocol::Bonk, None, ) .await?; @@ -371,6 +371,7 @@ async fn test_bonk() -> Result<(), Box> { let amount_token = 0; // 写上真实的amount_token solana_trade_client .sell( + DexType::Bonk, mint_pubkey, None, amount_token, @@ -378,7 +379,6 @@ async fn test_bonk() -> Result<(), Box> { recent_blockhash, None, false, - TradingProtocol::Bonk, None, ) .await?; diff --git a/src/lib.rs b/src/lib.rs index de2f9f7..2c48a5c 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ use crate::trading::core::params::BonkParams; use crate::trading::core::params::PumpFunParams; use crate::trading::core::params::PumpSwapParams; use crate::trading::core::traits::ProtocolParams; -use crate::trading::factory::TradingProtocol; +use crate::trading::factory::DexType; use crate::trading::BuyParams; use crate::trading::SellParams; use crate::trading::TradeFactory; @@ -153,28 +153,28 @@ impl SolanaTrade { /// ``` pub async fn buy( &self, + dex_type: DexType, mint: Pubkey, creator: Option, amount_sol: u64, slippage_basis_points: Option, recent_blockhash: Hash, custom_buy_tip_fee: Option, - with_tip: bool, - protocol: TradingProtocol, - protocol_params: Option>, + with_tip: bool, + extension_params: Option>, ) -> Result<(), anyhow::Error> { - let executor = TradeFactory::create_executor(protocol.clone()); - let protocol_params = if let Some(params) = protocol_params { + let executor = TradeFactory::create_executor(dex_type.clone()); + let protocol_params = if let Some(params) = extension_params { params } else { - match protocol { - TradingProtocol::PumpFun => { + match dex_type { + DexType::PumpFun => { Box::new(PumpFunParams::default()) as Box } - TradingProtocol::PumpSwap => { + DexType::PumpSwap => { Box::new(PumpSwapParams::default()) as Box } - TradingProtocol::Bonk => Box::new(BonkParams::default()) as Box, + DexType::Bonk => Box::new(BonkParams::default()) as Box, } }; let buy_params = BuyParams { @@ -203,16 +203,16 @@ impl SolanaTrade { let buy_with_tip_params = buy_params.clone().with_tip(self.swqos_clients.clone()); // Validate protocol params - let is_valid_params = match protocol { - TradingProtocol::PumpFun => protocol_params + let is_valid_params = match dex_type { + DexType::PumpFun => protocol_params .as_any() .downcast_ref::() .is_some(), - TradingProtocol::PumpSwap => protocol_params + DexType::PumpSwap => protocol_params .as_any() .downcast_ref::() .is_some(), - TradingProtocol::Bonk => protocol_params + DexType::Bonk => protocol_params .as_any() .downcast_ref::() .is_some(), @@ -262,7 +262,7 @@ impl SolanaTrade { /// ```rust /// use solana_sdk::pubkey::Pubkey; /// use solana_sdk::hash::Hash; - /// use crate::trading::factory::TradingProtocol; + /// use crate::trading::factory::DexType; /// /// let mint = Pubkey::new_unique(); /// let amount_token = 1_000_000; // Amount of tokens to sell @@ -277,12 +277,13 @@ impl SolanaTrade { /// recent_blockhash, /// None, /// true, - /// TradingProtocol::PumpFun, + /// DexType::PumpFun, /// None, /// ).await?; /// ``` pub async fn sell( &self, + dex_type: DexType, mint: Pubkey, creator: Option, amount_token: u64, @@ -290,21 +291,20 @@ impl SolanaTrade { recent_blockhash: Hash, custom_buy_tip_fee: Option, with_tip: bool, - protocol: TradingProtocol, - protocol_params: Option>, + extension_params: Option>, ) -> Result<(), anyhow::Error> { - let executor = TradeFactory::create_executor(protocol.clone()); - let protocol_params = if let Some(params) = protocol_params { + let executor = TradeFactory::create_executor(dex_type.clone()); + let protocol_params = if let Some(params) = extension_params { params } else { - match protocol { - TradingProtocol::PumpFun => { + match dex_type { + DexType::PumpFun => { Box::new(PumpFunParams::default()) as Box } - TradingProtocol::PumpSwap => { + DexType::PumpSwap => { Box::new(PumpSwapParams::default()) as Box } - TradingProtocol::Bonk => Box::new(BonkParams::default()) as Box, + DexType::Bonk => Box::new(BonkParams::default()) as Box, } }; let sell_params = SellParams { @@ -332,16 +332,16 @@ impl SolanaTrade { let sell_with_tip_params = sell_params.clone().with_tip(self.swqos_clients.clone()); // Validate protocol params - let is_valid_params = match protocol { - TradingProtocol::PumpFun => protocol_params + let is_valid_params = match dex_type { + DexType::PumpFun => protocol_params .as_any() .downcast_ref::() .is_some(), - TradingProtocol::PumpSwap => protocol_params + DexType::PumpSwap => protocol_params .as_any() .downcast_ref::() .is_some(), - TradingProtocol::Bonk => protocol_params + DexType::Bonk => protocol_params .as_any() .downcast_ref::() .is_some(), @@ -414,12 +414,13 @@ impl SolanaTrade { /// recent_blockhash, /// None, /// true, - /// TradingProtocol::PumpFun, + /// DexType::PumpFun, /// None, /// ).await?; /// ``` pub async fn sell_by_percent( &self, + dex_type: DexType, mint: Pubkey, creator: Option, amount_token: u64, @@ -428,14 +429,14 @@ impl SolanaTrade { recent_blockhash: Hash, custom_buy_tip_fee: Option, with_tip: bool, - protocol: TradingProtocol, - protocol_params: Option>, + extension_params: Option>, ) -> Result<(), anyhow::Error> { if percent == 0 || percent > 100 { return Err(anyhow::anyhow!("Percentage must be between 1 and 100")); } let amount = amount_token * percent / 100; self.sell( + dex_type, mint, creator, amount, @@ -443,8 +444,7 @@ impl SolanaTrade { recent_blockhash, custom_buy_tip_fee, with_tip, - protocol, - protocol_params, + extension_params, ) .await } diff --git a/src/main.rs b/src/main.rs index d096338..f612085 100755 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use sol_trade_sdk::{ }, swqos::{SwqosConfig, SwqosRegion}, trading::{ - core::params::PumpFunParams, factory::TradingProtocol, + core::params::PumpFunParams, factory::DexType, pumpfun::common::get_bonding_curve_account_v2, }, SolanaTrade, @@ -105,6 +105,7 @@ async fn test_pumpfun() -> AnyResult<()> { // buy solana_trade_client .buy( + DexType::PumpFun, mint_pubkey, Some(creator), buy_sol_cost, @@ -112,7 +113,6 @@ async fn test_pumpfun() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpFun, Some(Box::new(PumpFunParams { bonding_curve: Some(Arc::new(bonding_curve.clone())), })), @@ -123,6 +123,7 @@ async fn test_pumpfun() -> AnyResult<()> { let amount_token = 0; // 写上真实的amount_token solana_trade_client .sell( + DexType::PumpFun, mint_pubkey, Some(creator), amount_token, @@ -130,7 +131,6 @@ async fn test_pumpfun() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpFun, None, ) .await?; @@ -151,6 +151,7 @@ async fn test_pumpswap() -> AnyResult<()> { // buy solana_trade_client .buy( + DexType::PumpFun, mint_pubkey, Some(creator), buy_sol_cost, @@ -158,7 +159,6 @@ async fn test_pumpswap() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpSwap, None, ) .await?; @@ -167,6 +167,7 @@ async fn test_pumpswap() -> AnyResult<()> { let amount_token = 0; // 写上真实的amount_token solana_trade_client .sell( + DexType::PumpSwap, mint_pubkey, Some(creator), amount_token, @@ -174,7 +175,6 @@ async fn test_pumpswap() -> AnyResult<()> { recent_blockhash, None, false, - TradingProtocol::PumpSwap, None, ) .await?; @@ -194,6 +194,7 @@ async fn test_bonk() -> Result<(), Box> { // buy solana_trade_client .buy( + DexType::Bonk, mint_pubkey, None, buy_sol_cost, @@ -201,7 +202,6 @@ async fn test_bonk() -> Result<(), Box> { recent_blockhash, None, false, - TradingProtocol::Bonk, None, ) .await?; @@ -210,6 +210,7 @@ async fn test_bonk() -> Result<(), Box> { let amount_token = 0; // 写上真实的amount_token solana_trade_client .sell( + DexType::Bonk, mint_pubkey, None, amount_token, @@ -217,7 +218,6 @@ async fn test_bonk() -> Result<(), Box> { recent_blockhash, None, false, - TradingProtocol::Bonk, None, ) .await?; diff --git a/src/trading/factory.rs b/src/trading/factory.rs index 923f793..48bce45 100755 --- a/src/trading/factory.rs +++ b/src/trading/factory.rs @@ -9,30 +9,30 @@ use super::{ /// 支持的交易协议 #[derive(Debug, Clone, PartialEq, Eq)] -pub enum TradingProtocol { +pub enum DexType { PumpFun, PumpSwap, Bonk, } -impl std::fmt::Display for TradingProtocol { +impl std::fmt::Display for DexType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - TradingProtocol::PumpFun => write!(f, "PumpFun"), - TradingProtocol::PumpSwap => write!(f, "PumpSwap"), - TradingProtocol::Bonk => write!(f, "Bonk"), + DexType::PumpFun => write!(f, "PumpFun"), + DexType::PumpSwap => write!(f, "PumpSwap"), + DexType::Bonk => write!(f, "Bonk"), } } } -impl std::str::FromStr for TradingProtocol { +impl std::str::FromStr for DexType { type Err = anyhow::Error; fn from_str(s: &str) -> Result { match s.to_lowercase().as_str() { - "pumpfun" => Ok(TradingProtocol::PumpFun), - "pumpswap" => Ok(TradingProtocol::PumpSwap), - "bonk" => Ok(TradingProtocol::Bonk), + "pumpfun" => Ok(DexType::PumpFun), + "pumpswap" => Ok(DexType::PumpSwap), + "bonk" => Ok(DexType::Bonk), _ => Err(anyhow!("Unsupported protocol: {}", s)), } } @@ -43,17 +43,17 @@ pub struct TradeFactory; impl TradeFactory { /// 创建指定协议的交易执行器 - pub fn create_executor(protocol: TradingProtocol) -> Arc { + pub fn create_executor(protocol: DexType) -> Arc { match protocol { - TradingProtocol::PumpFun => { + DexType::PumpFun => { let instruction_builder = Arc::new(PumpFunInstructionBuilder); Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpFun")) } - TradingProtocol::PumpSwap => { + DexType::PumpSwap => { let instruction_builder = Arc::new(PumpSwapInstructionBuilder); Arc::new(GenericTradeExecutor::new(instruction_builder, "PumpSwap")) } - TradingProtocol::Bonk => { + DexType::Bonk => { let instruction_builder = Arc::new(BonkInstructionBuilder); Arc::new(GenericTradeExecutor::new( instruction_builder, @@ -64,16 +64,12 @@ impl TradeFactory { } /// 获取所有支持的协议 - pub fn supported_protocols() -> Vec { - vec![ - TradingProtocol::PumpFun, - TradingProtocol::PumpSwap, - TradingProtocol::Bonk, - ] + pub fn supported_protocols() -> Vec { + vec![DexType::PumpFun, DexType::PumpSwap, DexType::Bonk] } /// 检查协议是否支持 - pub fn is_supported(protocol: &TradingProtocol) -> bool { + pub fn is_supported(protocol: &DexType) -> bool { Self::supported_protocols().contains(protocol) } }