feat: Add PumpSwap direct trading example and RPC client optimization

This commit is contained in:
ysq
2025-09-08 00:24:29 +08:00
parent 1252446c90
commit 8d77f6e5ca
4 changed files with 120 additions and 3 deletions
+1
View File
@@ -28,6 +28,7 @@ members = [
"examples/raydium_amm_v4_trading",
"examples/address_lookup",
"examples/nonce_cache",
"examples/pumpswap_direct_trading",
]
[lib]
@@ -0,0 +1,12 @@
[package]
name = "pumpswap_direct_trading"
version = "0.1.0"
edition = "2021"
[dependencies]
sol-trade-sdk = { path = "../.." }
solana-sdk = "2.3.0"
spl-associated-token-account = "7.0.0"
tokio = { version = "1", features = ["full"] }
spl-token= "8.0.0"
spl-token-2022 = { version = "8.0.0", features = ["no-entrypoint"] }
@@ -0,0 +1,94 @@
use sol_trade_sdk::{
common::{AnyResult, PriorityFee, TradeConfig},
swqos::SwqosConfig,
trading::{core::params::PumpSwapParams, factory::DexType},
SolanaTrade,
};
use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair};
use solana_sdk::{pubkey::Pubkey, signer::Signer};
use spl_associated_token_account::get_associated_token_address_with_program_id;
use std::{str::FromStr, sync::Arc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Testing PumpSwap trading...");
let client = create_solana_trade_client().await?;
let slippage_basis_points = Some(100);
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
let pool = Pubkey::from_str("539m4mVWt6iduB6W8rDGPMarzNCMesuqY5eUTiiYHAgR").unwrap();
let mint_pubkey = Pubkey::from_str("pumpCmXqMfrsAkQ5r49WcJnRayYRqmXz6ae8H7H9Dfn").unwrap();
// Buy tokens
println!("Buying tokens from PumpSwap...");
let buy_sol_amount = 100_000;
client
.buy(
DexType::PumpSwap,
mint_pubkey,
buy_sol_amount,
slippage_basis_points,
recent_blockhash,
None,
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool).await?),
None,
true,
)
.await?;
// Sell tokens
println!("Selling tokens from PumpSwap...");
let rpc = client.rpc.clone();
let payer = client.payer.pubkey();
let program_id = spl_token_2022::ID;
let account = get_associated_token_address_with_program_id(&payer, &mint_pubkey, &program_id);
let balance = rpc.get_token_account_balance(&account).await?;
let amount_token = balance.amount.parse::<u64>().unwrap();
client
.sell(
DexType::PumpSwap,
mint_pubkey,
amount_token,
slippage_basis_points,
recent_blockhash,
None,
false,
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool).await?),
None,
true,
)
.await?;
tokio::signal::ctrl_c().await?;
Ok(())
}
/// Create SolanaTrade client
/// Initializes a new SolanaTrade client with configuration
async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
println!("Creating SolanaTrade client...");
let payer = Keypair::from_base58_string("use_your_own_keypair");
let rpc_url = "https://api.mainnet-beta.solana.com".to_string();
let swqos_configs = vec![SwqosConfig::Default(rpc_url.clone())];
let mut priority_fee = PriorityFee::default();
priority_fee.buy_tip_fees = vec![0.001];
// Configure according to your needs
priority_fee.rpc_unit_limit = 150000;
let trade_config = TradeConfig {
rpc_url,
commitment: CommitmentConfig::confirmed(),
priority_fee: priority_fee,
swqos_configs,
lookup_table_key: None,
};
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
println!("SolanaTrade client created successfully!");
Ok(solana_trade_client)
}
+13 -3
View File
@@ -31,6 +31,7 @@ use swqos::SwqosClient;
pub struct SolanaTrade {
pub payer: Arc<Keypair>,
pub rpc: Arc<SolanaRpcClient>,
pub rpc_client: Vec<Arc<SwqosClient>>,
pub swqos_clients: Vec<Arc<SwqosClient>>,
pub priority_fee: PriorityFee,
pub trade_config: TradeConfig,
@@ -44,6 +45,7 @@ impl Clone for SolanaTrade {
Self {
payer: self.payer.clone(),
rpc: self.rpc.clone(),
rpc_client: self.rpc_client.clone(),
swqos_clients: self.swqos_clients.clone(),
priority_fee: self.priority_fee.clone(),
trade_config: self.trade_config.clone(),
@@ -75,9 +77,16 @@ impl SolanaTrade {
let rpc = Arc::new(SolanaRpcClient::new_with_commitment(rpc_url.clone(), commitment));
let rpc_client = SwqosConfig::get_swqos_client(
rpc_url.clone(),
commitment,
SwqosConfig::Default(rpc_url),
);
let instance = Self {
payer,
rpc,
rpc_client: vec![rpc_client],
swqos_clients,
priority_fee,
trade_config: trade_config.clone(),
@@ -285,10 +294,11 @@ impl SolanaTrade {
return Err(anyhow::anyhow!("Invalid protocol params for Trade"));
}
let _swqos_clients =
if !with_tip { self.rpc_client.clone() } else { self.swqos_clients.clone() };
// Execute sell based on tip preference
executor
.sell_with_tip(sell_params, self.swqos_clients.clone(), self.middleware_manager.clone())
.await
executor.sell_with_tip(sell_params, _swqos_clients, self.middleware_manager.clone()).await
}
/// Execute a sell order for a percentage of the specified token amount