Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl LiquidityHandler for CompoundingLiquidity {
Ok((self.token_a_amount, self.token_b_amount))
}

// xyk, the price is determined by the ratio of reserves and it always rounded down.
fn get_next_sqrt_price(&self, _next_sqrt_price: u128) -> Result<u128> {
get_sqrt_price_from_amounts(self.token_a_amount, self.token_b_amount)
}
Expand Down
32 changes: 14 additions & 18 deletions programs/cp-amm/src/liquidity_handler/concentrated_liquidity.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#[cfg(test)]
use crate::params::swap::TradeDirection;
use crate::{
// curve::{
// get_delta_amount_a_unsigned, get_delta_amount_a_unsigned_unchecked,
// get_delta_amount_b_unsigned, get_delta_amount_b_unsigned_unchecked,
// get_next_sqrt_price_from_input, get_next_sqrt_price_from_output,
// },
safe_math::SafeMath,
state::{SwapAmountFromInput, SwapAmountFromOutput},
u128x128_math::{mul_div_u256, Rounding},
InitialPoolInformation,
LiquidityHandler,
PoolError,
InitialPoolInformation, LiquidityHandler, PoolError,
};
use anchor_lang::prelude::*;
use ruint::aliases::U256;
Expand Down Expand Up @@ -257,6 +250,7 @@ impl LiquidityHandler for ConcentratedLiquidity {
Ok((reserve_a_amount, reserve_b_amount))
}

// It does nothing because next_sqrt_price is computed by swap-path + rounding direction.
fn get_next_sqrt_price(&self, next_sqrt_price: u128) -> Result<u128> {
Ok(next_sqrt_price)
}
Expand All @@ -268,13 +262,13 @@ impl LiquidityHandler for ConcentratedLiquidity {
self.sqrt_min_price,
self.sqrt_price,
self.liquidity,
Rounding::Down,
Rounding::Up,
)?,
TradeDirection::BtoA => get_delta_amount_b_unsigned_unchecked(
self.sqrt_price,
self.sqrt_max_price,
self.liquidity,
Rounding::Down,
Rounding::Up,
)?,
};
if amount > U256::from(u64::MAX) {
Expand Down Expand Up @@ -368,7 +362,7 @@ pub fn get_delta_amount_b_unsigned_unchecked(
}

/// Gets the next sqrt price given an input amount of token_a or token_b
/// Throws if price or liquidity are 0, or if the next price is out of bounds
/// Throws if price or liquidity are 0, or if the next price overflow q64.64
pub fn get_next_sqrt_price_from_input(
sqrt_price: u128,
liquidity: u128,
Expand All @@ -378,6 +372,10 @@ pub fn get_next_sqrt_price_from_input(
assert!(sqrt_price > 0);
assert!(liquidity > 0);

if amount_in == 0 {
return Ok(sqrt_price);
}

// round to make sure that we don't pass the target price
if a_for_b {
get_next_sqrt_price_from_amount_in_a_rounding_up(sqrt_price, liquidity, amount_in)
Expand All @@ -387,7 +385,7 @@ pub fn get_next_sqrt_price_from_input(
}

/// Gets the next sqrt price given an output amount of token_a or token_b
/// Throws if price or liquidity are 0, or if the next price is out of bounds
/// Throws if price or liquidity are 0, or if the next price overflow q64.64
pub fn get_next_sqrt_price_from_output(
sqrt_price: u128,
liquidity: u128,
Expand All @@ -397,6 +395,10 @@ pub fn get_next_sqrt_price_from_output(
assert!(sqrt_price > 0);
assert!(liquidity > 0);

if amount_out == 0 {
return Ok(sqrt_price);
}

// round to make sure that we don't pass the target price
if a_for_b {
get_next_sqrt_price_from_amount_out_b_rounding_down(sqrt_price, liquidity, amount_out)
Expand Down Expand Up @@ -438,9 +440,6 @@ pub fn get_next_sqrt_price_from_amount_in_a_rounding_up(
liquidity: u128,
amount: u64,
) -> Result<u128> {
if amount == 0 {
return Ok(sqrt_price);
}
let sqrt_price = U256::from(sqrt_price);
let liquidity = U256::from(liquidity);

Expand All @@ -457,9 +456,6 @@ pub fn get_next_sqrt_price_from_amount_out_a_rounding_up(
liquidity: u128,
amount: u64,
) -> Result<u128> {
if amount == 0 {
return Ok(sqrt_price);
}
let sqrt_price = U256::from(sqrt_price);
let liquidity = U256::from(liquidity);

Expand Down
6 changes: 6 additions & 0 deletions programs/cp-amm/src/liquidity_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ pub trait LiquidityHandler {

fn get_reserves_amount(&self) -> Result<(u64, u64)>;

// Note: Due to different way of concentrated liquidity and compounding liquidity calculating price, compounding and concentrated pools can update dynamic-fee volatility differently for equivalent swap price moves.
// Additionally the market cap based base fee will also behave differently:
// Concentrated Amount_In B to A -> Rounding Down
// Concentrated Amount_Out B to A -> Rounding Up
// Compounding Amount_In B to A -> Rounding Down
// Compounding Amount_Out B to A -> Rounding Down
fn get_next_sqrt_price(&self, next_sqrt_price: u128) -> Result<u128>;

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"esModuleInterop": true,
"resolveJsonModule": true,
"noEmit": true,
"skipLibCheck": true
"skipLibCheck": true,
"strict": false
}
}
Loading