Skip to content

Commit

Permalink
Merge pull request #93 from invariant-labs/remove-seconds-per-liquidity
Browse files Browse the repository at this point in the history
Removed seconds per liquidity
  • Loading branch information
wojciech-cichocki authored Dec 3, 2023
2 parents 4ff795a + 92f6f89 commit 464e887
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 281 deletions.
7 changes: 0 additions & 7 deletions src/contracts/entrypoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,6 @@ pub trait Invariant {
#[ink(message)]
fn get_all_positions(&mut self) -> Vec<Position>;

#[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
Expand Down
207 changes: 0 additions & 207 deletions src/contracts/storage/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
Expand All @@ -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]),
Expand Down Expand Up @@ -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<SecondsPerLiquidity> {
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,
))
}

#[allow(clippy::too_many_arguments)]
pub fn cross_tick(
&mut self,
Expand Down Expand Up @@ -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
// );
// }
// }
}
27 changes: 1 addition & 26 deletions src/contracts/storage/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -26,7 +25,6 @@ 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,
Expand All @@ -44,11 +42,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);
Expand Down Expand Up @@ -196,7 +190,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),
Expand Down Expand Up @@ -247,24 +240,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)]
Expand Down
20 changes: 4 additions & 16 deletions src/contracts/storage/tick.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -22,7 +22,6 @@ 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,
}

Expand All @@ -36,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,
}
}
Expand All @@ -62,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()
}
}
Expand All @@ -83,14 +77,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 {
Expand Down Expand Up @@ -177,6 +164,7 @@ mod tests {

use super::*;

// TODO: do sth about these comments
// #[test]
// fn test_cross() {
// {
Expand Down
Loading

0 comments on commit 464e887

Please sign in to comment.