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:
+20
-29
@@ -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/)
|
||||
|
||||
+20
-29
@@ -1,10 +1,10 @@
|
||||
# Nonce 缓存指南
|
||||
# Nonce 使用指南
|
||||
|
||||
本指南介绍如何在 Sol Trade SDK 中使用 Nonce 缓存来实现交易重放保护和优化交易处理。
|
||||
本指南介绍如何在 Sol Trade SDK 中使用 Durable Nonce 来实现交易重放保护和优化交易处理。
|
||||
|
||||
## 📋 什么是 Nonce 缓存?
|
||||
## 📋 什么是 Durable Nonce?
|
||||
|
||||
Nonce 缓存是一个全局单例模式的缓存系统,用于管理 Solana 网络中的 durable nonce 账户。Durable nonce 是 Solana 的一项功能,允许您创建在较长时间内有效的交易,而不受最近区块哈希的 150 个区块限制。
|
||||
Durable Nonce 是 Solana 的一项功能,允许您创建在较长时间内有效的交易,而不受最近区块哈希的 150 个区块限制。
|
||||
|
||||
## 🚀 核心优势
|
||||
|
||||
@@ -21,31 +21,23 @@ Nonce 缓存是一个全局单例模式的缓存系统,用于管理 Solana 网
|
||||
需要先创建你 payer 账号使用的 nonce 账户。
|
||||
参考资料: https://solana.com/zh/developers/guides/advanced/introduction-to-durable-nonces
|
||||
|
||||
### 1. 初始化 Nonce 缓存
|
||||
### 1. 获取 Nonce 信息
|
||||
|
||||
首先需要设置 nonce 账户并初始化缓存:
|
||||
从 RPC 直接获取 nonce 信息:
|
||||
|
||||
```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;
|
||||
|
||||
// 设置 nonce 账户
|
||||
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")?;
|
||||
|
||||
// 获取 nonce 信息
|
||||
let durable_nonce = fetch_nonce_info(&client.rpc, nonce_account).await;
|
||||
```
|
||||
|
||||
### 2. 获取 Nonce 信息
|
||||
|
||||
从 RPC 获取最新的 nonce 信息:
|
||||
|
||||
```rust
|
||||
// 获取并更新 nonce 信息
|
||||
NonceCache::get_instance().fetch_nonce_info_use_rpc(&client.rpc).await?;
|
||||
// 或者手动管理nonce
|
||||
// NonceCache::get_instance().update_nonce_info_partial(nonce_account, current_nonce, used);
|
||||
let durable_nonce = NonceCache::get_durable_nonce_info();
|
||||
```
|
||||
|
||||
### 3. 在交易中使用 Nonce
|
||||
### 2. 在交易中使用 Nonce
|
||||
|
||||
设置 nonce 参数: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), // 设置 durable nonce
|
||||
durable_nonce: durable_nonce, // 设置 durable nonce
|
||||
};
|
||||
|
||||
// 执行交易
|
||||
client.buy(buy_params).await?;
|
||||
```
|
||||
|
||||
## 🔄 Nonce 生命周期
|
||||
## 🔄 Nonce 使用流程
|
||||
|
||||
1. **初始化**: 设置 nonce 账户地址
|
||||
2. **获取**: 从 RPC 获取最新 nonce 值
|
||||
3. **使用**: 在交易中设置 nonce 参数
|
||||
4. **刷新**: 下次使用前重新获取新的 nonce 值
|
||||
1. **获取**: 从 RPC 获取最新 nonce 值
|
||||
2. **使用**: 在交易中设置 nonce 参数
|
||||
3. **刷新**: 下次使用前重新调用 `fetch_nonce_info` 获取新的 nonce 值
|
||||
|
||||
## 🔗 相关文档
|
||||
|
||||
- [示例:Nonce 缓存](../examples/nonce_cache/)
|
||||
- [示例:Durable Nonce](../examples/nonce_cache/)
|
||||
|
||||
Reference in New Issue
Block a user