Code Monkey home page Code Monkey logo

Comments (1)

theKata avatar theKata commented on July 28, 2024

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().

from trident.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.