From 6f953ffa0d8d493c2391250dd563659adda8d5d6 Mon Sep 17 00:00:00 2001 From: wei <1415121722@qq.com> Date: Tue, 2 Sep 2025 09:43:53 +0800 Subject: [PATCH] The buy and sell methods have added the priority_fee parameter, which can control the priority fee for each transaction --- Cargo.toml | 2 +- README.md | 18 +++++++- README_CN.md | 18 +++++++- examples/bonk_copy_trading/src/main.rs | 2 + examples/bonk_sniper_trading/src/main.rs | 2 + examples/middleware_system/src/main.rs | 1 + examples/pumpfun_copy_trading/src/main.rs | 2 + examples/pumpfun_sniper_trading/src/main.rs | 2 + examples/pumpswap_trading/src/main.rs | 2 + examples/raydium_amm_v4_trading/src/main.rs | 2 + examples/raydium_cpmm_trading/src/main.rs | 2 + src/lib.rs | 46 +++++++++++++++++++-- src/main.rs | 17 ++++++++ 13 files changed, 107 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7fada4e..051cc67 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sol-trade-sdk" -version = "0.5.3" +version = "0.5.4" edition = "2021" authors = [ "William ", diff --git a/README.md b/README.md index 29fb925..a6fac1d 100644 --- a/README.md +++ b/README.md @@ -33,14 +33,14 @@ Add the dependency to your `Cargo.toml`: ```toml # Add to your Cargo.toml -sol-trade-sdk = { path = "./sol-trade-sdk", version = "0.5.3" } +sol-trade-sdk = { path = "./sol-trade-sdk", version = "0.5.4" } ``` ### Use crates.io ```toml # Add to your Cargo.toml -sol-trade-sdk = "0.5.3" +sol-trade-sdk = "0.5.4" ``` ## Usage Examples @@ -71,6 +71,20 @@ The `lookup_table_key` parameter is an optional `Pubkey` that specifies an addre - Improves transaction success rate and speed - Particularly useful for complex transactions with many account references +#### priority_fee Parameter + +The `priority_fee` parameter is an optional `PriorityFee` that allows you to override the default priority fee settings for individual transactions: + +- **Purpose**: Provides fine-grained control over transaction priority fees on a per-transaction basis +- **Usage**: + - Can be passed to `buy()` and `sell()` methods to override the global priority fee settings + - If not provided, defaults to `None` and uses the priority fee settings from `TradeConfig` + - When provided, the `buy_tip_fees` array will be automatically padded to match the number of SWQOS clients +- **Benefits**: + - Allows dynamic adjustment of priority fees based on market conditions + - Enables different fee strategies for different types of transactions + - Provides flexibility for high-frequency trading scenarios + #### About ShredStream When using shred to subscribe to events, due to the nature of shreds, you cannot get complete information about transaction events. diff --git a/README_CN.md b/README_CN.md index 85d310a..6656177 100755 --- a/README_CN.md +++ b/README_CN.md @@ -33,14 +33,14 @@ git clone https://github.com/0xfnzero/sol-trade-sdk ```toml # 添加到您的 Cargo.toml -sol-trade-sdk = { path = "./sol-trade-sdk", version = "0.5.3" } +sol-trade-sdk = { path = "./sol-trade-sdk", version = "0.5.4" } ``` ### 使用 crates.io ```toml # 添加到您的 Cargo.toml -sol-trade-sdk = "0.5.3" +sol-trade-sdk = "0.5.4" ``` ## 使用示例 @@ -71,6 +71,20 @@ sol-trade-sdk = "0.5.3" - 提高交易成功率和速度 - 特别适用于具有许多账户引用的复杂交易 +#### priority_fee 参数 + +`priority_fee` 参数是一个可选的 `PriorityFee`,允许您为单个交易覆盖默认的优先级费用设置: + +- **用途**:为每个交易提供对交易优先级费用的细粒度控制 +- **使用方法**: + - 可以传递给 `buy()` 和 `sell()` 方法来覆盖全局优先级费用设置 + - 如果不提供,默认为 `None` 并使用 `TradeConfig` 中的优先级费用设置 + - 当提供时,`buy_tip_fees` 数组将自动填充以匹配 SWQOS 客户端的数量 +- **优势**: + - 允许根据市场条件动态调整优先级费用 + - 为不同类型的交易启用不同的费用策略 + - 为高频交易场景提供灵活性 + #### 关于shredstream 当你使用 shred 订阅事件时,由于 shred 的特性,你无法获取到交易事件的完整信息。 diff --git a/examples/bonk_copy_trading/src/main.rs b/examples/bonk_copy_trading/src/main.rs index 210b999..8897d64 100644 --- a/examples/bonk_copy_trading/src/main.rs +++ b/examples/bonk_copy_trading/src/main.rs @@ -152,6 +152,7 @@ async fn bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult<()> Box::new(BonkParams::from_trade(trade_info.clone())), None, true, + None, ) .await?; @@ -178,6 +179,7 @@ async fn bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult<()> Box::new(BonkParams::from_trade(trade_info.clone())), None, true, + None, ) .await?; diff --git a/examples/bonk_sniper_trading/src/main.rs b/examples/bonk_sniper_trading/src/main.rs index d31328f..708e10a 100644 --- a/examples/bonk_sniper_trading/src/main.rs +++ b/examples/bonk_sniper_trading/src/main.rs @@ -121,6 +121,7 @@ async fn bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyResult< Box::new(BonkParams::from_dev_trade(trade_info.clone())), None, true, + None, ) .await?; @@ -152,6 +153,7 @@ async fn bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyResult< )), None, true, + None, ) .await?; diff --git a/examples/middleware_system/src/main.rs b/examples/middleware_system/src/main.rs index 3bc3914..82c0cd4 100644 --- a/examples/middleware_system/src/main.rs +++ b/examples/middleware_system/src/main.rs @@ -103,6 +103,7 @@ async fn test_middleware() -> AnyResult<()> { Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?), None, true, + None, ) .await?; println!("tip: This transaction will not succeed because we're using a test account. You can modify the code to initialize the payer with your own private key"); diff --git a/examples/pumpfun_copy_trading/src/main.rs b/examples/pumpfun_copy_trading/src/main.rs index 60aed62..29a4b3f 100644 --- a/examples/pumpfun_copy_trading/src/main.rs +++ b/examples/pumpfun_copy_trading/src/main.rs @@ -146,6 +146,7 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul Box::new(PumpFunParams::from_trade(&trade_info, None)), None, true, + None, ) .await?; @@ -172,6 +173,7 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul Box::new(PumpFunParams::from_trade(&trade_info, Some(true))), None, true, + None, ) .await?; diff --git a/examples/pumpfun_sniper_trading/src/main.rs b/examples/pumpfun_sniper_trading/src/main.rs index c64d9c8..77733c6 100644 --- a/examples/pumpfun_sniper_trading/src/main.rs +++ b/examples/pumpfun_sniper_trading/src/main.rs @@ -114,6 +114,7 @@ async fn pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> AnyR Box::new(PumpFunParams::from_dev_trade(&trade_info, None)), None, true, + None, ) .await?; @@ -140,6 +141,7 @@ async fn pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> AnyR Box::new(PumpFunParams::immediate_sell(trade_info.creator_vault, true)), None, true, + None, ) .await?; diff --git a/examples/pumpswap_trading/src/main.rs b/examples/pumpswap_trading/src/main.rs index 77938a4..f7aeec6 100644 --- a/examples/pumpswap_trading/src/main.rs +++ b/examples/pumpswap_trading/src/main.rs @@ -188,6 +188,7 @@ async fn pumpswap_trade_with_grpc(mint_pubkey: Pubkey, params: PumpSwapParams) - Box::new(params.clone()), None, true, + None, ) .await?; @@ -216,6 +217,7 @@ async fn pumpswap_trade_with_grpc(mint_pubkey: Pubkey, params: PumpSwapParams) - Box::new(params.clone()), None, true, + None, ) .await?; diff --git a/examples/raydium_amm_v4_trading/src/main.rs b/examples/raydium_amm_v4_trading/src/main.rs index 0bd1c54..749ed7d 100644 --- a/examples/raydium_amm_v4_trading/src/main.rs +++ b/examples/raydium_amm_v4_trading/src/main.rs @@ -161,6 +161,7 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent) Box::new(params), None, true, + None, ) .await?; @@ -188,6 +189,7 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent) Box::new(params), None, true, + None, ) .await?; diff --git a/examples/raydium_cpmm_trading/src/main.rs b/examples/raydium_cpmm_trading/src/main.rs index a7d4738..e7995fe 100644 --- a/examples/raydium_cpmm_trading/src/main.rs +++ b/examples/raydium_cpmm_trading/src/main.rs @@ -162,6 +162,7 @@ async fn raydium_cpmm_copy_trade_with_grpc(trade_info: RaydiumCpmmSwapEvent) -> Box::new(buy_params), None, true, + None, ) .await?; @@ -191,6 +192,7 @@ async fn raydium_cpmm_copy_trade_with_grpc(trade_info: RaydiumCpmmSwapEvent) -> Box::new(sell_params), None, true, + None, ) .await?; diff --git a/src/lib.rs b/src/lib.rs index 827f6b5..6f1240c 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,16 +65,16 @@ impl SolanaTrade { let mut priority_fee = trade_config.priority_fee.clone(); let commitment = trade_config.commitment.clone(); if priority_fee.buy_tip_fees.len() < swqos_configs.len() { - // 补齐数组,只补齐缺少的 + // Fill the array, only fill the missing elements let mut buy_tip_fees = priority_fee.buy_tip_fees.clone(); let default_fee = priority_fee.buy_tip_fee; - // 计算需要补充的元素数量 + // Calculate the number of elements that need to be added let missing_count = swqos_configs.len() - buy_tip_fees.len(); - // 添加缺少的元素,使用默认值 + // Add missing elements using default values for _ in 0..missing_count { buy_tip_fees.push(default_fee); } - // 更新 priority_fee 中的 buy_tip_fees + // Update buy_tip_fees in priority_fee priority_fee.buy_tip_fees = buy_tip_fees; trade_config.priority_fee = priority_fee.clone(); } @@ -159,6 +159,7 @@ impl SolanaTrade { extension_params: Box, lookup_table_key: Option, wait_transaction_confirmed: bool, + priority_fee: Option, ) -> Result<(), anyhow::Error> { let executor = TradeFactory::create_executor(dex_type.clone()); let protocol_params = extension_params; @@ -178,6 +179,23 @@ impl SolanaTrade { wait_transaction_confirmed: wait_transaction_confirmed, protocol_params: protocol_params.clone(), }; + if priority_fee.is_some() { + let mut custom_priority_fee = priority_fee.unwrap(); + // Fill the array, only fill the missing elements + if custom_priority_fee.buy_tip_fees.len() < self.swqos_clients.len() { + let mut buy_tip_fees = custom_priority_fee.buy_tip_fees.clone(); + let default_fee = custom_priority_fee.buy_tip_fee; + // Calculate the number of elements that need to be added + let missing_count = self.swqos_clients.len() - buy_tip_fees.len(); + // Add missing elements using default values + for _ in 0..missing_count { + buy_tip_fees.push(default_fee); + } + // Update buy_tip_fees in custom_priority_fee + custom_priority_fee.buy_tip_fees = buy_tip_fees; + } + buy_params.priority_fee = custom_priority_fee; + } if custom_buy_tip_fee.is_some() { buy_params.priority_fee.buy_tip_fee = custom_buy_tip_fee.unwrap(); buy_params.priority_fee.buy_tip_fees = buy_params @@ -250,6 +268,7 @@ impl SolanaTrade { extension_params: Box, lookup_table_key: Option, wait_transaction_confirmed: bool, + priority_fee: Option, ) -> Result<(), anyhow::Error> { let executor = TradeFactory::create_executor(dex_type.clone()); let protocol_params = extension_params; @@ -268,6 +287,23 @@ impl SolanaTrade { wait_transaction_confirmed: wait_transaction_confirmed, protocol_params: protocol_params.clone(), }; + if priority_fee.is_some() { + let mut custom_priority_fee = priority_fee.unwrap(); + // Fill the array, only fill the missing elements + if custom_priority_fee.buy_tip_fees.len() < self.swqos_clients.len() { + let mut buy_tip_fees = custom_priority_fee.buy_tip_fees.clone(); + let default_fee = custom_priority_fee.buy_tip_fee; + // Calculate the number of elements that need to be added + let missing_count = self.swqos_clients.len() - buy_tip_fees.len(); + // Add missing elements using default values + for _ in 0..missing_count { + buy_tip_fees.push(default_fee); + } + // Update buy_tip_fees in custom_priority_fee + custom_priority_fee.buy_tip_fees = buy_tip_fees; + } + sell_params.priority_fee = custom_priority_fee; + } if custom_buy_tip_fee.is_some() { sell_params.priority_fee.buy_tip_fee = custom_buy_tip_fee.unwrap(); sell_params.priority_fee.buy_tip_fees = sell_params @@ -351,6 +387,7 @@ impl SolanaTrade { extension_params: Box, lookup_table_key: Option, wait_transaction_confirmed: bool, + priority_fee: Option, ) -> Result<(), anyhow::Error> { if percent == 0 || percent > 100 { return Err(anyhow::anyhow!("Percentage must be between 1 and 100")); @@ -367,6 +404,7 @@ impl SolanaTrade { extension_params, lookup_table_key, wait_transaction_confirmed, + priority_fee, ) .await } diff --git a/src/main.rs b/src/main.rs index e2fdcec..9d8beaf 100755 --- a/src/main.rs +++ b/src/main.rs @@ -120,6 +120,7 @@ async fn test_middleware() -> AnyResult<()> { Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?), None, true, + None, ) .await?; Ok(()) @@ -147,6 +148,7 @@ async fn test_pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> Any Box::new(PumpFunParams::from_trade(&trade_info, None)), None, true, + None, ) .await?; @@ -165,6 +167,7 @@ async fn test_pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> Any Box::new(PumpFunParams::from_trade(&trade_info, None)), None, true, + None, ) .await?; @@ -197,6 +200,7 @@ async fn test_pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> Box::new(PumpFunParams::from_trade(&trade_info, None)), None, true, + None, ) .await?; @@ -215,6 +219,7 @@ async fn test_pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> Box::new(PumpFunParams::from_trade(&trade_info, None)), None, true, + None, ) .await?; @@ -245,6 +250,7 @@ async fn test_pumpswap() -> AnyResult<()> { Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?), None, true, + None, ) .await?; @@ -264,6 +270,7 @@ async fn test_pumpswap() -> AnyResult<()> { Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?), None, true, + None, ) .await?; @@ -292,6 +299,7 @@ async fn test_bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult Box::new(BonkParams::from_trade(trade_info.clone())), None, true, + None, ) .await?; @@ -310,6 +318,7 @@ async fn test_bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult Box::new(BonkParams::from_trade(trade_info)), None, true, + None, ) .await?; @@ -342,6 +351,7 @@ async fn test_bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyRe Box::new(BonkParams::from_dev_trade(trade_info.clone())), None, true, + None, ) .await?; @@ -360,6 +370,7 @@ async fn test_bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyRe Box::new(BonkParams::from_dev_trade(trade_info)), None, true, + None, ) .await?; @@ -389,6 +400,7 @@ async fn test_bonk() -> Result<(), Box> { Box::new(BonkParams::from_mint_by_rpc(&client.rpc, &mint_pubkey).await?), None, true, + None, ) .await?; @@ -408,6 +420,7 @@ async fn test_bonk() -> Result<(), Box> { Box::new(BonkParams::from_mint_by_rpc(&client.rpc, &mint_pubkey).await?), None, true, + None, ) .await?; @@ -439,6 +452,7 @@ async fn test_raydium_cpmm() -> Result<(), Box> { ), None, true, + None, ) .await?; @@ -460,6 +474,7 @@ async fn test_raydium_cpmm() -> Result<(), Box> { ), None, true, + None, ) .await?; @@ -489,6 +504,7 @@ async fn test_raydium_amm_v4() -> Result<(), Box> { Box::new(RaydiumAmmV4Params::from_amm_address_by_rpc(&client.rpc, amm_address).await?), None, true, + None, ) .await?; @@ -508,6 +524,7 @@ async fn test_raydium_amm_v4() -> Result<(), Box> { Box::new(RaydiumAmmV4Params::from_amm_address_by_rpc(&client.rpc, amm_address).await?), None, true, + None, ) .await?;