2025-08-25 00:21:15 +08:00
use anyhow ::Result ;
use sol_trade_sdk ::{
2025-09-19 00:49:45 +08:00
common ::{ AnyResult , TradeConfig },
2026-02-27 15:00:13 +08:00
swqos ::SwqosConfig ,
2025-08-25 00:21:15 +08:00
trading ::{
2026-02-27 15:00:13 +08:00
core ::params ::{ PumpSwapParams , DexParamEnum }, factory ::DexType ,
2025-08-25 00:21:15 +08:00
InstructionMiddleware , MiddlewareManager ,
},
2025-09-22 23:18:53 +08:00
SolanaTrade , TradeTokenType ,
2025-08-25 00:21:15 +08:00
};
2025-09-27 14:24:41 +08:00
use solana_commitment_config ::CommitmentConfig ;
use solana_sdk ::{ instruction ::Instruction , pubkey ::Pubkey , signature ::Keypair };
2025-08-25 00:21:15 +08:00
use std ::{ str ::FromStr , sync ::Arc };
#[tokio::main]
async fn main () -> Result < (), Box < dyn std ::error ::Error >> {
test_middleware (). await ? ;
Ok (())
}
/// Custom middleware
#[derive(Clone)]
pub struct CustomMiddleware ;
impl InstructionMiddleware for CustomMiddleware {
fn name ( & self ) -> & 'static str {
"CustomMiddleware"
}
fn process_protocol_instructions (
& self ,
protocol_instructions : Vec < Instruction > ,
2026-02-27 15:00:13 +08:00
_protocol_name : String ,
_is_buy : bool ,
2025-08-25 00:21:15 +08:00
) -> Result < Vec < Instruction >> {
// do anything you want here
// you can modify the instructions here
Ok ( protocol_instructions )
}
fn process_full_instructions (
& self ,
full_instructions : Vec < Instruction > ,
2026-02-27 15:00:13 +08:00
_protocol_name : String ,
_is_buy : bool ,
2025-08-25 00:21:15 +08:00
) -> Result < Vec < Instruction >> {
// do anything you want here
// you can modify the instructions here
Ok ( full_instructions )
}
fn clone_box ( & self ) -> Box < dyn InstructionMiddleware > {
Box ::new ( self . clone ())
}
}
/// 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-08-25 00:21:15 +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 ;
println! ( "✅ SolanaTrade client initialized successfully!" );
Ok ( solana_trade )
2025-08-25 00:21:15 +08:00
}
async fn test_middleware () -> AnyResult < () > {
let mut client = create_solana_trade_client (). await ? ;
// SDK example middleware that prints instruction information
// You can reference LoggingMiddleware to implement the InstructionMiddleware trait for your own middleware
let middleware_manager = MiddlewareManager ::new (). add_middleware ( Box ::new ( CustomMiddleware ));
client = client . with_middleware_manager ( middleware_manager );
let mint_pubkey = Pubkey ::from_str ( "pumpCmXqMfrsAkQ5r49WcJnRayYRqmXz6ae8H7H9Dfn" ) ? ;
let buy_sol_cost = 100_000 ;
let slippage_basis_points = Some ( 100 );
2026-01-08 07:59:50 +00:00
let recent_blockhash = client . infrastructure . rpc . get_latest_blockhash (). await ? ;
2025-08-25 00:21:15 +08:00
let pool_address = Pubkey ::from_str ( "539m4mVWt6iduB6W8rDGPMarzNCMesuqY5eUTiiYHAgR" ) ? ;
2025-09-19 00:49:45 +08:00
2025-10-07 23:12:37 +08:00
let gas_fee_strategy = sol_trade_sdk ::common ::GasFeeStrategy ::new ();
2025-12-28 16:16:10 +08:00
gas_fee_strategy . set_global_fee_strategy ( 150000 , 150000 , 500000 , 500000 , 0.001 , 0.001 );
2025-10-07 23:12:37 +08:00
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_cost ,
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-12-08 01:35:40 +08:00
extension_params : DexParamEnum ::PumpSwap (
2026-01-08 07:59:50 +00:00
PumpSwapParams ::from_pool_address_by_rpc ( & client . infrastructure . rpc , & pool_address ). await ? ,
2025-09-19 00:49:45 +08:00
),
2025-10-07 20:56:02 +08:00
address_lookup_table_account : None ,
2025-09-19 00:49:45 +08:00
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 ,
2025-09-21 21:56:17 +08:00
durable_nonce : None ,
2025-10-05 22:27:04 +08:00
fixed_output_token_amount : None ,
2025-10-07 23:12:37 +08:00
gas_fee_strategy : gas_fee_strategy ,
2025-10-30 22:11:55 +08:00
simulate : false ,
2026-01-06 11:53:09 +00:00
use_exact_sol_amount : None ,
2026-02-27 13:42:15 +08:00
grpc_recv_us : None ,
2025-09-19 00:49:45 +08:00
};
client . buy ( buy_params ). await ? ;
2025-08-25 00:21:15 +08:00
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" );
Ok (())
}