2025-07-09 22:29:29 +08:00
|
|
|
use anyhow::{anyhow, Result};
|
2025-07-10 18:14:21 +08:00
|
|
|
use solana_sdk::{instruction::Instruction, signer::Signer};
|
2025-08-03 22:51:11 +08:00
|
|
|
use solana_system_interface::instruction::transfer;
|
2025-07-09 22:29:29 +08:00
|
|
|
use spl_associated_token_account::instruction::create_associated_token_account_idempotent;
|
2025-07-12 01:14:05 +08:00
|
|
|
use spl_token::instruction::close_account;
|
2025-07-09 22:29:29 +08:00
|
|
|
|
|
|
|
|
use crate::{
|
2025-08-14 22:45:33 +08:00
|
|
|
constants::{
|
|
|
|
|
bonk::{accounts, BUY_EXECT_IN_DISCRIMINATOR, SELL_EXECT_IN_DISCRIMINATOR},
|
|
|
|
|
trade::trade::DEFAULT_SLIPPAGE,
|
2025-07-09 22:29:29 +08:00
|
|
|
},
|
2025-08-14 22:45:33 +08:00
|
|
|
trading::{
|
2025-08-18 18:00:14 +08:00
|
|
|
bonk::common::{get_pool_pda, get_vault_pda},
|
2025-08-14 22:45:33 +08:00
|
|
|
common::utils::get_token_balance,
|
|
|
|
|
core::{
|
|
|
|
|
params::{BonkParams, BuyParams, SellParams},
|
|
|
|
|
traits::InstructionBuilder,
|
|
|
|
|
},
|
2025-07-09 22:29:29 +08:00
|
|
|
},
|
2025-08-18 18:00:14 +08:00
|
|
|
utils::calc::bonk::{
|
|
|
|
|
get_buy_token_amount_from_sol_amount, get_sell_sol_amount_from_token_amount,
|
|
|
|
|
},
|
2025-07-09 22:29:29 +08:00
|
|
|
};
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
/// Instruction builder for Bonk protocol
|
2025-07-10 00:23:29 +08:00
|
|
|
pub struct BonkInstructionBuilder;
|
2025-07-09 22:29:29 +08:00
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2025-07-10 00:23:29 +08:00
|
|
|
impl InstructionBuilder for BonkInstructionBuilder {
|
2025-07-09 22:29:29 +08:00
|
|
|
async fn build_buy_instructions(&self, params: &BuyParams) -> Result<Vec<Instruction>> {
|
2025-07-10 23:54:49 +08:00
|
|
|
if params.sol_amount == 0 {
|
2025-07-09 22:29:29 +08:00
|
|
|
return Err(anyhow!("Amount cannot be zero"));
|
|
|
|
|
}
|
|
|
|
|
self.build_buy_instructions_with_accounts(params).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn build_sell_instructions(&self, params: &SellParams) -> Result<Vec<Instruction>> {
|
|
|
|
|
self.build_sell_instructions_with_accounts(params).await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-10 00:23:29 +08:00
|
|
|
impl BonkInstructionBuilder {
|
2025-08-18 18:00:14 +08:00
|
|
|
/// Build buy instructions with provided account information
|
2025-07-09 22:29:29 +08:00
|
|
|
async fn build_buy_instructions_with_accounts(
|
|
|
|
|
&self,
|
|
|
|
|
params: &BuyParams,
|
|
|
|
|
) -> Result<Vec<Instruction>> {
|
|
|
|
|
let protocol_params = params
|
|
|
|
|
.protocol_params
|
|
|
|
|
.as_any()
|
2025-07-10 00:23:29 +08:00
|
|
|
.downcast_ref::<BonkParams>()
|
|
|
|
|
.ok_or_else(|| anyhow!("Invalid protocol params for Bonk"))?;
|
2025-07-09 22:29:29 +08:00
|
|
|
|
|
|
|
|
let pool_state = get_pool_pda(¶ms.mint, &accounts::WSOL_TOKEN_ACCOUNT).unwrap();
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
// Create user token accounts
|
2025-07-09 22:29:29 +08:00
|
|
|
let user_base_token_account = spl_associated_token_account::get_associated_token_address(
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
¶ms.mint,
|
|
|
|
|
);
|
|
|
|
|
let user_quote_token_account = spl_associated_token_account::get_associated_token_address(
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
&accounts::WSOL_TOKEN_ACCOUNT,
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
// Get pool token accounts
|
2025-07-09 22:29:29 +08:00
|
|
|
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();
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
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;
|
2025-07-09 22:29:29 +08:00
|
|
|
|
2025-07-10 23:54:49 +08:00
|
|
|
let amount_in: u64 = params.sol_amount;
|
2025-07-09 22:29:29 +08:00
|
|
|
let share_fee_rate: u64 = 0;
|
2025-08-18 18:00:14 +08:00
|
|
|
let minimum_amount_out: u64 = get_buy_token_amount_from_sol_amount(
|
2025-07-09 22:29:29 +08:00
|
|
|
amount_in,
|
|
|
|
|
virtual_base,
|
|
|
|
|
virtual_quote,
|
2025-07-11 16:38:30 +08:00
|
|
|
real_base,
|
|
|
|
|
real_quote,
|
2025-07-09 22:29:29 +08:00
|
|
|
params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE) as u128,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut instructions = vec![];
|
|
|
|
|
|
|
|
|
|
if protocol_params.auto_handle_wsol {
|
2025-08-18 18:00:14 +08:00
|
|
|
// Handle wSOL
|
2025-07-09 22:29:29 +08:00
|
|
|
instructions.push(
|
2025-08-18 18:00:14 +08:00
|
|
|
// Create wSOL ATA account if it doesn't exist
|
2025-07-09 22:29:29 +08:00
|
|
|
create_associated_token_account_idempotent(
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
&accounts::WSOL_TOKEN_ACCOUNT,
|
|
|
|
|
&accounts::TOKEN_PROGRAM,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
instructions.push(
|
2025-08-18 18:00:14 +08:00
|
|
|
// Transfer SOL to wSOL ATA account
|
2025-08-03 22:51:11 +08:00
|
|
|
transfer(¶ms.payer.pubkey(), &user_quote_token_account, amount_in),
|
2025-07-09 22:29:29 +08:00
|
|
|
);
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
// Sync wSOL balance
|
2025-07-09 22:29:29 +08:00
|
|
|
instructions.push(
|
|
|
|
|
spl_token::instruction::sync_native(
|
|
|
|
|
&accounts::TOKEN_PROGRAM,
|
|
|
|
|
&user_quote_token_account,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
// Create user's base token account
|
2025-07-09 22:29:29 +08:00
|
|
|
instructions.push(create_associated_token_account_idempotent(
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
¶ms.mint,
|
|
|
|
|
&accounts::TOKEN_PROGRAM,
|
|
|
|
|
));
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
// Create buy instruction
|
2025-07-09 22:29:29 +08:00
|
|
|
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)
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::GLOBAL_CONFIG, false), // Global Config (readonly)
|
2025-08-22 21:49:14 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(protocol_params.platform_onfig, false), // Platform Config (readonly)
|
2025-07-09 22:29:29 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new(pool_state, false), // Pool State
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(user_base_token_account, false), // User Base Token
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(user_quote_token_account, false), // User Quote Token
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(base_vault_account, false), // Base Vault
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(quote_vault_account, false), // Quote Vault
|
2025-07-19 21:28:01 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // Base Token Mint (readonly)
|
2025-07-09 22:29:29 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Quote Token Mint (readonly)
|
2025-08-18 18:00:14 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(
|
|
|
|
|
protocol_params.mint_token_program,
|
|
|
|
|
false,
|
|
|
|
|
), // Base Token Program (readonly)
|
2025-07-09 22:29:29 +08:00
|
|
|
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)
|
2025-07-10 00:23:29 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::BONK, false), // Program (readonly)
|
2025-08-22 21:49:14 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::SYSTEM_PROGRAM, false), // System Program (readonly)
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(protocol_params.platform_associated_account, false), // Platform Associated Account
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(protocol_params.creator_associated_account, false), // Creator Associated Account
|
2025-07-09 22:29:29 +08:00
|
|
|
];
|
2025-08-18 18:00:14 +08:00
|
|
|
// Create instruction data
|
2025-07-09 22:29:29 +08:00
|
|
|
let mut data = vec![];
|
|
|
|
|
data.extend_from_slice(&BUY_EXECT_IN_DISCRIMINATOR);
|
|
|
|
|
data.extend_from_slice(&amount_in.to_le_bytes());
|
|
|
|
|
data.extend_from_slice(&minimum_amount_out.to_le_bytes());
|
|
|
|
|
data.extend_from_slice(&share_fee_rate.to_le_bytes());
|
|
|
|
|
|
2025-08-03 22:51:11 +08:00
|
|
|
instructions.push(Instruction { program_id: accounts::BONK, accounts, data });
|
2025-07-09 22:29:29 +08:00
|
|
|
|
|
|
|
|
if protocol_params.auto_handle_wsol {
|
2025-08-18 18:00:14 +08:00
|
|
|
// Close wSOL ATA account, reclaim rent
|
2025-07-09 22:29:29 +08:00
|
|
|
instructions.push(
|
|
|
|
|
spl_token::instruction::close_account(
|
|
|
|
|
&accounts::TOKEN_PROGRAM,
|
|
|
|
|
&user_quote_token_account,
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
&[],
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(instructions)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
/// Build sell instructions with provided account information
|
2025-07-09 22:29:29 +08:00
|
|
|
async fn build_sell_instructions_with_accounts(
|
|
|
|
|
&self,
|
|
|
|
|
params: &SellParams,
|
|
|
|
|
) -> Result<Vec<Instruction>> {
|
|
|
|
|
if params.rpc.is_none() {
|
|
|
|
|
return Err(anyhow!("RPC is not set"));
|
|
|
|
|
}
|
2025-08-18 18:00:14 +08:00
|
|
|
|
|
|
|
|
let protocol_params = params
|
|
|
|
|
.protocol_params
|
|
|
|
|
.as_any()
|
|
|
|
|
.downcast_ref::<BonkParams>()
|
|
|
|
|
.ok_or_else(|| anyhow!("Invalid protocol params for Bonk"))?;
|
|
|
|
|
|
2025-07-09 22:29:29 +08:00
|
|
|
let rpc = params.rpc.as_ref().unwrap().clone();
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
// Get token balance
|
2025-07-10 23:54:49 +08:00
|
|
|
let mut amount = params.token_amount;
|
|
|
|
|
if params.token_amount.is_none() || params.token_amount.unwrap_or(0) == 0 {
|
2025-07-09 22:29:29 +08:00
|
|
|
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"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let pool_state = get_pool_pda(¶ms.mint, &accounts::WSOL_TOKEN_ACCOUNT).unwrap();
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
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
|
2025-07-09 22:29:29 +08:00
|
|
|
let user_base_token_account = spl_associated_token_account::get_associated_token_address(
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
¶ms.mint,
|
|
|
|
|
);
|
|
|
|
|
let user_quote_token_account = spl_associated_token_account::get_associated_token_address(
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
&accounts::WSOL_TOKEN_ACCOUNT,
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
// Get pool token accounts
|
2025-07-09 22:29:29 +08:00
|
|
|
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 share_fee_rate: u64 = 0;
|
|
|
|
|
|
|
|
|
|
let mut instructions = vec![];
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
// Handle wSOL
|
2025-07-09 22:29:29 +08:00
|
|
|
instructions.push(
|
2025-08-18 18:00:14 +08:00
|
|
|
// Create wSOL ATA account if it doesn't exist
|
2025-07-09 22:29:29 +08:00
|
|
|
create_associated_token_account_idempotent(
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
&accounts::WSOL_TOKEN_ACCOUNT,
|
|
|
|
|
&accounts::TOKEN_PROGRAM,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
// Create sell instruction
|
2025-07-09 22:29:29 +08:00
|
|
|
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)
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::GLOBAL_CONFIG, false), // Global Config (readonly)
|
2025-08-22 21:49:14 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(protocol_params.platform_onfig, false), // Platform Config (readonly)
|
2025-07-09 22:29:29 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new(pool_state, false), // Pool State
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(user_base_token_account, false), // User Base Token
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(user_quote_token_account, false), // User Quote Token
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(base_vault_account, false), // Base Vault
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(quote_vault_account, false), // Quote Vault
|
2025-07-19 21:28:01 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // Base Token Mint (readonly)
|
2025-07-09 22:29:29 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Quote Token Mint (readonly)
|
2025-08-18 18:00:14 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(
|
|
|
|
|
protocol_params.mint_token_program,
|
|
|
|
|
false,
|
|
|
|
|
), // Base Token Program (readonly)
|
2025-07-09 22:29:29 +08:00
|
|
|
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)
|
2025-07-10 00:23:29 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::BONK, false), // Program (readonly)
|
2025-08-22 21:49:14 +08:00
|
|
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::SYSTEM_PROGRAM, false), // System Program (readonly)
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(protocol_params.platform_associated_account, false), // Platform Associated Account
|
|
|
|
|
solana_sdk::instruction::AccountMeta::new(protocol_params.creator_associated_account, false), // Creator Associated Account
|
2025-07-09 22:29:29 +08:00
|
|
|
];
|
|
|
|
|
|
2025-08-18 18:00:14 +08:00
|
|
|
// Create instruction data
|
2025-07-09 22:29:29 +08:00
|
|
|
let mut data = vec![];
|
|
|
|
|
data.extend_from_slice(&SELL_EXECT_IN_DISCRIMINATOR);
|
|
|
|
|
data.extend_from_slice(&amount.to_le_bytes());
|
|
|
|
|
data.extend_from_slice(&minimum_amount_out.to_le_bytes());
|
|
|
|
|
data.extend_from_slice(&share_fee_rate.to_le_bytes());
|
|
|
|
|
|
2025-08-03 22:51:11 +08:00
|
|
|
instructions.push(Instruction { program_id: accounts::BONK, accounts, data });
|
2025-07-09 22:29:29 +08:00
|
|
|
|
2025-07-19 21:28:01 +08:00
|
|
|
if protocol_params.auto_handle_wsol {
|
|
|
|
|
instructions.push(
|
|
|
|
|
close_account(
|
|
|
|
|
&accounts::TOKEN_PROGRAM,
|
|
|
|
|
&user_quote_token_account,
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
¶ms.payer.pubkey(),
|
|
|
|
|
&[¶ms.payer.pubkey()],
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-07-12 01:14:05 +08:00
|
|
|
|
2025-07-09 22:29:29 +08:00
|
|
|
Ok(instructions)
|
|
|
|
|
}
|
|
|
|
|
}
|