feat: major refactor with calculation utilities and instruction optimization
- Add comprehensive calculation utilities for all protocols (bonk, pumpfun, pumpswap, raydium) - Refactor instruction modules to reduce code complexity and improve maintainability - Optimize trading parameters structure and enhance common utilities - Separate calculation logic from trading modules for better code organization - Update dependencies and module exports
This commit is contained in:
+75
-124
@@ -5,20 +5,21 @@ use spl_associated_token_account::instruction::create_associated_token_account_i
|
||||
use spl_token::instruction::close_account;
|
||||
|
||||
use crate::{
|
||||
constants::raydium_cpmm::{accounts, SWAP_BASE_IN_DISCRIMINATOR},
|
||||
constants::trade::trade::DEFAULT_SLIPPAGE,
|
||||
trading::common::utils::get_token_balance,
|
||||
trading::core::{
|
||||
params::{BuyParams, RaydiumCpmmParams, SellParams},
|
||||
traits::InstructionBuilder,
|
||||
constants::{
|
||||
raydium_cpmm::{accounts, SWAP_BASE_IN_DISCRIMINATOR},
|
||||
trade::trade::DEFAULT_SLIPPAGE,
|
||||
},
|
||||
trading::raydium_cpmm::{
|
||||
common::{get_observation_state_pda, get_pool_pda, get_vault_pda},
|
||||
// pool::Pool,
|
||||
trading::{
|
||||
core::{
|
||||
params::{BuyParams, RaydiumCpmmParams, SellParams},
|
||||
traits::InstructionBuilder,
|
||||
},
|
||||
raydium_cpmm::common::{get_observation_state_pda, get_pool_pda, get_vault_pda},
|
||||
},
|
||||
utils::calc::raydium_cpmm::compute_swap_amount,
|
||||
};
|
||||
|
||||
/// RaydiumCpmm协议的指令构建器
|
||||
/// Instruction builder for RaydiumCpmm protocol
|
||||
pub struct RaydiumCpmmInstructionBuilder;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
@@ -36,7 +37,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
}
|
||||
|
||||
impl RaydiumCpmmInstructionBuilder {
|
||||
/// 使用提供的账户信息构建买入指令
|
||||
/// Build buy instructions with provided account information
|
||||
async fn build_buy_instructions_with_accounts(
|
||||
&self,
|
||||
params: &BuyParams,
|
||||
@@ -47,26 +48,12 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
.downcast_ref::<RaydiumCpmmParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for RaydiumCpmm"))?;
|
||||
|
||||
let pool_state = if protocol_params.pool_state.is_some() {
|
||||
protocol_params.pool_state.unwrap()
|
||||
} else {
|
||||
let mint_token_in_pool_state_index =
|
||||
protocol_params.mint_token_in_pool_state_index.unwrap_or(1);
|
||||
get_pool_pda(
|
||||
&accounts::AMM_CONFIG,
|
||||
if mint_token_in_pool_state_index == 1 {
|
||||
&accounts::WSOL_TOKEN_ACCOUNT
|
||||
} else {
|
||||
¶ms.mint
|
||||
},
|
||||
if mint_token_in_pool_state_index == 1 {
|
||||
¶ms.mint
|
||||
} else {
|
||||
&accounts::WSOL_TOKEN_ACCOUNT
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
let pool_state = get_pool_pda(
|
||||
&accounts::AMM_CONFIG,
|
||||
&protocol_params.base_mint,
|
||||
&protocol_params.quote_mint,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let wsol_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
@@ -77,36 +64,29 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
¶ms.mint,
|
||||
);
|
||||
|
||||
// 获取池的代币账户
|
||||
// Get pool token accounts
|
||||
let wsol_vault_account = get_vault_pda(&pool_state, &accounts::WSOL_TOKEN_ACCOUNT).unwrap();
|
||||
let mint_vault_account = get_vault_pda(&pool_state, ¶ms.mint).unwrap();
|
||||
|
||||
let observation_state_account = get_observation_state_pda(&pool_state).unwrap();
|
||||
let is_base_in = protocol_params.base_mint == accounts::WSOL_TOKEN_ACCOUNT;
|
||||
|
||||
let amount_in: u64 = params.sol_amount;
|
||||
let mut minimum_amount_out: u64 = if protocol_params.minimum_amount_out.is_some() {
|
||||
protocol_params.minimum_amount_out.unwrap()
|
||||
} else {
|
||||
println!("未提供minimum_amount_out,使用默认值0");
|
||||
0
|
||||
};
|
||||
if minimum_amount_out != 0 {
|
||||
let slippage_basis_points: u64 = if params.slippage_basis_points.is_some() {
|
||||
params.slippage_basis_points.unwrap()
|
||||
} else {
|
||||
DEFAULT_SLIPPAGE
|
||||
} as u64;
|
||||
minimum_amount_out = minimum_amount_out * (10000 - slippage_basis_points) / 10000;
|
||||
println!("slippage_basis_points: {}", slippage_basis_points);
|
||||
}
|
||||
println!("minimum_amount_out: {}", minimum_amount_out);
|
||||
let result = compute_swap_amount(
|
||||
protocol_params.base_reserve,
|
||||
protocol_params.quote_reserve,
|
||||
is_base_in,
|
||||
amount_in,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
);
|
||||
let minimum_amount_out = result.min_amount_out;
|
||||
|
||||
let mut instructions = vec![];
|
||||
|
||||
if protocol_params.auto_handle_wsol {
|
||||
// 插入wsol
|
||||
// Handle wSOL
|
||||
instructions.push(
|
||||
// 创建wSOL ATA账户,如果不存在
|
||||
// Create wSOL ATA account if it doesn't exist
|
||||
create_associated_token_account_idempotent(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
@@ -115,26 +95,31 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
),
|
||||
);
|
||||
instructions.push(
|
||||
// 将SOL转入wSOL ATA账户
|
||||
// Transfer SOL to wSOL ATA account
|
||||
transfer(¶ms.payer.pubkey(), &wsol_token_account, amount_in),
|
||||
);
|
||||
|
||||
// 同步wSOL余额
|
||||
// Sync wSOL balance
|
||||
instructions.push(
|
||||
spl_token::instruction::sync_native(&accounts::TOKEN_PROGRAM, &wsol_token_account)
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
// 创建用户的基础代币账户
|
||||
let mint_token_program = if is_base_in {
|
||||
protocol_params.quote_token_program
|
||||
} else {
|
||||
protocol_params.base_token_program
|
||||
};
|
||||
|
||||
instructions.push(create_associated_token_account_idempotent(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&protocol_params.mint_token_program.unwrap_or(accounts::TOKEN_PROGRAM),
|
||||
&mint_token_program,
|
||||
));
|
||||
|
||||
// 创建买入指令
|
||||
// Create buy instruction
|
||||
let accounts = vec![
|
||||
solana_sdk::instruction::AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::AUTHORITY, false), // Authority (readonly)
|
||||
@@ -145,15 +130,12 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new(wsol_vault_account, false), // Input Vault Account
|
||||
solana_sdk::instruction::AccountMeta::new(mint_vault_account, false), // Output Vault Account
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Input Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(
|
||||
protocol_params.mint_token_program.unwrap_or(accounts::TOKEN_PROGRAM),
|
||||
false,
|
||||
), // Output Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(mint_token_program, false), // Output Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Input token mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // Output token mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(observation_state_account, false), // Observation State Account
|
||||
];
|
||||
// 创建指令数据
|
||||
// Create instruction data
|
||||
let mut data = vec![];
|
||||
data.extend_from_slice(&SWAP_BASE_IN_DISCRIMINATOR);
|
||||
data.extend_from_slice(&amount_in.to_le_bytes());
|
||||
@@ -162,7 +144,7 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
instructions.push(Instruction { program_id: accounts::RAYDIUM_CPMM, accounts, data });
|
||||
|
||||
if protocol_params.auto_handle_wsol {
|
||||
// 关闭wSOL ATA账户,回收租金
|
||||
// Close wSOL ATA account, reclaim rent
|
||||
instructions.push(
|
||||
spl_token::instruction::close_account(
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
@@ -178,7 +160,7 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
Ok(instructions)
|
||||
}
|
||||
|
||||
/// 使用提供的账户信息构建卖出指令
|
||||
/// Build sell instructions with provided account information
|
||||
async fn build_sell_instructions_with_accounts(
|
||||
&self,
|
||||
params: &SellParams,
|
||||
@@ -189,61 +171,27 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
.downcast_ref::<RaydiumCpmmParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for RaydiumCpmm"))?;
|
||||
|
||||
if params.rpc.is_none() {
|
||||
return Err(anyhow!("RPC is not set"));
|
||||
}
|
||||
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 balance_u64 =
|
||||
get_token_balance(rpc.as_ref(), ¶ms.payer.pubkey(), ¶ms.mint).await?;
|
||||
amount = Some(balance_u64);
|
||||
}
|
||||
let amount = amount.unwrap_or(0);
|
||||
|
||||
if amount == 0 {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
return Err(anyhow!("Token amount is not set"));
|
||||
}
|
||||
|
||||
let mut minimum_amount_out: u64 = if protocol_params.minimum_amount_out.is_some() {
|
||||
protocol_params.minimum_amount_out.unwrap()
|
||||
} else {
|
||||
println!("未提供minimum_amount_out,使用默认值0");
|
||||
0
|
||||
};
|
||||
if minimum_amount_out != 0 {
|
||||
let slippage_basis_points: u64 = if params.slippage_basis_points.is_some() {
|
||||
params.slippage_basis_points.unwrap()
|
||||
} else {
|
||||
DEFAULT_SLIPPAGE
|
||||
} as u64;
|
||||
minimum_amount_out = minimum_amount_out * (10000 - slippage_basis_points) / 10000;
|
||||
println!("slippage_basis_points: {}", slippage_basis_points);
|
||||
}
|
||||
println!("minimum_amount_out: {}", minimum_amount_out);
|
||||
let is_base_in = protocol_params.base_mint == params.mint;
|
||||
|
||||
let pool_state = if protocol_params.pool_state.is_some() {
|
||||
protocol_params.pool_state.unwrap()
|
||||
} else {
|
||||
let mint_token_in_pool_state_index =
|
||||
protocol_params.mint_token_in_pool_state_index.unwrap_or(1);
|
||||
get_pool_pda(
|
||||
&accounts::AMM_CONFIG,
|
||||
if mint_token_in_pool_state_index == 1 {
|
||||
&accounts::WSOL_TOKEN_ACCOUNT
|
||||
} else {
|
||||
¶ms.mint
|
||||
},
|
||||
if mint_token_in_pool_state_index == 1 {
|
||||
¶ms.mint
|
||||
} else {
|
||||
&accounts::WSOL_TOKEN_ACCOUNT
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
let minimum_amount_out: u64 = compute_swap_amount(
|
||||
protocol_params.base_reserve,
|
||||
protocol_params.quote_reserve,
|
||||
is_base_in,
|
||||
params.token_amount.unwrap_or(0),
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
)
|
||||
.min_amount_out;
|
||||
|
||||
let pool_state = get_pool_pda(
|
||||
&accounts::AMM_CONFIG,
|
||||
&protocol_params.base_mint,
|
||||
&protocol_params.quote_mint,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let wsol_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
@@ -254,7 +202,7 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
¶ms.mint,
|
||||
);
|
||||
|
||||
// 获取池的代币账户
|
||||
// Get pool token accounts
|
||||
let wsol_vault_account = get_vault_pda(&pool_state, &accounts::WSOL_TOKEN_ACCOUNT).unwrap();
|
||||
let mint_vault_account = get_vault_pda(&pool_state, ¶ms.mint).unwrap();
|
||||
|
||||
@@ -262,18 +210,24 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
|
||||
let mut instructions = vec![];
|
||||
|
||||
// 插入wsol
|
||||
// Handle wSOL
|
||||
instructions.push(
|
||||
// 创建wSOL ATA账户,如果不存在
|
||||
// Create wSOL ATA account if it doesn't exist
|
||||
create_associated_token_account_idempotent(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||
&protocol_params.mint_token_program.unwrap(),
|
||||
&spl_token::ID,
|
||||
),
|
||||
);
|
||||
|
||||
// 创建卖出指令
|
||||
let mint_token_program = if is_base_in {
|
||||
protocol_params.base_token_program
|
||||
} else {
|
||||
protocol_params.quote_token_program
|
||||
};
|
||||
|
||||
// Create sell instruction
|
||||
let accounts = vec![
|
||||
solana_sdk::instruction::AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::AUTHORITY, false), // Authority (readonly)
|
||||
@@ -283,19 +237,16 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new(wsol_token_account, false), // Output Token Account
|
||||
solana_sdk::instruction::AccountMeta::new(mint_vault_account, false), // Input Vault Account
|
||||
solana_sdk::instruction::AccountMeta::new(wsol_vault_account, false), // Output Vault Account
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(
|
||||
protocol_params.mint_token_program.unwrap_or(accounts::TOKEN_PROGRAM),
|
||||
false,
|
||||
), // Input Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(mint_token_program, false), // Input Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Output Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // Input token mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Output token mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(observation_state_account, false), // Observation State Account
|
||||
];
|
||||
// 创建指令数据
|
||||
// Create instruction data
|
||||
let mut data = vec![];
|
||||
data.extend_from_slice(&SWAP_BASE_IN_DISCRIMINATOR);
|
||||
data.extend_from_slice(&amount.to_le_bytes());
|
||||
data.extend_from_slice(¶ms.token_amount.unwrap_or(0).to_le_bytes());
|
||||
data.extend_from_slice(&minimum_amount_out.to_le_bytes());
|
||||
|
||||
instructions.push(Instruction { program_id: accounts::RAYDIUM_CPMM, accounts, data });
|
||||
|
||||
Reference in New Issue
Block a user