refactor: unify trading parameters and add USD1 token pool support

- Merge BuyParams and SellParams into unified SwapParams structure
- Add USD1 token pool support with related constants and configurations
- Refactor trade executor by combining buy_with_tip and sell_with_tip into swap method
- Update all protocol instruction builders to support new parameter structure
- Standardize ATA creation/closing parameter naming conventions
- Update example code to use new API interfaces
- Optimize trading logic with automatic direction detection based on input token type
This commit is contained in:
ysq
2025-09-22 00:05:20 +08:00
parent f8891f3147
commit 4780e71c11
16 changed files with 274 additions and 235 deletions
+82 -30
View File
@@ -7,7 +7,7 @@ use crate::{
trading::{
common::utils::get_token_balance,
core::{
params::{BonkParams, BuyParams, SellParams},
params::{BonkParams, SwapParams},
traits::InstructionBuilder,
},
},
@@ -27,11 +27,11 @@ pub struct BonkInstructionBuilder;
#[async_trait::async_trait]
impl InstructionBuilder for BonkInstructionBuilder {
async fn build_buy_instructions(&self, params: &BuyParams) -> Result<Vec<Instruction>> {
async fn build_buy_instructions(&self, params: &SwapParams) -> Result<Vec<Instruction>> {
// ========================================
// Parameter validation and basic data preparation
// ========================================
if params.sol_amount == 0 {
if params.input_amount.unwrap_or(0) == 0 {
return Err(anyhow!("Amount cannot be zero"));
}
let protocol_params = params
@@ -40,16 +40,34 @@ impl InstructionBuilder for BonkInstructionBuilder {
.downcast_ref::<BonkParams>()
.ok_or_else(|| anyhow!("Invalid protocol params for Bonk"))?;
let usd1_pool = protocol_params.global_config == accounts::USD1_GLOBAL_CONFIG;
let pool_state = if protocol_params.pool_state == Pubkey::default() {
get_pool_pda(&params.mint, &crate::constants::WSOL_TOKEN_ACCOUNT).unwrap()
if usd1_pool {
get_pool_pda(&params.output_mint, &crate::constants::USD1_TOKEN_ACCOUNT).unwrap()
} else {
get_pool_pda(&params.output_mint, &crate::constants::WSOL_TOKEN_ACCOUNT).unwrap()
}
} else {
protocol_params.pool_state
};
let global_config = if usd1_pool {
accounts::USD1_GLOBAL_CONFIG_META
} else {
accounts::GLOBAL_CONFIG_META
};
let quote_token_mint = if usd1_pool {
crate::constants::USD1_TOKEN_ACCOUNT_META
} else {
crate::constants::WSOL_TOKEN_ACCOUNT_META
};
// ========================================
// Trade calculation and account address preparation
// ========================================
let amount_in: u64 = params.sol_amount;
let amount_in: u64 = params.input_amount.unwrap_or(0);
let share_fee_rate: u64 = 0;
let minimum_amount_out: u64 = get_buy_token_amount_from_sol_amount(
amount_in,
@@ -63,25 +81,33 @@ impl InstructionBuilder for BonkInstructionBuilder {
let user_base_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.mint,
&params.output_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_use_seed(
&params.payer.pubkey(),
&crate::constants::WSOL_TOKEN_ACCOUNT,
if usd1_pool {
&crate::constants::USD1_TOKEN_ACCOUNT
} else {
&crate::constants::WSOL_TOKEN_ACCOUNT
},
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
);
let base_vault_account = if protocol_params.base_vault == Pubkey::default() {
get_vault_pda(&pool_state, &params.mint).unwrap()
get_vault_pda(&pool_state, &params.output_mint).unwrap()
} else {
protocol_params.base_vault
};
let quote_vault_account = if protocol_params.quote_vault == Pubkey::default() {
get_vault_pda(&pool_state, &crate::constants::WSOL_TOKEN_ACCOUNT).unwrap()
if usd1_pool {
get_vault_pda(&pool_state, &crate::constants::USD1_TOKEN_ACCOUNT).unwrap()
} else {
get_vault_pda(&pool_state, &crate::constants::WSOL_TOKEN_ACCOUNT).unwrap()
}
} else {
protocol_params.quote_vault
};
@@ -91,17 +117,17 @@ impl InstructionBuilder for BonkInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(6);
if params.create_wsol_ata {
if params.create_input_mint_ata && !usd1_pool {
instructions
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), amount_in));
}
if params.create_mint_ata {
if params.create_output_mint_ata {
instructions.extend(
crate::common::fast_fn::create_associated_token_account_idempotent_fast_use_seed(
&params.payer.pubkey(),
&params.payer.pubkey(),
&params.mint,
&params.output_mint,
&protocol_params.mint_token_program,
params.open_seed_optimize,
),
@@ -117,15 +143,15 @@ impl InstructionBuilder for BonkInstructionBuilder {
let accounts: [AccountMeta; 18] = [
AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
accounts::AUTHORITY_META, // Authority (readonly)
accounts::GLOBAL_CONFIG_META, // Global Config (readonly)
global_config, // Global Config (readonly)
AccountMeta::new_readonly(protocol_params.platform_config, false), // Platform Config (readonly)
AccountMeta::new(pool_state, false), // Pool State
AccountMeta::new(user_base_token_account, false), // User Base Token
AccountMeta::new(user_quote_token_account, false), // User Quote Token
AccountMeta::new(base_vault_account, false), // Base Vault
AccountMeta::new(quote_vault_account, false), // Quote Vault
AccountMeta::new_readonly(params.mint, false), // Base Token Mint (readonly)
crate::constants::WSOL_TOKEN_ACCOUNT_META, // Quote Token Mint (readonly)
AccountMeta::new_readonly(params.output_mint, false), // Base Token Mint (readonly)
quote_token_mint, // Quote Token Mint (readonly)
AccountMeta::new_readonly(protocol_params.mint_token_program, false), // Base Token Program (readonly)
crate::constants::TOKEN_PROGRAM_META, // Quote Token Program (readonly)
accounts::EVENT_AUTHORITY_META, // Event Authority (readonly)
@@ -137,14 +163,14 @@ impl InstructionBuilder for BonkInstructionBuilder {
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
if params.close_wsol_ata {
if params.close_input_mint_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
Ok(instructions)
}
async fn build_sell_instructions(&self, params: &SellParams) -> Result<Vec<Instruction>> {
async fn build_sell_instructions(&self, params: &SwapParams) -> Result<Vec<Instruction>> {
// ========================================
// Parameter validation and basic data preparation
// ========================================
@@ -158,12 +184,14 @@ impl InstructionBuilder for BonkInstructionBuilder {
.downcast_ref::<BonkParams>()
.ok_or_else(|| anyhow!("Invalid protocol params for Bonk"))?;
let usd1_pool = protocol_params.global_config == accounts::USD1_GLOBAL_CONFIG;
let rpc = params.rpc.as_ref().unwrap().clone();
let mut amount = params.token_amount;
if params.token_amount.is_none() || params.token_amount.unwrap_or(0) == 0 {
let mut amount = params.input_amount;
if params.input_amount.is_none() || params.input_amount.unwrap_or(0) == 0 {
let balance_u64 =
get_token_balance(rpc.as_ref(), &params.payer.pubkey(), &params.mint).await?;
get_token_balance(rpc.as_ref(), &params.payer.pubkey(), &params.input_mint).await?;
amount = Some(balance_u64);
}
let amount = amount.unwrap_or(0);
@@ -173,11 +201,27 @@ impl InstructionBuilder for BonkInstructionBuilder {
}
let pool_state = if protocol_params.pool_state == Pubkey::default() {
get_pool_pda(&params.mint, &crate::constants::WSOL_TOKEN_ACCOUNT).unwrap()
if usd1_pool {
get_pool_pda(&params.input_mint, &crate::constants::USD1_TOKEN_ACCOUNT).unwrap()
} else {
get_pool_pda(&params.input_mint, &crate::constants::WSOL_TOKEN_ACCOUNT).unwrap()
}
} else {
protocol_params.pool_state
};
let global_config = if usd1_pool {
accounts::USD1_GLOBAL_CONFIG_META
} else {
accounts::GLOBAL_CONFIG_META
};
let quote_token_mint = if usd1_pool {
crate::constants::USD1_TOKEN_ACCOUNT_META
} else {
crate::constants::WSOL_TOKEN_ACCOUNT_META
};
// ========================================
// Trade calculation and account address preparation
// ========================================
@@ -194,25 +238,33 @@ impl InstructionBuilder for BonkInstructionBuilder {
let user_base_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.mint,
&params.input_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_use_seed(
&params.payer.pubkey(),
&crate::constants::WSOL_TOKEN_ACCOUNT,
if usd1_pool {
&crate::constants::USD1_TOKEN_ACCOUNT
} else {
&crate::constants::WSOL_TOKEN_ACCOUNT
},
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
);
let base_vault_account = if protocol_params.base_vault == Pubkey::default() {
get_vault_pda(&pool_state, &params.mint).unwrap()
get_vault_pda(&pool_state, &params.input_mint).unwrap()
} else {
protocol_params.base_vault
};
let quote_vault_account = if protocol_params.quote_vault == Pubkey::default() {
get_vault_pda(&pool_state, &crate::constants::WSOL_TOKEN_ACCOUNT).unwrap()
if usd1_pool {
get_vault_pda(&pool_state, &crate::constants::USD1_TOKEN_ACCOUNT).unwrap()
} else {
get_vault_pda(&pool_state, &crate::constants::WSOL_TOKEN_ACCOUNT).unwrap()
}
} else {
protocol_params.quote_vault
};
@@ -222,7 +274,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
if params.create_wsol_ata {
if params.close_output_mint_ata && !usd1_pool {
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
}
@@ -235,15 +287,15 @@ impl InstructionBuilder for BonkInstructionBuilder {
let accounts: [AccountMeta; 18] = [
AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
accounts::AUTHORITY_META, // Authority (readonly)
accounts::GLOBAL_CONFIG_META, // Global Config (readonly)
global_config, // Global Config (readonly)
AccountMeta::new_readonly(protocol_params.platform_config, false), // Platform Config (readonly)
AccountMeta::new(pool_state, false), // Pool State
AccountMeta::new(user_base_token_account, false), // User Base Token
AccountMeta::new(user_quote_token_account, false), // User Quote Token
AccountMeta::new(base_vault_account, false), // Base Vault
AccountMeta::new(quote_vault_account, false), // Quote Vault
AccountMeta::new_readonly(params.mint, false), // Base Token Mint (readonly)
crate::constants::WSOL_TOKEN_ACCOUNT_META, // Quote Token Mint (readonly)
AccountMeta::new_readonly(params.input_mint, false), // Base Token Mint (readonly)
quote_token_mint, // Quote Token Mint (readonly)
AccountMeta::new_readonly(protocol_params.mint_token_program, false), // Base Token Program (readonly)
crate::constants::TOKEN_PROGRAM_META, // Quote Token Program (readonly)
accounts::EVENT_AUTHORITY_META, // Event Authority (readonly)
@@ -255,7 +307,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
if params.close_wsol_ata {
if params.close_output_mint_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}