feat: add flexible nonce parameter support

- Add nonce_account and current_nonce to trade parameters
- Remove hardcoded NonceCache dependency from nonce_manager
- Update examples and documentation for new nonce usage
- Fix nonce documentation errors and improve clarity
This commit is contained in:
ysq
2025-09-19 18:02:11 +08:00
parent a53038855e
commit 4907aafead
26 changed files with 214 additions and 71 deletions
+2
View File
@@ -160,6 +160,8 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
close_wsol_ata: false,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).await?;
+11 -1
View File
@@ -3,7 +3,6 @@ use std::sync::{
Arc,
};
use sol_trade_sdk::solana_streamer_sdk::streaming::event_parser::common::filter::EventTypeFilter;
use sol_trade_sdk::solana_streamer_sdk::streaming::event_parser::common::EventType;
use sol_trade_sdk::solana_streamer_sdk::streaming::event_parser::protocols::bonk::parser::BONK_PROGRAM_ID;
use sol_trade_sdk::solana_streamer_sdk::streaming::event_parser::protocols::bonk::BonkTradeEvent;
@@ -19,6 +18,10 @@ use sol_trade_sdk::{
SolanaTrade,
};
use sol_trade_sdk::{common::TradeConfig, solana_streamer_sdk::match_event};
use sol_trade_sdk::{
constants::WSOL_TOKEN_ACCOUNT,
solana_streamer_sdk::streaming::event_parser::common::filter::EventTypeFilter,
};
use solana_sdk::signer::Signer;
use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair};
use spl_associated_token_account::get_associated_token_address;
@@ -85,6 +88,9 @@ fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|event: Box<dyn UnifiedEvent>| {
match_event!(event, {
BonkTradeEvent => |e: BonkTradeEvent| {
if e.base_token_mint != WSOL_TOKEN_ACCOUNT && e.quote_token_mint != WSOL_TOKEN_ACCOUNT {
return;
}
// Test code, only test one transaction
if !ALREADY_EXECUTED.swap(true, Ordering::SeqCst) {
let event_clone = e.clone();
@@ -142,6 +148,8 @@ async fn bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult<()>
close_wsol_ata: true,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).await?;
@@ -169,6 +177,8 @@ async fn bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult<()>
close_wsol_ata: true,
open_seed_optimize: false,
with_tip: false,
nonce_account: None,
current_nonce: None,
};
client.sell(sell_params).await?;
+4
View File
@@ -111,6 +111,8 @@ async fn bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyResult<
close_wsol_ata: true,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).await?;
@@ -143,6 +145,8 @@ async fn bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyResult<
close_wsol_ata: true,
open_seed_optimize: false,
with_tip: false,
nonce_account: None,
current_nonce: None,
};
client.sell(sell_params).await?;
+20
View File
@@ -616,6 +616,8 @@ async fn handle_buy_pumpfun(
close_wsol_ata: false,
create_mint_ata: create_mint_ata,
open_seed_optimize: use_seed,
nonce_account: None,
current_nonce: None,
};
match client.buy(buy_params).await {
Ok(signature) => {
@@ -663,6 +665,8 @@ async fn handle_buy_pumpswap(
close_wsol_ata: false,
create_mint_ata: create_mint_ata,
open_seed_optimize: use_seed,
nonce_account: None,
current_nonce: None,
};
match client.buy(buy_params).await {
Ok(signature) => {
@@ -709,6 +713,8 @@ async fn handle_buy_bonk(
close_wsol_ata: false,
create_mint_ata: create_mint_ata,
open_seed_optimize: use_seed,
nonce_account: None,
current_nonce: None,
};
match client.buy(buy_params).await {
Ok(signature) => {
@@ -759,6 +765,8 @@ async fn handle_buy_raydium_v4(
close_wsol_ata: false,
create_mint_ata: create_mint_ata,
open_seed_optimize: use_seed,
nonce_account: None,
current_nonce: None,
};
match client.buy(buy_params).await {
Ok(signature) => {
@@ -809,6 +817,8 @@ async fn handle_buy_raydium_cpmm(
close_wsol_ata: false,
create_mint_ata: create_mint_ata,
open_seed_optimize: use_seed,
nonce_account: None,
current_nonce: None,
};
match client.buy(buy_params).await {
Ok(signature) => {
@@ -969,6 +979,8 @@ async fn handle_sell_pumpfun(
create_wsol_ata: true,
close_wsol_ata: false,
open_seed_optimize: use_seed,
nonce_account: None,
current_nonce: None,
};
match client.sell(sell_params).await {
@@ -1019,6 +1031,8 @@ async fn handle_sell_pumpswap(
create_wsol_ata: true,
close_wsol_ata: false,
open_seed_optimize: use_seed,
nonce_account: None,
current_nonce: None,
};
match client.sell(sell_params).await {
Ok(signature) => {
@@ -1068,6 +1082,8 @@ async fn handle_sell_bonk(
create_wsol_ata: true,
close_wsol_ata: false,
open_seed_optimize: use_seed,
nonce_account: None,
current_nonce: None,
};
match client.sell(sell_params).await {
Ok(signature) => {
@@ -1120,6 +1136,8 @@ async fn handle_sell_raydium_v4(
create_wsol_ata: true,
close_wsol_ata: false,
open_seed_optimize: use_seed,
nonce_account: None,
current_nonce: None,
};
match client.sell(sell_params).await {
Ok(signature) => {
@@ -1172,6 +1190,8 @@ async fn handle_sell_raydium_cpmm(
create_wsol_ata: true,
close_wsol_ata: false,
open_seed_optimize: use_seed,
nonce_account: None,
current_nonce: None,
};
match client.sell(sell_params).await {
Ok(signature) => {
+2
View File
@@ -99,6 +99,8 @@ async fn test_middleware() -> AnyResult<()> {
close_wsol_ata: true,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).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");
+7 -3
View File
@@ -119,13 +119,15 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
let client = create_solana_trade_client().await?;
let mint_pubkey = trade_info.mint;
let slippage_basis_points = Some(100);
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
// Setup nonce cache
let nonce_account_str = "use_your_nonce_account_here";
NonceCache::get_instance().init(Some(nonce_account_str.to_string()));
NonceCache::get_instance().fetch_nonce_info_use_rpc(&client.rpc).await?;
let last_nonce = NonceCache::get_instance().get_nonce_info().current_nonce;
println!("Last nonce: {}", last_nonce);
let current_nonce = NonceCache::get_instance().get_nonce_info().current_nonce;
let nonce_account = NonceCache::get_instance().get_nonce_info().nonce_account;
println!("current_nonce: {}", current_nonce);
// Buy tokens
println!("Buying tokens from PumpFun...");
@@ -135,7 +137,7 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
mint: mint_pubkey,
sol_amount: buy_sol_amount,
slippage_basis_points: slippage_basis_points,
recent_blockhash: last_nonce,
recent_blockhash: recent_blockhash,
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
lookup_table_key: None,
wait_transaction_confirmed: true,
@@ -143,6 +145,8 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
close_wsol_ata: false,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: nonce_account,
current_nonce: Some(current_nonce),
};
client.buy(buy_params).await?;
@@ -136,6 +136,8 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
close_wsol_ata: false,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).await?;
@@ -163,6 +165,8 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
create_wsol_ata: false,
close_wsol_ata: false,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.sell(sell_params).await?;
@@ -105,6 +105,8 @@ async fn pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> AnyR
close_wsol_ata: true,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).await?;
@@ -132,6 +134,8 @@ async fn pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> AnyR
create_wsol_ata: true,
close_wsol_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.sell(sell_params).await?;
@@ -37,6 +37,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
close_wsol_ata: true,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).await?;
@@ -64,6 +66,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
create_wsol_ata: true,
close_wsol_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.sell(sell_params).await?;
+4
View File
@@ -181,6 +181,8 @@ async fn pumpswap_trade_with_grpc(mint_pubkey: Pubkey, params: PumpSwapParams) -
close_wsol_ata: true,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).await?;
@@ -210,6 +212,8 @@ async fn pumpswap_trade_with_grpc(mint_pubkey: Pubkey, params: PumpSwapParams) -
create_wsol_ata: true,
close_wsol_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.sell(sell_params).await?;
@@ -149,6 +149,8 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent)
close_wsol_ata: true,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).await?;
@@ -177,6 +179,8 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent)
create_wsol_ata: true,
close_wsol_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.sell(sell_params).await?;
+22 -3
View File
@@ -3,7 +3,6 @@ use std::sync::{
Arc,
};
use sol_trade_sdk::solana_streamer_sdk::streaming::event_parser::{Protocol, UnifiedEvent};
use sol_trade_sdk::solana_streamer_sdk::streaming::YellowstoneGrpc;
use sol_trade_sdk::solana_streamer_sdk::{
match_event, streaming::event_parser::protocols::raydium_cpmm::RaydiumCpmmSwapEvent,
@@ -13,6 +12,10 @@ use sol_trade_sdk::{
common::TradeConfig,
solana_streamer_sdk::streaming::yellowstone_grpc::{AccountFilter, TransactionFilter},
};
use sol_trade_sdk::{
constants::WSOL_TOKEN_ACCOUNT,
solana_streamer_sdk::streaming::event_parser::{Protocol, UnifiedEvent},
};
use sol_trade_sdk::{
instruction::utils::raydium_cpmm::accounts,
solana_streamer_sdk::streaming::event_parser::protocols::raydium_cpmm::parser::RAYDIUM_CPMM_PROGRAM_ID,
@@ -86,6 +89,9 @@ fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|event: Box<dyn UnifiedEvent>| {
match_event!(event, {
RaydiumCpmmSwapEvent => |e: RaydiumCpmmSwapEvent| {
if e.input_token_mint != WSOL_TOKEN_ACCOUNT && e.output_token_mint != WSOL_TOKEN_ACCOUNT {
return;
}
// Test code, only test one transaction
if !ALREADY_EXECUTED.swap(true, Ordering::SeqCst) {
let event_clone = e.clone();
@@ -105,8 +111,17 @@ fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
/// Initializes a new SolanaTrade client with configuration
async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
println!("🚀 Initializing SolanaTrade client...");
let payer = Keypair::from_base58_string("use_your_payer_keypair_here");
let rpc_url = "https://api.mainnet-beta.solana.com".to_string();
let payer = Keypair::from_bytes(
&std::fs::read_to_string("/Users/ysq/.config/solana/sdk_test.json")
.unwrap()
.trim_matches(|c| c == '[' || c == ']')
.split(',')
.map(|s| s.trim().parse::<u8>().unwrap())
.collect::<Vec<u8>>(),
)
.unwrap();
let rpc_url = "https://ultra-bold-sunset.solana-mainnet.quiknode.pro/1210ea22139565495810678ac0aa33243fea8406/".to_string();
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);
@@ -150,6 +165,8 @@ async fn raydium_cpmm_copy_trade_with_grpc(trade_info: RaydiumCpmmSwapEvent) ->
close_wsol_ata: true,
create_mint_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).await?;
@@ -180,6 +197,8 @@ async fn raydium_cpmm_copy_trade_with_grpc(trade_info: RaydiumCpmmSwapEvent) ->
create_wsol_ata: true,
close_wsol_ata: true,
open_seed_optimize: false,
nonce_account: None,
current_nonce: None,
};
client.sell(sell_params).await?;
+4
View File
@@ -38,6 +38,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
close_wsol_ata: true,
create_mint_ata: true,
open_seed_optimize: true, // ❗️❗️❗️❗️ open seed optimize
nonce_account: None,
current_nonce: None,
};
client.buy(buy_params).await?;
@@ -73,6 +75,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
create_wsol_ata: true,
close_wsol_ata: true,
open_seed_optimize: true, // ❗️❗️❗️❗️ open seed optimize
nonce_account: None,
current_nonce: None,
};
client.sell(sell_params).await?;