Revert "refactor: split seed optimization config into wsol_use_seed and mint_use_seed"

This reverts commit eb8de36f50.
This commit is contained in:
Wood
2025-11-22 01:41:06 +08:00
parent eb8de36f50
commit ce83b5ae68
11 changed files with 131 additions and 192 deletions
+17 -10
View File
@@ -9,10 +9,8 @@ pub struct TradeConfig {
/// 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 WSOL ATA operations (default: false)
pub wsol_use_seed: bool,
/// Whether to use seed optimization for other mint ATA operations (default: true)
pub mint_use_seed: bool,
/// Whether to use seed optimization for all ATA operations (default: true)
pub use_seed_optimize: bool,
}
impl TradeConfig {
@@ -20,19 +18,28 @@ impl TradeConfig {
rpc_url: String,
swqos_configs: Vec<SwqosConfig>,
commitment: CommitmentConfig,
create_wsol_ata_on_startup: bool,
wsol_use_seed: bool,
mint_use_seed: bool,
) -> Self {
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,
wsol_use_seed,
mint_use_seed,
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
}
}
pub type SolanaRpcClient = solana_client::nonblocking::rpc_client::RpcClient;
+9 -9
View File
@@ -86,7 +86,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
&params.payer.pubkey(),
&params.output_mint,
&protocol_params.mint_token_program,
params.mint_use_seed,
params.open_seed_optimize,
);
let user_quote_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
@@ -97,7 +97,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
&crate::constants::WSOL_TOKEN_ACCOUNT
},
&crate::constants::TOKEN_PROGRAM,
if usd1_pool { params.mint_use_seed } else { params.wsol_use_seed },
params.open_seed_optimize,
);
let base_vault_account = if protocol_params.base_vault == Pubkey::default() {
@@ -122,7 +122,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
if params.create_input_mint_ata && !usd1_pool {
instructions
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in, params.wsol_use_seed));
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
if params.create_output_mint_ata {
@@ -132,7 +132,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
&params.payer.pubkey(),
&params.output_mint,
&protocol_params.mint_token_program,
params.mint_use_seed,
params.open_seed_optimize,
),
);
}
@@ -167,7 +167,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
if params.close_input_mint_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
@@ -246,7 +246,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
&params.payer.pubkey(),
&params.input_mint,
&protocol_params.mint_token_program,
params.mint_use_seed,
params.open_seed_optimize,
);
let user_quote_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
@@ -257,7 +257,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
&crate::constants::WSOL_TOKEN_ACCOUNT
},
&crate::constants::TOKEN_PROGRAM,
params.wsol_use_seed,
params.open_seed_optimize,
);
let base_vault_account = if protocol_params.base_vault == Pubkey::default() {
@@ -281,7 +281,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
let mut instructions = Vec::with_capacity(3);
if params.close_output_mint_ata && !usd1_pool {
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
}
let mut data = [0u8; 32];
@@ -314,7 +314,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
if params.close_output_mint_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
if params.close_input_mint_ata {
instructions.push(crate::common::spl_token::close_account(
+9 -9
View File
@@ -54,7 +54,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
} else {
&protocol_params.token_b_program
},
params.wsol_use_seed,
params.open_seed_optimize,
);
let output_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
@@ -65,7 +65,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
} else {
&protocol_params.token_a_program
},
params.mint_use_seed,
params.open_seed_optimize,
);
// ========================================
@@ -75,7 +75,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
if params.create_input_mint_ata {
instructions
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in, params.wsol_use_seed));
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
if params.create_output_mint_ata {
@@ -85,7 +85,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
&params.payer.pubkey(),
&params.output_mint,
&crate::constants::TOKEN_PROGRAM,
params.mint_use_seed,
params.open_seed_optimize,
),
);
}
@@ -121,7 +121,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
if params.close_input_mint_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
@@ -165,7 +165,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
} else {
&protocol_params.token_b_program
},
params.mint_use_seed,
params.open_seed_optimize,
);
let output_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
@@ -176,7 +176,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
} else {
&protocol_params.token_a_program
},
params.wsol_use_seed,
params.open_seed_optimize,
);
// ========================================
@@ -185,7 +185,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
let mut instructions = Vec::with_capacity(3);
if params.create_output_mint_ata {
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
}
// Create buy instruction
@@ -218,7 +218,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
));
if params.close_output_mint_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
if params.close_input_mint_ata {
instructions.push(crate::common::spl_token::close_account(
+3 -3
View File
@@ -93,7 +93,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
&params.payer.pubkey(),
&params.output_mint,
&token_program,
params.mint_use_seed,
params.open_seed_optimize,
);
let user_volume_accumulator =
@@ -112,7 +112,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
&params.payer.pubkey(),
&params.output_mint,
&token_program,
params.mint_use_seed,
params.open_seed_optimize,
),
);
}
@@ -229,7 +229,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
&params.payer.pubkey(),
&params.input_mint,
&token_program,
params.mint_use_seed,
params.open_seed_optimize,
);
// ========================================
+9 -9
View File
@@ -108,14 +108,14 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
&params.payer.pubkey(),
&base_mint,
&base_token_program,
params.mint_use_seed,
params.open_seed_optimize,
);
let user_quote_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&quote_mint,
&quote_token_program,
params.mint_use_seed,
params.open_seed_optimize,
);
// Determine fee recipient based on mayhem mode
@@ -136,7 +136,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
if create_wsol_ata {
instructions
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), sol_amount, params.wsol_use_seed));
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), sol_amount));
}
if params.create_output_mint_ata {
@@ -146,7 +146,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
&params.payer.pubkey(),
if quote_is_wsol_or_usdc { &base_mint } else { &quote_mint },
if quote_is_wsol_or_usdc { &base_token_program } else { &quote_token_program },
params.mint_use_seed,
params.open_seed_optimize,
),
);
}
@@ -209,7 +209,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
instructions.push(buy_instruction);
if close_wsol_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
}
@@ -308,14 +308,14 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
&params.payer.pubkey(),
&base_mint,
&base_token_program,
params.mint_use_seed,
params.open_seed_optimize,
);
let user_quote_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&quote_mint,
&quote_token_program,
params.mint_use_seed,
params.open_seed_optimize,
);
// ========================================
@@ -324,7 +324,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
let mut instructions = Vec::with_capacity(3);
if create_wsol_ata {
instructions.extend(wsol_manager::create_wsol_ata(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(wsol_manager::create_wsol_ata(&params.payer.pubkey()));
}
// Create sell instruction
@@ -386,7 +386,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
instructions.push(sell_instruction);
if close_wsol_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
if params.close_input_mint_ata {
instructions.push(crate::common::spl_token::close_account(
+9 -9
View File
@@ -64,14 +64,14 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
&params.payer.pubkey(),
if is_wsol { &crate::constants::WSOL_TOKEN_ACCOUNT } else { &crate::constants::USDC_TOKEN_ACCOUNT },
&crate::constants::TOKEN_PROGRAM,
params.wsol_use_seed,
params.open_seed_optimize,
);
let user_destination_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.output_mint,
&crate::constants::TOKEN_PROGRAM,
params.mint_use_seed,
params.open_seed_optimize,
);
// ========================================
@@ -81,7 +81,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
if params.create_input_mint_ata {
instructions
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in, params.wsol_use_seed));
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
if params.create_output_mint_ata {
@@ -91,7 +91,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
&params.payer.pubkey(),
&params.output_mint,
&crate::constants::TOKEN_PROGRAM,
params.mint_use_seed,
params.open_seed_optimize,
),
);
}
@@ -130,7 +130,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
if params.close_input_mint_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
@@ -182,14 +182,14 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
&params.payer.pubkey(),
&params.input_mint,
&crate::constants::TOKEN_PROGRAM,
params.mint_use_seed,
params.open_seed_optimize,
);
let user_destination_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
if is_wsol { &crate::constants::WSOL_TOKEN_ACCOUNT } else { &crate::constants::USDC_TOKEN_ACCOUNT },
&crate::constants::TOKEN_PROGRAM,
params.wsol_use_seed,
params.open_seed_optimize,
);
// ========================================
@@ -198,7 +198,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
let mut instructions = Vec::with_capacity(3);
if params.create_output_mint_ata {
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
}
// Create buy instruction
@@ -234,7 +234,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
));
if params.close_output_mint_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
if params.close_input_mint_ata {
instructions.push(crate::common::spl_token::close_account(
+9 -9
View File
@@ -86,13 +86,13 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
&params.payer.pubkey(),
if is_wsol { &crate::constants::WSOL_TOKEN_ACCOUNT } else { &crate::constants::USDC_TOKEN_ACCOUNT },
&crate::constants::TOKEN_PROGRAM,
params.wsol_use_seed,
params.open_seed_optimize,
);
let output_token_account = get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.output_mint,
&mint_token_program,
params.mint_use_seed,
params.open_seed_optimize,
);
let input_vault_account = get_vault_account(
@@ -115,7 +115,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
if params.create_input_mint_ata {
instructions
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in, params.wsol_use_seed));
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
if params.create_output_mint_ata {
@@ -125,7 +125,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
&params.payer.pubkey(),
&params.output_mint,
&mint_token_program,
params.mint_use_seed,
params.open_seed_optimize,
),
);
}
@@ -160,7 +160,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
if params.close_input_mint_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
@@ -230,13 +230,13 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
&params.payer.pubkey(),
if is_wsol { &crate::constants::WSOL_TOKEN_ACCOUNT } else { &crate::constants::USDC_TOKEN_ACCOUNT },
&crate::constants::TOKEN_PROGRAM,
params.wsol_use_seed,
params.open_seed_optimize,
);
let input_token_account = get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.input_mint,
&mint_token_program,
params.mint_use_seed,
params.open_seed_optimize,
);
let output_vault_account = get_vault_account(
@@ -258,7 +258,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
let mut instructions = Vec::with_capacity(3);
if params.create_output_mint_ata {
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
}
// Create sell instruction
@@ -291,7 +291,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
if params.close_output_mint_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey(), params.wsol_use_seed));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
if params.close_input_mint_ata {
instructions.push(crate::common::spl_token::close_account(
+23 -36
View File
@@ -59,10 +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 WSOL ATA operations (default: false)
pub wsol_use_seed: bool,
/// Whether to use seed optimization for other mint ATA operations (default: true)
pub mint_use_seed: bool,
/// 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);
@@ -74,8 +73,7 @@ impl Clone for SolanaTrade {
rpc: self.rpc.clone(),
swqos_clients: self.swqos_clients.clone(),
middleware_manager: self.middleware_manager.clone(),
wsol_use_seed: self.wsol_use_seed,
mint_use_seed: self.mint_use_seed,
use_seed_optimize: self.use_seed_optimize,
}
}
}
@@ -225,9 +223,9 @@ impl SolanaTrade {
Err(_) => {
// WSOL ATA不存在,创建它
println!("🔨 创建WSOL ATA: {}", wsol_ata);
// 使用配置中的wsol_use_seed设置创建WSOL ATA
// 使用seed优化创建WSOL ATA
let create_ata_ixs =
crate::trading::common::wsol_manager::create_wsol_ata(&payer.pubkey(), trade_config.wsol_use_seed);
crate::trading::common::wsol_manager::create_wsol_ata(&payer.pubkey());
if !create_ata_ixs.is_empty() {
// 构建并发送交易
@@ -275,8 +273,7 @@ impl SolanaTrade {
rpc,
swqos_clients,
middleware_manager: None,
wsol_use_seed: trade_config.wsol_use_seed,
mint_use_seed: trade_config.mint_use_seed,
use_seed_optimize: trade_config.use_seed_optimize,
};
let mut current = INSTANCE.lock();
@@ -389,8 +386,7 @@ impl SolanaTrade {
.unwrap_or(256 * 1024),
wait_transaction_confirmed: params.wait_transaction_confirmed,
protocol_params: protocol_params.clone(),
wsol_use_seed: self.wsol_use_seed, // 使用wsol_use_seed配置
mint_use_seed: self.mint_use_seed, // 使用mint_use_seed配置
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,
@@ -487,8 +483,7 @@ impl SolanaTrade {
wait_transaction_confirmed: params.wait_transaction_confirmed,
protocol_params: protocol_params.clone(),
with_tip: params.with_tip,
wsol_use_seed: self.wsol_use_seed, // 使用wsol_use_seed配置
mint_use_seed: self.mint_use_seed, // 使用mint_use_seed配置
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,
@@ -579,7 +574,6 @@ impl SolanaTrade {
///
/// # Arguments
/// * `amount` - The amount of SOL to wrap (in lamports)
/// * `is_use_seed` - Whether to use seed optimization for WSOL ATA creation
///
/// # Returns
/// * `Ok(String)` - Transaction signature if successful
@@ -592,11 +586,11 @@ impl SolanaTrade {
/// - wSOL associated token account creation fails
/// - Transaction fails to execute or confirm
/// - Network or RPC errors occur
pub async fn wrap_sol_to_wsol(&self, amount: u64, is_use_seed: bool) -> Result<String, anyhow::Error> {
pub async fn wrap_sol_to_wsol(&self, amount: u64) -> Result<String, anyhow::Error> {
use crate::trading::common::wsol_manager::handle_wsol;
use solana_sdk::transaction::Transaction;
let recent_blockhash = self.rpc.get_latest_blockhash().await?;
let instructions = handle_wsol(&self.payer.pubkey(), amount, is_use_seed);
let instructions = handle_wsol(&self.payer.pubkey(), amount);
let mut transaction =
Transaction::new_with_payer(&instructions, Some(&self.payer.pubkey()));
transaction.sign(&[&*self.payer], recent_blockhash);
@@ -609,9 +603,6 @@ impl SolanaTrade {
/// transfers any remaining wSOL balance back to the account owner as native SOL.
/// This is useful for cleaning up wSOL accounts and recovering wrapped SOL after trading operations.
///
/// # Arguments
/// * `is_use_seed` - Whether the WSOL ATA was created using seed optimization
///
/// # Returns
/// * `Ok(String)` - Transaction signature if successful
/// * `Err(anyhow::Error)` - If the transaction fails to execute
@@ -623,11 +614,11 @@ impl SolanaTrade {
/// - Account closure fails due to insufficient permissions
/// - Transaction fails to execute or confirm
/// - Network or RPC errors occur
pub async fn close_wsol(&self, is_use_seed: bool) -> Result<String, anyhow::Error> {
pub async fn close_wsol(&self) -> Result<String, anyhow::Error> {
use crate::trading::common::wsol_manager::close_wsol;
use solana_sdk::transaction::Transaction;
let recent_blockhash = self.rpc.get_latest_blockhash().await?;
let instructions = close_wsol(&self.payer.pubkey(), is_use_seed);
let instructions = close_wsol(&self.payer.pubkey());
let mut transaction =
Transaction::new_with_payer(&instructions, Some(&self.payer.pubkey()));
transaction.sign(&[&*self.payer], recent_blockhash);
@@ -641,9 +632,6 @@ impl SolanaTrade {
/// without transferring any SOL into it. This is useful when you want to set up
/// the account infrastructure in advance without committing funds yet.
///
/// # Arguments
/// * `is_use_seed` - Whether to use seed optimization for WSOL ATA creation
///
/// # Returns
/// * `Ok(String)` - Transaction signature if successful
/// * `Err(anyhow::Error)` - If the transaction fails to execute
@@ -655,12 +643,12 @@ impl SolanaTrade {
/// - Transaction fails to execute or confirm
/// - Network or RPC errors occur
/// - Insufficient SOL for transaction fees
pub async fn create_wsol_ata(&self, is_use_seed: bool) -> Result<String, anyhow::Error> {
pub async fn create_wsol_ata(&self) -> Result<String, anyhow::Error> {
use crate::trading::common::wsol_manager::create_wsol_ata;
use solana_sdk::transaction::Transaction;
let recent_blockhash = self.rpc.get_latest_blockhash().await?;
let instructions = create_wsol_ata(&self.payer.pubkey(), is_use_seed);
let instructions = create_wsol_ata(&self.payer.pubkey());
// If instructions are empty, ATA already exists
if instructions.is_empty() {
@@ -676,17 +664,16 @@ impl SolanaTrade {
Ok(signature.to_string())
}
/// 将 WSOL 转换为 SOL,使用临时账户
/// 将 WSOL 转换为 SOL,使用 seed 账户
///
/// 这个函数实现以下步骤:
/// 1. 创建临时 WSOL 账号(如果原始账号是seed创建,则临时账号使用普通ATA;否则使用seed)
/// 2. 获取临时账号的 ATA 地址
/// 3. 添加从用户 WSOL ATA 转账到临时 ATA 账号的指令
/// 4. 添加关闭临时 WSOL 账号的指令
/// 1. 使用 super::seed::create_associated_token_account_use_seed 创建 WSOL seed 账号
/// 2. 使用 get_associated_token_address_with_program_id_use_seed 获取该账号的 ATA 地址
/// 3. 添加从用户 WSOL ATA 转账到该 seed ATA 账号的指令
/// 4. 添加关闭 WSOL seed 账号的指令
///
/// # Arguments
/// * `amount` - 要转换的 WSOL 数量(以 lamports 为单位)
/// * `source_is_seed` - 原始 WSOL 账号是否是 seed 创建的
///
/// # Returns
/// * `Ok(String)` - 交易签名
@@ -696,16 +683,16 @@ impl SolanaTrade {
///
/// 此函数在以下情况下会返回错误:
/// - 用户 WSOL ATA 中余额不足
/// - 临时账户创建失败
/// - seed 账户创建失败
/// - 转账指令执行失败
/// - 交易执行或确认失败
/// - 网络或 RPC 错误
pub async fn wrap_wsol_to_sol(&self, amount: u64, source_is_seed: bool) -> Result<String, anyhow::Error> {
pub async fn wrap_wsol_to_sol(&self, amount: u64) -> Result<String, anyhow::Error> {
use crate::trading::common::wsol_manager::wrap_wsol_to_sol as wrap_wsol_to_sol_internal;
use solana_sdk::transaction::Transaction;
let recent_blockhash = self.rpc.get_latest_blockhash().await?;
let instructions = wrap_wsol_to_sol_internal(&self.payer.pubkey(), amount, source_is_seed)?;
let instructions = wrap_wsol_to_sol_internal(&self.payer.pubkey(), amount)?;
let mut transaction = Transaction::new_with_payer(&instructions, Some(&self.payer.pubkey()));
transaction.sign(&[&*self.payer], recent_blockhash);
+34 -76
View File
@@ -8,21 +8,13 @@ use solana_sdk::{instruction::Instruction, message::AccountMeta, pubkey::Pubkey}
use solana_system_interface::instruction::transfer;
#[inline]
pub fn handle_wsol(payer: &Pubkey, amount_in: u64, is_use_seed: bool) -> SmallVec<[Instruction; 3]> {
let wsol_token_account = if is_use_seed {
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
true,
)
} else {
pub fn handle_wsol(payer: &Pubkey, amount_in: u64) -> SmallVec<[Instruction; 3]> {
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; 3]>::new();
insts.extend(create_associated_token_account_idempotent_fast(
@@ -44,21 +36,13 @@ pub fn handle_wsol(payer: &Pubkey, amount_in: u64, is_use_seed: bool) -> SmallVe
insts
}
pub fn close_wsol(payer: &Pubkey, is_use_seed: bool) -> Vec<Instruction> {
let wsol_token_account = if is_use_seed {
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
true,
)
} else {
pub fn close_wsol(payer: &Pubkey) -> Vec<Instruction> {
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,
)
};
);
crate::common::fast_fn::get_cached_instructions(
crate::common::fast_fn::InstructionCacheKey::CloseWsolAccount {
payer: *payer,
@@ -78,33 +62,24 @@ pub fn close_wsol(payer: &Pubkey, is_use_seed: bool) -> Vec<Instruction> {
}
#[inline]
pub fn create_wsol_ata(payer: &Pubkey, is_use_seed: bool) -> Vec<Instruction> {
crate::common::fast_fn::create_associated_token_account_idempotent_fast_use_seed(
pub fn create_wsol_ata(payer: &Pubkey) -> Vec<Instruction> {
create_associated_token_account_idempotent_fast(
&payer,
&payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
is_use_seed,
)
}
/// 只充值SOL到已存在的WSOL ATA(不创建账户)- 标准方式
#[inline]
pub fn wrap_sol_only(payer: &Pubkey, amount_in: u64, is_use_seed: bool) -> SmallVec<[Instruction; 2]> {
let wsol_token_account = if is_use_seed {
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
true,
)
} else {
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([
@@ -120,72 +95,55 @@ pub fn wrap_sol_only(payer: &Pubkey, amount_in: u64, is_use_seed: bool) -> Small
insts
}
/// 将 WSOL 转换为 SOL,使用临时账户
/// 1. 创建临时 WSOL 账号(如果原始账号是seed创建,则临时账号使用普通ATA;否则使用seed)
/// 2. 获取临时账号的 ATA 地址
/// 3. 添加从用户 WSOL ATA 转账到临时 ATA 账号的指令
/// 4. 添加关闭临时 WSOL 账号的指令
///
/// # Arguments
/// * `payer` - 支付者公钥
/// * `amount` - 要转换的 WSOL 数量
/// * `source_is_seed` - 原始 WSOL 账号是否是 seed 创建的
/// 将 WSOL 转换为 SOL,使用 seed 账户
/// 1. 使用 super::seed::create_associated_token_account_use_seed 创建 WSOL seed 账号
/// 2. 使用 get_associated_token_address_with_program_id_use_seed 获取该账号的 ATA 地址
/// 3. 添加从用户 WSOL ATA 转账到该 seed ATA 账号的指令
/// 4. 添加关闭 WSOL seed 账号的指令
pub fn wrap_wsol_to_sol(
payer: &Pubkey,
amount: u64,
source_is_seed: bool,
) -> Result<Vec<Instruction>, anyhow::Error> {
let mut instructions = Vec::new();
// 1. 分别获取普通ATA和seed ATA地址
let normal_ata = crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
// 1. 创建 WSOL seed 账户
let seed_account_instructions = create_associated_token_account_use_seed(
payer,
payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
);
let seed_ata = get_associated_token_address_with_program_id_use_seed(
)?;
instructions.extend(seed_account_instructions);
// 2. 获取 seed 账户的 ATA 地址
let seed_ata_address = get_associated_token_address_with_program_id_use_seed(
payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
)?;
// 2. 根据原始账号类型决定用户账号和临时账号
// 如果原始账号是seed,则用户账号=seed_ata,临时账号=normal_ata(避免地址冲突)
// 如果原始账号是普通ATA,则用户账号=normal_ata,临时账号=seed_ata(提高性能)
let (user_wsol_ata, temp_ata_address, temp_account_instructions) = if source_is_seed {
let create_insts = crate::common::fast_fn::create_associated_token_account_idempotent_fast(
payer,
payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
);
(seed_ata, normal_ata, create_insts)
} else {
let create_insts = create_associated_token_account_use_seed(
payer,
payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
)?;
(normal_ata, seed_ata, create_insts)
};
instructions.extend(temp_account_instructions);
// 3. 获取用户的 WSOL ATA 地址
let user_wsol_ata = crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
);
// 3. 添加从用户 WSOL ATA 转账到临时 ATA 的指令
// 4. 添加从用户 WSOL ATA 转账到 seed ATA 的指令
let transfer_instruction = crate::common::spl_token::transfer(
&crate::constants::TOKEN_PROGRAM,
&user_wsol_ata,
&temp_ata_address,
&seed_ata_address,
payer,
amount,
&[],
)?;
instructions.push(transfer_instruction);
// 4. 添加关闭临时 WSOL 账户的指令
// 5. 添加关闭 WSOL seed 账户的指令
let close_instruction = close_account(
&crate::constants::TOKEN_PROGRAM,
&temp_ata_address,
&seed_ata_address,
payer,
payer,
&[],
+1 -2
View File
@@ -31,8 +31,7 @@ pub struct SwapParams {
pub data_size_limit: u32,
pub wait_transaction_confirmed: bool,
pub protocol_params: Box<dyn ProtocolParams>,
pub wsol_use_seed: bool,
pub mint_use_seed: bool,
pub open_seed_optimize: bool,
pub swqos_clients: Vec<Arc<SwqosClient>>,
pub middleware_manager: Option<Arc<MiddlewareManager>>,
pub durable_nonce: Option<DurableNonceInfo>,