use solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
};
use super::types::discriminators;
use crate::constants::*;
use crate::pda;
pub fn buy_exact_in(
payer: &Pubkey,
global_config: &Pubkey,
platform_config: &Pubkey,
pool_state: &Pubkey,
user_base_token: &Pubkey,
user_quote_token: &Pubkey,
base_vault: &Pubkey,
quote_vault: &Pubkey,
base_token_mint: &Pubkey,
quote_token_mint: &Pubkey,
base_token_program: &Pubkey,
quote_token_program: &Pubkey,
amount_in: u64,
minimum_amount_out: u64,
share_fee_rate: u64,
) -> Instruction {
let (authority, _) = pda::get_authority_pda();
let (event_authority, _) = pda::get_event_authority_pda();
let accounts = vec![
AccountMeta::new(*payer, true),
AccountMeta::new_readonly(authority, false),
AccountMeta::new_readonly(*global_config, false),
AccountMeta::new_readonly(*platform_config, false),
AccountMeta::new(*pool_state, false),
AccountMeta::new(*user_base_token, false),
AccountMeta::new(*user_quote_token, false),
AccountMeta::new(*base_vault, false),
AccountMeta::new(*quote_vault, false),
AccountMeta::new_readonly(*base_token_mint, false),
AccountMeta::new_readonly(*quote_token_mint, false),
AccountMeta::new_readonly(*base_token_program, false),
AccountMeta::new_readonly(*quote_token_program, false),
AccountMeta::new_readonly(event_authority, false),
AccountMeta::new_readonly(PROGRAM_ID, false),
];
let mut data = discriminators::BUY_EXACT_IN.to_vec();
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());
Instruction {
program_id: PROGRAM_ID,
accounts,
data,
}
}
pub fn buy_exact_out(
payer: &Pubkey,
global_config: &Pubkey,
platform_config: &Pubkey,
pool_state: &Pubkey,
user_base_token: &Pubkey,
user_quote_token: &Pubkey,
base_vault: &Pubkey,
quote_vault: &Pubkey,
base_token_mint: &Pubkey,
quote_token_mint: &Pubkey,
base_token_program: &Pubkey,
quote_token_program: &Pubkey,
amount_out: u64,
maximum_amount_in: u64,
share_fee_rate: u64,
) -> Instruction {
let (authority, _) = pda::get_authority_pda();
let (event_authority, _) = pda::get_event_authority_pda();
let accounts = vec![
AccountMeta::new(*payer, true),
AccountMeta::new_readonly(authority, false),
AccountMeta::new_readonly(*global_config, false),
AccountMeta::new_readonly(*platform_config, false),
AccountMeta::new(*pool_state, false),
AccountMeta::new(*user_base_token, false),
AccountMeta::new(*user_quote_token, false),
AccountMeta::new(*base_vault, false),
AccountMeta::new(*quote_vault, false),
AccountMeta::new_readonly(*base_token_mint, false),
AccountMeta::new_readonly(*quote_token_mint, false),
AccountMeta::new_readonly(*base_token_program, false),
AccountMeta::new_readonly(*quote_token_program, false),
AccountMeta::new_readonly(event_authority, false),
AccountMeta::new_readonly(PROGRAM_ID, false),
];
let mut data = discriminators::BUY_EXACT_OUT.to_vec();
data.extend_from_slice(&amount_out.to_le_bytes()); data.extend_from_slice(&maximum_amount_in.to_le_bytes()); data.extend_from_slice(&share_fee_rate.to_le_bytes());
Instruction {
program_id: PROGRAM_ID,
accounts,
data,
}
}
pub fn sell_exact_in(
payer: &Pubkey,
global_config: &Pubkey,
platform_config: &Pubkey,
pool_state: &Pubkey,
user_base_token: &Pubkey,
user_quote_token: &Pubkey,
base_vault: &Pubkey,
quote_vault: &Pubkey,
base_token_mint: &Pubkey,
quote_token_mint: &Pubkey,
base_token_program: &Pubkey,
quote_token_program: &Pubkey,
amount_in: u64,
minimum_amount_out: u64,
share_fee_rate: u64,
) -> Instruction {
let (authority, _) = pda::get_authority_pda();
let (event_authority, _) = pda::get_event_authority_pda();
let accounts = vec![
AccountMeta::new(*payer, true),
AccountMeta::new_readonly(authority, false),
AccountMeta::new_readonly(*global_config, false),
AccountMeta::new_readonly(*platform_config, false),
AccountMeta::new(*pool_state, false),
AccountMeta::new(*user_base_token, false),
AccountMeta::new(*user_quote_token, false),
AccountMeta::new(*base_vault, false),
AccountMeta::new(*quote_vault, false),
AccountMeta::new_readonly(*base_token_mint, false),
AccountMeta::new_readonly(*quote_token_mint, false),
AccountMeta::new_readonly(*base_token_program, false),
AccountMeta::new_readonly(*quote_token_program, false),
AccountMeta::new_readonly(event_authority, false),
AccountMeta::new_readonly(PROGRAM_ID, false),
];
let mut data = discriminators::SELL_EXACT_IN.to_vec();
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());
Instruction {
program_id: PROGRAM_ID,
accounts,
data,
}
}
pub fn sell_exact_out(
payer: &Pubkey,
global_config: &Pubkey,
platform_config: &Pubkey,
pool_state: &Pubkey,
user_base_token: &Pubkey,
user_quote_token: &Pubkey,
base_vault: &Pubkey,
quote_vault: &Pubkey,
base_token_mint: &Pubkey,
quote_token_mint: &Pubkey,
base_token_program: &Pubkey,
quote_token_program: &Pubkey,
amount_out: u64,
maximum_amount_in: u64,
share_fee_rate: u64,
) -> Instruction {
let (authority, _) = pda::get_authority_pda();
let (event_authority, _) = pda::get_event_authority_pda();
let accounts = vec![
AccountMeta::new(*payer, true),
AccountMeta::new_readonly(authority, false),
AccountMeta::new_readonly(*global_config, false),
AccountMeta::new_readonly(*platform_config, false),
AccountMeta::new(*pool_state, false),
AccountMeta::new(*user_base_token, false),
AccountMeta::new(*user_quote_token, false),
AccountMeta::new(*base_vault, false),
AccountMeta::new(*quote_vault, false),
AccountMeta::new_readonly(*base_token_mint, false),
AccountMeta::new_readonly(*quote_token_mint, false),
AccountMeta::new_readonly(*base_token_program, false),
AccountMeta::new_readonly(*quote_token_program, false),
AccountMeta::new_readonly(event_authority, false),
AccountMeta::new_readonly(PROGRAM_ID, false),
];
let mut data = discriminators::SELL_EXACT_OUT.to_vec();
data.extend_from_slice(&amount_out.to_le_bytes()); data.extend_from_slice(&maximum_amount_in.to_le_bytes()); data.extend_from_slice(&share_fee_rate.to_le_bytes());
Instruction {
program_id: PROGRAM_ID,
accounts,
data,
}
}