From 806852a5e886113be2a5bf7456b11f959ab0c37d Mon Sep 17 00:00:00 2001 From: Sniezka Date: Fri, 1 Dec 2023 12:57:24 +0100 Subject: [PATCH 1/4] Removed seconds per liquidity --- src/contracts/entrypoints.rs | 7 - src/contracts/storage/pool.rs | 207 ------------------------------ src/contracts/storage/position.rs | 29 +---- src/contracts/storage/tick.rs | 22 +--- src/lib.rs | 26 ---- src/test_helpers/lib.rs | 4 - 6 files changed, 9 insertions(+), 286 deletions(-) diff --git a/src/contracts/entrypoints.rs b/src/contracts/entrypoints.rs index a4428ac3..da8b371b 100644 --- a/src/contracts/entrypoints.rs +++ b/src/contracts/entrypoints.rs @@ -213,13 +213,6 @@ pub trait Invariant { #[ink(message)] fn get_all_positions(&mut self) -> Vec; - #[ink(message)] - fn update_position_seconds_per_liquidity( - &mut self, - index: u32, - pool_key: PoolKey, - ) -> Result<(), InvariantError>; - /// Allows an authorized user (owner of the position) to claim collected fees. /// /// # Parameters diff --git a/src/contracts/storage/pool.rs b/src/contracts/storage/pool.rs index f1e46e2b..208cab7e 100644 --- a/src/contracts/storage/pool.rs +++ b/src/contracts/storage/pool.rs @@ -8,7 +8,6 @@ use crate::{ fee_growth::FeeGrowth, liquidity::Liquidity, percentage::Percentage, - seconds_per_liquidity::{calculate_seconds_per_liquidity_inside, SecondsPerLiquidity}, sqrt_price::{log::get_tick_at_sqrt_price, sqrt_price::SqrtPrice}, token_amount::TokenAmount, }, @@ -31,7 +30,6 @@ pub struct Pool { pub fee_growth_global_y: FeeGrowth, pub fee_protocol_token_x: TokenAmount, pub fee_protocol_token_y: TokenAmount, - pub seconds_per_liquidity_global: SecondsPerLiquidity, pub start_timestamp: u64, pub last_timestamp: u64, pub fee_receiver: AccountId, @@ -49,7 +47,6 @@ impl Default for Pool { fee_growth_global_y: FeeGrowth::default(), fee_protocol_token_x: TokenAmount(0u128), fee_protocol_token_y: TokenAmount(0u128), - seconds_per_liquidity_global: SecondsPerLiquidity::default(), start_timestamp: u64::default(), last_timestamp: u64::default(), fee_receiver: AccountId::from([0x0; 32]), @@ -132,48 +129,6 @@ impl Pool { } } - pub fn update_seconds_per_liquidity_global( - &mut self, - current_timestamp: u64, - ) -> TrackableResult<()> { - // let seconds_per_liquidity_global = - // SecondsPerLiquidity::calculate_seconds_per_liquidity_global( - // self.liquidity, - // current_timestamp, - // self.last_timestamp, - // )?; - - // self.seconds_per_liquidity_global = self - // .seconds_per_liquidity_global - // .unchecked_add(seconds_per_liquidity_global); - self.last_timestamp = current_timestamp; - Ok(()) - } - - pub fn update_seconds_per_liquidity_inside( - &mut self, - tick_lower: i32, - tick_lower_seconds_per_liquidity_outside: SecondsPerLiquidity, - tick_upper: i32, - tick_upper_seconds_per_liquidity_outside: SecondsPerLiquidity, - current_timestamp: u64, - ) -> TrackableResult { - if !self.liquidity.is_zero() { - ok_or_mark_trace!(self.update_seconds_per_liquidity_global(current_timestamp))?; - } else { - self.last_timestamp = current_timestamp; - } - - ok_or_mark_trace!(calculate_seconds_per_liquidity_inside( - tick_lower, - tick_upper, - self.current_tick_index, - tick_lower_seconds_per_liquidity_outside, - tick_upper_seconds_per_liquidity_outside, - self.seconds_per_liquidity_global, - )) - } - pub fn cross_tick( &mut self, result: SwapResult, @@ -519,166 +474,4 @@ mod tests { assert_eq!(pool.liquidity, Liquidity::from_integer(5)) } } - - // #[test] - // fn test_update_seconds_per_liquidity_inside() { - // let mut tick_lower = Tick { - // index: 0, - // seconds_per_liquidity_outside: SecondsPerLiquidity::new(3012300000), - // ..Default::default() - // }; - // let mut tick_upper = Tick { - // index: 10, - // seconds_per_liquidity_outside: SecondsPerLiquidity::new(2030400000), - // ..Default::default() - // }; - // let mut pool = Pool { - // liquidity: Liquidity::from_integer(1000), - // start_timestamp: 0, - // last_timestamp: 0, - // seconds_per_liquidity_global: SecondsPerLiquidity::new(0), - // ..Default::default() - // }; - // let mut current_timestamp = 0; - - // { - // current_timestamp += 100; - // pool.current_tick_index = -10; - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(seconds_per_liquidity_inside.unwrap().get(), 981900000); - // } - // { - // current_timestamp += 100; - // pool.current_tick_index = 0; - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!( - // seconds_per_liquidity_inside.unwrap().get(), - // 199999999999994957300000 - // ); - // } - // { - // current_timestamp += 100; - // tick_lower.seconds_per_liquidity_outside = SecondsPerLiquidity::new(2012333200); - // tick_upper.seconds_per_liquidity_outside = SecondsPerLiquidity::new(3012333310); - // pool.current_tick_index = 20; - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(seconds_per_liquidity_inside.unwrap().get(), 1000000110); - // } - // { - // current_timestamp += 100; - // tick_lower.seconds_per_liquidity_outside = SecondsPerLiquidity::new(201233320000); - // tick_upper.seconds_per_liquidity_outside = SecondsPerLiquidity::new(301233331000); - // pool.current_tick_index = 20; - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(seconds_per_liquidity_inside.unwrap().get(), 100000011000); - // } - // { - // current_timestamp += 100; - // tick_lower.seconds_per_liquidity_outside = SecondsPerLiquidity::new(201233320000); - // tick_upper.seconds_per_liquidity_outside = SecondsPerLiquidity::new(301233331000); - // pool.current_tick_index = -20; - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!( - // seconds_per_liquidity_inside.unwrap().get(), - // 340282366920938463463374607331768200456 - // ); - // assert_eq!( - // pool.seconds_per_liquidity_global.get(), - // 500000000000000000000000 - // ); - // } - // // updates timestamp - // { - // current_timestamp += 100; - // pool.liquidity = Liquidity::new(0); - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(pool.last_timestamp, current_timestamp); - // assert_eq!( - // seconds_per_liquidity_inside.unwrap().get(), - // 340282366920938463463374607331768200456 - // ); - // assert_eq!( - // pool.seconds_per_liquidity_global.get(), - // 500000000000000000000000 - // ); - // } - // // L > 0 - // { - // current_timestamp += 100; - // pool.liquidity = Liquidity::from_integer(1000); - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(pool.last_timestamp, current_timestamp); - // assert_eq!( - // seconds_per_liquidity_inside.unwrap().get(), - // 340282366920938463463374607331768200456 - // ); - // assert_eq!( - // pool.seconds_per_liquidity_global.get(), - // 600000000000000000000000 - // ); - // } - // // L == 0 - // { - // current_timestamp += 100; - // pool.liquidity = Liquidity::new(0); - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(pool.last_timestamp, current_timestamp); - // assert_eq!( - // seconds_per_liquidity_inside.unwrap().get(), - // 340282366920938463463374607331768200456 - // ); - // assert_eq!( - // pool.seconds_per_liquidity_global.get(), - // 600000000000000000000000 - // ); - // } - // } } diff --git a/src/contracts/storage/position.rs b/src/contracts/storage/position.rs index cc8ffa33..84b61025 100644 --- a/src/contracts/storage/position.rs +++ b/src/contracts/storage/position.rs @@ -5,7 +5,6 @@ use crate::{ types::{ fee_growth::{calculate_fee_growth_inside, FeeGrowth}, liquidity::Liquidity, - seconds_per_liquidity::{calculate_seconds_per_liquidity_inside, SecondsPerLiquidity}, sqrt_price::sqrt_price::SqrtPrice, token_amount::TokenAmount, }, @@ -28,7 +27,7 @@ pub struct Position { pub upper_tick_index: i32, pub fee_growth_inside_x: FeeGrowth, pub fee_growth_inside_y: FeeGrowth, - pub seconds_per_liquidity_inside: SecondsPerLiquidity, + pub last_block_number: u64, pub tokens_owed_x: TokenAmount, pub tokens_owed_y: TokenAmount, @@ -45,11 +44,7 @@ impl Position { current_timestamp: u64, tick_spacing: u16, ) -> TrackableResult<(TokenAmount, TokenAmount)> { - if !pool.liquidity.is_zero() { - ok_or_mark_trace!(pool.update_seconds_per_liquidity_global(current_timestamp))?; - } else { - pool.last_timestamp = current_timestamp; - } + pool.last_timestamp = current_timestamp; // calculate dynamically limit allows easy modification let max_liquidity_per_tick = calculate_max_liquidity_per_tick(tick_spacing); @@ -196,7 +191,7 @@ impl Position { upper_tick_index: upper_tick.index, fee_growth_inside_x: FeeGrowth::new(0), fee_growth_inside_y: FeeGrowth::new(0), - seconds_per_liquidity_inside: SecondsPerLiquidity::new(0), + last_block_number: block_number, tokens_owed_x: TokenAmount::new(0), tokens_owed_y: TokenAmount::new(0), @@ -247,24 +242,6 @@ impl Position { deinitialize_upper_tick, ) } - - pub fn update_seconds_per_liquidity( - &mut self, - pool: Pool, - lower_tick: Tick, - upper_tick: Tick, - current_timestamp: u64, - ) { - self.seconds_per_liquidity_inside = unwrap!(calculate_seconds_per_liquidity_inside( - lower_tick.index, - upper_tick.index, - pool.current_tick_index, - lower_tick.seconds_per_liquidity_outside, - upper_tick.seconds_per_liquidity_outside, - pool.seconds_per_liquidity_global, - )); - self.last_block_number = current_timestamp; - } } #[cfg(test)] diff --git a/src/contracts/storage/tick.rs b/src/contracts/storage/tick.rs index d790dd36..8c8c0e07 100644 --- a/src/contracts/storage/tick.rs +++ b/src/contracts/storage/tick.rs @@ -1,6 +1,6 @@ use crate::math::types::{ - fee_growth::FeeGrowth, liquidity::Liquidity, seconds_per_liquidity::SecondsPerLiquidity, - sqrt_price::sqrt_price::calculate_sqrt_price, sqrt_price::sqrt_price::SqrtPrice, + fee_growth::FeeGrowth, liquidity::Liquidity, sqrt_price::sqrt_price::calculate_sqrt_price, + sqrt_price::sqrt_price::SqrtPrice, }; use traceable_result::*; @@ -22,7 +22,7 @@ pub struct Tick { pub sqrt_price: SqrtPrice, pub fee_growth_outside_x: FeeGrowth, pub fee_growth_outside_y: FeeGrowth, - pub seconds_per_liquidity_outside: SecondsPerLiquidity, + pub seconds_outside: u64, } @@ -36,7 +36,7 @@ impl Default for Tick { sqrt_price: SqrtPrice::from_integer(1), fee_growth_outside_x: FeeGrowth::new(0), fee_growth_outside_y: FeeGrowth::new(0), - seconds_per_liquidity_outside: SecondsPerLiquidity::new(0), + seconds_outside: 0u64, } } @@ -62,10 +62,7 @@ impl Tick { true => current_timestamp - pool.start_timestamp, false => 0, }, - seconds_per_liquidity_outside: match below_current_tick { - true => pool.seconds_per_liquidity_global, - false => SecondsPerLiquidity::new(0), - }, + ..Self::default() } } @@ -83,14 +80,7 @@ impl Tick { .ok_or_else(|| err!("current_timestamp - pool.start_timestamp underflow"))?; self.seconds_outside = seconds_passed.wrapping_sub(self.seconds_outside); - if !pool.liquidity.is_zero() { - ok_or_mark_trace!(pool.update_seconds_per_liquidity_global(current_timestamp))?; - } else { - pool.last_timestamp = current_timestamp; - } - self.seconds_per_liquidity_outside = pool - .seconds_per_liquidity_global - .unchecked_sub(self.seconds_per_liquidity_outside); + pool.last_timestamp = current_timestamp; // When going to higher tick net_liquidity should be added and for going lower subtracted if (pool.current_tick_index >= self.index) ^ self.sign { diff --git a/src/lib.rs b/src/lib.rs index b84100a8..8a6c5398 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -711,32 +711,6 @@ pub mod contract { self.positions.get_all(caller) } - #[ink(message)] - fn update_position_seconds_per_liquidity( - &mut self, - index: u32, - pool_key: PoolKey, - ) -> Result<(), InvariantError> { - let caller = self.env().caller(); - let current_timestamp = self.env().block_timestamp(); - - let mut position = self.positions.get(caller, index)?; - - let lower_tick = self.ticks.get(pool_key, position.lower_tick_index)?; - - let upper_tick = self.ticks.get(pool_key, position.upper_tick_index)?; - - let pool = self.pools.get(pool_key)?; - - position.update_seconds_per_liquidity( - pool, - lower_tick, - upper_tick, - current_timestamp as u64, - ); - Ok(()) - } - #[ink(message)] fn claim_fee(&mut self, index: u32) -> Result<(TokenAmount, TokenAmount), InvariantError> { let caller = self.env().caller(); diff --git a/src/test_helpers/lib.rs b/src/test_helpers/lib.rs index ed8ebfe3..b0cef9cb 100644 --- a/src/test_helpers/lib.rs +++ b/src/test_helpers/lib.rs @@ -587,10 +587,6 @@ macro_rules! positions_equals { assert_eq!($a.lower_tick_index, $b.lower_tick_index); assert_eq!($a.upper_tick_index, $b.upper_tick_index); assert_eq!($a.pool_key, $b.pool_key); - assert_eq!( - $a.seconds_per_liquidity_inside, - $b.seconds_per_liquidity_inside - ); assert_eq!($a.tokens_owed_x, $b.tokens_owed_x); assert_eq!($a.tokens_owed_y, $b.tokens_owed_y); }}; From d4bdef4161ba28bd2d6524be398f1b4d47d960da Mon Sep 17 00:00:00 2001 From: Sniezka Date: Fri, 1 Dec 2023 13:33:26 +0100 Subject: [PATCH 2/4] update --- src/contracts/storage/pool.rs | 209 ++++++++++++++++++++++++++++++ src/contracts/storage/position.rs | 25 +++- src/contracts/storage/tick.rs | 22 +++- src/test_helpers/lib.rs | 4 + 4 files changed, 252 insertions(+), 8 deletions(-) diff --git a/src/contracts/storage/pool.rs b/src/contracts/storage/pool.rs index 208cab7e..762fe9d2 100644 --- a/src/contracts/storage/pool.rs +++ b/src/contracts/storage/pool.rs @@ -8,6 +8,7 @@ use crate::{ fee_growth::FeeGrowth, liquidity::Liquidity, percentage::Percentage, + seconds_per_liquidity::{calculate_seconds_per_liquidity_inside, SecondsPerLiquidity}, sqrt_price::{log::get_tick_at_sqrt_price, sqrt_price::SqrtPrice}, token_amount::TokenAmount, }, @@ -30,6 +31,8 @@ pub struct Pool { pub fee_growth_global_y: FeeGrowth, pub fee_protocol_token_x: TokenAmount, pub fee_protocol_token_y: TokenAmount, + #[allow(dead_code)] + pub seconds_per_liquidity_global: SecondsPerLiquidity, pub start_timestamp: u64, pub last_timestamp: u64, pub fee_receiver: AccountId, @@ -47,6 +50,7 @@ impl Default for Pool { fee_growth_global_y: FeeGrowth::default(), fee_protocol_token_x: TokenAmount(0u128), fee_protocol_token_y: TokenAmount(0u128), + seconds_per_liquidity_global: SecondsPerLiquidity::default(), start_timestamp: u64::default(), last_timestamp: u64::default(), fee_receiver: AccountId::from([0x0; 32]), @@ -129,6 +133,50 @@ impl Pool { } } + #[allow(dead_code)] + pub fn update_seconds_per_liquidity_global( + &mut self, + current_timestamp: u64, + ) -> TrackableResult<()> { + let seconds_per_liquidity_global = + SecondsPerLiquidity::calculate_seconds_per_liquidity_global( + self.liquidity, + current_timestamp, + self.last_timestamp, + )?; + + self.seconds_per_liquidity_global = self + .seconds_per_liquidity_global + .unchecked_add(seconds_per_liquidity_global); + self.last_timestamp = current_timestamp; + Ok(()) + } + + #[allow(dead_code)] + pub fn update_seconds_per_liquidity_inside( + &mut self, + tick_lower: i32, + tick_lower_seconds_per_liquidity_outside: SecondsPerLiquidity, + tick_upper: i32, + tick_upper_seconds_per_liquidity_outside: SecondsPerLiquidity, + current_timestamp: u64, + ) -> TrackableResult { + if !self.liquidity.is_zero() { + ok_or_mark_trace!(self.update_seconds_per_liquidity_global(current_timestamp))?; + } else { + self.last_timestamp = current_timestamp; + } + + ok_or_mark_trace!(calculate_seconds_per_liquidity_inside( + tick_lower, + tick_upper, + self.current_tick_index, + tick_lower_seconds_per_liquidity_outside, + tick_upper_seconds_per_liquidity_outside, + self.seconds_per_liquidity_global, + )) + } + pub fn cross_tick( &mut self, result: SwapResult, @@ -474,4 +522,165 @@ mod tests { assert_eq!(pool.liquidity, Liquidity::from_integer(5)) } } + // #[test] + // fn test_update_seconds_per_liquidity_inside() { + // let mut tick_lower = Tick { + // index: 0, + // seconds_per_liquidity_outside: SecondsPerLiquidity::new(3012300000), + // ..Default::default() + // }; + // let mut tick_upper = Tick { + // index: 10, + // seconds_per_liquidity_outside: SecondsPerLiquidity::new(2030400000), + // ..Default::default() + // }; + // let mut pool = Pool { + // liquidity: Liquidity::from_integer(1000), + // start_timestamp: 0, + // last_timestamp: 0, + // seconds_per_liquidity_global: SecondsPerLiquidity::new(0), + // ..Default::default() + // }; + // let mut current_timestamp = 0; + + // { + // current_timestamp += 100; + // pool.current_tick_index = -10; + // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( + // tick_lower.index, + // tick_lower.seconds_per_liquidity_outside, + // tick_upper.index, + // tick_upper.seconds_per_liquidity_outside, + // current_timestamp, + // ); + // assert_eq!(seconds_per_liquidity_inside.unwrap().get(), 981900000); + // } + // { + // current_timestamp += 100; + // pool.current_tick_index = 0; + // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( + // tick_lower.index, + // tick_lower.seconds_per_liquidity_outside, + // tick_upper.index, + // tick_upper.seconds_per_liquidity_outside, + // current_timestamp, + // ); + // assert_eq!( + // seconds_per_liquidity_inside.unwrap().get(), + // 199999999999994957300000 + // ); + // } + // { + // current_timestamp += 100; + // tick_lower.seconds_per_liquidity_outside = SecondsPerLiquidity::new(2012333200); + // tick_upper.seconds_per_liquidity_outside = SecondsPerLiquidity::new(3012333310); + // pool.current_tick_index = 20; + // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( + // tick_lower.index, + // tick_lower.seconds_per_liquidity_outside, + // tick_upper.index, + // tick_upper.seconds_per_liquidity_outside, + // current_timestamp, + // ); + // assert_eq!(seconds_per_liquidity_inside.unwrap().get(), 1000000110); + // } + // { + // current_timestamp += 100; + // tick_lower.seconds_per_liquidity_outside = SecondsPerLiquidity::new(201233320000); + // tick_upper.seconds_per_liquidity_outside = SecondsPerLiquidity::new(301233331000); + // pool.current_tick_index = 20; + // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( + // tick_lower.index, + // tick_lower.seconds_per_liquidity_outside, + // tick_upper.index, + // tick_upper.seconds_per_liquidity_outside, + // current_timestamp, + // ); + // assert_eq!(seconds_per_liquidity_inside.unwrap().get(), 100000011000); + // } + // { + // current_timestamp += 100; + // tick_lower.seconds_per_liquidity_outside = SecondsPerLiquidity::new(201233320000); + // tick_upper.seconds_per_liquidity_outside = SecondsPerLiquidity::new(301233331000); + // pool.current_tick_index = -20; + // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( + // tick_lower.index, + // tick_lower.seconds_per_liquidity_outside, + // tick_upper.index, + // tick_upper.seconds_per_liquidity_outside, + // current_timestamp, + // ); + // assert_eq!( + // seconds_per_liquidity_inside.unwrap().get(), + // 340282366920938463463374607331768200456 + // ); + // assert_eq!( + // pool.seconds_per_liquidity_global.get(), + // 500000000000000000000000 + // ); + // } + // // updates timestamp + // { + // current_timestamp += 100; + // pool.liquidity = Liquidity::new(0); + // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( + // tick_lower.index, + // tick_lower.seconds_per_liquidity_outside, + // tick_upper.index, + // tick_upper.seconds_per_liquidity_outside, + // current_timestamp, + // ); + // assert_eq!(pool.last_timestamp, current_timestamp); + // assert_eq!( + // seconds_per_liquidity_inside.unwrap().get(), + // 340282366920938463463374607331768200456 + // ); + // assert_eq!( + // pool.seconds_per_liquidity_global.get(), + // 500000000000000000000000 + // ); + // } + // // L > 0 + // { + // current_timestamp += 100; + // pool.liquidity = Liquidity::from_integer(1000); + // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( + // tick_lower.index, + // tick_lower.seconds_per_liquidity_outside, + // tick_upper.index, + // tick_upper.seconds_per_liquidity_outside, + // current_timestamp, + // ); + // assert_eq!(pool.last_timestamp, current_timestamp); + // assert_eq!( + // seconds_per_liquidity_inside.unwrap().get(), + // 340282366920938463463374607331768200456 + // ); + // assert_eq!( + // pool.seconds_per_liquidity_global.get(), + // 600000000000000000000000 + // ); + // } + // // L == 0 + // { + // current_timestamp += 100; + // pool.liquidity = Liquidity::new(0); + // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( + // tick_lower.index, + // tick_lower.seconds_per_liquidity_outside, + // tick_upper.index, + // tick_upper.seconds_per_liquidity_outside, + // current_timestamp, + // ); + // assert_eq!(pool.last_timestamp, current_timestamp); + // assert_eq!( + // seconds_per_liquidity_inside.unwrap().get(), + // 340282366920938463463374607331768200456 + // ); + // assert_eq!( + // pool.seconds_per_liquidity_global.get(), + // 600000000000000000000000 + // ); + // } + // } } diff --git a/src/contracts/storage/position.rs b/src/contracts/storage/position.rs index 84b61025..d64d79df 100644 --- a/src/contracts/storage/position.rs +++ b/src/contracts/storage/position.rs @@ -5,6 +5,7 @@ use crate::{ types::{ fee_growth::{calculate_fee_growth_inside, FeeGrowth}, liquidity::Liquidity, + seconds_per_liquidity::{calculate_seconds_per_liquidity_inside, SecondsPerLiquidity}, sqrt_price::sqrt_price::SqrtPrice, token_amount::TokenAmount, }, @@ -27,7 +28,8 @@ pub struct Position { pub upper_tick_index: i32, pub fee_growth_inside_x: FeeGrowth, pub fee_growth_inside_y: FeeGrowth, - + #[allow(dead_code)] + pub seconds_per_liquidity_inside: SecondsPerLiquidity, pub last_block_number: u64, pub tokens_owed_x: TokenAmount, pub tokens_owed_y: TokenAmount, @@ -191,7 +193,7 @@ impl Position { upper_tick_index: upper_tick.index, fee_growth_inside_x: FeeGrowth::new(0), fee_growth_inside_y: FeeGrowth::new(0), - + seconds_per_liquidity_inside: SecondsPerLiquidity::new(0), last_block_number: block_number, tokens_owed_x: TokenAmount::new(0), tokens_owed_y: TokenAmount::new(0), @@ -242,6 +244,25 @@ impl Position { deinitialize_upper_tick, ) } + + #[allow(dead_code)] + pub fn update_seconds_per_liquidity( + &mut self, + pool: Pool, + lower_tick: Tick, + upper_tick: Tick, + current_timestamp: u64, + ) { + self.seconds_per_liquidity_inside = unwrap!(calculate_seconds_per_liquidity_inside( + lower_tick.index, + upper_tick.index, + pool.current_tick_index, + lower_tick.seconds_per_liquidity_outside, + upper_tick.seconds_per_liquidity_outside, + pool.seconds_per_liquidity_global, + )); + self.last_block_number = current_timestamp; + } } #[cfg(test)] diff --git a/src/contracts/storage/tick.rs b/src/contracts/storage/tick.rs index 8c8c0e07..577b6172 100644 --- a/src/contracts/storage/tick.rs +++ b/src/contracts/storage/tick.rs @@ -1,6 +1,6 @@ use crate::math::types::{ - fee_growth::FeeGrowth, liquidity::Liquidity, sqrt_price::sqrt_price::calculate_sqrt_price, - sqrt_price::sqrt_price::SqrtPrice, + fee_growth::FeeGrowth, liquidity::Liquidity, seconds_per_liquidity::SecondsPerLiquidity, + sqrt_price::sqrt_price::calculate_sqrt_price, sqrt_price::sqrt_price::SqrtPrice, }; use traceable_result::*; @@ -22,7 +22,7 @@ pub struct Tick { pub sqrt_price: SqrtPrice, pub fee_growth_outside_x: FeeGrowth, pub fee_growth_outside_y: FeeGrowth, - + pub seconds_per_liquidity_outside: SecondsPerLiquidity, pub seconds_outside: u64, } @@ -36,7 +36,7 @@ impl Default for Tick { sqrt_price: SqrtPrice::from_integer(1), fee_growth_outside_x: FeeGrowth::new(0), fee_growth_outside_y: FeeGrowth::new(0), - + seconds_per_liquidity_outside: SecondsPerLiquidity::new(0), seconds_outside: 0u64, } } @@ -62,7 +62,10 @@ impl Tick { true => current_timestamp - pool.start_timestamp, false => 0, }, - + seconds_per_liquidity_outside: match below_current_tick { + true => pool.seconds_per_liquidity_global, + false => SecondsPerLiquidity::new(0), + }, ..Self::default() } } @@ -80,7 +83,14 @@ impl Tick { .ok_or_else(|| err!("current_timestamp - pool.start_timestamp underflow"))?; self.seconds_outside = seconds_passed.wrapping_sub(self.seconds_outside); - pool.last_timestamp = current_timestamp; + if !pool.liquidity.is_zero() { + // ok_or_mark_trace!(pool.update_seconds_per_liquidity_global(current_timestamp))?; + } else { + pool.last_timestamp = current_timestamp; + } + self.seconds_per_liquidity_outside = pool + .seconds_per_liquidity_global + .unchecked_sub(self.seconds_per_liquidity_outside); // When going to higher tick net_liquidity should be added and for going lower subtracted if (pool.current_tick_index >= self.index) ^ self.sign { diff --git a/src/test_helpers/lib.rs b/src/test_helpers/lib.rs index b0cef9cb..ed8ebfe3 100644 --- a/src/test_helpers/lib.rs +++ b/src/test_helpers/lib.rs @@ -587,6 +587,10 @@ macro_rules! positions_equals { assert_eq!($a.lower_tick_index, $b.lower_tick_index); assert_eq!($a.upper_tick_index, $b.upper_tick_index); assert_eq!($a.pool_key, $b.pool_key); + assert_eq!( + $a.seconds_per_liquidity_inside, + $b.seconds_per_liquidity_inside + ); assert_eq!($a.tokens_owed_x, $b.tokens_owed_x); assert_eq!($a.tokens_owed_y, $b.tokens_owed_y); }}; From 3003041a2b0d7878278d0a7ba8b6a8c86a883a54 Mon Sep 17 00:00:00 2001 From: Sniezka Date: Fri, 1 Dec 2023 13:35:09 +0100 Subject: [PATCH 3/4] added macro --- src/contracts/storage/tick.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/contracts/storage/tick.rs b/src/contracts/storage/tick.rs index 577b6172..24ead438 100644 --- a/src/contracts/storage/tick.rs +++ b/src/contracts/storage/tick.rs @@ -22,6 +22,7 @@ pub struct Tick { pub sqrt_price: SqrtPrice, pub fee_growth_outside_x: FeeGrowth, pub fee_growth_outside_y: FeeGrowth, + #[allow(dead_code)] pub seconds_per_liquidity_outside: SecondsPerLiquidity, pub seconds_outside: u64, } @@ -83,11 +84,8 @@ impl Tick { .ok_or_else(|| err!("current_timestamp - pool.start_timestamp underflow"))?; self.seconds_outside = seconds_passed.wrapping_sub(self.seconds_outside); - if !pool.liquidity.is_zero() { - // ok_or_mark_trace!(pool.update_seconds_per_liquidity_global(current_timestamp))?; - } else { - pool.last_timestamp = current_timestamp; - } + pool.last_timestamp = current_timestamp; + self.seconds_per_liquidity_outside = pool .seconds_per_liquidity_global .unchecked_sub(self.seconds_per_liquidity_outside); From 83ce8adb8330806cc6b19f186d5457e73d1765af Mon Sep 17 00:00:00 2001 From: Wojciech Date: Sun, 3 Dec 2023 12:27:28 +0100 Subject: [PATCH 4/4] Update SecondsPerLiquidity --- src/contracts/storage/pool.rs | 209 ------------------------------ src/contracts/storage/position.rs | 25 ---- src/contracts/storage/tick.rs | 16 +-- src/test_helpers/lib.rs | 4 - 4 files changed, 3 insertions(+), 251 deletions(-) diff --git a/src/contracts/storage/pool.rs b/src/contracts/storage/pool.rs index 762fe9d2..208cab7e 100644 --- a/src/contracts/storage/pool.rs +++ b/src/contracts/storage/pool.rs @@ -8,7 +8,6 @@ use crate::{ fee_growth::FeeGrowth, liquidity::Liquidity, percentage::Percentage, - seconds_per_liquidity::{calculate_seconds_per_liquidity_inside, SecondsPerLiquidity}, sqrt_price::{log::get_tick_at_sqrt_price, sqrt_price::SqrtPrice}, token_amount::TokenAmount, }, @@ -31,8 +30,6 @@ pub struct Pool { pub fee_growth_global_y: FeeGrowth, pub fee_protocol_token_x: TokenAmount, pub fee_protocol_token_y: TokenAmount, - #[allow(dead_code)] - pub seconds_per_liquidity_global: SecondsPerLiquidity, pub start_timestamp: u64, pub last_timestamp: u64, pub fee_receiver: AccountId, @@ -50,7 +47,6 @@ impl Default for Pool { fee_growth_global_y: FeeGrowth::default(), fee_protocol_token_x: TokenAmount(0u128), fee_protocol_token_y: TokenAmount(0u128), - seconds_per_liquidity_global: SecondsPerLiquidity::default(), start_timestamp: u64::default(), last_timestamp: u64::default(), fee_receiver: AccountId::from([0x0; 32]), @@ -133,50 +129,6 @@ impl Pool { } } - #[allow(dead_code)] - pub fn update_seconds_per_liquidity_global( - &mut self, - current_timestamp: u64, - ) -> TrackableResult<()> { - let seconds_per_liquidity_global = - SecondsPerLiquidity::calculate_seconds_per_liquidity_global( - self.liquidity, - current_timestamp, - self.last_timestamp, - )?; - - self.seconds_per_liquidity_global = self - .seconds_per_liquidity_global - .unchecked_add(seconds_per_liquidity_global); - self.last_timestamp = current_timestamp; - Ok(()) - } - - #[allow(dead_code)] - pub fn update_seconds_per_liquidity_inside( - &mut self, - tick_lower: i32, - tick_lower_seconds_per_liquidity_outside: SecondsPerLiquidity, - tick_upper: i32, - tick_upper_seconds_per_liquidity_outside: SecondsPerLiquidity, - current_timestamp: u64, - ) -> TrackableResult { - if !self.liquidity.is_zero() { - ok_or_mark_trace!(self.update_seconds_per_liquidity_global(current_timestamp))?; - } else { - self.last_timestamp = current_timestamp; - } - - ok_or_mark_trace!(calculate_seconds_per_liquidity_inside( - tick_lower, - tick_upper, - self.current_tick_index, - tick_lower_seconds_per_liquidity_outside, - tick_upper_seconds_per_liquidity_outside, - self.seconds_per_liquidity_global, - )) - } - pub fn cross_tick( &mut self, result: SwapResult, @@ -522,165 +474,4 @@ mod tests { assert_eq!(pool.liquidity, Liquidity::from_integer(5)) } } - // #[test] - // fn test_update_seconds_per_liquidity_inside() { - // let mut tick_lower = Tick { - // index: 0, - // seconds_per_liquidity_outside: SecondsPerLiquidity::new(3012300000), - // ..Default::default() - // }; - // let mut tick_upper = Tick { - // index: 10, - // seconds_per_liquidity_outside: SecondsPerLiquidity::new(2030400000), - // ..Default::default() - // }; - // let mut pool = Pool { - // liquidity: Liquidity::from_integer(1000), - // start_timestamp: 0, - // last_timestamp: 0, - // seconds_per_liquidity_global: SecondsPerLiquidity::new(0), - // ..Default::default() - // }; - // let mut current_timestamp = 0; - - // { - // current_timestamp += 100; - // pool.current_tick_index = -10; - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(seconds_per_liquidity_inside.unwrap().get(), 981900000); - // } - // { - // current_timestamp += 100; - // pool.current_tick_index = 0; - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!( - // seconds_per_liquidity_inside.unwrap().get(), - // 199999999999994957300000 - // ); - // } - // { - // current_timestamp += 100; - // tick_lower.seconds_per_liquidity_outside = SecondsPerLiquidity::new(2012333200); - // tick_upper.seconds_per_liquidity_outside = SecondsPerLiquidity::new(3012333310); - // pool.current_tick_index = 20; - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(seconds_per_liquidity_inside.unwrap().get(), 1000000110); - // } - // { - // current_timestamp += 100; - // tick_lower.seconds_per_liquidity_outside = SecondsPerLiquidity::new(201233320000); - // tick_upper.seconds_per_liquidity_outside = SecondsPerLiquidity::new(301233331000); - // pool.current_tick_index = 20; - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(seconds_per_liquidity_inside.unwrap().get(), 100000011000); - // } - // { - // current_timestamp += 100; - // tick_lower.seconds_per_liquidity_outside = SecondsPerLiquidity::new(201233320000); - // tick_upper.seconds_per_liquidity_outside = SecondsPerLiquidity::new(301233331000); - // pool.current_tick_index = -20; - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!( - // seconds_per_liquidity_inside.unwrap().get(), - // 340282366920938463463374607331768200456 - // ); - // assert_eq!( - // pool.seconds_per_liquidity_global.get(), - // 500000000000000000000000 - // ); - // } - // // updates timestamp - // { - // current_timestamp += 100; - // pool.liquidity = Liquidity::new(0); - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(pool.last_timestamp, current_timestamp); - // assert_eq!( - // seconds_per_liquidity_inside.unwrap().get(), - // 340282366920938463463374607331768200456 - // ); - // assert_eq!( - // pool.seconds_per_liquidity_global.get(), - // 500000000000000000000000 - // ); - // } - // // L > 0 - // { - // current_timestamp += 100; - // pool.liquidity = Liquidity::from_integer(1000); - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(pool.last_timestamp, current_timestamp); - // assert_eq!( - // seconds_per_liquidity_inside.unwrap().get(), - // 340282366920938463463374607331768200456 - // ); - // assert_eq!( - // pool.seconds_per_liquidity_global.get(), - // 600000000000000000000000 - // ); - // } - // // L == 0 - // { - // current_timestamp += 100; - // pool.liquidity = Liquidity::new(0); - // let seconds_per_liquidity_inside = pool.update_seconds_per_liquidity_inside( - // tick_lower.index, - // tick_lower.seconds_per_liquidity_outside, - // tick_upper.index, - // tick_upper.seconds_per_liquidity_outside, - // current_timestamp, - // ); - // assert_eq!(pool.last_timestamp, current_timestamp); - // assert_eq!( - // seconds_per_liquidity_inside.unwrap().get(), - // 340282366920938463463374607331768200456 - // ); - // assert_eq!( - // pool.seconds_per_liquidity_global.get(), - // 600000000000000000000000 - // ); - // } - // } } diff --git a/src/contracts/storage/position.rs b/src/contracts/storage/position.rs index d64d79df..c011550a 100644 --- a/src/contracts/storage/position.rs +++ b/src/contracts/storage/position.rs @@ -5,7 +5,6 @@ use crate::{ types::{ fee_growth::{calculate_fee_growth_inside, FeeGrowth}, liquidity::Liquidity, - seconds_per_liquidity::{calculate_seconds_per_liquidity_inside, SecondsPerLiquidity}, sqrt_price::sqrt_price::SqrtPrice, token_amount::TokenAmount, }, @@ -13,8 +12,6 @@ use crate::{ InvariantError, }; use decimal::*; -use ink::prelude::vec; -use ink::primitives::AccountId; use traceable_result::*; #[derive(PartialEq, Default, Debug, Copy, Clone, scale::Decode, scale::Encode)] #[cfg_attr( @@ -28,8 +25,6 @@ pub struct Position { pub upper_tick_index: i32, pub fee_growth_inside_x: FeeGrowth, pub fee_growth_inside_y: FeeGrowth, - #[allow(dead_code)] - pub seconds_per_liquidity_inside: SecondsPerLiquidity, pub last_block_number: u64, pub tokens_owed_x: TokenAmount, pub tokens_owed_y: TokenAmount, @@ -193,7 +188,6 @@ impl Position { upper_tick_index: upper_tick.index, fee_growth_inside_x: FeeGrowth::new(0), fee_growth_inside_y: FeeGrowth::new(0), - seconds_per_liquidity_inside: SecondsPerLiquidity::new(0), last_block_number: block_number, tokens_owed_x: TokenAmount::new(0), tokens_owed_y: TokenAmount::new(0), @@ -244,25 +238,6 @@ impl Position { deinitialize_upper_tick, ) } - - #[allow(dead_code)] - pub fn update_seconds_per_liquidity( - &mut self, - pool: Pool, - lower_tick: Tick, - upper_tick: Tick, - current_timestamp: u64, - ) { - self.seconds_per_liquidity_inside = unwrap!(calculate_seconds_per_liquidity_inside( - lower_tick.index, - upper_tick.index, - pool.current_tick_index, - lower_tick.seconds_per_liquidity_outside, - upper_tick.seconds_per_liquidity_outside, - pool.seconds_per_liquidity_global, - )); - self.last_block_number = current_timestamp; - } } #[cfg(test)] diff --git a/src/contracts/storage/tick.rs b/src/contracts/storage/tick.rs index 24ead438..c041101a 100644 --- a/src/contracts/storage/tick.rs +++ b/src/contracts/storage/tick.rs @@ -1,6 +1,6 @@ use crate::math::types::{ - fee_growth::FeeGrowth, liquidity::Liquidity, seconds_per_liquidity::SecondsPerLiquidity, - sqrt_price::sqrt_price::calculate_sqrt_price, sqrt_price::sqrt_price::SqrtPrice, + fee_growth::FeeGrowth, liquidity::Liquidity, sqrt_price::sqrt_price::calculate_sqrt_price, + sqrt_price::sqrt_price::SqrtPrice, }; use traceable_result::*; @@ -22,8 +22,6 @@ pub struct Tick { pub sqrt_price: SqrtPrice, pub fee_growth_outside_x: FeeGrowth, pub fee_growth_outside_y: FeeGrowth, - #[allow(dead_code)] - pub seconds_per_liquidity_outside: SecondsPerLiquidity, pub seconds_outside: u64, } @@ -37,7 +35,6 @@ impl Default for Tick { sqrt_price: SqrtPrice::from_integer(1), fee_growth_outside_x: FeeGrowth::new(0), fee_growth_outside_y: FeeGrowth::new(0), - seconds_per_liquidity_outside: SecondsPerLiquidity::new(0), seconds_outside: 0u64, } } @@ -63,10 +60,6 @@ impl Tick { true => current_timestamp - pool.start_timestamp, false => 0, }, - seconds_per_liquidity_outside: match below_current_tick { - true => pool.seconds_per_liquidity_global, - false => SecondsPerLiquidity::new(0), - }, ..Self::default() } } @@ -86,10 +79,6 @@ impl Tick { pool.last_timestamp = current_timestamp; - self.seconds_per_liquidity_outside = pool - .seconds_per_liquidity_global - .unchecked_sub(self.seconds_per_liquidity_outside); - // When going to higher tick net_liquidity should be added and for going lower subtracted if (pool.current_tick_index >= self.index) ^ self.sign { // trunk-ignore(clippy/assign_op_pattern) @@ -175,6 +164,7 @@ mod tests { use super::*; + // TODO: do sth about these comments // #[test] // fn test_cross() { // { diff --git a/src/test_helpers/lib.rs b/src/test_helpers/lib.rs index ed8ebfe3..b0cef9cb 100644 --- a/src/test_helpers/lib.rs +++ b/src/test_helpers/lib.rs @@ -587,10 +587,6 @@ macro_rules! positions_equals { assert_eq!($a.lower_tick_index, $b.lower_tick_index); assert_eq!($a.upper_tick_index, $b.upper_tick_index); assert_eq!($a.pool_key, $b.pool_key); - assert_eq!( - $a.seconds_per_liquidity_inside, - $b.seconds_per_liquidity_inside - ); assert_eq!($a.tokens_owed_x, $b.tokens_owed_x); assert_eq!($a.tokens_owed_y, $b.tokens_owed_y); }};