rename protocol to dex_type

This commit is contained in:
wood
2025-07-10 22:15:53 +08:00
parent 7b1951cf29
commit 42952dfc8b
5 changed files with 68 additions and 72 deletions
+6 -6
View File
@@ -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<dyn std::error::Error>> {
// buy
solana_trade_client
.buy(
DexType::Bonk,
mint_pubkey,
None,
buy_sol_cost,
@@ -361,7 +362,6 @@ async fn test_bonk() -> Result<(), Box<dyn std::error::Error>> {
recent_blockhash,
None,
false,
TradingProtocol::Bonk,
None,
)
.await?;
@@ -371,6 +371,7 @@ async fn test_bonk() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
recent_blockhash,
None,
false,
TradingProtocol::Bonk,
None,
)
.await?;
+6 -6
View File
@@ -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<dyn std::error::Error>> {
// buy
solana_trade_client
.buy(
DexType::Bonk,
mint_pubkey,
None,
buy_sol_cost,
@@ -361,7 +362,6 @@ async fn test_bonk() -> Result<(), Box<dyn std::error::Error>> {
recent_blockhash,
None,
false,
TradingProtocol::Bonk,
None,
)
.await?;
@@ -371,6 +371,7 @@ async fn test_bonk() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
recent_blockhash,
None,
false,
TradingProtocol::Bonk,
None,
)
.await?;
+33 -33
View File
@@ -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<Pubkey>,
amount_sol: u64,
slippage_basis_points: Option<u64>,
recent_blockhash: Hash,
custom_buy_tip_fee: Option<f64>,
with_tip: bool,
protocol: TradingProtocol,
protocol_params: Option<Box<dyn ProtocolParams>>,
with_tip: bool,
extension_params: Option<Box<dyn ProtocolParams>>,
) -> 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<dyn ProtocolParams>
}
TradingProtocol::PumpSwap => {
DexType::PumpSwap => {
Box::new(PumpSwapParams::default()) as Box<dyn ProtocolParams>
}
TradingProtocol::Bonk => Box::new(BonkParams::default()) as Box<dyn ProtocolParams>,
DexType::Bonk => Box::new(BonkParams::default()) as Box<dyn ProtocolParams>,
}
};
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::<PumpFunParams>()
.is_some(),
TradingProtocol::PumpSwap => protocol_params
DexType::PumpSwap => protocol_params
.as_any()
.downcast_ref::<PumpSwapParams>()
.is_some(),
TradingProtocol::Bonk => protocol_params
DexType::Bonk => protocol_params
.as_any()
.downcast_ref::<BonkParams>()
.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<Pubkey>,
amount_token: u64,
@@ -290,21 +291,20 @@ impl SolanaTrade {
recent_blockhash: Hash,
custom_buy_tip_fee: Option<f64>,
with_tip: bool,
protocol: TradingProtocol,
protocol_params: Option<Box<dyn ProtocolParams>>,
extension_params: Option<Box<dyn ProtocolParams>>,
) -> 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<dyn ProtocolParams>
}
TradingProtocol::PumpSwap => {
DexType::PumpSwap => {
Box::new(PumpSwapParams::default()) as Box<dyn ProtocolParams>
}
TradingProtocol::Bonk => Box::new(BonkParams::default()) as Box<dyn ProtocolParams>,
DexType::Bonk => Box::new(BonkParams::default()) as Box<dyn ProtocolParams>,
}
};
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::<PumpFunParams>()
.is_some(),
TradingProtocol::PumpSwap => protocol_params
DexType::PumpSwap => protocol_params
.as_any()
.downcast_ref::<PumpSwapParams>()
.is_some(),
TradingProtocol::Bonk => protocol_params
DexType::Bonk => protocol_params
.as_any()
.downcast_ref::<BonkParams>()
.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<Pubkey>,
amount_token: u64,
@@ -428,14 +429,14 @@ impl SolanaTrade {
recent_blockhash: Hash,
custom_buy_tip_fee: Option<f64>,
with_tip: bool,
protocol: TradingProtocol,
protocol_params: Option<Box<dyn ProtocolParams>>,
extension_params: Option<Box<dyn ProtocolParams>>,
) -> 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
}
+7 -7
View File
@@ -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<dyn std::error::Error>> {
// buy
solana_trade_client
.buy(
DexType::Bonk,
mint_pubkey,
None,
buy_sol_cost,
@@ -201,7 +202,6 @@ async fn test_bonk() -> Result<(), Box<dyn std::error::Error>> {
recent_blockhash,
None,
false,
TradingProtocol::Bonk,
None,
)
.await?;
@@ -210,6 +210,7 @@ async fn test_bonk() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
recent_blockhash,
None,
false,
TradingProtocol::Bonk,
None,
)
.await?;
+16 -20
View File
@@ -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<Self, Self::Err> {
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<dyn TradeExecutor> {
pub fn create_executor(protocol: DexType) -> Arc<dyn TradeExecutor> {
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<TradingProtocol> {
vec![
TradingProtocol::PumpFun,
TradingProtocol::PumpSwap,
TradingProtocol::Bonk,
]
pub fn supported_protocols() -> Vec<DexType> {
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)
}
}