refactor: simplify trading parameters API and nonce handling

- Change recent_blockhash from required to optional parameter
- Replace separate nonce_account and current_nonce with unified durable_nonce
- Update all examples and documentation to reflect API changes
- Improve nonce cache usage pattern for better developer experience

BREAKING CHANGE: TradeBuyParams and TradeSellParams API has changed
- recent_blockhash is now Option<Hash> instead of Hash
- nonce_account and current_nonce fields replaced with durable_nonce: Option<DurableNonceInfo>
This commit is contained in:
ysq
2025-09-21 21:56:17 +08:00
parent b4769749f5
commit f8891f3147
21 changed files with 89 additions and 136 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
mint: mint_pubkey,
sol_amount: buy_sol_amount,
slippage_basis_points: Some(100),
recent_blockhash: recent_blockhash,
recent_blockhash: Some(recent_blockhash),
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
lookup_table_key: Some(lookup_table_key), // Include lookup table
wait_transaction_confirmed: true,
+1 -1
View File
@@ -52,7 +52,7 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
mint: mint_pubkey,
sol_amount: buy_sol_amount,
slippage_basis_points: Some(100),
recent_blockhash: recent_blockhash,
recent_blockhash: Some(recent_blockhash),
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
lookup_table_key: Some(lookup_table_key), // 包含查找表
wait_transaction_confirmed: true,
+4 -8
View File
@@ -42,15 +42,12 @@ Get the latest nonce information from RPC:
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 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);
let durable_nonce = NonceCache::get_durable_nonce_info();
```
### 3. Use Nonce in Transactions
Set nonce parameters: nonce_account and current_nonce
Set nonce parameters: durable_nonce
```rust
let buy_params = sol_trade_sdk::TradeBuyParams {
@@ -58,7 +55,7 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
mint: mint_pubkey,
sol_amount: buy_sol_amount,
slippage_basis_points: Some(100),
recent_blockhash: recent_blockhash,
recent_blockhash: Some(recent_blockhash),
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
lookup_table_key: None,
wait_transaction_confirmed: true,
@@ -66,8 +63,7 @@ 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
durable_nonce: Some(durable_nonce), // Set durable nonce
};
// Execute transaction
+4 -8
View File
@@ -42,15 +42,12 @@ NonceCache::get_instance().init(Some(nonce_account_str.to_string()));
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 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);
let durable_nonce = NonceCache::get_durable_nonce_info();
```
### 3. 在交易中使用 Nonce
设置 nonce 参数:nonce_account 和 recent_nonce
设置 nonce 参数:durable_nonce
```rust
let buy_params = sol_trade_sdk::TradeBuyParams {
@@ -58,7 +55,7 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
mint: mint_pubkey,
sol_amount: buy_sol_amount,
slippage_basis_points: Some(100),
recent_blockhash: recent_blockhash,
recent_blockhash: Some(recent_blockhash),
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
lookup_table_key: None,
wait_transaction_confirmed: true,
@@ -66,8 +63,7 @@ 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 值
durable_nonce: Some(durable_nonce), // 设置 durable nonce
};
// 执行交易
+6 -9
View File
@@ -21,7 +21,7 @@ The `TradeBuyParams` struct contains all parameters required for executing buy o
| `mint` | `Pubkey` | ✅ | The public key of the token mint to purchase |
| `sol_amount` | `u64` | ✅ | Amount of SOL to spend (in lamports) |
| `slippage_basis_points` | `Option<u64>` | ❌ | Slippage tolerance in basis points (e.g., 100 = 1%, 500 = 5%) |
| `recent_blockhash` | `Hash` | | Recent blockhash for transaction validity |
| `recent_blockhash` | `Option<Hash>` | | Recent blockhash for transaction validity |
| `extension_params` | `Box<dyn ProtocolParams>` | ✅ | Protocol-specific parameters (PumpFunParams, PumpSwapParams, etc.) |
### Advanced Configuration Parameters
@@ -34,8 +34,7 @@ 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 |
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | Durable nonce information containing nonce account and current nonce value |
## TradeSellParams
@@ -50,7 +49,7 @@ The `TradeSellParams` struct contains all parameters required for executing sell
| `mint` | `Pubkey` | ✅ | The public key of the token mint to sell |
| `token_amount` | `u64` | ✅ | Amount of tokens to sell (in smallest token units) |
| `slippage_basis_points` | `Option<u64>` | ❌ | Slippage tolerance in basis points (e.g., 100 = 1%, 500 = 5%) |
| `recent_blockhash` | `Hash` | | Recent blockhash for transaction validity |
| `recent_blockhash` | `Option<Hash>` | | Recent blockhash for transaction validity |
| `with_tip` | `bool` | ✅ | Whether to include tip in the transaction |
| `extension_params` | `Box<dyn ProtocolParams>` | ✅ | Protocol-specific parameters (PumpFunParams, PumpSwapParams, etc.) |
@@ -63,8 +62,7 @@ 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 |
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | Durable nonce information containing nonce account and current nonce value |
## Parameter Categories
@@ -102,9 +100,8 @@ These parameters enable advanced optimizations:
### 🔄 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
When you need to use durable nonce, you need to fill in this parameter:
- **durable_nonce**: Durable nonce information containing nonce account and current nonce value
## Important Notes
+6 -9
View File
@@ -21,7 +21,7 @@
| `mint` | `Pubkey` | ✅ | 要购买的代币 mint 公钥 |
| `sol_amount` | `u64` | ✅ | 要花费的 SOL 数量(以 lamports 为单位) |
| `slippage_basis_points` | `Option<u64>` | ❌ | 滑点容忍度(基点单位,例如 100 = 1%, 500 = 5% |
| `recent_blockhash` | `Hash` | | 用于交易有效性的最新区块哈希 |
| `recent_blockhash` | `Option<Hash>` | | 用于交易有效性的最新区块哈希 |
| `extension_params` | `Box<dyn ProtocolParams>` | ✅ | 协议特定参数 (PumpFunParams, PumpSwapParams 等) |
### 高级配置参数
@@ -34,8 +34,7 @@
| `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 值 |
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | 持久 nonce 信息,包含 nonce 账户和当前 nonce 值 |
## TradeSellParams
@@ -50,7 +49,7 @@
| `mint` | `Pubkey` | ✅ | 要出售的代币 mint 公钥 |
| `token_amount` | `u64` | ✅ | 要出售的代币数量(最小代币单位) |
| `slippage_basis_points` | `Option<u64>` | ❌ | 滑点容忍度(基点单位,例如 100 = 1%, 500 = 5% |
| `recent_blockhash` | `Hash` | | 用于交易有效性的最新区块哈希 |
| `recent_blockhash` | `Option<Hash>` | | 用于交易有效性的最新区块哈希 |
| `with_tip` | `bool` | ✅ | 交易中是否包含小费 |
| `extension_params` | `Box<dyn ProtocolParams>` | ✅ | 协议特定参数 (PumpFunParams, PumpSwapParams 等) |
@@ -63,8 +62,7 @@
| `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 值 |
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | 持久 nonce 信息,包含 nonce 账户和当前 nonce 值 |
## 参数分类
@@ -102,9 +100,8 @@
### 🔄 非必填参数
当你需要使用 durable nonce 时,需要填入这个参数:
- **nonce_account**: nonce 账户
- **current_nonce**: nonce 值
当你需要使用 durable nonce 时,需要填入这个参数:
- **durable_nonce**: 持久 nonce 信息,包含 nonce 账户和当前 nonce 值
## 重要说明