feat: refactor seed optimization to global configuration in v3.2.0

- Remove open_seed_optimize parameter from TradeBuyParams and TradeSellParams structs
- Add global use_seed_optimize configuration in TradeConfig with default value true
- Add create_wsol_ata_on_startup configuration in TradeConfig with default value true
- Implement automatic WSOL ATA creation and verification on SDK initialization
- Add with_wsol_ata_config() builder method to TradeConfig for custom WSOL settings
- Update all examples to remove open_seed_optimize parameter usage
- Update documentation (README, TRADING_PARAMETERS) to reflect new configuration approach
- Upgrade package version from 3.1.7 to 3.2.0

Breaking Changes:
- open_seed_optimize parameter removed from trade parameters (now global setting)
- Seed optimization is now enabled by default for all operations
This commit is contained in:
ysq
2025-11-07 16:57:28 +08:00
parent 9824576164
commit c5a55b1c14
23 changed files with 173 additions and 74 deletions
+77 -7
View File
@@ -59,6 +59,9 @@ pub struct SolanaTrade {
pub swqos_clients: Vec<Arc<SwqosClient>>,
/// Optional middleware manager for custom transaction processing
pub middleware_manager: Option<Arc<MiddlewareManager>>,
/// Whether to use seed optimization for all ATA operations (default: true)
/// Applies to all token account creations across buy and sell operations
pub use_seed_optimize: bool,
}
static INSTANCE: Mutex<Option<Arc<SolanaTrade>>> = Mutex::new(None);
@@ -70,6 +73,7 @@ impl Clone for SolanaTrade {
rpc: self.rpc.clone(),
swqos_clients: self.swqos_clients.clone(),
middleware_manager: self.middleware_manager.clone(),
use_seed_optimize: self.use_seed_optimize,
}
}
}
@@ -106,8 +110,6 @@ pub struct TradeBuyParams {
pub close_input_token_ata: bool,
/// Whether to create token mint associated token account
pub create_mint_ata: bool,
/// Whether to enable seed-based optimization for account creation
pub open_seed_optimize: bool,
/// Durable nonce information
pub durable_nonce: Option<DurableNonceInfo>,
/// Optional fixed output token amount (If this value is set, it will be directly assigned to the output amount instead of being calculated)
@@ -152,8 +154,6 @@ pub struct TradeSellParams {
pub close_output_token_ata: bool,
/// Whether to close mint token associated token account after trade
pub close_mint_token_ata: bool,
/// Whether to enable seed-based optimization for account creation
pub open_seed_optimize: bool,
/// Durable nonce information
pub durable_nonce: Option<DurableNonceInfo>,
/// Optional fixed output token amount (If this value is set, it will be directly assigned to the output amount instead of being calculated)
@@ -204,7 +204,77 @@ impl SolanaTrade {
common::seed::update_rents(&rpc).await.unwrap();
common::seed::start_rent_updater(rpc.clone());
let instance = Self { payer, rpc, swqos_clients, middleware_manager: None };
// 🔧 初始化WSOL ATA:如果配置为启动时创建,则检查并创建
if trade_config.create_wsol_ata_on_startup {
// 根据seed配置计算WSOL ATA地址
let wsol_ata =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&payer.pubkey(),
&WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
);
// 查询账户是否存在
match rpc.get_account(&wsol_ata).await {
Ok(_) => {
// WSOL ATA已存在
println!("✅ WSOL ATA已存在: {}", wsol_ata);
}
Err(_) => {
// WSOL ATA不存在,创建它
println!("🔨 创建WSOL ATA: {}", wsol_ata);
// 使用seed优化创建WSOL ATA
let create_ata_ixs =
crate::trading::common::wsol_manager::create_wsol_ata(&payer.pubkey());
if !create_ata_ixs.is_empty() {
// 构建并发送交易
use solana_sdk::transaction::Transaction;
let recent_blockhash = rpc.get_latest_blockhash().await.unwrap();
let tx = Transaction::new_signed_with_payer(
&create_ata_ixs,
Some(&payer.pubkey()),
&[payer.as_ref()],
recent_blockhash,
);
match rpc.send_and_confirm_transaction(&tx).await {
Ok(signature) => {
println!("✅ WSOL ATA创建成功: {}", signature);
}
Err(e) => {
// 创建失败,检查是否是因为已存在
match rpc.get_account(&wsol_ata).await {
Ok(_) => {
println!(
"✅ WSOL ATA已存在(交易失败但账户存在): {}",
wsol_ata
);
}
Err(_) => {
// 账户不存在且创建失败 - 这是严重错误,应该让启动失败
panic!(
"❌ WSOL ATA创建失败且账户不存在: {}. 错误: {}",
wsol_ata, e
);
}
}
}
}
} else {
println!("ℹ️ WSOL ATA已存在(无需创建)");
}
}
}
}
let instance = Self {
payer,
rpc,
swqos_clients,
middleware_manager: None,
use_seed_optimize: trade_config.use_seed_optimize,
};
let mut current = INSTANCE.lock();
*current = Some(Arc::new(instance.clone()));
@@ -313,7 +383,7 @@ impl SolanaTrade {
data_size_limit: 256 * 1024,
wait_transaction_confirmed: params.wait_transaction_confirmed,
protocol_params: protocol_params.clone(),
open_seed_optimize: params.open_seed_optimize,
open_seed_optimize: self.use_seed_optimize, // 使用全局seed优化配置
swqos_clients: self.swqos_clients.clone(),
middleware_manager: self.middleware_manager.clone(),
durable_nonce: params.durable_nonce,
@@ -410,7 +480,7 @@ impl SolanaTrade {
wait_transaction_confirmed: params.wait_transaction_confirmed,
protocol_params: protocol_params.clone(),
with_tip: params.with_tip,
open_seed_optimize: params.open_seed_optimize,
open_seed_optimize: self.use_seed_optimize, // 使用全局seed优化配置
swqos_clients: self.swqos_clients.clone(),
middleware_manager: self.middleware_manager.clone(),
durable_nonce: params.durable_nonce,