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:
+59
-48
@@ -10,16 +10,19 @@ use crate::{
|
||||
trade::trade::DEFAULT_SLIPPAGE,
|
||||
},
|
||||
trading::{
|
||||
bonk::common::{fetch_pool_state, get_amount_out, get_pool_pda, get_vault_pda},
|
||||
bonk::common::{get_pool_pda, get_vault_pda},
|
||||
common::utils::get_token_balance,
|
||||
core::{
|
||||
params::{BonkParams, BuyParams, SellParams},
|
||||
traits::InstructionBuilder,
|
||||
},
|
||||
},
|
||||
utils::calc::bonk::{
|
||||
get_buy_token_amount_from_sol_amount, get_sell_sol_amount_from_token_amount,
|
||||
},
|
||||
};
|
||||
|
||||
/// Bonk协议的指令构建器
|
||||
/// Instruction builder for Bonk protocol
|
||||
pub struct BonkInstructionBuilder;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
@@ -37,7 +40,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
|
||||
}
|
||||
|
||||
impl BonkInstructionBuilder {
|
||||
/// 使用提供的账户信息构建买入指令
|
||||
/// Build buy instructions with provided account information
|
||||
async fn build_buy_instructions_with_accounts(
|
||||
&self,
|
||||
params: &BuyParams,
|
||||
@@ -50,7 +53,7 @@ impl BonkInstructionBuilder {
|
||||
|
||||
let pool_state = get_pool_pda(¶ms.mint, &accounts::WSOL_TOKEN_ACCOUNT).unwrap();
|
||||
|
||||
// 创建用户代币账户
|
||||
// Create user token accounts
|
||||
let user_base_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
@@ -60,31 +63,20 @@ impl BonkInstructionBuilder {
|
||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||
);
|
||||
|
||||
// 获取池的代币账户
|
||||
// Get pool token accounts
|
||||
let base_vault_account = get_vault_pda(&pool_state, ¶ms.mint).unwrap();
|
||||
let quote_vault_account =
|
||||
get_vault_pda(&pool_state, &accounts::WSOL_TOKEN_ACCOUNT).unwrap();
|
||||
|
||||
let mut virtual_base = protocol_params.virtual_base.unwrap_or(0);
|
||||
let mut virtual_quote = protocol_params.virtual_quote.unwrap_or(0);
|
||||
let mut real_base = protocol_params.real_base.unwrap_or(0);
|
||||
let mut real_quote = protocol_params.real_quote.unwrap_or(0);
|
||||
|
||||
if virtual_base == 0 || virtual_quote == 0 || real_base == 0 || real_quote == 0 {
|
||||
let pool = fetch_pool_state(params.rpc.as_ref().unwrap(), &pool_state).await?;
|
||||
virtual_base = pool.virtual_base as u128;
|
||||
virtual_quote = pool.virtual_quote as u128;
|
||||
real_base = pool.real_base as u128;
|
||||
real_quote = pool.real_quote as u128;
|
||||
}
|
||||
let virtual_base = protocol_params.virtual_base;
|
||||
let virtual_quote = protocol_params.virtual_quote;
|
||||
let real_base = protocol_params.real_base;
|
||||
let real_quote = protocol_params.real_quote;
|
||||
|
||||
let amount_in: u64 = params.sol_amount;
|
||||
let share_fee_rate: u64 = 0;
|
||||
let minimum_amount_out: u64 = get_amount_out(
|
||||
let minimum_amount_out: u64 = get_buy_token_amount_from_sol_amount(
|
||||
amount_in,
|
||||
accounts::PROTOCOL_FEE_RATE,
|
||||
accounts::PLATFORM_FEE_RATE,
|
||||
accounts::SHARE_FEE_RATE,
|
||||
virtual_base,
|
||||
virtual_quote,
|
||||
real_base,
|
||||
@@ -95,9 +87,9 @@ impl BonkInstructionBuilder {
|
||||
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(),
|
||||
@@ -106,11 +98,11 @@ impl BonkInstructionBuilder {
|
||||
),
|
||||
);
|
||||
instructions.push(
|
||||
// 将SOL转入wSOL ATA账户
|
||||
// Transfer SOL to wSOL ATA account
|
||||
transfer(¶ms.payer.pubkey(), &user_quote_token_account, amount_in),
|
||||
);
|
||||
|
||||
// 同步wSOL余额
|
||||
// Sync wSOL balance
|
||||
instructions.push(
|
||||
spl_token::instruction::sync_native(
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
@@ -120,7 +112,7 @@ impl BonkInstructionBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
// 创建用户的基础代币账户
|
||||
// Create user's base token account
|
||||
instructions.push(create_associated_token_account_idempotent(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
@@ -128,7 +120,7 @@ impl BonkInstructionBuilder {
|
||||
&accounts::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)
|
||||
@@ -141,12 +133,15 @@ impl BonkInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new(quote_vault_account, false), // Quote Vault
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // Base Token Mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Quote Token Mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Base Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(
|
||||
protocol_params.mint_token_program,
|
||||
false,
|
||||
), // Base Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Quote Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::EVENT_AUTHORITY, false), // Event Authority (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::BONK, false), // Program (readonly)
|
||||
];
|
||||
// 创建指令数据
|
||||
// Create instruction data
|
||||
let mut data = vec![];
|
||||
data.extend_from_slice(&BUY_EXECT_IN_DISCRIMINATOR);
|
||||
data.extend_from_slice(&amount_in.to_le_bytes());
|
||||
@@ -156,7 +151,7 @@ impl BonkInstructionBuilder {
|
||||
instructions.push(Instruction { program_id: accounts::BONK, 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,
|
||||
@@ -172,7 +167,7 @@ impl BonkInstructionBuilder {
|
||||
Ok(instructions)
|
||||
}
|
||||
|
||||
/// 使用提供的账户信息构建卖出指令
|
||||
/// Build sell instructions with provided account information
|
||||
async fn build_sell_instructions_with_accounts(
|
||||
&self,
|
||||
params: &SellParams,
|
||||
@@ -180,9 +175,16 @@ impl BonkInstructionBuilder {
|
||||
if params.rpc.is_none() {
|
||||
return Err(anyhow!("RPC is not set"));
|
||||
}
|
||||
|
||||
let protocol_params = params
|
||||
.protocol_params
|
||||
.as_any()
|
||||
.downcast_ref::<BonkParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for Bonk"))?;
|
||||
|
||||
let rpc = params.rpc.as_ref().unwrap().clone();
|
||||
|
||||
// 获取代币余额
|
||||
// Get token balance
|
||||
let mut amount = params.token_amount;
|
||||
if params.token_amount.is_none() || params.token_amount.unwrap_or(0) == 0 {
|
||||
let balance_u64 =
|
||||
@@ -195,12 +197,24 @@ impl BonkInstructionBuilder {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
|
||||
// 计算预期的SOL数量
|
||||
let minimum_amount_out: u64 = 1;
|
||||
|
||||
let pool_state = get_pool_pda(¶ms.mint, &accounts::WSOL_TOKEN_ACCOUNT).unwrap();
|
||||
|
||||
// 创建用户代币账户
|
||||
let virtual_base = protocol_params.virtual_base;
|
||||
let virtual_quote = protocol_params.virtual_quote;
|
||||
let real_base = protocol_params.real_base;
|
||||
let real_quote = protocol_params.real_quote;
|
||||
|
||||
// Calculate expected SOL amount
|
||||
let minimum_amount_out: u64 = get_sell_sol_amount_from_token_amount(
|
||||
amount,
|
||||
virtual_base,
|
||||
virtual_quote,
|
||||
real_base,
|
||||
real_quote,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE) as u128,
|
||||
);
|
||||
|
||||
// Create user token accounts
|
||||
let user_base_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
@@ -210,7 +224,7 @@ impl BonkInstructionBuilder {
|
||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||
);
|
||||
|
||||
// 获取池的代币账户
|
||||
// Get pool token accounts
|
||||
let base_vault_account = get_vault_pda(&pool_state, ¶ms.mint).unwrap();
|
||||
let quote_vault_account =
|
||||
get_vault_pda(&pool_state, &accounts::WSOL_TOKEN_ACCOUNT).unwrap();
|
||||
@@ -219,9 +233,9 @@ impl BonkInstructionBuilder {
|
||||
|
||||
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(),
|
||||
@@ -230,7 +244,7 @@ impl BonkInstructionBuilder {
|
||||
),
|
||||
);
|
||||
|
||||
// 创建卖出指令
|
||||
// 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)
|
||||
@@ -243,13 +257,16 @@ impl BonkInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new(quote_vault_account, false), // Quote Vault
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // Base Token Mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Quote Token Mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Base Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(
|
||||
protocol_params.mint_token_program,
|
||||
false,
|
||||
), // Base Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Quote Token Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::EVENT_AUTHORITY, false), // Event Authority (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::BONK, false), // Program (readonly)
|
||||
];
|
||||
|
||||
// 创建指令数据
|
||||
// Create instruction data
|
||||
let mut data = vec![];
|
||||
data.extend_from_slice(&SELL_EXECT_IN_DISCRIMINATOR);
|
||||
data.extend_from_slice(&amount.to_le_bytes());
|
||||
@@ -258,12 +275,6 @@ impl BonkInstructionBuilder {
|
||||
|
||||
instructions.push(Instruction { program_id: accounts::BONK, accounts, data });
|
||||
|
||||
let protocol_params = params
|
||||
.protocol_params
|
||||
.as_any()
|
||||
.downcast_ref::<BonkParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for Bonk"))?;
|
||||
|
||||
if protocol_params.auto_handle_wsol {
|
||||
instructions.push(
|
||||
close_account(
|
||||
|
||||
+41
-46
@@ -1,5 +1,5 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use solana_sdk::{instruction::Instruction, native_token::sol_str_to_lamports};
|
||||
use solana_sdk::instruction::Instruction;
|
||||
use spl_associated_token_account::{
|
||||
get_associated_token_address, instruction::create_associated_token_account,
|
||||
};
|
||||
@@ -10,6 +10,10 @@ use crate::{
|
||||
trading::pumpfun::common::{
|
||||
get_bonding_curve_pda, get_global_volume_accumulator_pda, get_user_volume_accumulator_pda,
|
||||
},
|
||||
utils::calc::{
|
||||
common::{calculate_with_slippage_buy, calculate_with_slippage_sell},
|
||||
pumpfun::{get_buy_token_amount_from_sol_amount, get_sell_sol_amount_from_token_amount},
|
||||
},
|
||||
};
|
||||
|
||||
use solana_sdk::{instruction::AccountMeta, pubkey::Pubkey, signature::Keypair, signer::Signer};
|
||||
@@ -17,21 +21,20 @@ use solana_sdk::{instruction::AccountMeta, pubkey::Pubkey, signature::Keypair, s
|
||||
use crate::{
|
||||
constants::pumpfun::global_constants::FEE_RECIPIENT,
|
||||
constants::trade::trade::DEFAULT_SLIPPAGE,
|
||||
trading::common::utils::calculate_with_slippage_buy,
|
||||
trading::core::{
|
||||
params::{BuyParams, PumpFunParams, SellParams},
|
||||
traits::InstructionBuilder,
|
||||
},
|
||||
trading::pumpfun::common::{get_buy_token_amount_from_sol_amount, get_creator_vault_pda},
|
||||
trading::pumpfun::common::get_creator_vault_pda,
|
||||
};
|
||||
|
||||
/// PumpFun协议的指令构建器
|
||||
/// Instruction builder for PumpFun protocol
|
||||
pub struct PumpFunInstructionBuilder;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl InstructionBuilder for PumpFunInstructionBuilder {
|
||||
async fn build_buy_instructions(&self, params: &BuyParams) -> Result<Vec<Instruction>> {
|
||||
// 获取PumpFun特定参数
|
||||
// Get PumpFun specific parameters
|
||||
let protocol_params = params
|
||||
.protocol_params
|
||||
.as_any()
|
||||
@@ -42,11 +45,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
|
||||
let bonding_curve = if protocol_params.bonding_curve.is_some() {
|
||||
protocol_params.bonding_curve.clone().unwrap()
|
||||
} else {
|
||||
return Err(anyhow!("Bonding curve not found"));
|
||||
};
|
||||
let bonding_curve = protocol_params.bonding_curve.clone();
|
||||
|
||||
let max_sol_cost = calculate_with_slippage_buy(
|
||||
params.sol_amount,
|
||||
@@ -54,19 +53,17 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
||||
);
|
||||
let creator_vault_pda = bonding_curve.get_creator_vault_pda();
|
||||
|
||||
let mut buy_token_amount =
|
||||
get_buy_token_amount_from_sol_amount(&bonding_curve, params.sol_amount);
|
||||
if buy_token_amount <= 100 * 1_000_000_u64 {
|
||||
buy_token_amount = if max_sol_cost > sol_str_to_lamports("0.01").unwrap_or(0) {
|
||||
25547619 * 1_000_000_u64
|
||||
} else {
|
||||
255476 * 1_000_000_u64
|
||||
};
|
||||
}
|
||||
let buy_token_amount = get_buy_token_amount_from_sol_amount(
|
||||
bonding_curve.virtual_token_reserves as u128,
|
||||
bonding_curve.virtual_sol_reserves as u128,
|
||||
bonding_curve.real_token_reserves as u128,
|
||||
bonding_curve.creator,
|
||||
params.sol_amount,
|
||||
);
|
||||
|
||||
let mut instructions = vec![];
|
||||
|
||||
// 创建关联代币账户
|
||||
// Create associated token account
|
||||
instructions.push(create_associated_token_account(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
@@ -74,23 +71,29 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
||||
&constants::pumpfun::accounts::TOKEN_PROGRAM,
|
||||
));
|
||||
|
||||
// 创建买入指令
|
||||
// Create buy instruction
|
||||
instructions.push(buy(
|
||||
params.payer.as_ref(),
|
||||
¶ms.mint,
|
||||
&bonding_curve.account,
|
||||
&creator_vault_pda,
|
||||
&FEE_RECIPIENT,
|
||||
Buy {
|
||||
_amount: buy_token_amount,
|
||||
_max_sol_cost: max_sol_cost,
|
||||
},
|
||||
Buy { _amount: buy_token_amount, _max_sol_cost: max_sol_cost },
|
||||
));
|
||||
|
||||
Ok(instructions)
|
||||
}
|
||||
|
||||
async fn build_sell_instructions(&self, params: &SellParams) -> Result<Vec<Instruction>> {
|
||||
// Get PumpFun specific parameters
|
||||
let protocol_params = params
|
||||
.protocol_params
|
||||
.as_any()
|
||||
.downcast_ref::<PumpFunParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for PumpFun"))?;
|
||||
|
||||
let bonding_curve = protocol_params.bonding_curve.clone();
|
||||
|
||||
let token_amount = if let Some(amount) = params.token_amount {
|
||||
if amount == 0 {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
@@ -102,35 +105,27 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
||||
let creator_vault_pda = get_creator_vault_pda(¶ms.creator).unwrap();
|
||||
let ata = get_associated_token_address(¶ms.payer.pubkey(), ¶ms.mint);
|
||||
|
||||
// 获取代币余额
|
||||
let balance_u64 = if let Some(rpc) = ¶ms.rpc {
|
||||
let balance = rpc.get_token_account_balance(&ata).await?;
|
||||
balance
|
||||
.amount
|
||||
.parse::<u64>()
|
||||
.map_err(|_| anyhow!("Failed to parse token balance"))?
|
||||
} else {
|
||||
return Err(anyhow!("RPC client is required to get token balance"));
|
||||
};
|
||||
|
||||
let mut token_amount = token_amount;
|
||||
if token_amount > balance_u64 {
|
||||
token_amount = balance_u64;
|
||||
}
|
||||
let sol_amount = get_sell_sol_amount_from_token_amount(
|
||||
bonding_curve.virtual_token_reserves as u128,
|
||||
bonding_curve.virtual_sol_reserves as u128,
|
||||
bonding_curve.creator,
|
||||
token_amount,
|
||||
);
|
||||
let min_sol_output = calculate_with_slippage_sell(
|
||||
sol_amount,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
);
|
||||
|
||||
let mut instructions = vec![sell(
|
||||
params.payer.as_ref(),
|
||||
¶ms.mint,
|
||||
&creator_vault_pda,
|
||||
&FEE_RECIPIENT,
|
||||
Sell {
|
||||
_amount: token_amount,
|
||||
_min_sol_output: 1,
|
||||
},
|
||||
Sell { _amount: token_amount, _min_sol_output: min_sol_output },
|
||||
)];
|
||||
|
||||
// 如果卖出全部代币,关闭账户
|
||||
if token_amount >= balance_u64 {
|
||||
// If selling all tokens, close the account
|
||||
if protocol_params.close_token_account_when_sell.unwrap_or(false) {
|
||||
instructions.push(close_account(
|
||||
&spl_token::ID,
|
||||
&ata,
|
||||
|
||||
+100
-181
@@ -10,20 +10,16 @@ use crate::{
|
||||
trade::trade::DEFAULT_SLIPPAGE,
|
||||
},
|
||||
trading::{
|
||||
common::utils::{
|
||||
calculate_with_slippage_buy, calculate_with_slippage_sell, get_token_balance,
|
||||
},
|
||||
core::{
|
||||
params::{BuyParams, PumpSwapParams, SellParams},
|
||||
traits::InstructionBuilder,
|
||||
},
|
||||
pumpswap::{
|
||||
self,
|
||||
common::{
|
||||
coin_creator_vault_ata, coin_creator_vault_authority, fee_recipient_ata, fetch_pool, find_pool, get_global_volume_accumulator_pda, get_token_amount, get_user_volume_accumulator_pda, get_wsol_amount
|
||||
},
|
||||
pumpswap::common::{
|
||||
coin_creator_vault_ata, coin_creator_vault_authority, fee_recipient_ata,
|
||||
get_global_volume_accumulator_pda, get_user_volume_accumulator_pda,
|
||||
},
|
||||
},
|
||||
utils::calc::pumpswap::{buy_quote_input_internal, sell_base_input_internal},
|
||||
};
|
||||
|
||||
/// Instruction builder for PumpSwap protocol
|
||||
@@ -44,40 +40,21 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
}
|
||||
|
||||
// Build instructions based on whether account information is provided
|
||||
match (&protocol_params.pool,) {
|
||||
(Some(pool),) => {
|
||||
let mut base_mint = params.mint;
|
||||
let mut quote_mint = accounts::WSOL_TOKEN_ACCOUNT;
|
||||
let mut pool_base_token_reserves = 0;
|
||||
let mut pool_quote_token_reserves = 0;
|
||||
let base_mint = protocol_params.base_mint;
|
||||
let quote_mint = protocol_params.quote_mint;
|
||||
let pool_base_token_reserves = protocol_params.pool_base_token_reserves;
|
||||
let pool_quote_token_reserves = protocol_params.pool_quote_token_reserves;
|
||||
|
||||
if let Some(p_base_mint) = protocol_params.base_mint {
|
||||
base_mint = p_base_mint;
|
||||
}
|
||||
if let Some(p_quote_mint) = protocol_params.quote_mint {
|
||||
quote_mint = p_quote_mint;
|
||||
}
|
||||
if let Some(p_pool_base_token_reserves) = protocol_params.pool_base_token_reserves {
|
||||
pool_base_token_reserves = p_pool_base_token_reserves;
|
||||
}
|
||||
if let Some(p_pool_quote_token_reserves) = protocol_params.pool_quote_token_reserves
|
||||
{
|
||||
pool_quote_token_reserves = p_pool_quote_token_reserves;
|
||||
}
|
||||
|
||||
self.build_buy_instructions_with_accounts(
|
||||
params,
|
||||
*pool,
|
||||
base_mint,
|
||||
quote_mint,
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
protocol_params.auto_handle_wsol,
|
||||
)
|
||||
.await
|
||||
}
|
||||
_ => self.build_buy_instructions_auto_discover(params).await,
|
||||
}
|
||||
self.build_buy_instructions_with_accounts(
|
||||
params,
|
||||
protocol_params.pool,
|
||||
base_mint,
|
||||
quote_mint,
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
protocol_params.auto_handle_wsol,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn build_sell_instructions(&self, params: &SellParams) -> Result<Vec<Instruction>> {
|
||||
@@ -88,104 +65,25 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
.downcast_ref::<PumpSwapParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for PumpSwap"))?;
|
||||
// Build instructions based on whether account information is provided
|
||||
match (&protocol_params.pool,) {
|
||||
(Some(pool),) => {
|
||||
let mut base_mint = params.mint;
|
||||
let mut quote_mint = accounts::WSOL_TOKEN_ACCOUNT;
|
||||
let mut pool_base_token_reserves = 0;
|
||||
let mut pool_quote_token_reserves = 0;
|
||||
if let Some(p_base_mint) = protocol_params.base_mint {
|
||||
base_mint = p_base_mint;
|
||||
}
|
||||
if let Some(p_quote_mint) = protocol_params.quote_mint {
|
||||
quote_mint = p_quote_mint;
|
||||
}
|
||||
if let Some(p_pool_base_token_reserves) = protocol_params.pool_base_token_reserves {
|
||||
pool_base_token_reserves = p_pool_base_token_reserves;
|
||||
}
|
||||
if let Some(p_pool_quote_token_reserves) = protocol_params.pool_quote_token_reserves
|
||||
{
|
||||
pool_quote_token_reserves = p_pool_quote_token_reserves;
|
||||
}
|
||||
self.build_sell_instructions_with_accounts(
|
||||
params,
|
||||
*pool,
|
||||
base_mint,
|
||||
quote_mint,
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
protocol_params.auto_handle_wsol,
|
||||
)
|
||||
.await
|
||||
}
|
||||
_ => self.build_sell_instructions_auto_discover(params).await,
|
||||
}
|
||||
let base_mint = protocol_params.base_mint;
|
||||
let quote_mint = protocol_params.quote_mint;
|
||||
let pool_base_token_reserves = protocol_params.pool_base_token_reserves;
|
||||
let pool_quote_token_reserves = protocol_params.pool_quote_token_reserves;
|
||||
|
||||
self.build_sell_instructions_with_accounts(
|
||||
params,
|
||||
protocol_params.pool,
|
||||
base_mint,
|
||||
quote_mint,
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
protocol_params.auto_handle_wsol,
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl PumpSwapInstructionBuilder {
|
||||
/// Auto-discover pool and account information and build buy instructions
|
||||
async fn build_buy_instructions_auto_discover(
|
||||
&self,
|
||||
params: &BuyParams,
|
||||
) -> Result<Vec<Instruction>> {
|
||||
if params.rpc.is_none() {
|
||||
return Err(anyhow!("RPC is not set"));
|
||||
}
|
||||
println!("❗️Going through RPC request, increasing instruction building time");
|
||||
let rpc = params.rpc.as_ref().unwrap().clone();
|
||||
// Find pool
|
||||
let pool = find_pool(rpc.as_ref(), ¶ms.mint).await?;
|
||||
let pool_data = fetch_pool(rpc.as_ref(), &pool).await?;
|
||||
let pool_base_token_reserves =
|
||||
get_token_balance(rpc.as_ref(), &pool, &pool_data.base_mint).await?;
|
||||
let pool_quote_token_reserves =
|
||||
get_token_balance(rpc.as_ref(), &pool, &pool_data.quote_mint).await?;
|
||||
let mut params = params.clone();
|
||||
params.creator = pool_data.coin_creator;
|
||||
self.build_buy_instructions_with_accounts(
|
||||
¶ms,
|
||||
pool,
|
||||
pool_data.base_mint,
|
||||
pool_data.quote_mint,
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
true,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Auto-discover pool and account information and build sell instructions
|
||||
async fn build_sell_instructions_auto_discover(
|
||||
&self,
|
||||
params: &SellParams,
|
||||
) -> Result<Vec<Instruction>> {
|
||||
if params.rpc.is_none() {
|
||||
return Err(anyhow!("RPC is not set"));
|
||||
}
|
||||
println!("❗️Going through RPC request, increasing instruction building time");
|
||||
let rpc = params.rpc.as_ref().unwrap().clone();
|
||||
// Find pool
|
||||
let pool = find_pool(rpc.as_ref(), ¶ms.mint).await?;
|
||||
let pool_data = fetch_pool(rpc.as_ref(), &pool).await?;
|
||||
let pool_base_token_reserves =
|
||||
get_token_balance(rpc.as_ref(), &pool, &pool_data.base_mint).await?;
|
||||
let pool_quote_token_reserves =
|
||||
get_token_balance(rpc.as_ref(), &pool, &pool_data.quote_mint).await?;
|
||||
let mut params = params.clone();
|
||||
params.creator = pool_data.coin_creator;
|
||||
self.build_sell_instructions_with_accounts(
|
||||
¶ms,
|
||||
pool,
|
||||
pool_data.base_mint,
|
||||
pool_data.quote_mint,
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
true,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Build buy instructions with provided account information
|
||||
async fn build_buy_instructions_with_accounts(
|
||||
&self,
|
||||
@@ -201,38 +99,36 @@ impl PumpSwapInstructionBuilder {
|
||||
return Err(anyhow!("RPC is not set"));
|
||||
}
|
||||
let quote_mint_is_wsol = quote_mint == accounts::WSOL_TOKEN_ACCOUNT;
|
||||
// Calculate token amount
|
||||
let mut token_amount = get_token_amount(
|
||||
quote_mint_is_wsol,
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
params.sol_amount,
|
||||
accounts::LP_FEE_BASIS_POINTS,
|
||||
accounts::PROTOCOL_FEE_BASIS_POINTS,
|
||||
if params.creator == Pubkey::default() {
|
||||
0
|
||||
} else {
|
||||
accounts::COIN_CREATOR_FEE_BASIS_POINTS
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
if !quote_mint_is_wsol {
|
||||
// min_quote_amount_out
|
||||
token_amount = calculate_with_slippage_sell(
|
||||
token_amount,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
);
|
||||
}
|
||||
let sol_amount = if quote_mint_is_wsol {
|
||||
// max_quote_amount_in
|
||||
calculate_with_slippage_buy(
|
||||
|
||||
let mut token_amount = 0;
|
||||
let mut sol_amount = 0;
|
||||
if quote_mint_is_wsol {
|
||||
let result = buy_quote_input_internal(
|
||||
params.sol_amount,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
¶ms.creator,
|
||||
)
|
||||
.unwrap();
|
||||
// base_amount_out
|
||||
token_amount = result.base;
|
||||
// max_quote_amount_in
|
||||
sol_amount = result.max_quote;
|
||||
} else {
|
||||
let result = sell_base_input_internal(
|
||||
params.sol_amount,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
¶ms.creator,
|
||||
)
|
||||
.unwrap();
|
||||
// min_quote_amount_out
|
||||
token_amount = result.min_quote;
|
||||
// base_amount_in
|
||||
params.sol_amount
|
||||
};
|
||||
sol_amount = params.sol_amount;
|
||||
}
|
||||
|
||||
// Create user token accounts
|
||||
let user_base_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
@@ -351,11 +247,15 @@ impl PumpSwapInstructionBuilder {
|
||||
let mut data = vec![];
|
||||
if quote_mint_is_wsol {
|
||||
data.extend_from_slice(&BUY_DISCRIMINATOR);
|
||||
// base_amount_out
|
||||
data.extend_from_slice(&token_amount.to_le_bytes());
|
||||
// max_quote_amount_in
|
||||
data.extend_from_slice(&sol_amount.to_le_bytes());
|
||||
} else {
|
||||
data.extend_from_slice(&SELL_DISCRIMINATOR);
|
||||
// base_amount_in
|
||||
data.extend_from_slice(&sol_amount.to_le_bytes());
|
||||
// min_quote_amount_out
|
||||
data.extend_from_slice(&token_amount.to_le_bytes());
|
||||
}
|
||||
|
||||
@@ -394,27 +294,42 @@ impl PumpSwapInstructionBuilder {
|
||||
if params.rpc.is_none() {
|
||||
return Err(anyhow!("RPC is not set"));
|
||||
}
|
||||
if params.token_amount.is_none() {
|
||||
return Err(anyhow!("Token amount is not set"));
|
||||
}
|
||||
|
||||
let quote_mint_is_wsol = quote_mint == accounts::WSOL_TOKEN_ACCOUNT;
|
||||
let mut sol_amount = get_wsol_amount(
|
||||
quote_mint_is_wsol,
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
params.token_amount.unwrap_or(0),
|
||||
accounts::LP_FEE_BASIS_POINTS,
|
||||
accounts::PROTOCOL_FEE_BASIS_POINTS,
|
||||
if params.creator == Pubkey::default() {
|
||||
0
|
||||
} else {
|
||||
accounts::COIN_CREATOR_FEE_BASIS_POINTS
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
sol_amount = calculate_with_slippage_sell(
|
||||
sol_amount,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
);
|
||||
let token_amount = params.token_amount.unwrap_or(0);
|
||||
|
||||
let mut token_amount = 0;
|
||||
let mut sol_amount = 0;
|
||||
|
||||
if quote_mint_is_wsol {
|
||||
let result = sell_base_input_internal(
|
||||
params.token_amount.unwrap(),
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
¶ms.creator,
|
||||
)
|
||||
.unwrap();
|
||||
// base_amount_in
|
||||
token_amount = params.token_amount.unwrap();
|
||||
// min_quote_amount_out
|
||||
sol_amount = result.min_quote;
|
||||
} else {
|
||||
let result = buy_quote_input_internal(
|
||||
params.token_amount.unwrap(),
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
¶ms.creator,
|
||||
)
|
||||
.unwrap();
|
||||
// max_quote_amount_in
|
||||
token_amount = result.max_quote;
|
||||
// base_amount_out
|
||||
sol_amount = result.base;
|
||||
}
|
||||
|
||||
let coin_creator_vault_ata = coin_creator_vault_ata(params.creator, quote_mint);
|
||||
let coin_creator_vault_authority = coin_creator_vault_authority(params.creator);
|
||||
@@ -502,11 +417,15 @@ impl PumpSwapInstructionBuilder {
|
||||
let mut data = vec![];
|
||||
if quote_mint_is_wsol {
|
||||
data.extend_from_slice(&SELL_DISCRIMINATOR);
|
||||
// base_amount_in
|
||||
data.extend_from_slice(&token_amount.to_le_bytes());
|
||||
// min_quote_amount_out
|
||||
data.extend_from_slice(&sol_amount.to_le_bytes());
|
||||
} else {
|
||||
data.extend_from_slice(&BUY_DISCRIMINATOR);
|
||||
// base_amount_out
|
||||
data.extend_from_slice(&sol_amount.to_le_bytes());
|
||||
// max_quote_amount_in
|
||||
data.extend_from_slice(&token_amount.to_le_bytes());
|
||||
}
|
||||
|
||||
|
||||
+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