Description
I noticed this potential issue when trying to calculate the maximum amount I could swap within a certain tick array. Given I have an input amount that exceeds the available liquidity within a TickArraySequence I would like to calculate the maximum output amount.
I assume this should be possible by calling compute_swap with the sqrt_price_limit set to the sqrt_price corresponding to the first tick outside the TickArraySequence. The TickArraySequence has two methods start_index() and end_index() which calculate the last tick inside the TickArraySequence.
But then I noticed the implementation of compute_swap behaves slightly different for a_to_b true vs false:
pub fn next_initialized_tick(
&self,
tick_index: i32,
) -> Result<(Option<&TickFacade>, i32), CoreError> {
let array_end_index = self.end_index();
if tick_index >= array_end_index {
return Err(INVALID_TICK_ARRAY_SEQUENCE);
}
next_initialized_tick returns Err(INVALID_TICK_ARRAY_SEQUENCE when the current_tick_index has reached the last tick inside the TickArraySequence.
pub fn prev_initialized_tick(
&self,
tick_index: i32,
) -> Result<(Option<&TickFacade>, i32), CoreError> {
let array_start_index = self.start_index();
if tick_index < array_start_index {
return Err(INVALID_TICK_ARRAY_SEQUENCE);
}
prev_initialized_tick returns Err(INVALID_TICK_ARRAY_SEQUENCE when the current_tick_index has reached the first tick outside the TickArraySequence.
The issue here is that the compute swap method returns early with an Error and I don't have access to the so far accumulated values for amount_remaining and amount_calculated. I was wondering if the difference in behavior I pointed out is an implementation error and should be corrected. Anyways I would appreciate your advice how I can reach the desired behavior of calculating the maximum possible swap given a TickArraySequence.