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:
+14
-11
@@ -4,13 +4,13 @@ This guide explains how to use Nonce Cache in Sol Trade SDK to implement transac
|
||||
|
||||
## 📋 What is Nonce Cache?
|
||||
|
||||
Nonce Cache is a global singleton cache system for managing durable nonce accounts in the Solana network. Durable nonce is a Solana feature that allows you to create transactions that remain valid for extended periods, not limited by the 150-block constraint of recent block hashes.
|
||||
Nonce Cache is a global singleton cache system for managing durable nonce accounts in the Solana network. Durable nonce is a Solana feature that allows you to create transactions that remain valid for extended periods, beyond the 150-block limitation of recent block hashes.
|
||||
|
||||
## 🚀 Core Benefits
|
||||
|
||||
- **Transaction Replay Protection**: Prevents the same transaction from being executed multiple times
|
||||
- **Transaction Replay Protection**: Prevents identical transactions from being executed multiple times
|
||||
- **Extended Time Window**: Transactions can remain valid for longer periods
|
||||
- **Network Performance Optimization**: Reduces dependency on recent block hashes
|
||||
- **Network Performance Optimization**: Reduces dependency on the latest block hash
|
||||
- **Transaction Determinism**: Provides consistent transaction processing experience
|
||||
- **Offline Transaction Support**: Supports offline processing of pre-signed transactions
|
||||
|
||||
@@ -28,7 +28,7 @@ First, set up the nonce account and initialize the cache:
|
||||
```rust
|
||||
use sol_trade_sdk::common::nonce_cache::NonceCache;
|
||||
|
||||
// Set nonce account
|
||||
// Set up nonce account
|
||||
let nonce_account_str = "your_nonce_account_address_here";
|
||||
NonceCache::get_instance().init(Some(nonce_account_str.to_string()));
|
||||
```
|
||||
@@ -40,16 +40,17 @@ Get the latest nonce information from RPC:
|
||||
```rust
|
||||
// Fetch and update nonce information
|
||||
NonceCache::get_instance().fetch_nonce_info_use_rpc(&client.rpc).await?;
|
||||
|
||||
// Get current nonce value
|
||||
// Or manually manage nonce
|
||||
// NonceCache::get_instance().update_nonce_info_partial(nonce_account, current_nonce, used);
|
||||
let nonce_info = NonceCache::get_instance().get_nonce_info();
|
||||
let current_nonce = nonce_info.current_nonce;
|
||||
let nonce_account = nonce_info.nonce_account;
|
||||
println!("Current nonce: {}", current_nonce);
|
||||
```
|
||||
|
||||
### 3. Use Nonce in Transactions
|
||||
|
||||
Pass the nonce as the recent_blockhash parameter to transactions:
|
||||
Set nonce parameters: nonce_account and current_nonce
|
||||
|
||||
```rust
|
||||
let buy_params = sol_trade_sdk::TradeBuyParams {
|
||||
@@ -57,7 +58,7 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
|
||||
mint: mint_pubkey,
|
||||
sol_amount: buy_sol_amount,
|
||||
slippage_basis_points: Some(100),
|
||||
recent_blockhash: current_nonce, // Use nonce as blockhash. Please use the latest nonce value for each transaction.
|
||||
recent_blockhash: recent_blockhash,
|
||||
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
@@ -65,6 +66,8 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
|
||||
close_wsol_ata: false,
|
||||
create_mint_ata: true,
|
||||
open_seed_optimize: false,
|
||||
nonce_account: nonce_account, // Set nonce account
|
||||
current_nonce: Some(current_nonce), // Set nonce value
|
||||
};
|
||||
|
||||
// Execute transaction
|
||||
@@ -75,9 +78,9 @@ client.buy(buy_params).await?;
|
||||
|
||||
1. **Initialize**: Set nonce account address
|
||||
2. **Fetch**: Get the latest nonce value from RPC
|
||||
4. **Use**: Use as blockhash in transactions
|
||||
6. **Refresh**: Re-fetch new nonce value before next use
|
||||
3. **Use**: Set nonce parameters in transactions
|
||||
4. **Refresh**: Fetch new nonce value before next use
|
||||
|
||||
## 🔗 Related Documentation
|
||||
|
||||
- [Example: Nonce Cache](../examples/nonce_cache/)
|
||||
- [Example: Nonce Cache](../examples/nonce_cache/)
|
||||
|
||||
Reference in New Issue
Block a user