feat(pumpswap): enhance flexible mint support and optimize performance
- Add base_mint/quote_mint parameters to PumpSwapParams for flexible trading pairs - Implement pool reserves caching to reduce RPC calls and improve speed - Refactor unified buy/sell instruction logic for different mint types - Add volume accumulator PDA support and proper fee calculations - Update pool discovery for both base and quote mint searches - Bump version to 0.2.11 Breaking: PumpSwapParams now requires mint and reserves parameters
This commit is contained in:
+278
-115
@@ -4,28 +4,34 @@ use spl_associated_token_account::instruction::create_associated_token_account_i
|
||||
use spl_token::instruction::close_account;
|
||||
|
||||
use crate::{
|
||||
constants::pumpswap::{accounts, BUY_DISCRIMINATOR, SELL_DISCRIMINATOR},
|
||||
constants::trade::trade::DEFAULT_SLIPPAGE,
|
||||
trading::common::utils::{
|
||||
calculate_with_slippage_buy, calculate_with_slippage_sell, get_token_balance,
|
||||
constants::{
|
||||
pumpswap::{accounts, BUY_DISCRIMINATOR, SELL_DISCRIMINATOR},
|
||||
trade::trade::DEFAULT_SLIPPAGE,
|
||||
},
|
||||
trading::core::{
|
||||
params::{BuyParams, PumpSwapParams, SellParams},
|
||||
traits::InstructionBuilder,
|
||||
},
|
||||
trading::pumpswap::common::{
|
||||
coin_creator_vault_ata, coin_creator_vault_authority, find_pool, get_buy_token_amount,
|
||||
get_sell_sol_amount,
|
||||
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, find_pool, get_global_volume_accumulator_pda, get_token_amount, get_user_volume_accumulator_pda, get_wsol_amount
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
/// PumpSwap协议的指令构建器
|
||||
/// Instruction builder for PumpSwap protocol
|
||||
pub struct PumpSwapInstructionBuilder;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
async fn build_buy_instructions(&self, params: &BuyParams) -> Result<Vec<Instruction>> {
|
||||
// 获取PumpSwap特定参数
|
||||
// Get PumpSwap specific parameters
|
||||
let protocol_params = params
|
||||
.protocol_params
|
||||
.as_any()
|
||||
@@ -36,12 +42,35 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
|
||||
// 根据是否提供了账户信息来构建指令
|
||||
// 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_buy_instructions_with_accounts(
|
||||
params,
|
||||
*pool,
|
||||
base_mint,
|
||||
quote_mint,
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
protocol_params.auto_handle_wsol,
|
||||
)
|
||||
.await
|
||||
@@ -51,18 +80,42 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
}
|
||||
|
||||
async fn build_sell_instructions(&self, params: &SellParams) -> Result<Vec<Instruction>> {
|
||||
// 获取PumpSwap特定参数
|
||||
// Get PumpSwap specific parameters
|
||||
let protocol_params = params
|
||||
.protocol_params
|
||||
.as_any()
|
||||
.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),) => {
|
||||
self.build_sell_instructions_with_accounts(params, *pool)
|
||||
.await
|
||||
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,
|
||||
}
|
||||
@@ -70,7 +123,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
}
|
||||
|
||||
impl PumpSwapInstructionBuilder {
|
||||
/// 自动发现池和账户信息并构建买入指令
|
||||
/// Auto-discover pool and account information and build buy instructions
|
||||
async fn build_buy_instructions_auto_discover(
|
||||
&self,
|
||||
params: &BuyParams,
|
||||
@@ -78,15 +131,30 @@ impl PumpSwapInstructionBuilder {
|
||||
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?;
|
||||
|
||||
self.build_buy_instructions_with_accounts(params, pool, true)
|
||||
.await
|
||||
let pool_data = pumpswap::pool::Pool::fetch(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,
|
||||
@@ -94,66 +162,108 @@ impl PumpSwapInstructionBuilder {
|
||||
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?;
|
||||
|
||||
self.build_sell_instructions_with_accounts(params, pool)
|
||||
.await
|
||||
let pool_data = pumpswap::pool::Pool::fetch(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,
|
||||
params: &BuyParams,
|
||||
pool: Pubkey,
|
||||
base_mint: Pubkey,
|
||||
quote_mint: Pubkey,
|
||||
pool_base_token_reserves: u64,
|
||||
pool_quote_token_reserves: u64,
|
||||
auto_handle_wsol: bool,
|
||||
) -> Result<Vec<Instruction>> {
|
||||
if params.rpc.is_none() {
|
||||
return Err(anyhow!("RPC is not set"));
|
||||
}
|
||||
let rpc = params.rpc.as_ref().unwrap().clone();
|
||||
// 计算预期的代币数量
|
||||
let token_amount = get_buy_token_amount(rpc.as_ref(), &pool, params.sol_amount).await?;
|
||||
|
||||
// 计算滑点后的最大SOL数量
|
||||
let max_sol_amount = calculate_with_slippage_buy(
|
||||
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,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
);
|
||||
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(
|
||||
params.sol_amount,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
)
|
||||
} else {
|
||||
// base_amount_in
|
||||
params.sol_amount
|
||||
};
|
||||
|
||||
// 创建用户代币账户
|
||||
// Create user token accounts
|
||||
let user_base_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&base_mint,
|
||||
);
|
||||
let user_quote_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||
"e_mint,
|
||||
);
|
||||
|
||||
// 获取池的代币账户
|
||||
// Get pool token accounts
|
||||
let pool_base_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
&pool,
|
||||
¶ms.mint,
|
||||
&base_mint,
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
);
|
||||
|
||||
let pool_quote_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
&pool,
|
||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||
"e_mint,
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
);
|
||||
|
||||
let mut instructions = vec![];
|
||||
|
||||
if 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(),
|
||||
@@ -162,48 +272,61 @@ impl PumpSwapInstructionBuilder {
|
||||
),
|
||||
);
|
||||
instructions.push(
|
||||
// 将SOL转入wSOL ATA账户
|
||||
// Transfer SOL to wSOL ATA account
|
||||
solana_sdk::system_instruction::transfer(
|
||||
¶ms.payer.pubkey(),
|
||||
&user_quote_token_account,
|
||||
max_sol_amount,
|
||||
if quote_mint_is_wsol {
|
||||
&user_quote_token_account
|
||||
} else {
|
||||
&user_base_token_account
|
||||
},
|
||||
sol_amount,
|
||||
),
|
||||
);
|
||||
|
||||
// 同步wSOL余额
|
||||
// Sync wSOL balance
|
||||
instructions.push(
|
||||
spl_token::instruction::sync_native(
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
&user_quote_token_account,
|
||||
if quote_mint_is_wsol {
|
||||
&user_quote_token_account
|
||||
} else {
|
||||
&user_base_token_account
|
||||
},
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
// 创建用户的基础代币账户
|
||||
// Create user's base token account
|
||||
instructions.push(create_associated_token_account_idempotent(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
if quote_mint_is_wsol {
|
||||
&base_mint
|
||||
} else {
|
||||
"e_mint
|
||||
},
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
));
|
||||
|
||||
let coin_creator_vault_ata = coin_creator_vault_ata(params.creator);
|
||||
let coin_creator_vault_ata = coin_creator_vault_ata(params.creator, quote_mint);
|
||||
let coin_creator_vault_authority = coin_creator_vault_authority(params.creator);
|
||||
let fee_recipient_ata = fee_recipient_ata(accounts::FEE_RECIPIENT, quote_mint);
|
||||
|
||||
// 创建买入指令
|
||||
let accounts = vec![
|
||||
// Create buy instruction
|
||||
let mut accounts = vec![
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(pool, false), // pool_id (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(params.payer.pubkey(), true), // user (signer)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::GLOBAL_ACCOUNT, false), // global (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // WSOL_TOKEN_ACCOUNT (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(base_mint, false), // base_mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(quote_mint, false), // quote_mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(user_base_token_account, false), // user_base_token_account
|
||||
solana_sdk::instruction::AccountMeta::new(user_quote_token_account, false), // user_quote_token_account
|
||||
solana_sdk::instruction::AccountMeta::new(pool_base_token_account, false), // pool_base_token_account
|
||||
solana_sdk::instruction::AccountMeta::new(pool_quote_token_account, false), // pool_quote_token_account
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::FEE_RECIPIENT, false), // fee_recipient (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(accounts::FEE_RECIPIENT_ATA, false), // fee_recipient_ata
|
||||
solana_sdk::instruction::AccountMeta::new(fee_recipient_ata, false), // fee_recipient_ata
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // TOKEN_PROGRAM_ID (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // TOKEN_PROGRAM_ID (readonly, duplicated as in JS)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::SYSTEM_PROGRAM, false), // System Program (readonly)
|
||||
@@ -216,98 +339,120 @@ impl PumpSwapInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new(coin_creator_vault_ata, false), // coin_creator_vault_ata
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(coin_creator_vault_authority, false), // coin_creator_vault_authority (readonly)
|
||||
];
|
||||
if quote_mint_is_wsol {
|
||||
accounts.push(solana_sdk::instruction::AccountMeta::new(
|
||||
get_global_volume_accumulator_pda().unwrap(),
|
||||
false,
|
||||
));
|
||||
accounts.push(solana_sdk::instruction::AccountMeta::new(
|
||||
get_user_volume_accumulator_pda(¶ms.payer.pubkey()).unwrap(),
|
||||
false,
|
||||
));
|
||||
}
|
||||
|
||||
// 创建指令数据
|
||||
// Create instruction data
|
||||
let mut data = vec![];
|
||||
data.extend_from_slice(&BUY_DISCRIMINATOR);
|
||||
data.extend_from_slice(&token_amount.to_le_bytes());
|
||||
data.extend_from_slice(&max_sol_amount.to_le_bytes());
|
||||
if quote_mint_is_wsol {
|
||||
data.extend_from_slice(&BUY_DISCRIMINATOR);
|
||||
data.extend_from_slice(&token_amount.to_le_bytes());
|
||||
data.extend_from_slice(&sol_amount.to_le_bytes());
|
||||
} else {
|
||||
data.extend_from_slice(&SELL_DISCRIMINATOR);
|
||||
data.extend_from_slice(&sol_amount.to_le_bytes());
|
||||
data.extend_from_slice(&token_amount.to_le_bytes());
|
||||
}
|
||||
|
||||
instructions.push(Instruction {
|
||||
program_id: accounts::AMM_PROGRAM,
|
||||
accounts,
|
||||
data,
|
||||
});
|
||||
|
||||
if auto_handle_wsol {
|
||||
// 关闭wSOL ATA账户,回收租金
|
||||
// Close wSOL ATA account, reclaim rent
|
||||
instructions.push(
|
||||
spl_token::instruction::close_account(
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
&user_quote_token_account,
|
||||
if quote_mint_is_wsol {
|
||||
&user_quote_token_account
|
||||
} else {
|
||||
&user_base_token_account
|
||||
},
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
&[],
|
||||
&[¶ms.payer.pubkey()],
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(instructions)
|
||||
}
|
||||
|
||||
/// 使用提供的账户信息构建卖出指令
|
||||
/// Build sell instructions with provided account information
|
||||
async fn build_sell_instructions_with_accounts(
|
||||
&self,
|
||||
params: &SellParams,
|
||||
pool: Pubkey,
|
||||
base_mint: Pubkey,
|
||||
quote_mint: Pubkey,
|
||||
pool_base_token_reserves: u64,
|
||||
pool_quote_token_reserves: u64,
|
||||
auto_handle_wsol: bool,
|
||||
) -> Result<Vec<Instruction>> {
|
||||
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() {
|
||||
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"));
|
||||
}
|
||||
|
||||
// 计算预期的SOL数量
|
||||
let sol_amount = get_sell_sol_amount(rpc.as_ref(), &pool, amount).await?;
|
||||
|
||||
// 计算滑点后的最小SOL数量
|
||||
let min_sol_amount = calculate_with_slippage_sell(
|
||||
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 coin_creator_vault_ata = coin_creator_vault_ata(params.creator);
|
||||
let coin_creator_vault_ata = coin_creator_vault_ata(params.creator, quote_mint);
|
||||
let coin_creator_vault_authority = coin_creator_vault_authority(params.creator);
|
||||
let fee_recipient_ata = fee_recipient_ata(accounts::FEE_RECIPIENT, quote_mint);
|
||||
|
||||
let user_base_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&base_mint,
|
||||
);
|
||||
let user_quote_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||
"e_mint,
|
||||
);
|
||||
let pool_base_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
&pool,
|
||||
¶ms.mint,
|
||||
&base_mint,
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
);
|
||||
let pool_quote_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
&pool,
|
||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||
"e_mint,
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
);
|
||||
|
||||
let mut instructions = vec![];
|
||||
|
||||
// 插入wsol
|
||||
// Insert 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(),
|
||||
@@ -316,27 +461,31 @@ impl PumpSwapInstructionBuilder {
|
||||
),
|
||||
);
|
||||
|
||||
// 创建用户的代币账户
|
||||
// Create user's token account
|
||||
instructions.push(create_associated_token_account_idempotent(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
if quote_mint_is_wsol {
|
||||
&base_mint
|
||||
} else {
|
||||
"e_mint
|
||||
},
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
));
|
||||
|
||||
// 创建卖出指令
|
||||
let accounts = vec![
|
||||
// Create sell instruction
|
||||
let mut accounts = vec![
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(pool, false), // pool_id (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(params.payer.pubkey(), true), // user (signer)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::GLOBAL_ACCOUNT, false), // global (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // WSOL_TOKEN_ACCOUNT (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(base_mint, false), // mint (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(quote_mint, false), // WSOL_TOKEN_ACCOUNT (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(user_base_token_account, false), // user_base_token_account
|
||||
solana_sdk::instruction::AccountMeta::new(user_quote_token_account, false), // user_quote_token_account
|
||||
solana_sdk::instruction::AccountMeta::new(pool_base_token_account, false), // pool_base_token_account
|
||||
solana_sdk::instruction::AccountMeta::new(pool_quote_token_account, false), // pool_quote_token_account
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::FEE_RECIPIENT, false), // fee_recipient (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(accounts::FEE_RECIPIENT_ATA, false), // fee_recipient_ata
|
||||
solana_sdk::instruction::AccountMeta::new(fee_recipient_ata, false), // fee_recipient_ata
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // TOKEN_PROGRAM_ID (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // TOKEN_PROGRAM_ID (readonly, duplicated as in JS)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::SYSTEM_PROGRAM, false), // System Program (readonly)
|
||||
@@ -349,12 +498,28 @@ impl PumpSwapInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new(coin_creator_vault_ata, false), // coin_creator_vault_ata
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(coin_creator_vault_authority, false), // coin_creator_vault_authority (readonly)
|
||||
];
|
||||
if !quote_mint_is_wsol {
|
||||
accounts.push(solana_sdk::instruction::AccountMeta::new(
|
||||
get_global_volume_accumulator_pda().unwrap(),
|
||||
false,
|
||||
));
|
||||
accounts.push(solana_sdk::instruction::AccountMeta::new(
|
||||
get_user_volume_accumulator_pda(¶ms.payer.pubkey()).unwrap(),
|
||||
false,
|
||||
));
|
||||
}
|
||||
|
||||
// 创建指令数据
|
||||
// Create instruction data
|
||||
let mut data = vec![];
|
||||
data.extend_from_slice(&SELL_DISCRIMINATOR);
|
||||
data.extend_from_slice(&amount.to_le_bytes());
|
||||
data.extend_from_slice(&min_sol_amount.to_le_bytes());
|
||||
if quote_mint_is_wsol {
|
||||
data.extend_from_slice(&SELL_DISCRIMINATOR);
|
||||
data.extend_from_slice(&token_amount.to_le_bytes());
|
||||
data.extend_from_slice(&sol_amount.to_le_bytes());
|
||||
} else {
|
||||
data.extend_from_slice(&BUY_DISCRIMINATOR);
|
||||
data.extend_from_slice(&sol_amount.to_le_bytes());
|
||||
data.extend_from_slice(&token_amount.to_le_bytes());
|
||||
}
|
||||
|
||||
instructions.push(Instruction {
|
||||
program_id: accounts::AMM_PROGRAM,
|
||||
@@ -362,17 +527,15 @@ impl PumpSwapInstructionBuilder {
|
||||
data,
|
||||
});
|
||||
|
||||
let protocol_params = params
|
||||
.protocol_params
|
||||
.as_any()
|
||||
.downcast_ref::<PumpSwapParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for PumpSwap"))?;
|
||||
|
||||
if protocol_params.auto_handle_wsol {
|
||||
if auto_handle_wsol {
|
||||
instructions.push(
|
||||
close_account(
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
&user_quote_token_account,
|
||||
if quote_mint_is_wsol {
|
||||
&user_quote_token_account
|
||||
} else {
|
||||
&user_base_token_account
|
||||
},
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
&[¶ms.payer.pubkey()],
|
||||
|
||||
Reference in New Issue
Block a user