refactor: split auto_handle_wsol into create/close_wsol_ata params

- Replace auto_handle_wsol with create_wsol_ata and close_wsol_ata
- Enable fine-grained control over wSOL account lifecycle
- Update all trading protocols and documentation
- Add open_seed_optimize support in fast functions

This provides better flexibility for batch operations and advanced trading.
This commit is contained in:
ysq
2025-09-09 17:35:09 +08:00
parent 1c5cfd4511
commit 7d7ee3cfbf
7 changed files with 94 additions and 66 deletions
+14 -5
View File
@@ -47,16 +47,25 @@ sol-trade-sdk = "0.6.1"
### Important Parameter Description
#### auto_handle_wsol Parameter
#### create_wsol_ata and close_wsol_ata Parameters
In PumpSwap, Bonk, and Raydium CPMM trading, the `auto_handle_wsol` parameter is used to automatically handle wSOL (Wrapped SOL):
In PumpSwap, Bonk, and Raydium trading, the `create_wsol_ata` and `close_wsol_ata` parameters provide fine-grained control over wSOL (Wrapped SOL) account management:
- **Mechanism**:
- When `auto_handle_wsol: true`, the SDK automatically handles the conversion between SOL and wSOL
- **create_wsol_ata**:
- When `create_wsol_ata: true`, the SDK automatically creates and wraps SOL to wSOL before trading
- When buying: automatically wraps SOL to wSOL for trading
- When selling: automatically unwraps the received wSOL to SOL
- Default value is `true`
- **close_wsol_ata**:
- When `close_wsol_ata: true`, the SDK automatically closes the wSOL account and unwraps to SOL after trading
- When selling: automatically unwraps the received wSOL to SOL and reclaims rent
- Default value is `true`
- **Benefits of Separate Parameters**:
- Allows independent control of wSOL account creation and closure
- Useful for batch operations where you want to create once and close after multiple transactions
- Provides flexibility for advanced trading strategies
#### lookup_table_key Parameter
The `lookup_table_key` parameter is an optional `Pubkey` that specifies an address lookup table for transaction optimization. You need to use `AddressLookupTableCache` to manage the cached address lookup table before using it.
+14 -5
View File
@@ -47,16 +47,25 @@ sol-trade-sdk = "0.6.1"
### 重要说明
#### auto_handle_wsol 参数
#### create_wsol_ata 和 close_wsol_ata 参数
在 PumpSwap、Bonk、Raydium CPMM 交易中,`auto_handle_wsol` 参数用于自动处理 wSOLWrapped SOL):
在 PumpSwap、Bonk、Raydium 交易中,`create_wsol_ata``close_wsol_ata` 参数提供对 wSOLWrapped SOL账户管理的精细控制
- **作用机制**
-`auto_handle_wsol: true` 时,SDK 会自动处理 SOL wSOL 之间的转换
- **create_wsol_ata**
-`create_wsol_ata: true` 时,SDK 会在交易前自动创建并将 SOL 包装为 wSOL
- 买入时:自动将 SOL 包装为 wSOL 进行交易
- 卖出时:自动将获得的 wSOL 解包装为 SOL
- 默认值为 `true`
- **close_wsol_ata**
-`close_wsol_ata: true` 时,SDK 会在交易后自动关闭 wSOL 账户并解包装为 SOL
- 卖出时:自动将获得的 wSOL 解包装为 SOL 并回收租金
- 默认值为 `true`
- **分离参数的优势**
- 允许独立控制 wSOL 账户的创建和关闭
- 适用于批量操作,可以创建一次,在多次交易后再关闭
- 为高级交易策略提供灵活性
#### lookup_table_key 参数
`lookup_table_key` 参数是一个可选的 `Pubkey`,用于指定地址查找表以优化交易。在使用前你需要通过`AddressLookupTableCache`来管理缓存地址查找表。
+16 -9
View File
@@ -61,16 +61,18 @@ impl InstructionBuilder for BonkInstructionBuilder {
);
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(),
&params.mint,
&protocol_params.mint_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(),
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
);
let base_vault_account = if protocol_params.base_vault == Pubkey::default() {
@@ -89,17 +91,18 @@ impl InstructionBuilder for BonkInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(6);
if protocol_params.auto_handle_wsol {
if protocol_params.create_wsol_ata {
instructions
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
instructions.extend(
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
crate::common::fast_fn::create_associated_token_account_idempotent_fast_use_seed(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&protocol_params.mint_token_program,
params.open_seed_optimize,
),
);
@@ -132,7 +135,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
if protocol_params.auto_handle_wsol {
if protocol_params.close_wsol_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
@@ -187,16 +190,18 @@ impl InstructionBuilder for BonkInstructionBuilder {
);
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(),
&params.mint,
&protocol_params.mint_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(),
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
);
let base_vault_account = if protocol_params.base_vault == Pubkey::default() {
@@ -215,7 +220,9 @@ impl InstructionBuilder for BonkInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
if protocol_params.create_wsol_ata {
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);
@@ -246,7 +253,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
if protocol_params.auto_handle_wsol {
if protocol_params.close_wsol_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
+8 -6
View File
@@ -46,7 +46,8 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
let pool_quote_token_reserves = protocol_params.pool_quote_token_reserves;
let params_coin_creator_vault_ata = protocol_params.coin_creator_vault_ata;
let params_coin_creator_vault_authority = protocol_params.coin_creator_vault_authority;
let auto_handle_wsol = protocol_params.auto_handle_wsol;
let create_wsol_ata = protocol_params.create_wsol_ata;
let close_wsol_ata = protocol_params.close_wsol_ata;
let base_token_program = protocol_params.base_token_program;
let quote_token_program = protocol_params.quote_token_program;
let pool_base_token_account = protocol_params.pool_base_token_account;
@@ -118,7 +119,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(6);
if auto_handle_wsol {
if create_wsol_ata {
instructions
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), sol_amount));
}
@@ -187,7 +188,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
accounts,
data: data.to_vec(),
});
if auto_handle_wsol {
if close_wsol_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
@@ -213,7 +214,8 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
let pool_quote_token_account = protocol_params.pool_quote_token_account;
let params_coin_creator_vault_ata = protocol_params.coin_creator_vault_ata;
let params_coin_creator_vault_authority = protocol_params.coin_creator_vault_authority;
let auto_handle_wsol = protocol_params.auto_handle_wsol;
let create_wsol_ata = protocol_params.create_wsol_ata;
let close_wsol_ata = protocol_params.close_wsol_ata;
let base_token_program = protocol_params.base_token_program;
let quote_token_program = protocol_params.quote_token_program;
@@ -286,7 +288,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
if auto_handle_wsol {
if create_wsol_ata {
instructions.extend(wsol_manager::create_wsol_ata(&params.payer.pubkey()));
}
@@ -346,7 +348,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
data: data.to_vec(),
});
if auto_handle_wsol {
if close_wsol_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
+6 -11
View File
@@ -63,7 +63,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(6);
if protocol_params.auto_handle_wsol {
if protocol_params.create_wsol_ata {
instructions
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
@@ -109,7 +109,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
accounts.to_vec(),
));
if protocol_params.auto_handle_wsol {
if protocol_params.close_wsol_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
@@ -162,14 +162,9 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
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,
),
);
if protocol_params.create_wsol_ata {
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
}
// Create buy instruction
let accounts: [AccountMeta; 17] = [
@@ -203,7 +198,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
accounts.to_vec(),
));
if protocol_params.auto_handle_wsol {
if protocol_params.close_wsol_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
+6 -11
View File
@@ -98,7 +98,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(6);
if protocol_params.auto_handle_wsol {
if protocol_params.create_wsol_ata {
instructions
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
@@ -140,7 +140,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
accounts.to_vec(),
));
if protocol_params.auto_handle_wsol {
if protocol_params.close_wsol_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
@@ -223,14 +223,9 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
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,
),
);
if protocol_params.create_wsol_ata {
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
}
// Create sell instruction
let accounts: [AccountMeta; 13] = [
@@ -260,7 +255,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
accounts.to_vec(),
));
if protocol_params.auto_handle_wsol {
if protocol_params.close_wsol_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
+30 -19
View File
@@ -150,9 +150,8 @@ pub struct PumpSwapParams {
pub base_token_program: Pubkey,
/// Quote token program ID
pub quote_token_program: Pubkey,
/// Automatically handle WSOL wrapping
/// When true, automatically handles wrapping and unwrapping operations between SOL and WSOL
pub auto_handle_wsol: bool,
pub create_wsol_ata: bool,
pub close_wsol_ata: bool,
}
impl PumpSwapParams {
@@ -169,7 +168,8 @@ impl PumpSwapParams {
coin_creator_vault_authority: event.coin_creator_vault_authority,
base_token_program: event.base_token_program,
quote_token_program: event.quote_token_program,
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
}
}
@@ -186,7 +186,8 @@ impl PumpSwapParams {
coin_creator_vault_authority: event.coin_creator_vault_authority,
base_token_program: event.base_token_program,
quote_token_program: event.quote_token_program,
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
}
}
@@ -238,7 +239,8 @@ impl PumpSwapParams {
} else {
crate::constants::TOKEN_PROGRAM_2022
},
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
})
}
}
@@ -270,7 +272,8 @@ pub struct BonkParams {
pub platform_config: Pubkey,
pub platform_associated_account: Pubkey,
pub creator_associated_account: Pubkey,
pub auto_handle_wsol: bool,
pub create_wsol_ata: bool,
pub close_wsol_ata: bool,
}
impl BonkParams {
@@ -281,7 +284,8 @@ impl BonkParams {
creator_associated_account: Pubkey,
) -> Self {
Self {
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
mint_token_program,
platform_config,
platform_associated_account,
@@ -302,7 +306,8 @@ impl BonkParams {
platform_config: trade_info.platform_config,
platform_associated_account: trade_info.platform_associated_account,
creator_associated_account: trade_info.creator_associated_account,
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
}
}
@@ -358,7 +363,8 @@ impl BonkParams {
platform_config: trade_info.platform_config,
platform_associated_account: trade_info.platform_associated_account,
creator_associated_account: trade_info.creator_associated_account,
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
}
}
@@ -394,7 +400,8 @@ impl BonkParams {
platform_config: pool_data.platform_config,
platform_associated_account,
creator_associated_account,
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
})
}
}
@@ -433,8 +440,8 @@ pub struct RaydiumCpmmParams {
pub quote_token_program: Pubkey,
/// Observation state account
pub observation_state: Pubkey,
/// Whether to automatically handle wSOL wrapping and unwrapping
pub auto_handle_wsol: bool,
pub create_wsol_ata: bool,
pub close_wsol_ata: bool,
}
impl RaydiumCpmmParams {
@@ -454,7 +461,8 @@ impl RaydiumCpmmParams {
base_token_program: trade_info.input_token_program,
quote_token_program: trade_info.output_token_program,
observation_state: trade_info.observation_state,
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
}
}
@@ -483,7 +491,8 @@ impl RaydiumCpmmParams {
base_token_program: pool.token0_program,
quote_token_program: pool.token1_program,
observation_state: pool.observation_key,
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
})
}
}
@@ -516,8 +525,8 @@ pub struct RaydiumAmmV4Params {
pub coin_reserve: u64,
/// Current pc reserve amount in the pool
pub pc_reserve: u64,
/// Whether to automatically handle wSOL wrapping and unwrapping
pub auto_handle_wsol: bool,
pub create_wsol_ata: bool,
pub close_wsol_ata: bool,
}
impl RaydiumAmmV4Params {
@@ -535,7 +544,8 @@ impl RaydiumAmmV4Params {
token_pc: amm_info.token_pc,
coin_reserve,
pc_reserve,
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
}
}
pub async fn from_amm_address_by_rpc(
@@ -553,7 +563,8 @@ impl RaydiumAmmV4Params {
token_pc: amm_info.token_pc,
coin_reserve,
pc_reserve,
auto_handle_wsol: true,
create_wsol_ata: true,
close_wsol_ata: true,
})
}
}