2025-09-10 00:05:13 +08:00
|
|
|
use sol_trade_sdk::{
|
2025-09-17 11:14:15 +08:00
|
|
|
common::{
|
2025-09-19 00:49:45 +08:00
|
|
|
fast_fn::get_associated_token_address_with_program_id_fast_use_seed, AnyResult, TradeConfig,
|
2025-09-17 11:14:15 +08:00
|
|
|
},
|
|
|
|
|
swqos::SwqosConfig,
|
2025-09-10 00:05:13 +08:00
|
|
|
trading::{core::params::PumpSwapParams, factory::DexType},
|
2025-09-22 23:18:53 +08:00
|
|
|
SolanaTrade, TradeTokenType,
|
2025-09-10 00:05:13 +08:00
|
|
|
};
|
|
|
|
|
use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair};
|
|
|
|
|
use solana_sdk::{pubkey::Pubkey, signer::Signer};
|
|
|
|
|
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("9qKxzRejsV6Bp2zkefXWCbGvg61c3hHei7ShXJ4FythA").unwrap();
|
|
|
|
|
let mint_pubkey = Pubkey::from_str("2zMMhcVQEXDtdE6vsFS7S7D5oUodfJHE8vd1gnBouauv").unwrap();
|
|
|
|
|
|
|
|
|
|
// Buy tokens
|
|
|
|
|
println!("Buying tokens from PumpSwap...");
|
|
|
|
|
let buy_sol_amount = 100_000;
|
2025-09-19 00:49:45 +08:00
|
|
|
let buy_params = sol_trade_sdk::TradeBuyParams {
|
|
|
|
|
dex_type: DexType::PumpSwap,
|
2025-09-22 23:18:53 +08:00
|
|
|
input_token_type: TradeTokenType::WSOL,
|
2025-09-19 00:49:45 +08:00
|
|
|
mint: mint_pubkey,
|
2025-09-22 23:18:53 +08:00
|
|
|
input_token_amount: buy_sol_amount,
|
2025-09-19 00:49:45 +08:00
|
|
|
slippage_basis_points: slippage_basis_points,
|
2025-09-21 21:56:17 +08:00
|
|
|
recent_blockhash: Some(recent_blockhash),
|
2025-09-19 00:49:45 +08:00
|
|
|
extension_params: Box::new(
|
|
|
|
|
PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool).await?,
|
|
|
|
|
),
|
|
|
|
|
lookup_table_key: None,
|
|
|
|
|
wait_transaction_confirmed: true,
|
2025-09-22 23:18:53 +08:00
|
|
|
create_input_token_ata: true,
|
|
|
|
|
close_input_token_ata: true,
|
2025-09-19 00:49:45 +08:00
|
|
|
create_mint_ata: true,
|
|
|
|
|
open_seed_optimize: true, // ❗️❗️❗️❗️ open seed optimize
|
2025-09-21 21:56:17 +08:00
|
|
|
durable_nonce: None,
|
2025-09-19 00:49:45 +08:00
|
|
|
};
|
|
|
|
|
client.buy(buy_params).await?;
|
2025-09-10 00:05:13 +08:00
|
|
|
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(4)).await;
|
|
|
|
|
|
|
|
|
|
// Sell tokens
|
|
|
|
|
println!("Selling tokens from PumpSwap...");
|
|
|
|
|
|
|
|
|
|
let rpc = client.rpc.clone();
|
|
|
|
|
let payer = client.payer.pubkey();
|
|
|
|
|
let program_id = spl_token::ID;
|
|
|
|
|
// ❗️❗️❗️❗️ Must use the 'use seed' method to get the ATA account, otherwise the transaction will fail
|
|
|
|
|
let account = get_associated_token_address_with_program_id_fast_use_seed(
|
|
|
|
|
&payer,
|
|
|
|
|
&mint_pubkey,
|
|
|
|
|
&program_id,
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
let balance = rpc.get_token_account_balance(&account).await?;
|
|
|
|
|
let amount_token = balance.amount.parse::<u64>().unwrap();
|
2025-09-19 00:49:45 +08:00
|
|
|
let sell_params = sol_trade_sdk::TradeSellParams {
|
|
|
|
|
dex_type: DexType::PumpSwap,
|
2025-09-22 23:18:53 +08:00
|
|
|
output_token_type: TradeTokenType::WSOL,
|
2025-09-19 00:49:45 +08:00
|
|
|
mint: mint_pubkey,
|
2025-09-22 23:18:53 +08:00
|
|
|
input_token_amount: amount_token,
|
2025-09-19 00:49:45 +08:00
|
|
|
slippage_basis_points: slippage_basis_points,
|
2025-09-21 21:56:17 +08:00
|
|
|
recent_blockhash: Some(recent_blockhash),
|
2025-09-19 00:49:45 +08:00
|
|
|
with_tip: false,
|
|
|
|
|
extension_params: Box::new(
|
|
|
|
|
PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool).await?,
|
|
|
|
|
),
|
|
|
|
|
lookup_table_key: None,
|
|
|
|
|
wait_transaction_confirmed: true,
|
2025-09-22 23:18:53 +08:00
|
|
|
create_output_token_ata: true,
|
|
|
|
|
close_output_token_ata: true,
|
2025-09-19 00:49:45 +08:00
|
|
|
open_seed_optimize: true, // ❗️❗️❗️❗️ open seed optimize
|
2025-09-21 21:56:17 +08:00
|
|
|
durable_nonce: None,
|
2025-09-19 00:49:45 +08:00
|
|
|
};
|
|
|
|
|
client.sell(sell_params).await?;
|
2025-09-10 00:05:13 +08:00
|
|
|
|
|
|
|
|
tokio::signal::ctrl_c().await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create SolanaTrade client
|
|
|
|
|
/// Initializes a new SolanaTrade client with configuration
|
|
|
|
|
async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
2025-09-19 00:49:45 +08:00
|
|
|
println!("🚀 Initializing SolanaTrade client...");
|
|
|
|
|
let payer = Keypair::from_base58_string("use_your_payer_keypair_here");
|
2025-09-10 00:05:13 +08:00
|
|
|
let rpc_url = "https://api.mainnet-beta.solana.com".to_string();
|
2025-09-19 00:49:45 +08:00
|
|
|
let commitment = CommitmentConfig::confirmed();
|
|
|
|
|
let swqos_configs: Vec<SwqosConfig> = vec![SwqosConfig::Default(rpc_url.clone())];
|
|
|
|
|
let trade_config = TradeConfig::new(rpc_url, swqos_configs, commitment);
|
|
|
|
|
let solana_trade = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
2025-09-19 23:31:41 +08:00
|
|
|
// set global strategy
|
2025-09-19 23:59:52 +08:00
|
|
|
sol_trade_sdk::common::GasFeeStrategy::set_global_fee_strategy(150000, 500000, 0.001, 0.001);
|
2025-09-19 00:49:45 +08:00
|
|
|
println!("✅ SolanaTrade client initialized successfully!");
|
|
|
|
|
Ok(solana_trade)
|
2025-09-10 00:05:13 +08:00
|
|
|
}
|