Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConcentratedLiquidityPool ensureTickSpacing logic issue #335

Open
theKata opened this issue Mar 14, 2022 · 1 comment
Open

ConcentratedLiquidityPool ensureTickSpacing logic issue #335

theKata opened this issue Mar 14, 2022 · 1 comment

Comments

@theKata
Copy link

theKata commented Mar 14, 2022

when the value of lower is TickMath.MIN_TICK and upper is TickMath.MAX_TICK, I think it should be passed _ensureTickSpacing() method. But current _ensureTickSpacing() block this case.

My Suggestion is below.

Current

function _ensureTickSpacing(int24 lower, int24 upper) internal view {
if (lower % int24(tickSpacing) != 0) revert InvalidTick();
if ((lower / int24(tickSpacing)) % 2 != 0) revert LowerEven();
if (upper % int24(tickSpacing) != 0) revert InvalidTick();
if ((upper / int24(tickSpacing)) % 2 == 0) revert UpperOdd();
}

Suggestion

    function _ensureTickSpacing(int24 lower, int24 upper) internal view {
        if (lower != TickMath.MIN_TICK) {
            if (lower % int24(tickSpacing) != 0) revert InvalidTick();
            if ((lower / int24(tickSpacing)) % 2 != 0) revert LowerEven();
        }

        if (upper != TickMath.MAX_TICK) {
            if (upper % int24(tickSpacing) != 0) revert InvalidTick();
            if ((upper / int24(tickSpacing)) % 2 == 0) revert UpperOdd();
        }
    }
@theKata theKata changed the title ConcentratedLiquidityPool ensureTick logic issue ConcentratedLiquidityPool ensureTickSpacing logic issue Mar 14, 2022
@theKata
Copy link
Author

theKata commented Mar 14, 2022

function insert(
mapping(int24 => IConcentratedLiquidityPoolStruct.Tick) storage ticks,
uint256 feeGrowthGlobal0,
uint256 feeGrowthGlobal1,
uint160 secondsGrowthGlobal,
int24 lowerOld,
int24 lower,
int24 upperOld,
int24 upper,
uint128 amount,
int24 nearestTick,
uint160 currentPrice
) public returns (int24) {
require(lower < upper, "WRONG_ORDER");
require(TickMath.MIN_TICK <= lower, "LOWER_RANGE");
require(upper <= TickMath.MAX_TICK, "UPPER_RANGE");
{
// Stack overflow.
uint128 currentLowerLiquidity = ticks[lower].liquidity;
if (currentLowerLiquidity != 0 || lower == TickMath.MIN_TICK) {
// We are adding liquidity to an existing tick.
ticks[lower].liquidity = currentLowerLiquidity + amount;
} else {
// We are inserting a new tick.
IConcentratedLiquidityPoolStruct.Tick storage old = ticks[lowerOld];
int24 oldNextTick = old.nextTick;
require((old.liquidity != 0 || lowerOld == TickMath.MIN_TICK) && lowerOld < lower && lower < oldNextTick, "LOWER_ORDER");
if (lower <= nearestTick) {
ticks[lower] = IConcentratedLiquidityPoolStruct.Tick(
lowerOld,
oldNextTick,
amount,
feeGrowthGlobal0,
feeGrowthGlobal1,
secondsGrowthGlobal
);
} else {
ticks[lower] = IConcentratedLiquidityPoolStruct.Tick(lowerOld, oldNextTick, amount, 0, 0, 0);
}
old.nextTick = lower;
ticks[oldNextTick].previousTick = lower;
}
}
uint128 currentUpperLiquidity = ticks[upper].liquidity;
if (currentUpperLiquidity != 0 || upper == TickMath.MAX_TICK) {
// We are adding liquidity to an existing tick.
ticks[upper].liquidity = currentUpperLiquidity + amount;
} else {
// Inserting a new tick.
IConcentratedLiquidityPoolStruct.Tick storage old = ticks[upperOld];
int24 oldNextTick = old.nextTick;
require(old.liquidity != 0 && oldNextTick > upper && upperOld < upper, "UPPER_ORDER");
if (upper <= nearestTick) {
ticks[upper] = IConcentratedLiquidityPoolStruct.Tick(
upperOld,
oldNextTick,
amount,
feeGrowthGlobal0,
feeGrowthGlobal1,
secondsGrowthGlobal
);
} else {
ticks[upper] = IConcentratedLiquidityPoolStruct.Tick(upperOld, oldNextTick, amount, 0, 0, 0);
}
old.nextTick = upper;
ticks[oldNextTick].previousTick = upper;
}
int24 tickAtPrice = TickMath.getTickAtSqrtRatio(currentPrice);
if (nearestTick < upper && upper <= tickAtPrice) {
nearestTick = upper;
} else if (nearestTick < lower && lower <= tickAtPrice) {
nearestTick = lower;
}
return nearestTick;
}

Tick Library is designed so that MIN_TICK and MAX_TICK can enter the lower and upper, but it is blocked by _ensureTickSpacing().

@mshakeg mshakeg mentioned this issue Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant