update example
This commit is contained in:
@@ -24,7 +24,7 @@ pub struct BonkInstructionBuilder;
|
||||
#[async_trait::async_trait]
|
||||
impl InstructionBuilder for BonkInstructionBuilder {
|
||||
async fn build_buy_instructions(&self, params: &BuyParams) -> Result<Vec<Instruction>> {
|
||||
if params.amount_sol == 0 {
|
||||
if params.sol_amount == 0 {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
self.build_buy_instructions_with_accounts(params).await
|
||||
@@ -81,7 +81,7 @@ impl BonkInstructionBuilder {
|
||||
real_quote_before = pool.real_quote as u128;
|
||||
}
|
||||
|
||||
let amount_in: u64 = params.amount_sol;
|
||||
let amount_in: u64 = params.sol_amount;
|
||||
let share_fee_rate: u64 = 0;
|
||||
let minimum_amount_out: u64 = get_amount_out(
|
||||
amount_in,
|
||||
@@ -194,8 +194,8 @@ impl BonkInstructionBuilder {
|
||||
let rpc = params.rpc.as_ref().unwrap().clone();
|
||||
|
||||
// 获取代币余额
|
||||
let mut amount = params.amount_token;
|
||||
if params.amount_token.is_none() || params.amount_token.unwrap_or(0) == 0 {
|
||||
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);
|
||||
|
||||
@@ -38,7 +38,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
||||
.downcast_ref::<PumpFunParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for PumpFun"))?;
|
||||
|
||||
if params.amount_sol == 0 {
|
||||
if params.sol_amount == 0 {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
|
||||
@@ -49,13 +49,13 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
||||
};
|
||||
|
||||
let max_sol_cost = calculate_with_slippage_buy(
|
||||
params.amount_sol,
|
||||
params.sol_amount,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
);
|
||||
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.amount_sol);
|
||||
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_to_lamports(0.01) {
|
||||
25547619 * 1_000_000_u64
|
||||
@@ -91,7 +91,7 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
||||
}
|
||||
|
||||
async fn build_sell_instructions(&self, params: &SellParams) -> Result<Vec<Instruction>> {
|
||||
let amount_token = if let Some(amount) = params.amount_token {
|
||||
let token_amount = if let Some(amount) = params.token_amount {
|
||||
if amount == 0 {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
@@ -113,9 +113,9 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
||||
return Err(anyhow!("RPC client is required to get token balance"));
|
||||
};
|
||||
|
||||
let mut amount_token = amount_token;
|
||||
if amount_token > balance_u64 {
|
||||
amount_token = balance_u64;
|
||||
let mut token_amount = token_amount;
|
||||
if token_amount > balance_u64 {
|
||||
token_amount = balance_u64;
|
||||
}
|
||||
|
||||
let mut instructions = vec![sell(
|
||||
@@ -124,13 +124,13 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
||||
&creator_vault_pda,
|
||||
&FEE_RECIPIENT,
|
||||
Sell {
|
||||
_amount: amount_token,
|
||||
_amount: token_amount,
|
||||
_min_sol_output: 1,
|
||||
},
|
||||
)];
|
||||
|
||||
// 如果卖出全部代币,关闭账户
|
||||
if amount_token >= balance_u64 {
|
||||
if token_amount >= balance_u64 {
|
||||
instructions.push(close_account(
|
||||
&spl_token::ID,
|
||||
&ata,
|
||||
|
||||
@@ -31,7 +31,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
.downcast_ref::<PumpSwapParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for PumpSwap"))?;
|
||||
|
||||
if params.amount_sol == 0 {
|
||||
if params.sol_amount == 0 {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
|
||||
@@ -114,11 +114,11 @@ impl PumpSwapInstructionBuilder {
|
||||
}
|
||||
let rpc = params.rpc.as_ref().unwrap().clone();
|
||||
// 计算预期的代币数量
|
||||
let token_amount = get_buy_token_amount(rpc.as_ref(), &pool, params.amount_sol).await?;
|
||||
let token_amount = get_buy_token_amount(rpc.as_ref(), &pool, params.sol_amount).await?;
|
||||
|
||||
// 计算滑点后的最大SOL数量
|
||||
let max_sol_amount = calculate_with_slippage_buy(
|
||||
params.amount_sol,
|
||||
params.sol_amount,
|
||||
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
|
||||
);
|
||||
|
||||
@@ -257,8 +257,8 @@ impl PumpSwapInstructionBuilder {
|
||||
let rpc = params.rpc.as_ref().unwrap().clone();
|
||||
|
||||
// 获取代币余额
|
||||
let mut amount = params.amount_token;
|
||||
if params.amount_token.is_none() {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user