Merge commit 'bee7f5b78c3ee060472dbf9874d46d2f4b8a03c1'

This commit is contained in:
ysq
2025-11-16 23:47:17 +08:00
24 changed files with 71 additions and 67 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "sol-trade-sdk"
version = "3.3.0"
version = "3.3.1"
edition = "2021"
authors = [
"William <byteblock6@gmail.com>",
+3 -3
View File
@@ -87,14 +87,14 @@ Add the dependency to your `Cargo.toml`:
```toml
# Add to your Cargo.toml
sol-trade-sdk = { path = "./sol-trade-sdk", version = "3.3.0" }
sol-trade-sdk = { path = "./sol-trade-sdk", version = "3.3.1" }
```
### Use crates.io
```toml
# Add to your Cargo.toml
sol-trade-sdk = "3.3.0"
sol-trade-sdk = "3.3.1"
```
## 🛠️ Usage Examples
@@ -146,7 +146,7 @@ For detailed information about Gas Fee Strategy, see the [Gas Fee Strategy Refer
// Create GasFeeStrategy instance
let gas_fee_strategy = GasFeeStrategy::new();
// Set global strategy
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
```
#### 3. Build Trading Parameters
+3 -3
View File
@@ -87,14 +87,14 @@ git clone https://github.com/0xfnzero/sol-trade-sdk
```toml
# 添加到您的 Cargo.toml
sol-trade-sdk = { path = "./sol-trade-sdk", version = "3.3.0" }
sol-trade-sdk = { path = "./sol-trade-sdk", version = "3.3.1" }
```
### 使用 crates.io
```toml
# 添加到您的 Cargo.toml
sol-trade-sdk = "3.3.0"
sol-trade-sdk = "3.3.1"
```
## 🛠️ 使用示例
@@ -146,7 +146,7 @@ let client = SolanaTrade::new(Arc::new(payer), trade_config).await;
// 创建 GasFeeStrategy 实例
let gas_fee_strategy = GasFeeStrategy::new();
// 设置全局策略
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
```
#### 3. 构建交易参数
+1 -1
View File
@@ -125,7 +125,7 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
fetch_address_lookup_table_account(&client.rpc, &lookup_table_key).await.ok();
let gas_fee_strategy = GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
// Buy tokens
println!("Buying tokens from PumpFun...");
+1 -1
View File
@@ -125,7 +125,7 @@ async fn bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult<()>
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
// Buy tokens
println!("Buying tokens from Bonk...");
+1 -1
View File
@@ -95,7 +95,7 @@ async fn bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyResult<
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let token_type = if trade_info.quote_token_mint == sol_trade_sdk::constants::USD1_TOKEN_ACCOUNT
{
+20 -20
View File
@@ -615,7 +615,7 @@ async fn handle_buy_pumpfun(
let sol_lamports = sol_str_to_lamports(sol_amount.to_string().as_str()).unwrap();
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let buy_params = TradeBuyParams {
dex_type: DexType::PumpFun,
@@ -636,7 +636,7 @@ async fn handle_buy_pumpfun(
simulate: false,
};
match client.buy(buy_params).await {
Ok((_, signature)) => {
Ok((_, signature, _)) => {
println!(" ✅ Successfully bought tokens from PumpFun!");
println!(" ✅ Transaction Signature: {:?}", signature);
}
@@ -669,7 +669,7 @@ async fn handle_buy_pumpswap(
let sol_lamports = sol_str_to_lamports(sol_amount.to_string().as_str()).unwrap();
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let buy_params = TradeBuyParams {
dex_type: DexType::PumpSwap,
@@ -690,7 +690,7 @@ async fn handle_buy_pumpswap(
simulate: false,
};
match client.buy(buy_params).await {
Ok((_, signature)) => {
Ok((_, signature, _)) => {
println!(" ✅ Successfully bought tokens from PumpSwap!");
println!(" ✅ Transaction Signature: {:?}", signature);
}
@@ -722,7 +722,7 @@ async fn handle_buy_bonk(
let sol_lamports = sol_str_to_lamports(sol_amount.to_string().as_str()).unwrap();
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let buy_params = TradeBuyParams {
dex_type: DexType::Bonk,
@@ -743,7 +743,7 @@ async fn handle_buy_bonk(
simulate: false,
};
match client.buy(buy_params).await {
Ok((_, signature)) => {
Ok((_, signature, _)) => {
println!(" ✅ Successfully bought tokens from Bonk!");
println!(" ✅ Transaction Signature: {:?}", signature);
}
@@ -779,7 +779,7 @@ async fn handle_buy_raydium_v4(
let sol_lamports = sol_str_to_lamports(sol_amount.to_string().as_str()).unwrap();
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let buy_params = TradeBuyParams {
dex_type: DexType::RaydiumAmmV4,
@@ -800,7 +800,7 @@ async fn handle_buy_raydium_v4(
simulate: false,
};
match client.buy(buy_params).await {
Ok((_, signature)) => {
Ok((_, signature, _)) => {
println!(" ✅ Successfully bought tokens from Raydium V4!");
println!(" ✅ Transaction Signature: {:?}", signature);
}
@@ -836,7 +836,7 @@ async fn handle_buy_raydium_cpmm(
let sol_lamports = sol_str_to_lamports(sol_amount.to_string().as_str()).unwrap();
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let buy_params = TradeBuyParams {
dex_type: DexType::RaydiumCpmm,
@@ -857,7 +857,7 @@ async fn handle_buy_raydium_cpmm(
simulate: false,
};
match client.buy(buy_params).await {
Ok((_, signature)) => {
Ok((_, signature, _)) => {
println!(" ✅ Successfully bought tokens from Raydium CPMM!");
println!(" ✅ Transaction Signature: {:?}", signature);
}
@@ -1003,7 +1003,7 @@ async fn handle_sell_pumpfun(
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let sell_params = TradeSellParams {
dex_type: DexType::PumpFun,
@@ -1026,7 +1026,7 @@ async fn handle_sell_pumpfun(
};
match client.sell(sell_params).await {
Ok((_, signature)) => {
Ok((_, signature, _)) => {
println!(" ✅ Successfully sold tokens from PumpFun!");
println!(" ✅ Transaction Signature: {:?}", signature);
}
@@ -1061,7 +1061,7 @@ async fn handle_sell_pumpswap(
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let sell_params = TradeSellParams {
dex_type: DexType::PumpSwap,
@@ -1083,7 +1083,7 @@ async fn handle_sell_pumpswap(
simulate: false,
};
match client.sell(sell_params).await {
Ok((_, signature)) => {
Ok((_, signature, _)) => {
println!(" ✅ Successfully sold tokens from PumpSwap!");
println!(" ✅ Transaction Signature: {}", signature);
}
@@ -1118,7 +1118,7 @@ async fn handle_sell_bonk(
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let sell_params = TradeSellParams {
dex_type: DexType::Bonk,
@@ -1140,7 +1140,7 @@ async fn handle_sell_bonk(
simulate: false,
};
match client.sell(sell_params).await {
Ok((_, signature)) => {
Ok((_, signature, _)) => {
println!(" ✅ Successfully sold tokens from Bonk!");
println!(" ✅ Transaction Signature: {:?}", signature);
}
@@ -1178,7 +1178,7 @@ async fn handle_sell_raydium_v4(
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let sell_params = TradeSellParams {
dex_type: DexType::RaydiumAmmV4,
@@ -1200,7 +1200,7 @@ async fn handle_sell_raydium_v4(
simulate: false,
};
match client.sell(sell_params).await {
Ok((_, signature)) => {
Ok((_, signature, _)) => {
println!(" ✅ Successfully sold tokens from Raydium V4!");
println!(" ✅ Transaction Signature: {:?}", signature);
}
@@ -1238,7 +1238,7 @@ async fn handle_sell_raydium_cpmm(
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let sell_params = TradeSellParams {
dex_type: DexType::RaydiumCpmm,
@@ -1260,7 +1260,7 @@ async fn handle_sell_raydium_cpmm(
simulate: false,
};
match client.sell(sell_params).await {
Ok((_, signature)) => {
Ok((_, signature, _)) => {
println!(" ✅ Successfully sold tokens from Raydium CPMM!");
println!(" ✅ Transaction Signature: {:?}", signature);
}
+1 -1
View File
@@ -12,7 +12,7 @@ async fn main() {
// Set global strategy
println!("1. Set global strategy");
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
// Print all strategies
println!("\n2. Print all strategies");
@@ -23,7 +23,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mint_pubkey = Pubkey::from_str("PRVT6TB7uss3FrUd2D9xs2zqDBsa3GbMJMwCQsgmeta").unwrap();
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
// Buy tokens
println!("Buying tokens from Metaora Damm V2...");
+1 -1
View File
@@ -81,7 +81,7 @@ async fn test_middleware() -> AnyResult<()> {
let pool_address = Pubkey::from_str("539m4mVWt6iduB6W8rDGPMarzNCMesuqY5eUTiiYHAgR")?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let buy_params = sol_trade_sdk::TradeBuyParams {
dex_type: DexType::PumpSwap,
+1 -1
View File
@@ -125,7 +125,7 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
let durable_nonce = fetch_nonce_info(&client.rpc, nonce_account_str).await;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
// Buy tokens
println!("Buying tokens from PumpFun...");
+1 -1
View File
@@ -121,7 +121,7 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
// Buy tokens
println!("Buying tokens from PumpFun...");
+1 -1
View File
@@ -89,7 +89,7 @@ async fn pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> AnyR
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
// Buy tokens
println!("Buying tokens from PumpFun...");
+1 -1
View File
@@ -23,7 +23,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mint_pubkey = Pubkey::from_str("pumpCmXqMfrsAkQ5r49WcJnRayYRqmXz6ae8H7H9Dfn").unwrap();
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
// Buy tokens
println!("Buying tokens from PumpSwap...");
+1 -1
View File
@@ -195,7 +195,7 @@ async fn pumpswap_trade_with_grpc(mint_pubkey: Pubkey, params: PumpSwapParams) -
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let is_sol = params.base_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT
|| params.quote_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT;
+1 -1
View File
@@ -140,7 +140,7 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent)
);
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
// Buy tokens
println!("Buying tokens from Raydium_amm_v4...");
+1 -1
View File
@@ -130,7 +130,7 @@ async fn raydium_cpmm_copy_trade_with_grpc(trade_info: RaydiumCpmmSwapEvent) ->
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
let buy_params =
RaydiumCpmmParams::from_pool_address_by_rpc(&client.rpc, &trade_info.pool_state).await?;
+1 -1
View File
@@ -22,7 +22,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mint_pubkey = Pubkey::from_str("2zMMhcVQEXDtdE6vsFS7S7D5oUodfJHE8vd1gnBouauv").unwrap();
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
gas_fee_strategy.set_global_fee_strategy(150000, 500000, 0.001, 0.001, 256 * 1024, 0);
gas_fee_strategy.set_global_fee_strategy(150000,150000, 500000,500000, 0.001, 0.001, 256 * 1024, 0);
// Buy tokens
println!("Buying tokens from PumpSwap...");
+9 -7
View File
@@ -5,7 +5,10 @@ use solana_sdk::{
pubkey::Pubkey,
};
use crate::common::{spl_associated_token_account::get_associated_token_address_with_program_id, spl_token::close_account};
use crate::common::{
spl_associated_token_account::get_associated_token_address_with_program_id,
spl_token::close_account,
};
use crate::perf::compiler_optimization::CompileTimeOptimizedEventProcessor;
/// 🚀 编译时优化的哈希处理器
@@ -56,10 +59,7 @@ where
};
// Lock-free cache lookup with entry API
INSTRUCTION_CACHE
.entry(cache_key)
.or_insert_with(compute_fn)
.clone()
INSTRUCTION_CACHE.entry(cache_key).or_insert_with(compute_fn).clone()
}
// --------------------- Associated Token Account ---------------------
@@ -104,7 +104,8 @@ pub fn _create_associated_token_account_idempotent_fast(
if use_seed
&& !mint.eq(&crate::constants::WSOL_TOKEN_ACCOUNT)
&& !mint.eq(&crate::constants::SOL_TOKEN_ACCOUNT)
&& token_program.eq(&crate::constants::TOKEN_PROGRAM)
&& (token_program.eq(&crate::constants::TOKEN_PROGRAM)
|| token_program.eq(&crate::constants::TOKEN_PROGRAM_2022))
{
// Use cache to get instruction
get_cached_instructions(cache_key, || {
@@ -239,7 +240,8 @@ fn _get_associated_token_address_with_program_id_fast(
let ata = if use_seed
&& !token_mint_address.eq(&crate::constants::WSOL_TOKEN_ACCOUNT)
&& !token_mint_address.eq(&crate::constants::SOL_TOKEN_ACCOUNT)
&& token_program_id.eq(&crate::constants::TOKEN_PROGRAM)
&& (token_program_id.eq(&crate::constants::TOKEN_PROGRAM)
|| token_program_id.eq(&crate::constants::TOKEN_PROGRAM_2022))
{
super::seed::get_associated_token_address_with_program_id_use_seed(
wallet_address,
+1 -1
View File
@@ -61,7 +61,7 @@ pub mod global_constants {
pubkey!("GesfTA3X2arioaHp8bbKdjG9vJtskViWACZoYvxp4twS");
pub const MAYHEM_FEE_RECIPIENT_META: solana_sdk::instruction::AccountMeta =
solana_sdk::instruction::AccountMeta {
pubkey: FEE_RECIPIENT,
pubkey: MAYHEM_FEE_RECIPIENT,
is_signer: false,
is_writable: true,
};
+3 -3
View File
@@ -345,7 +345,7 @@ impl SolanaTrade {
/// - Network or RPC errors occur
/// - Insufficient SOL balance for the purchase
/// - Required accounts cannot be created or accessed
pub async fn buy(&self, params: TradeBuyParams) -> Result<(bool, Signature), anyhow::Error> {
pub async fn buy(&self, params: TradeBuyParams) -> Result<(bool, Signature, Option<anyhow::Error>), anyhow::Error> {
if params.slippage_basis_points.is_none() {
println!(
"slippage_basis_points is none, use default slippage basis points: {}",
@@ -445,7 +445,7 @@ impl SolanaTrade {
/// - Insufficient token balance for the sale
/// - Token account doesn't exist or is not properly initialized
/// - Required accounts cannot be created or accessed
pub async fn sell(&self, params: TradeSellParams) -> Result<(bool, Signature), anyhow::Error> {
pub async fn sell(&self, params: TradeSellParams) -> Result<(bool, Signature, Option<anyhow::Error>), anyhow::Error> {
if params.slippage_basis_points.is_none() {
println!(
"slippage_basis_points is none, use default slippage basis points: {}",
@@ -557,7 +557,7 @@ impl SolanaTrade {
mut params: TradeSellParams,
amount_token: u64,
percent: u64,
) -> Result<(bool, Signature), anyhow::Error> {
) -> Result<(bool, Signature, Option<anyhow::Error>), anyhow::Error> {
if percent == 0 || percent > 100 {
return Err(anyhow::anyhow!("Percentage must be between 1 and 100"));
}
+12 -10
View File
@@ -21,7 +21,7 @@ use crate::{
struct TaskResult {
success: bool,
signature: Signature,
_error: Option<anyhow::Error>,
error: Option<anyhow::Error>,
}
struct ResultCollector {
@@ -54,7 +54,7 @@ impl ResultCollector {
self.completed_count.fetch_add(1, Ordering::Release);
}
async fn wait_for_success(&self) -> Option<(bool, Signature)> {
async fn wait_for_success(&self) -> Option<(bool, Signature, Option<anyhow::Error>)> {
let start = Instant::now();
let timeout = std::time::Duration::from_secs(30);
@@ -63,7 +63,7 @@ impl ResultCollector {
if self.success_flag.load(Ordering::Acquire) {
while let Some(result) = self.results.pop() {
if result.success {
return Some((true, result.signature));
return Some((true, result.signature, None));
}
}
}
@@ -71,7 +71,7 @@ impl ResultCollector {
let completed = self.completed_count.load(Ordering::Acquire);
if completed >= self.total_tasks {
while let Some(result) = self.results.pop() {
return Some((result.success, result.signature));
return Some((result.success, result.signature, result.error));
}
return None;
}
@@ -83,9 +83,9 @@ impl ResultCollector {
}
}
fn get_first(&self) -> Option<(bool, Signature)> {
fn get_first(&self) -> Option<(bool, Signature, Option<anyhow::Error>,)> {
if let Some(result) = self.results.pop() {
Some((result.success, result.signature))
Some((result.success, result.signature, result.error))
} else {
None
}
@@ -107,7 +107,7 @@ pub async fn execute_parallel(
wait_transaction_confirmed: bool,
with_tip: bool,
gas_fee_strategy: GasFeeStrategy,
) -> Result<(bool, Signature)> {
) -> Result<(bool, Signature, Option<anyhow::Error>)> {
let _exec_start = Instant::now();
if swqos_clients.is_empty() {
@@ -208,7 +208,7 @@ pub async fn execute_parallel(
collector.submit(TaskResult {
success: false,
signature: Signature::default(),
_error: Some(e),
error: Some(e),
});
return;
}
@@ -217,6 +217,7 @@ pub async fn execute_parallel(
// Transaction built
let _send_start = Instant::now();
let mut err = None;
let success = match swqos_client
.send_transaction(
if is_buy { TradeType::Buy } else { TradeType::Sell },
@@ -225,7 +226,8 @@ pub async fn execute_parallel(
.await
{
Ok(()) => true,
Err(_e) => {
Err(e) => {
err = Some(e);
// Send transaction failed
false
}
@@ -234,7 +236,7 @@ pub async fn execute_parallel(
// Transaction sent
if let Some(signature) = transaction.signatures.first() {
collector.submit(TaskResult { success, signature: *signature, _error: None });
collector.submit(TaskResult { success, signature: *signature, error: err });
}
});
}
+4 -4
View File
@@ -44,7 +44,7 @@ impl GenericTradeExecutor {
#[async_trait::async_trait]
impl TradeExecutor for GenericTradeExecutor {
async fn swap(&self, params: SwapParams) -> Result<(bool, Signature)> {
async fn swap(&self, params: SwapParams) -> Result<(bool, Signature, Option<anyhow::Error>)> {
let total_start = Instant::now();
// 判断买卖方向
@@ -201,7 +201,7 @@ async fn simulate_transaction(
is_buy: bool,
with_tip: bool,
gas_fee_strategy: GasFeeStrategy,
) -> Result<(bool, Signature)> {
) -> Result<(bool, Signature, Option<anyhow::Error>)> {
use crate::trading::common::build_transaction;
use solana_client::rpc_config::RpcSimulateTransactionConfig;
use solana_commitment_config::CommitmentLevel;
@@ -288,7 +288,7 @@ async fn simulate_transaction(
}
println!("=========================================\n");
return Ok((false, signature));
return Ok((false, signature, Some(anyhow::anyhow!("{:?}", err))));
}
// Simulation succeeded
@@ -308,5 +308,5 @@ async fn simulate_transaction(
println!("============================================\n");
Ok((true, signature))
Ok((true, signature, None))
}
+1 -1
View File
@@ -5,7 +5,7 @@ use solana_sdk::{instruction::Instruction, signature::Signature};
/// 交易执行器trait - 定义了所有交易协议都需要实现的核心方法
#[async_trait::async_trait]
pub trait TradeExecutor: Send + Sync {
async fn swap(&self, params: SwapParams) -> Result<(bool, Signature)>;
async fn swap(&self, params: SwapParams) -> Result<(bool, Signature, Option<anyhow::Error>)>;
/// 获取协议名称
fn protocol_name(&self) -> &'static str;
}