refactor: Simplify nonce management by replacing global cache with direct fetch

Remove NonceCache singleton pattern and replace with fetch_nonce_info function
that directly fetches nonce information from RPC. This simplifies the API by
eliminating cache initialization and state management, making it easier for
users to manage durable nonces.
This commit is contained in:
ysq
2025-10-07 21:20:00 +08:00
parent 2d9976368b
commit e13c891cc9
7 changed files with 78 additions and 244 deletions
+20 -29
View File
@@ -1,10 +1,10 @@
# Nonce Cache Guide
# Durable Nonce Guide
This guide explains how to use Nonce Cache in Sol Trade SDK to implement transaction replay protection and optimize transaction processing.
This guide explains how to use Durable Nonce in Sol Trade SDK to implement transaction replay protection and optimize transaction processing.
## 📋 What is Nonce Cache?
## 📋 What is Durable Nonce?
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.
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
@@ -21,31 +21,23 @@ Nonce Cache is a global singleton cache system for managing durable nonce accoun
You need to create a nonce account for your payer account first.
Reference: https://solana.com/developers/guides/advanced/introduction-to-durable-nonces
### 1. Initialize Nonce Cache
### 1. Fetch Nonce Information
First, set up the nonce account and initialize the cache:
Directly fetch nonce information from RPC:
```rust
use sol_trade_sdk::common::nonce_cache::NonceCache;
use sol_trade_sdk::common::nonce_cache::fetch_nonce_info;
use solana_sdk::pubkey::Pubkey;
use std::str::FromStr;
// Set up nonce account
let nonce_account_str = "your_nonce_account_address_here";
NonceCache::get_instance().init(Some(nonce_account_str.to_string()));
let nonce_account = Pubkey::from_str("your_nonce_account_address_here")?;
// Fetch nonce information
let durable_nonce = fetch_nonce_info(&client.rpc, nonce_account).await;
```
### 2. Fetch Nonce Information
Get the latest nonce information from RPC:
```rust
// Fetch and update nonce information
NonceCache::get_instance().fetch_nonce_info_use_rpc(&client.rpc).await?;
// Or manually manage nonce
// NonceCache::get_instance().update_nonce_info_partial(nonce_account, current_nonce, used);
let durable_nonce = NonceCache::get_durable_nonce_info();
```
### 3. Use Nonce in Transactions
### 2. Use Nonce in Transactions
Set nonce parameters: durable_nonce
@@ -63,20 +55,19 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
close_wsol_ata: false,
create_mint_ata: true,
open_seed_optimize: false,
durable_nonce: Some(durable_nonce), // Set durable nonce
durable_nonce: durable_nonce, // Set durable nonce
};
// Execute transaction
client.buy(buy_params).await?;
```
## 🔄 Nonce Lifecycle
## 🔄 Nonce Usage Flow
1. **Initialize**: Set nonce account address
2. **Fetch**: Get the latest nonce value from RPC
3. **Use**: Set nonce parameters in transactions
4. **Refresh**: Fetch new nonce value before next use
1. **Fetch**: Get the latest nonce value from RPC
2. **Use**: Set nonce parameters in transactions
3. **Refresh**: Call `fetch_nonce_info` again before next use to get new nonce value
## 🔗 Related Documentation
- [Example: Nonce Cache](../examples/nonce_cache/)
- [Example: Durable Nonce](../examples/nonce_cache/)