refactor: optimize trading execution architecture and add wSOL management

- Simplify TradeExecutor interface by integrating middleware and swqos clients into params
- Refactor parallel execution module with dedicated buy/sell execute functions
- Add SOL wrapping/unwrapping functionality for wSOL management
- Remove redundant parameter passing in sell operations
- Delete example main.rs file
- Optimize transaction building with integrated parameter structures

BREAKING CHANGE: TradeExecutor interface simplified, middleware and swqos_clients now passed through params
This commit is contained in:
ysq
2025-09-09 17:02:24 +08:00
parent b9fa2f2f0f
commit 48a6c6283a
34 changed files with 586 additions and 853 deletions
+11 -9
View File
@@ -94,12 +94,14 @@ impl InstructionBuilder for BonkInstructionBuilder {
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&protocol_params.mint_token_program,
));
instructions.extend(
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&protocol_params.mint_token_program,
),
);
let mut data = [0u8; 32];
data[..8].copy_from_slice(&BUY_EXECT_IN_DISCRIMINATOR);
@@ -131,7 +133,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
if protocol_params.auto_handle_wsol {
instructions.push(crate::trading::common::close_wsol(&params.payer.pubkey()));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
@@ -213,7 +215,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
instructions.push(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
let mut data = [0u8; 32];
data[..8].copy_from_slice(&SELL_EXECT_IN_DISCRIMINATOR);
@@ -245,7 +247,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
if protocol_params.auto_handle_wsol {
instructions.push(crate::trading::common::close_wsol(&params.payer.pubkey()));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
+25 -17
View File
@@ -76,13 +76,16 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
protocol_params.associated_bonding_curve
};
let user_token_account = crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&params.payer.pubkey(),
&params.mint,
&crate::constants::TOKEN_PROGRAM,
);
let user_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.mint,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
);
let user_volume_accumulator = get_user_volume_accumulator_pda(&params.payer.pubkey()).unwrap();
let user_volume_accumulator =
get_user_volume_accumulator_pda(&params.payer.pubkey()).unwrap();
// ========================================
// Build instructions
@@ -90,12 +93,15 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
let mut instructions = Vec::with_capacity(2);
// Create associated token account
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&crate::constants::TOKEN_PROGRAM,
));
instructions.extend(
crate::common::fast_fn::create_associated_token_account_idempotent_fast_use_seed(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
),
);
let mut buy_data = [0u8; 24];
buy_data[..8].copy_from_slice(&[102, 6, 61, 18, 1, 218, 235, 234]); // Method ID
@@ -185,11 +191,13 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
protocol_params.associated_bonding_curve
};
let user_token_account = crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&params.payer.pubkey(),
&params.mint,
&crate::constants::TOKEN_PROGRAM,
);
let user_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.mint,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
);
// ========================================
// Build instructions
+28 -21
View File
@@ -4,9 +4,12 @@ use crate::{
accounts, fee_recipient_ata, get_user_volume_accumulator_pda, BUY_DISCRIMINATOR,
SELL_DISCRIMINATOR,
},
trading::core::{
params::{BuyParams, PumpSwapParams, SellParams},
traits::InstructionBuilder,
trading::{
common::wsol_manager,
core::{
params::{BuyParams, PumpSwapParams, SellParams},
traits::InstructionBuilder,
},
},
utils::calc::pumpswap::{buy_quote_input_internal, sell_base_input_internal},
};
@@ -95,16 +98,18 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
}
let user_base_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&base_mint,
&base_token_program,
params.open_seed_optimize,
);
let user_quote_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&quote_mint,
&quote_token_program,
params.open_seed_optimize,
);
let fee_recipient_ata = fee_recipient_ata(accounts::FEE_RECIPIENT, quote_mint);
@@ -118,12 +123,15 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), sol_amount));
}
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
if quote_mint_is_wsol { &base_mint } else { &quote_mint },
if quote_mint_is_wsol { &base_token_program } else { &quote_token_program },
));
instructions.extend(
crate::common::fast_fn::create_associated_token_account_idempotent_fast_use_seed(
&params.payer.pubkey(),
&params.payer.pubkey(),
if quote_mint_is_wsol { &base_mint } else { &quote_mint },
if quote_mint_is_wsol { &base_token_program } else { &quote_token_program },
params.open_seed_optimize,
),
);
// Create buy instruction
let mut accounts = Vec::with_capacity(23);
@@ -181,7 +189,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
});
if auto_handle_wsol {
// Close wSOL ATA account, reclaim rent
instructions.push(crate::trading::common::close_wsol(&params.payer.pubkey()));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
}
@@ -259,16 +267,18 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
let fee_recipient_ata = fee_recipient_ata(accounts::FEE_RECIPIENT, quote_mint);
let user_base_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&base_mint,
&base_token_program,
params.open_seed_optimize,
);
let user_quote_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&quote_mint,
&quote_token_program,
params.open_seed_optimize,
);
// ========================================
@@ -276,12 +286,9 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
));
if auto_handle_wsol {
instructions.extend(wsol_manager::create_wsol_ata(&params.payer.pubkey()));
}
// Create sell instruction
let mut accounts = Vec::with_capacity(23);
@@ -340,7 +347,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
});
if auto_handle_wsol {
instructions.push(crate::trading::common::close_wsol(&params.payer.pubkey()));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
}
+18 -14
View File
@@ -68,12 +68,14 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&crate::constants::TOKEN_PROGRAM,
));
instructions.extend(
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&crate::constants::TOKEN_PROGRAM,
),
);
// Create buy instruction
let accounts: [AccountMeta; 17] = [
@@ -109,7 +111,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
if protocol_params.auto_handle_wsol {
// Close wSOL ATA account, reclaim rent
instructions.push(crate::trading::common::close_wsol(&params.payer.pubkey()));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
@@ -160,12 +162,14 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
));
instructions.extend(
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
),
);
// Create buy instruction
let accounts: [AccountMeta; 17] = [
@@ -200,7 +204,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
));
if protocol_params.auto_handle_wsol {
instructions.push(crate::trading::common::close_wsol(&params.payer.pubkey()));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
+18 -14
View File
@@ -103,12 +103,14 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&mint_token_program,
));
instructions.extend(
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&mint_token_program,
),
);
// Create buy instruction
let accounts: [AccountMeta; 13] = [
@@ -140,7 +142,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
if protocol_params.auto_handle_wsol {
// Close wSOL ATA account, reclaim rent
instructions.push(crate::trading::common::close_wsol(&params.payer.pubkey()));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
@@ -221,12 +223,14 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
));
instructions.extend(
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
&params.payer.pubkey(),
&params.payer.pubkey(),
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
),
);
// Create sell instruction
let accounts: [AccountMeta; 13] = [
@@ -258,7 +262,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
if protocol_params.auto_handle_wsol {
// Close wSOL ATA account, reclaim rent
instructions.push(crate::trading::common::close_wsol(&params.payer.pubkey()));
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)