defituna_client/utils/
fusion.rs

1use fusionamm_client::get_tick_array_address;
2use fusionamm_core::{get_initializable_tick_index, get_tick_array_start_tick_index, TICK_ARRAY_SIZE};
3use solana_program::pubkey::Pubkey;
4
5pub fn get_swap_tick_arrays(tick_current_index: i32, tick_spacing: u16, whirlpool_address: &Pubkey) -> [Pubkey; 5] {
6    let mut tick_array_addresses = [Pubkey::default(); 5];
7
8    let current_tick_array_start_tick_index = get_tick_array_start_tick_index(tick_current_index, tick_spacing);
9    for offset in 0..5 {
10        let start_index = current_tick_array_start_tick_index + (offset - 2) * tick_spacing as i32 * TICK_ARRAY_SIZE as i32;
11        let pda = get_tick_array_address(whirlpool_address, start_index).unwrap().0;
12        tick_array_addresses[offset as usize] = pda;
13    }
14
15    tick_array_addresses
16}
17
18pub fn get_tick_arrays_for_rebalanced_position(
19    tick_current_index: i32,
20    tick_spacing: u16,
21    whirlpool_address: &Pubkey,
22    position_tick_lower_index: i32,
23    position_tick_upper_index: i32,
24) -> [(Pubkey, i32); 2] {
25    // Calculate the new position's range.
26    let position_range = position_tick_upper_index - position_tick_lower_index;
27    let new_tick_lower_index = get_initializable_tick_index(tick_current_index - position_range / 2, tick_spacing, None);
28    let new_tick_upper_index = new_tick_lower_index + position_range;
29
30    let lower_tick_array_start_index = orca_whirlpools_core::get_tick_array_start_tick_index(new_tick_lower_index, tick_spacing);
31    let lower_tick_array_address = get_tick_array_address(&whirlpool_address, lower_tick_array_start_index)
32        .unwrap()
33        .0;
34
35    let upper_tick_array_start_index = orca_whirlpools_core::get_tick_array_start_tick_index(new_tick_upper_index, tick_spacing);
36    let upper_tick_array_address = get_tick_array_address(&whirlpool_address, upper_tick_array_start_index)
37        .unwrap()
38        .0;
39
40    [
41        (lower_tick_array_address, lower_tick_array_start_index),
42        (upper_tick_array_address, upper_tick_array_start_index),
43    ]
44}