Skip to main content
Helpful?

Pool

Git Source | Generated with forge doc

a library with all actions that can be performed on a pool

Functions

checkTicks

Common checks for valid tick inputs.

function checkTicks(int24 tickLower, int24 tickUpper) private pure;

initialize

function initialize(State storage self, uint160 sqrtPriceX96, uint24 protocolFee, uint24 lpFee)
internal
returns (int24 tick);

setProtocolFee

function setProtocolFee(State storage self, uint24 protocolFee) internal;

setLPFee

Only dynamic fee pools may update the lp fee.

function setLPFee(State storage self, uint24 lpFee) internal;

modifyLiquidity

Effect changes to a position in a pool

PoolManager checks that the pool is initialized before calling

function modifyLiquidity(State storage self, ModifyLiquidityParams memory params)
internal
returns (BalanceDelta delta, BalanceDelta feeDelta);

Parameters

NameTypeDescription
selfState
paramsModifyLiquidityParamsthe position details and the change to the position's liquidity to effect

Returns

NameTypeDescription
deltaBalanceDeltathe deltas of the token balances of the pool, from the liquidity change
feeDeltaBalanceDeltathe fees generated by the liquidity range

swap

Executes a swap against the state, and returns the amount deltas of the pool

PoolManager checks that the pool is initialized before calling

function swap(State storage self, SwapParams memory params)
internal
returns (BalanceDelta swapDelta, uint256 amountToProtocol, uint24 swapFee, SwapResult memory result);

Donates the given amount of currency0 and currency1 to the pool

function donate(State storage state, uint256 amount0, uint256 amount1) internal returns (BalanceDelta delta);

getFeeGrowthInside

Retrieves fee growth data

function getFeeGrowthInside(State storage self, int24 tickLower, int24 tickUpper)
internal
view
returns (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128);

Parameters

NameTypeDescription
selfStateThe Pool state struct
tickLowerint24The lower tick boundary of the position
tickUpperint24The upper tick boundary of the position

Returns

NameTypeDescription
feeGrowthInside0X128uint256The all-time fee growth in token0, per unit of liquidity, inside the position's tick boundaries
feeGrowthInside1X128uint256The all-time fee growth in token1, per unit of liquidity, inside the position's tick boundaries

updateTick

Updates a tick and returns true if the tick was flipped from initialized to uninitialized, or vice versa

function updateTick(State storage self, int24 tick, int128 liquidityDelta, bool upper)
internal
returns (bool flipped, uint128 liquidityGrossAfter);

Parameters

NameTypeDescription
selfStateThe mapping containing all tick information for initialized ticks
tickint24The tick that will be updated
liquidityDeltaint128A new amount of liquidity to be added (subtracted) when tick is crossed from left to right (right to left)
upperbooltrue for updating a position's upper tick, or false for updating a position's lower tick

Returns

NameTypeDescription
flippedboolWhether the tick was flipped from initialized to uninitialized, or vice versa
liquidityGrossAfteruint128The total amount of liquidity for all positions that references the tick after the update

tickSpacingToMaxLiquidityPerTick

Derives max liquidity per tick from given tick spacing

Executed when adding liquidity

function tickSpacingToMaxLiquidityPerTick(int24 tickSpacing) internal pure returns (uint128 result);

Parameters

NameTypeDescription
tickSpacingint24The amount of required tick separation, realized in multiples of tickSpacing e.g., a tickSpacing of 3 requires ticks to be initialized every 3rd tick i.e., ..., -6, -3, 0, 3, 6, ...

Returns

NameTypeDescription
resultuint128The max liquidity per tick

checkPoolInitialized

Reverts if the given pool has not been initialized

function checkPoolInitialized(State storage self) internal view;

clearTick

Clears tick data

function clearTick(State storage self, int24 tick) internal;

Parameters

NameTypeDescription
selfStateThe mapping containing all initialized tick information for initialized ticks
tickint24The tick that will be cleared

crossTick

Transitions to next tick as needed by price movement

function crossTick(State storage self, int24 tick, uint256 feeGrowthGlobal0X128, uint256 feeGrowthGlobal1X128)
internal
returns (int128 liquidityNet);

Parameters

NameTypeDescription
selfStateThe Pool state struct
tickint24The destination tick of the transition
feeGrowthGlobal0X128uint256The all-time global fee growth, per unit of liquidity, in token0
feeGrowthGlobal1X128uint256The all-time global fee growth, per unit of liquidity, in token1

Returns

NameTypeDescription
liquidityNetint128The amount of liquidity added (subtracted) when tick is crossed from left to right (right to left)

Errors

TicksMisordered

Thrown when tickLower is not below tickUpper

error TicksMisordered(int24 tickLower, int24 tickUpper);

Parameters

NameTypeDescription
tickLowerint24The invalid tickLower
tickUpperint24The invalid tickUpper

TickLowerOutOfBounds

Thrown when tickLower is less than min tick

error TickLowerOutOfBounds(int24 tickLower);

Parameters

NameTypeDescription
tickLowerint24The invalid tickLower

TickUpperOutOfBounds

Thrown when tickUpper exceeds max tick

error TickUpperOutOfBounds(int24 tickUpper);

Parameters

NameTypeDescription
tickUpperint24The invalid tickUpper

TickLiquidityOverflow

For the tick spacing, the tick has too much liquidity

error TickLiquidityOverflow(int24 tick);

PoolAlreadyInitialized

Thrown when trying to initialize an already initialized pool

error PoolAlreadyInitialized();

PoolNotInitialized

Thrown when trying to interact with a non-initialized pool

error PoolNotInitialized();

PriceLimitAlreadyExceeded

Thrown when sqrtPriceLimitX96 on a swap has already exceeded its limit

error PriceLimitAlreadyExceeded(uint160 sqrtPriceCurrentX96, uint160 sqrtPriceLimitX96);

Parameters

NameTypeDescription
sqrtPriceCurrentX96uint160The invalid, already surpassed sqrtPriceLimitX96
sqrtPriceLimitX96uint160The surpassed price limit

PriceLimitOutOfBounds

Thrown when sqrtPriceLimitX96 lies outside of valid tick/price range

error PriceLimitOutOfBounds(uint160 sqrtPriceLimitX96);

Parameters

NameTypeDescription
sqrtPriceLimitX96uint160The invalid, out-of-bounds sqrtPriceLimitX96

NoLiquidityToReceiveFees

Thrown by donate if there is currently 0 liquidity, since the fees will not go to any liquidity providers

error NoLiquidityToReceiveFees();

InvalidFeeForExactOut

Thrown when trying to swap with max lp fee and specifying an output amount

error InvalidFeeForExactOut();

Structs

TickInfo

struct TickInfo {
uint128 liquidityGross;
int128 liquidityNet;
uint256 feeGrowthOutside0X128;
uint256 feeGrowthOutside1X128;
}

State

The state of a pool

struct State {
Slot0 slot0;
uint256 feeGrowthGlobal0X128;
uint256 feeGrowthGlobal1X128;
uint128 liquidity;
mapping(int24 tick => TickInfo) ticks;
mapping(int16 wordPos => uint256) tickBitmap;
mapping(bytes32 positionKey => Position.State) positions;
}

ModifyLiquidityParams

struct ModifyLiquidityParams {
address owner;
int24 tickLower;
int24 tickUpper;
int128 liquidityDelta;
int24 tickSpacing;
bytes32 salt;
}

ModifyLiquidityState

struct ModifyLiquidityState {
bool flippedLower;
uint128 liquidityGrossAfterLower;
bool flippedUpper;
uint128 liquidityGrossAfterUpper;
}

SwapResult

struct SwapResult {
uint160 sqrtPriceX96;
int24 tick;
uint128 liquidity;
}

StepComputations

struct StepComputations {
uint160 sqrtPriceStartX96;
int24 tickNext;
bool initialized;
uint160 sqrtPriceNextX96;
uint256 amountIn;
uint256 amountOut;
uint256 feeAmount;
uint256 feeGrowthGlobalX128;
}

SwapParams

struct SwapParams {
int256 amountSpecified;
int24 tickSpacing;
bool zeroForOne;
uint160 sqrtPriceLimitX96;
uint24 lpFeeOverride;
}
Helpful?