use crate::instructions::SwapRewardTwoHopOrca;
use crate::utils::orca::get_swap_tick_arrays;
use orca_whirlpools_client::{DecodedAccount, Whirlpool, get_oracle_address, WHIRLPOOL_ID};
use solana_instruction::{AccountMeta, Instruction};
use solana_pubkey::Pubkey;
use spl_associated_token_account::get_associated_token_address_with_program_id;
pub fn swap_reward_two_hop_orca_instruction(
authority: &Pubkey,
treasury: &Pubkey,
input_token_mint: &Pubkey,
intermediate_token_mint: &Pubkey,
reward_token_mint: &Pubkey,
input_token_program: &Pubkey,
whirlpool_one: &DecodedAccount<Whirlpool>,
whirlpool_two: &DecodedAccount<Whirlpool>,
) -> Instruction {
let input_token_account = get_associated_token_address_with_program_id(&treasury, &input_token_mint, &input_token_program);
let reward_token_account = get_associated_token_address_with_program_id(&treasury, &reward_token_mint, &spl_token::id());
let oracle_one = get_oracle_address(&whirlpool_one.address).unwrap().0;
let oracle_two = get_oracle_address(&whirlpool_two.address).unwrap().0;
let ticks_array_one = get_swap_tick_arrays(whirlpool_one.data.tick_current_index, whirlpool_one.data.tick_spacing, &whirlpool_one.address);
let ticks_array_two = get_swap_tick_arrays(whirlpool_two.data.tick_current_index, whirlpool_two.data.tick_spacing, &whirlpool_two.address);
let ix_builder = SwapRewardTwoHopOrca {
authority: *authority,
treasury: *treasury,
input_token_mint: *input_token_mint,
intermediate_token_mint: *intermediate_token_mint,
reward_token_mint: *reward_token_mint,
input_token_program: *input_token_program,
intermediate_token_program: spl_token::id(),
reward_token_program: spl_token::id(),
input_token_account,
reward_token_account,
whirlpool_program: WHIRLPOOL_ID,
whirlpool_one: whirlpool_one.address,
whirlpool_two: whirlpool_two.address,
memo_program: spl_memo::ID,
};
ix_builder.instruction_with_remaining_accounts(&[
AccountMeta::new(whirlpool_one.data.token_vault_a, false),
AccountMeta::new(whirlpool_one.data.token_vault_b, false),
AccountMeta::new(oracle_one, false),
AccountMeta::new(ticks_array_one[0], false),
AccountMeta::new(ticks_array_one[1], false),
AccountMeta::new(ticks_array_one[2], false),
AccountMeta::new(ticks_array_one[3], false),
AccountMeta::new(ticks_array_one[4], false),
AccountMeta::new(whirlpool_two.data.token_vault_a, false),
AccountMeta::new(whirlpool_two.data.token_vault_b, false),
AccountMeta::new(oracle_two, false),
AccountMeta::new(ticks_array_two[0], false),
AccountMeta::new(ticks_array_two[1], false),
AccountMeta::new(ticks_array_two[2], false),
AccountMeta::new(ticks_array_two[3], false),
AccountMeta::new(ticks_array_two[4], false),
])
}