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
+14 -11
View File
@@ -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/)
+10 -7
View File
@@ -40,16 +40,17 @@ NonceCache::get_instance().init(Some(nonce_account_str.to_string()));
```rust
// 获取并更新 nonce 信息
NonceCache::get_instance().fetch_nonce_info_use_rpc(&client.rpc).await?;
// 获取当前 nonce 值
// 或者手动管理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. 在交易中使用 Nonce
nonce 作为 recent_blockhash 参数传递给交易:
设置 nonce 参数:nonce_account 和 recent_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, // 使用 nonce 作为 blockhash。请在每次交易时,都使用最新的 nonce 值。
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, // 设置 nonce 账户
current_nonce: Some(current_nonce), // 设置 nonce 值
};
// 执行交易
@@ -75,9 +78,9 @@ client.buy(buy_params).await?;
1. **初始化**: 设置 nonce 账户地址
2. **获取**: 从 RPC 获取最新 nonce 值
4. **使用**: 在交易中作为 blockhash 使用
6. **刷新**: 下次使用前重新获取新的 nonce 值
3. **使用**: 在交易中设置 nonce 参数
4. **刷新**: 下次使用前重新获取新的 nonce 值
## 🔗 相关文档
- [示例:Nonce 缓存](../examples/nonce_cache/)
- [示例:Nonce 缓存](../examples/nonce_cache/)
+10
View File
@@ -34,6 +34,8 @@ The `TradeBuyParams` struct contains all parameters required for executing buy o
| `close_wsol_ata` | `bool` | ✅ | Whether to close wSOL ATA after transaction |
| `create_mint_ata` | `bool` | ✅ | Whether to create token mint ATA |
| `open_seed_optimize` | `bool` | ✅ | Whether to use seed optimization for reduced CU consumption |
| `nonce_account` | `Option<Pubkey>` | ❌ | nonce account |
| `current_nonce` | `Option<u64>` | ❌ | nonce value |
## TradeSellParams
@@ -61,6 +63,8 @@ The `TradeSellParams` struct contains all parameters required for executing sell
| `create_wsol_ata` | `bool` | ✅ | Whether to create wSOL Associated Token Account |
| `close_wsol_ata` | `bool` | ✅ | Whether to close wSOL ATA after transaction |
| `open_seed_optimize` | `bool` | ✅ | Whether to use seed optimization for reduced CU consumption |
| `nonce_account` | `Option<Pubkey>` | ❌ | nonce account |
| `current_nonce` | `Option<u64>` | ❌ | nonce value |
## Parameter Categories
@@ -96,6 +100,12 @@ These parameters enable advanced optimizations:
- **lookup_table_key**: Use address lookup tables for reduced transaction size
- **open_seed_optimize**: Use seed-based account creation for lower CU consumption
### 🔄 Optional Parameters
When you need to use durable nonce, you need to fill in these two parameters:
- **nonce_account**: nonce account
- **current_nonce**: nonce value
## Important Notes
### 🌱 Seed Optimization
+10
View File
@@ -34,6 +34,8 @@
| `close_wsol_ata` | `bool` | ✅ | 交易后是否关闭 wSOL ATA |
| `create_mint_ata` | `bool` | ✅ | 是否创建代币 mint ATA |
| `open_seed_optimize` | `bool` | ✅ | 是否使用 seed 优化以减少 CU 消耗 |
| `nonce_account` | `Option<Pubkey>` | ❌ | nonce 账户 |
| `current_nonce` | `Option<u64>` | ❌ | nonce 值 |
## TradeSellParams
@@ -61,6 +63,8 @@
| `create_wsol_ata` | `bool` | ✅ | 是否创建 wSOL 关联代币账户 |
| `close_wsol_ata` | `bool` | ✅ | 交易后是否关闭 wSOL ATA |
| `open_seed_optimize` | `bool` | ✅ | 是否使用 seed 优化以减少 CU 消耗 |
| `nonce_account` | `Option<Pubkey>` | ❌ | nonce 账户 |
| `current_nonce` | `Option<u64>` | ❌ | nonce 值 |
## 参数分类
@@ -96,6 +100,12 @@
- **lookup_table_key**: 使用地址查找表减少交易大小
- **open_seed_optimize**: 使用基于 seed 的账户创建以降低 CU 消耗
### 🔄 非必填参数
当你需要使用 durable nonce 时,需要填入这两个参数:
- **nonce_account**: nonce 账户
- **current_nonce**: nonce 值
## 重要说明
### 🌱 Seed 优化