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()));
}
+17 -17
View File
@@ -1,7 +1,7 @@
use crate::{
constants::trade::trade::DEFAULT_SLIPPAGE,
trading::core::{
params::{BuyParams, PumpFunParams, SellParams},
params::{PumpFunParams, SwapParams},
traits::InstructionBuilder,
},
};
@@ -25,7 +25,7 @@ pub struct PumpFunInstructionBuilder;
#[async_trait::async_trait]
impl InstructionBuilder for PumpFunInstructionBuilder {
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
// ========================================
@@ -35,7 +35,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
.downcast_ref::<PumpFunParams>()
.ok_or_else(|| anyhow!("Invalid protocol params for PumpFun"))?;
if params.sol_amount == 0 {
if params.input_amount.unwrap_or(0) == 0 {
return Err(anyhow!("Amount cannot be zero"));
}
@@ -51,16 +51,16 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
bonding_curve.virtual_sol_reserves as u128,
bonding_curve.real_token_reserves as u128,
creator,
params.sol_amount,
params.input_amount.unwrap_or(0),
);
let max_sol_cost = calculate_with_slippage_buy(
params.sol_amount,
params.input_amount.unwrap_or(0),
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
);
let bonding_curve_addr = if bonding_curve.account == Pubkey::default() {
get_bonding_curve_pda(&params.mint).unwrap()
get_bonding_curve_pda(&params.output_mint).unwrap()
} else {
bonding_curve.account
};
@@ -69,7 +69,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
if protocol_params.associated_bonding_curve == Pubkey::default() {
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&bonding_curve_addr,
&params.mint,
&params.output_mint,
&crate::constants::TOKEN_PROGRAM,
)
} else {
@@ -79,7 +79,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
let user_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.mint,
&params.output_mint,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
);
@@ -93,12 +93,12 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
let mut instructions = Vec::with_capacity(2);
// Create associated token account
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,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
),
@@ -113,7 +113,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
let accounts: [AccountMeta; 16] = [
global_constants::GLOBAL_ACCOUNT_META,
global_constants::FEE_RECIPIENT_META,
AccountMeta::new_readonly(params.mint, false),
AccountMeta::new_readonly(params.output_mint, false),
AccountMeta::new(bonding_curve_addr, false),
AccountMeta::new(associated_bonding_curve, false),
AccountMeta::new(user_token_account, false),
@@ -138,7 +138,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
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
// ========================================
@@ -148,7 +148,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
.downcast_ref::<PumpFunParams>()
.ok_or_else(|| anyhow!("Invalid protocol params for PumpFun"))?;
let token_amount = if let Some(amount) = params.token_amount {
let token_amount = if let Some(amount) = params.input_amount {
if amount == 0 {
return Err(anyhow!("Amount cannot be zero"));
}
@@ -177,7 +177,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
);
let bonding_curve_addr = if bonding_curve.account == Pubkey::default() {
get_bonding_curve_pda(&params.mint).unwrap()
get_bonding_curve_pda(&params.input_mint).unwrap()
} else {
bonding_curve.account
};
@@ -186,7 +186,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
if protocol_params.associated_bonding_curve == Pubkey::default() {
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
&bonding_curve_addr,
&params.mint,
&params.input_mint,
&crate::constants::TOKEN_PROGRAM,
)
} else {
@@ -196,7 +196,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
let user_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.mint,
&params.input_mint,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
);
@@ -214,7 +214,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
let accounts: [AccountMeta; 14] = [
global_constants::GLOBAL_ACCOUNT_META,
global_constants::FEE_RECIPIENT_META,
AccountMeta::new_readonly(params.mint, false),
AccountMeta::new_readonly(params.input_mint, false),
AccountMeta::new(bonding_curve_addr, false),
AccountMeta::new(associated_bonding_curve, false),
AccountMeta::new(user_token_account, false),
+16 -16
View File
@@ -7,7 +7,7 @@ use crate::{
trading::{
common::wsol_manager,
core::{
params::{BuyParams, PumpSwapParams, SellParams},
params::{PumpSwapParams, SwapParams},
traits::InstructionBuilder,
},
},
@@ -25,7 +25,7 @@ pub struct PumpSwapInstructionBuilder;
#[async_trait::async_trait]
impl InstructionBuilder for PumpSwapInstructionBuilder {
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
// ========================================
@@ -35,7 +35,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
.downcast_ref::<PumpSwapParams>()
.ok_or_else(|| anyhow!("Invalid protocol params for PumpSwap"))?;
if params.sol_amount == 0 {
if params.input_amount.unwrap_or(0) == 0 {
return Err(anyhow!("Amount cannot be zero"));
}
@@ -46,8 +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 create_wsol_ata = params.create_wsol_ata;
let close_wsol_ata = params.close_wsol_ata;
let create_wsol_ata = params.create_input_mint_ata;
let close_wsol_ata = params.close_input_mint_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;
@@ -70,7 +70,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
let (token_amount, sol_amount) = if quote_mint_is_wsol {
let result = buy_quote_input_internal(
params.sol_amount,
params.input_amount.unwrap_or(0),
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
pool_base_token_reserves,
pool_quote_token_reserves,
@@ -81,7 +81,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
(result.base, result.max_quote)
} else {
let result = sell_base_input_internal(
params.sol_amount,
params.input_amount.unwrap_or(0),
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
pool_base_token_reserves,
pool_quote_token_reserves,
@@ -89,7 +89,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
)
.unwrap();
// min_quote_amount_out, base_amount_in
(result.min_quote, params.sol_amount)
(result.min_quote, params.input_amount.unwrap_or(0))
};
let user_base_token_account =
@@ -118,7 +118,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
.extend(crate::trading::common::handle_wsol(&params.payer.pubkey(), sol_amount));
}
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(),
@@ -191,7 +191,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
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
// ========================================
@@ -210,8 +210,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 create_wsol_ata = params.create_wsol_ata;
let close_wsol_ata = params.close_wsol_ata;
let create_wsol_ata = params.create_output_mint_ata;
let close_wsol_ata = params.close_output_mint_ata;
let base_token_program = protocol_params.base_token_program;
let quote_token_program = protocol_params.quote_token_program;
@@ -220,7 +220,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
{
return Err(anyhow!("Invalid base mint and quote mint"));
}
if params.token_amount.is_none() {
if params.input_amount.is_none() {
return Err(anyhow!("Token amount is not set"));
}
@@ -235,7 +235,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
let (token_amount, sol_amount) = if quote_mint_is_wsol {
let result = sell_base_input_internal(
params.token_amount.unwrap(),
params.input_amount.unwrap(),
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
pool_base_token_reserves,
pool_quote_token_reserves,
@@ -243,10 +243,10 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
)
.unwrap();
// base_amount_in, min_quote_amount_out
(params.token_amount.unwrap(), result.min_quote)
(params.input_amount.unwrap(), result.min_quote)
} else {
let result = buy_quote_input_internal(
params.token_amount.unwrap(),
params.input_amount.unwrap(),
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
pool_base_token_reserves,
pool_quote_token_reserves,
+16 -16
View File
@@ -2,7 +2,7 @@ use crate::{
constants::trade::trade::DEFAULT_SLIPPAGE,
instruction::utils::raydium_amm_v4::{accounts, SWAP_BASE_IN_DISCRIMINATOR},
trading::core::{
params::{BuyParams, RaydiumAmmV4Params, SellParams},
params::{RaydiumAmmV4Params, SwapParams},
traits::InstructionBuilder,
},
utils::calc::raydium_amm_v4::compute_swap_amount,
@@ -18,11 +18,11 @@ pub struct RaydiumAmmV4InstructionBuilder;
#[async_trait::async_trait]
impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
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
@@ -35,7 +35,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
// Trade calculation and account address preparation
// ========================================
let is_base_in = protocol_params.coin_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
let amount_in: u64 = params.sol_amount;
let amount_in: u64 = params.input_amount.unwrap_or(0);
let swap_result = compute_swap_amount(
protocol_params.coin_reserve,
protocol_params.pc_reserve,
@@ -55,7 +55,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
let user_destination_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.mint,
&params.output_mint,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
);
@@ -65,17 +65,17 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(6);
if params.create_wsol_ata {
if params.create_input_mint_ata {
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,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
),
@@ -114,7 +114,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
accounts.to_vec(),
));
if params.close_wsol_ata {
if params.close_input_mint_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
@@ -122,7 +122,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
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
// ========================================
@@ -132,7 +132,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
.downcast_ref::<RaydiumAmmV4Params>()
.ok_or_else(|| anyhow!("Invalid protocol params for RaydiumCpmm"))?;
if params.token_amount.is_none() || params.token_amount.unwrap_or(0) == 0 {
if params.input_amount.is_none() || params.input_amount.unwrap_or(0) == 0 {
return Err(anyhow!("Token amount is not set"));
}
@@ -144,7 +144,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
protocol_params.coin_reserve,
protocol_params.pc_reserve,
is_base_in,
params.token_amount.unwrap_or(0),
params.input_amount.unwrap_or(0),
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
);
let minimum_amount_out = swap_result.min_amount_out;
@@ -152,7 +152,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
let user_source_token_account =
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.mint,
&params.input_mint,
&crate::constants::TOKEN_PROGRAM,
params.open_seed_optimize,
);
@@ -169,7 +169,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
if params.create_wsol_ata {
if params.create_output_mint_ata {
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
}
@@ -196,7 +196,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
// Create instruction data
let mut data = [0u8; 17];
data[..1].copy_from_slice(&SWAP_BASE_IN_DISCRIMINATOR);
data[1..9].copy_from_slice(&params.token_amount.unwrap_or(0).to_le_bytes());
data[1..9].copy_from_slice(&params.input_amount.unwrap_or(0).to_le_bytes());
data[9..17].copy_from_slice(&minimum_amount_out.to_le_bytes());
instructions.push(Instruction::new_with_bytes(
@@ -205,7 +205,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
accounts.to_vec(),
));
if params.close_wsol_ata {
if params.close_output_mint_ata {
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
+21 -21
View File
@@ -6,7 +6,7 @@ use crate::{
SWAP_BASE_IN_DISCRIMINATOR,
},
trading::core::{
params::{BuyParams, RaydiumCpmmParams, SellParams},
params::{RaydiumCpmmParams, SwapParams},
traits::InstructionBuilder,
},
utils::calc::raydium_cpmm::compute_swap_amount,
@@ -23,11 +23,11 @@ pub struct RaydiumCpmmInstructionBuilder;
#[async_trait::async_trait]
impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
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
@@ -57,7 +57,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
protocol_params.base_token_program
};
let amount_in: u64 = params.sol_amount;
let amount_in: u64 = params.input_amount.unwrap_or(0);
let result = compute_swap_amount(
protocol_params.base_reserve,
protocol_params.quote_reserve,
@@ -75,7 +75,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
);
let mint_token_account = get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.mint,
&params.output_mint,
&mint_token_program,
params.open_seed_optimize,
);
@@ -87,7 +87,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
true,
);
let mint_vault_account =
get_vault_account(&pool_state, &params.mint, protocol_params, false);
get_vault_account(&pool_state, &params.output_mint, protocol_params, false);
let observation_state_account = if protocol_params.observation_state == Pubkey::default() {
get_observation_state_pda(&pool_state).unwrap()
@@ -100,17 +100,17 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(6);
if params.create_wsol_ata {
if params.create_input_mint_ata {
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,
&mint_token_program,
params.open_seed_optimize,
),
@@ -130,7 +130,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
crate::constants::TOKEN_PROGRAM_META, // Input Token Program (readonly)
AccountMeta::new_readonly(mint_token_program, false), // Output Token Program (readonly)
crate::constants::WSOL_TOKEN_ACCOUNT_META, // Input token mint (readonly)
AccountMeta::new_readonly(params.mint, false), // Output token mint (readonly)
AccountMeta::new_readonly(params.output_mint, false), // Output token mint (readonly)
AccountMeta::new(observation_state_account, false), // Observation State Account
];
// Create instruction data
@@ -145,7 +145,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
accounts.to_vec(),
));
if params.close_wsol_ata {
if params.close_input_mint_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
@@ -153,7 +153,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
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
// ========================================
@@ -163,7 +163,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
.downcast_ref::<RaydiumCpmmParams>()
.ok_or_else(|| anyhow!("Invalid protocol params for RaydiumCpmm"))?;
if params.token_amount.is_none() || params.token_amount.unwrap_or(0) == 0 {
if params.input_amount.is_none() || params.input_amount.unwrap_or(0) == 0 {
return Err(anyhow!("Token amount is not set"));
}
@@ -181,7 +181,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
// ========================================
// Trade calculation and account address preparation
// ========================================
let is_base_in = protocol_params.base_mint == params.mint;
let is_base_in = protocol_params.base_mint == params.input_mint;
let mint_token_program = if is_base_in {
protocol_params.base_token_program
} else {
@@ -192,7 +192,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
protocol_params.base_reserve,
protocol_params.quote_reserve,
is_base_in,
params.token_amount.unwrap_or(0),
params.input_amount.unwrap_or(0),
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
)
.min_amount_out;
@@ -205,7 +205,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
);
let mint_token_account = get_associated_token_address_with_program_id_fast_use_seed(
&params.payer.pubkey(),
&params.mint,
&params.input_mint,
&mint_token_program,
params.open_seed_optimize,
);
@@ -217,7 +217,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
true,
);
let mint_vault_account =
get_vault_account(&pool_state, &params.mint, protocol_params, false);
get_vault_account(&pool_state, &params.input_mint, protocol_params, false);
let observation_state_account = if protocol_params.observation_state == Pubkey::default() {
get_observation_state_pda(&pool_state).unwrap()
@@ -230,7 +230,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
// ========================================
let mut instructions = Vec::with_capacity(3);
if params.create_wsol_ata {
if params.create_output_mint_ata {
instructions.extend(crate::trading::common::create_wsol_ata(&params.payer.pubkey()));
}
@@ -246,14 +246,14 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
AccountMeta::new(wsol_vault_account, false), // Output Vault Account
AccountMeta::new_readonly(mint_token_program, false), // Input Token Program (readonly)
crate::constants::TOKEN_PROGRAM_META, // Output Token Program (readonly)
AccountMeta::new_readonly(params.mint, false), // Input token mint (readonly)
AccountMeta::new_readonly(params.input_mint, false), // Input token mint (readonly)
crate::constants::WSOL_TOKEN_ACCOUNT_META, // Output token mint (readonly)
AccountMeta::new(observation_state_account, false), // Observation State Account
];
// Create instruction data
let mut data = [0u8; 24];
data[..8].copy_from_slice(&SWAP_BASE_IN_DISCRIMINATOR);
data[8..16].copy_from_slice(&params.token_amount.unwrap_or(0).to_le_bytes());
data[8..16].copy_from_slice(&params.input_amount.unwrap_or(0).to_le_bytes());
data[16..24].copy_from_slice(&minimum_amount_out.to_le_bytes());
instructions.push(Instruction::new_with_bytes(
@@ -262,7 +262,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
accounts.to_vec(),
));
if params.close_wsol_ata {
if params.close_output_mint_ata {
// Close wSOL ATA account, reclaim rent
instructions.extend(crate::trading::common::close_wsol(&params.payer.pubkey()));
}
+9
View File
@@ -17,6 +17,7 @@ pub mod accounts {
pub const AUTHORITY: Pubkey = pubkey!("WLHv2UAZm6z4KyaaELi5pjdbJh6RESMva1Rnn8pJVVh");
pub const GLOBAL_CONFIG: Pubkey = pubkey!("6s1xP3hpbAfFoNtUNF8mfHsjr2Bd97JxFJRWLbL6aHuX");
pub const USD1_GLOBAL_CONFIG: Pubkey = pubkey!("EPiZbnrThjyLnoQ6QQzkxeFqyL5uyg9RzNHHAudUPxBz");
pub const EVENT_AUTHORITY: Pubkey = pubkey!("2DPAtwB8L12vrMRExbLuyGnC7n2J5LNoZQSejeQGpwkr");
pub const BONK: Pubkey = pubkey!("LanMV9sAd7wArD4vJFi2qDdfnVhFxYSUg6eADduJ3uj");
@@ -37,6 +38,14 @@ pub mod accounts {
is_signer: false,
is_writable: false,
};
pub const USD1_GLOBAL_CONFIG_META: solana_sdk::instruction::AccountMeta =
solana_sdk::instruction::AccountMeta {
pubkey: USD1_GLOBAL_CONFIG,
is_signer: false,
is_writable: false,
};
pub const EVENT_AUTHORITY_META: solana_sdk::instruction::AccountMeta =
solana_sdk::instruction::AccountMeta {
pubkey: EVENT_AUTHORITY,