refactor: Replace global address lookup table cache with direct fetch approach

Replace the global AddressLookupTableCache with a direct fetch function to simplify address lookup table management. This change improves code maintainability by removing global state and makes the API more explicit.

Key changes:
- Remove AddressLookupTableCache and AddressLookupManager
- Add new fetch_address_lookup_table_account function
- Update TradeBuyParams and TradeSellParams to use AddressLookupTableAccount instead of Pubkey
- Update all examples to use the new direct fetch approach
- Update documentation to reflect the simplified workflow
This commit is contained in:
ysq
2025-10-07 20:56:02 +08:00
parent 2e7b9819d3
commit 2d9976368b
35 changed files with 168 additions and 308 deletions
+3 -29
View File
@@ -15,36 +15,11 @@ Address Lookup Tables are a Solana feature that allows you to store frequently u
## 🛠️ Implementation
### 1. Setting up Address Lookup Table Cache
The SDK provides a global cache to manage address lookup tables:
```rust
use sol_trade_sdk::common::address_lookup_cache::AddressLookupTableCache;
use solana_sdk::pubkey::Pubkey;
use std::str::FromStr;
/// Setup lookup table cache
async fn setup_lookup_table_cache(
client: Arc<SolanaRpcClient>,
lookup_table_address: Pubkey,
) -> AnyResult<()> {
AddressLookupTableCache::get_instance()
.set_address_lookup_table(client, &lookup_table_address)
.await
.map_err(|e| anyhow::anyhow!("Failed to set address lookup table: {}", e))?;
Ok(())
}
```
### 2. Using Lookup Tables in Trade Parameters
Include lookup tables in your trade parameters:
```rust
// Initialize lookup table
let lookup_table_key = Pubkey::from_str("your_lookup_table_address_here").unwrap();
setup_lookup_table_cache(client.rpc.clone(), lookup_table_key).await?;
let lookup_table_key = Pubkey::from_str("use_your_lookup_table_key_here").unwrap();
let address_lookup_table_account = fetch_address_lookup_table_account(&client.rpc, &lookup_table_key).await.ok();
// Include lookup table in trade parameters
let buy_params = sol_trade_sdk::TradeBuyParams {
@@ -54,7 +29,7 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
slippage_basis_points: Some(100),
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
address_lookup_table_account: address_lookup_table_account, // Include lookup table
wait_transaction_confirmed: true,
create_wsol_ata: false,
close_wsol_ata: false,
@@ -78,7 +53,6 @@ client.buy(buy_params).await?;
## ⚠️ Important Notes
1. **Lookup Table Address**: Must provide a valid address lookup table address
2. **Cache Management**: SDK automatically manages lookup table cache
3. **RPC Compatibility**: Ensure your RPC provider supports lookup tables
4. **Network Specific**: Lookup tables are network-specific (mainnet/devnet/testnet)
5. **Testing**: Always test on devnet before using on mainnet
+3 -29
View File
@@ -15,36 +15,11 @@
## 🛠️ 实现方法
### 1. 设置地址查找表缓存
SDK 提供了一个全局缓存来管理地址查找表:
```rust
use sol_trade_sdk::common::address_lookup_cache::AddressLookupTableCache;
use solana_sdk::pubkey::Pubkey;
use std::str::FromStr;
/// 设置查找表缓存
async fn setup_lookup_table_cache(
client: Arc<SolanaRpcClient>,
lookup_table_address: Pubkey,
) -> AnyResult<()> {
AddressLookupTableCache::get_instance()
.set_address_lookup_table(client, &lookup_table_address)
.await
.map_err(|e| anyhow::anyhow!("Failed to set address lookup table: {}", e))?;
Ok(())
}
```
### 2. 在交易参数中使用查找表
在您的交易参数中包含查找表:
```rust
// 初始化查找表
let lookup_table_key = Pubkey::from_str("your_lookup_table_address_here").unwrap();
setup_lookup_table_cache(client.rpc.clone(), lookup_table_key).await?;
let lookup_table_key = Pubkey::from_str("use_your_lookup_table_key_here").unwrap();
let address_lookup_table_account = fetch_address_lookup_table_account(&client.rpc, &lookup_table_key).await.ok();
// 在交易参数中包含查找表
let buy_params = sol_trade_sdk::TradeBuyParams {
@@ -54,7 +29,7 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
slippage_basis_points: Some(100),
recent_blockhash: Some(recent_blockhash),
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
lookup_table_key: Some(lookup_table_key), // 包含查找表
address_lookup_table_account: address_lookup_table_account, // 包含查找表
wait_transaction_confirmed: true,
create_wsol_ata: false,
close_wsol_ata: false,
@@ -78,7 +53,6 @@ client.buy(buy_params).await?;
## ⚠️ 重要注意事项
1. **查找表地址**: 必须提供有效的地址查找表地址
2. **缓存管理**: SDK 自动管理查找表缓存
3. **RPC 兼容性**: 确保您的 RPC 提供商支持查找表
4. **网络**: 查找表是特定于网络的(主网/开发网/测试网)
5. **测试**: 在主网使用前请务必在开发网测试
+1 -1
View File
@@ -57,7 +57,7 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
slippage_basis_points: Some(100),
recent_blockhash: Some(recent_blockhash),
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
lookup_table_key: None,
address_lookup_table_account: None,
wait_transaction_confirmed: true,
create_wsol_ata: false,
close_wsol_ata: false,
+1 -1
View File
@@ -57,7 +57,7 @@ let buy_params = sol_trade_sdk::TradeBuyParams {
slippage_basis_points: Some(100),
recent_blockhash: Some(recent_blockhash),
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
lookup_table_key: None,
address_lookup_table_account: None,
wait_transaction_confirmed: true,
create_wsol_ata: false,
close_wsol_ata: false,
+4 -5
View File
@@ -29,7 +29,7 @@ The `TradeBuyParams` struct contains all parameters required for executing buy o
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `lookup_table_key` | `Option<Pubkey>` | ❌ | Address lookup table key for transaction optimization |
| `address_lookup_table_account` | `Option<AddressLookupTableAccount>` | ❌ | Address lookup table for transaction optimization |
| `wait_transaction_confirmed` | `bool` | ✅ | Whether to wait for transaction confirmation |
| `create_input_token_ata` | `bool` | ✅ | Whether to create input token Associated Token Account |
| `close_input_token_ata` | `bool` | ✅ | Whether to close input token ATA after transaction |
@@ -60,7 +60,7 @@ The `TradeSellParams` struct contains all parameters required for executing sell
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `lookup_table_key` | `Option<Pubkey>` | ❌ | Address lookup table key for transaction optimization |
| `address_lookup_table_account` | `Option<Pubkey>` | ❌ | Address lookup table for transaction optimization |
| `wait_transaction_confirmed` | `bool` | ✅ | Whether to wait for transaction confirmation |
| `create_output_token_ata` | `bool` | ✅ | Whether to create output token Associated Token Account |
| `close_output_token_ata` | `bool` | ✅ | Whether to close output token ATA after transaction |
@@ -100,7 +100,7 @@ These parameters control automatic account creation and management:
These parameters enable advanced optimizations:
- **lookup_table_key**: Use address lookup tables for reduced transaction size
- **address_lookup_table_account**: Use address lookup tables for reduced transaction size
- **open_seed_optimize**: Use seed-based account creation for lower CU consumption
### 🔄 Token Type Parameters
@@ -134,8 +134,7 @@ The account management parameters provide granular control:
### 🔍 Address Lookup Tables
Before using `lookup_table_key`:
- Initialize `AddressLookupTableCache` to manage cached lookup tables
Before using `address_lookup_table_account`:
- Lookup tables reduce transaction size and improve success rates
- Particularly beneficial for complex transactions with many account references
+4 -5
View File
@@ -29,7 +29,7 @@
| 参数 | 类型 | 必需 | 描述 |
|------|------|------|------|
| `lookup_table_key` | `Option<Pubkey>` | ❌ | 用于交易优化的地址查找表 |
| `address_lookup_table_account` | `Option<Pubkey>` | ❌ | 用于交易优化的地址查找表 |
| `wait_transaction_confirmed` | `bool` | ✅ | 是否等待交易确认 |
| `create_input_token_ata` | `bool` | ✅ | 是否创建输入代币关联代币账户 |
| `close_input_token_ata` | `bool` | ✅ | 交易后是否关闭输入代币 ATA |
@@ -60,7 +60,7 @@
| 参数 | 类型 | 必需 | 描述 |
|------|------|------|------|
| `lookup_table_key` | `Option<Pubkey>` | ❌ | 用于交易优化的地址查找表 |
| `address_lookup_table_account` | `Option<AddressLookupTableAccount>` | ❌ | 用于交易优化的地址查找表 |
| `wait_transaction_confirmed` | `bool` | ✅ | 是否等待交易确认 |
| `create_output_token_ata` | `bool` | ✅ | 是否创建输出代币关联代币账户 |
| `close_output_token_ata` | `bool` | ✅ | 交易后是否关闭输出代币 ATA |
@@ -100,7 +100,7 @@
这些参数启用高级优化:
- **lookup_table_key**: 使用地址查找表减少交易大小
- **address_lookup_table_account**: 使用地址查找表减少交易大小
- **open_seed_optimize**: 使用基于 seed 的账户创建以降低 CU 消耗
### 🔄 代币类型参数
@@ -134,8 +134,7 @@
### 🔍 地址查找表
使用 `lookup_table_key` 之前:
- 初始化 `AddressLookupTableCache` 来管理缓存的查找表
使用 `address_lookup_table_account` 之前:
- 查找表减少交易大小并提高成功率
- 对于有许多账户引用的复杂交易特别有益