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:
+25
-1
@@ -6,6 +6,11 @@ pub struct TradeConfig {
|
||||
pub rpc_url: String,
|
||||
pub swqos_configs: Vec<SwqosConfig>,
|
||||
pub commitment: CommitmentConfig,
|
||||
/// Whether to create WSOL ATA on startup (default: true)
|
||||
/// If true, SDK will check WSOL ATA on initialization and create if not exists
|
||||
pub create_wsol_ata_on_startup: bool,
|
||||
/// Whether to use seed optimization for all ATA operations (default: true)
|
||||
pub use_seed_optimize: bool,
|
||||
}
|
||||
|
||||
impl TradeConfig {
|
||||
@@ -14,7 +19,26 @@ impl TradeConfig {
|
||||
swqos_configs: Vec<SwqosConfig>,
|
||||
commitment: CommitmentConfig,
|
||||
) -> Self {
|
||||
Self { rpc_url, swqos_configs, commitment }
|
||||
println!("🔧 TradeConfig create_wsol_ata_on_startup default value: true");
|
||||
println!("🔧 TradeConfig use_seed_optimize default value: true");
|
||||
Self {
|
||||
rpc_url,
|
||||
swqos_configs,
|
||||
commitment,
|
||||
create_wsol_ata_on_startup: true, // 默认:启动时检查并创建
|
||||
use_seed_optimize: true, // 默认:使用seed优化
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a TradeConfig with custom WSOL ATA settings
|
||||
pub fn with_wsol_ata_config(
|
||||
mut self,
|
||||
create_wsol_ata_on_startup: bool,
|
||||
use_seed_optimize: bool,
|
||||
) -> Self {
|
||||
self.create_wsol_ata_on_startup = create_wsol_ata_on_startup;
|
||||
self.use_seed_optimize = use_seed_optimize;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+77
-7
@@ -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,
|
||||
|
||||
@@ -68,3 +68,27 @@ pub fn create_wsol_ata(payer: &Pubkey) -> Vec<Instruction> {
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
)
|
||||
}
|
||||
|
||||
/// 只充值SOL到已存在的WSOL ATA(不创建账户)- 标准方式
|
||||
#[inline]
|
||||
pub fn wrap_sol_only(payer: &Pubkey, amount_in: u64) -> SmallVec<[Instruction; 2]> {
|
||||
let wsol_token_account =
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
||||
&payer,
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
);
|
||||
|
||||
let mut insts = SmallVec::<[Instruction; 2]>::new();
|
||||
insts.extend([
|
||||
transfer(&payer, &wsol_token_account, amount_in),
|
||||
// sync_native
|
||||
Instruction {
|
||||
program_id: crate::constants::TOKEN_PROGRAM,
|
||||
accounts: vec![AccountMeta::new(wsol_token_account, false)],
|
||||
data: vec![17],
|
||||
},
|
||||
]);
|
||||
|
||||
insts
|
||||
}
|
||||
|
||||
@@ -74,24 +74,6 @@ impl PreallocatedTxBuilder {
|
||||
|
||||
// ✅ 如果有查找表,使用 V0 消息
|
||||
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));
|
||||
|
||||
// // 构建 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,
|
||||
|
||||
Reference in New Issue
Block a user