From 839ed320f91ad7173594c887c58f3e533e565dfb Mon Sep 17 00:00:00 2001 From: Omar Date: Tue, 12 Mar 2024 16:32:33 +0300 Subject: [PATCH] [Ociswap v2 Adapter v1]: Account for the tick spacing when providing liquidity. --- packages/ociswap-v2-adapter-v1/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/ociswap-v2-adapter-v1/src/lib.rs b/packages/ociswap-v2-adapter-v1/src/lib.rs index e1f123fc..b25e8ae0 100644 --- a/packages/ociswap-v2-adapter-v1/src/lib.rs +++ b/packages/ociswap-v2-adapter-v1/src/lib.rs @@ -163,6 +163,24 @@ pub mod adapter { .expect(OVERFLOW_ERROR); let offset = 29959; + // Ociswap, just like Caviarnine, have a tick spacing parameter that + // means that not all ticks are valid. A valid tick is one that is + // divisible by the tick spacing. Therefore, the following step will + // convert the offset defined above to be valid for the tick spacing + // of the pool. If the offset is divisible by the tick spacing then + // nothing needs to be done. If it is not, then we round up to the + // tick space. + let tick_spacing = pool.tick_spacing() as i32; + let offset = if offset % tick_spacing == 0 { + offset + } else { + offset + .checked_div(tick_spacing) + .and_then(|value| value.checked_mul(tick_spacing)) + .and_then(|value| value.checked_add(tick_spacing)) + .expect(OVERFLOW_ERROR) + }; + let lower_tick = active_tick.checked_sub(offset).expect(OVERFLOW_ERROR); let upper_tick =