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:
@@ -3,6 +3,7 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use crossbeam_queue::ArrayQueue;
|
||||
use solana_hash::Hash;
|
||||
use solana_sdk::message::AddressLookupTableAccount;
|
||||
use solana_sdk::{
|
||||
instruction::Instruction, pubkey::Pubkey, signature::Keypair, signature::Signature,
|
||||
};
|
||||
@@ -96,7 +97,7 @@ pub async fn execute_parallel(
|
||||
payer: Arc<Keypair>,
|
||||
rpc: Option<Arc<SolanaRpcClient>>,
|
||||
instructions: Vec<Instruction>,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
address_lookup_table_account: Option<AddressLookupTableAccount>,
|
||||
recent_blockhash: Option<Hash>,
|
||||
durable_nonce: Option<DurableNonceInfo>,
|
||||
data_size_limit: u32,
|
||||
@@ -168,6 +169,7 @@ pub async fn execute_parallel(
|
||||
let unit_price = gas_fee_strategy_config.2.cu_price;
|
||||
let rpc = rpc.clone();
|
||||
let durable_nonce = durable_nonce.clone();
|
||||
let address_lookup_table_account = address_lookup_table_account.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let _task_start = Instant::now();
|
||||
@@ -182,7 +184,7 @@ pub async fn execute_parallel(
|
||||
unit_limit,
|
||||
unit_price,
|
||||
instructions.as_ref().clone(),
|
||||
lookup_table_key,
|
||||
address_lookup_table_account,
|
||||
recent_blockhash,
|
||||
data_size_limit,
|
||||
middleware_manager,
|
||||
|
||||
@@ -87,7 +87,7 @@ impl TradeExecutor for GenericTradeExecutor {
|
||||
params.payer,
|
||||
params.rpc,
|
||||
final_instructions,
|
||||
params.lookup_table_key,
|
||||
params.address_lookup_table_account,
|
||||
params.recent_blockhash,
|
||||
params.durable_nonce,
|
||||
if is_buy { params.data_size_limit } else { 0 },
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
pub mod params;
|
||||
pub mod traits;
|
||||
pub mod executor;
|
||||
pub mod parallel;
|
||||
pub mod async_executor;
|
||||
pub mod transaction_pool;
|
||||
pub mod execution;
|
||||
@@ -8,6 +8,7 @@ use crate::swqos::{SwqosClient, TradeType};
|
||||
use crate::trading::common::get_multi_token_balances;
|
||||
use crate::trading::MiddlewareManager;
|
||||
use solana_hash::Hash;
|
||||
use solana_sdk::message::AddressLookupTableAccount;
|
||||
use solana_sdk::{pubkey::Pubkey, signature::Keypair};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -23,7 +24,7 @@ pub struct SwapParams {
|
||||
pub output_token_program: Option<Pubkey>,
|
||||
pub input_amount: Option<u64>,
|
||||
pub slippage_basis_points: Option<u64>,
|
||||
pub lookup_table_key: Option<Pubkey>,
|
||||
pub address_lookup_table_account: Option<AddressLookupTableAccount>,
|
||||
pub recent_blockhash: Option<Hash>,
|
||||
pub data_size_limit: u32,
|
||||
pub wait_transaction_confirmed: bool,
|
||||
|
||||
@@ -9,10 +9,7 @@
|
||||
use crossbeam_queue::ArrayQueue;
|
||||
use once_cell::sync::Lazy;
|
||||
use solana_sdk::{
|
||||
instruction::Instruction,
|
||||
message::{v0, VersionedMessage, Message},
|
||||
pubkey::Pubkey,
|
||||
hash::Hash,
|
||||
hash::Hash, instruction::Instruction, message::{v0, AddressLookupTableAccount, Message, VersionedMessage}, pubkey::Pubkey
|
||||
};
|
||||
use std::sync::Arc;
|
||||
/// 预分配的交易构建器
|
||||
@@ -68,7 +65,7 @@ impl PreallocatedTxBuilder {
|
||||
&mut self,
|
||||
payer: &Pubkey,
|
||||
instructions: &[Instruction],
|
||||
lookup_table: Option<Pubkey>,
|
||||
address_lookup_table_account: Option<AddressLookupTableAccount>,
|
||||
recent_blockhash: Hash,
|
||||
) -> VersionedMessage {
|
||||
// 重用已分配的 vector
|
||||
@@ -76,24 +73,32 @@ impl PreallocatedTxBuilder {
|
||||
self.instructions.extend_from_slice(instructions);
|
||||
|
||||
// ✅ 如果有查找表,使用 V0 消息
|
||||
if let Some(table_key) = lookup_table {
|
||||
self.lookup_tables.push(v0::MessageAddressTableLookup {
|
||||
account_key: table_key,
|
||||
writable_indexes: vec![],
|
||||
readonly_indexes: vec![],
|
||||
});
|
||||
if let Some(address_lookup_table_account) = address_lookup_table_account {
|
||||
// self.lookup_tables.push(v0::MessageAddressTableLookup {
|
||||
// account_key: table_key,
|
||||
// writable_indexes: vec![],
|
||||
// readonly_indexes: vec![],
|
||||
// });
|
||||
|
||||
// 使用 Message::new 创建 legacy 消息,然后提取编译后的指令
|
||||
let legacy_msg = Message::new(&self.instructions, Some(payer));
|
||||
// // 使用 Message::new 创建 legacy 消息,然后提取编译后的指令
|
||||
// let legacy_msg = Message::new(&self.instructions, Some(payer));
|
||||
|
||||
// 构建 V0 消息
|
||||
let message = v0::Message {
|
||||
header: legacy_msg.header,
|
||||
account_keys: legacy_msg.account_keys,
|
||||
// // 构建 V0 消息
|
||||
// let message = v0::Message {
|
||||
// header: legacy_msg.header,
|
||||
// account_keys: legacy_msg.account_keys,
|
||||
// recent_blockhash,
|
||||
// instructions: legacy_msg.instructions,
|
||||
// address_table_lookups: self.lookup_tables.clone(),
|
||||
// };
|
||||
|
||||
let message = v0::Message::try_compile(
|
||||
payer,
|
||||
&self.instructions,
|
||||
&[address_lookup_table_account],
|
||||
recent_blockhash,
|
||||
instructions: legacy_msg.instructions,
|
||||
address_table_lookups: self.lookup_tables.clone(),
|
||||
};
|
||||
).expect("v0 message compile failed");
|
||||
|
||||
|
||||
VersionedMessage::V0(message)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user