Skip to main content
Helpful?

StateLibrary

Git Source - Generated with forge doc

A helper library to provide state getters that use extsload

State Variables

POOLS_SLOT

index of pools mapping in the PoolManager

bytes32 public constant POOLS_SLOT = bytes32(uint256(6));

FEE_GROWTH_GLOBAL0_OFFSET

index of feeGrowthGlobal0X128 in Pool.State

uint256 public constant FEE_GROWTH_GLOBAL0_OFFSET = 1;

LIQUIDITY_OFFSET

index of liquidity in Pool.State

uint256 public constant LIQUIDITY_OFFSET = 3;

TICKS_OFFSET

index of TicksInfo mapping in Pool.State: mapping(int24 => TickInfo) ticks;

uint256 public constant TICKS_OFFSET = 4;

TICK_BITMAP_OFFSET

index of tickBitmap mapping in Pool.State

uint256 public constant TICK_BITMAP_OFFSET = 5;

POSITIONS_OFFSET

index of Position.State mapping in Pool.State: mapping(bytes32 => Position.State) positions;

uint256 public constant POSITIONS_OFFSET = 6;

Functions

getSlot0

Get Slot0 of the pool: sqrtPriceX96, tick, protocolFee, lpFee

Corresponds to pools[poolId].slot0

function getSlot0(IPoolManager manager, PoolId poolId)
internal
view
returns (uint160 sqrtPriceX96, int24 tick, uint24 protocolFee, uint24 lpFee);

Parameters

NameTypeDescription
managerIPoolManagerThe pool manager contract.
poolIdPoolIdThe ID of the pool.

Returns

NameTypeDescription
sqrtPriceX96uint160The square root of the price of the pool, in Q96 precision.
tickint24The current tick of the pool.
protocolFeeuint24The protocol fee of the pool.
lpFeeuint24The swap fee of the pool.

getTickInfo

Retrieves the tick information of a pool at a specific tick.

Corresponds to pools[poolId].ticks[tick]

function getTickInfo(IPoolManager manager, PoolId poolId, int24 tick)
internal
view
returns (uint128 liquidityGross, int128 liquidityNet, uint256 feeGrowthOutside0X128, uint256 feeGrowthOutside1X128);

Parameters

NameTypeDescription
managerIPoolManagerThe pool manager contract.
poolIdPoolIdThe ID of the pool.
tickint24The tick to retrieve information for.

Returns

NameTypeDescription
liquidityGrossuint128The total position liquidity that references this tick
liquidityNetint128The amount of net liquidity added (subtracted) when tick is crossed from left to right (right to left)
feeGrowthOutside0X128uint256fee growth per unit of liquidity on the other side of this tick (relative to the current tick)
feeGrowthOutside1X128uint256fee growth per unit of liquidity on the other side of this tick (relative to the current tick)

getTickLiquidity

Retrieves the liquidity information of a pool at a specific tick.

Corresponds to pools[poolId].ticks[tick].liquidityGross and pools[poolId].ticks[tick].liquidityNet. A more gas efficient version of getTickInfo

function getTickLiquidity(IPoolManager manager, PoolId poolId, int24 tick)
internal
view
returns (uint128 liquidityGross, int128 liquidityNet);

Parameters

NameTypeDescription
managerIPoolManagerThe pool manager contract.
poolIdPoolIdThe ID of the pool.
tickint24The tick to retrieve liquidity for.

Returns

NameTypeDescription
liquidityGrossuint128The total position liquidity that references this tick
liquidityNetint128The amount of net liquidity added (subtracted) when tick is crossed from left to right (right to left)

getTickFeeGrowthOutside

Retrieves the fee growth outside a tick range of a pool

Corresponds to pools[poolId].ticks[tick].feeGrowthOutside0X128 and pools[poolId].ticks[tick].feeGrowthOutside1X128. A more gas efficient version of getTickInfo

function getTickFeeGrowthOutside(IPoolManager manager, PoolId poolId, int24 tick)
internal
view
returns (uint256 feeGrowthOutside0X128, uint256 feeGrowthOutside1X128);

Parameters

NameTypeDescription
managerIPoolManagerThe pool manager contract.
poolIdPoolIdThe ID of the pool.
tickint24The tick to retrieve fee growth for.

Returns

NameTypeDescription
feeGrowthOutside0X128uint256fee growth per unit of liquidity on the other side of this tick (relative to the current tick)
feeGrowthOutside1X128uint256fee growth per unit of liquidity on the other side of this tick (relative to the current tick)

getFeeGrowthGlobals

Retrieves the global fee growth of a pool.

Corresponds to pools[poolId].feeGrowthGlobal0X128 and pools[poolId].feeGrowthGlobal1X128

function getFeeGrowthGlobals(IPoolManager manager, PoolId poolId)
internal
view
returns (uint256 feeGrowthGlobal0, uint256 feeGrowthGlobal1);

Parameters

NameTypeDescription
managerIPoolManagerThe pool manager contract.
poolIdPoolIdThe ID of the pool.

Returns

NameTypeDescription
feeGrowthGlobal0uint256The global fee growth for token0.
feeGrowthGlobal1uint256The global fee growth for token1.

getLiquidity

Retrieves total the liquidity of a pool.

Corresponds to pools[poolId].liquidity

function getLiquidity(IPoolManager manager, PoolId poolId) internal view returns (uint128 liquidity);

Parameters

NameTypeDescription
managerIPoolManagerThe pool manager contract.
poolIdPoolIdThe ID of the pool.

Returns

NameTypeDescription
liquidityuint128The liquidity of the pool.

getTickBitmap

Retrieves the tick bitmap of a pool at a specific tick.

Corresponds to pools[poolId].tickBitmap[tick]

function getTickBitmap(IPoolManager manager, PoolId poolId, int16 tick) internal view returns (uint256 tickBitmap);

Parameters

NameTypeDescription
managerIPoolManagerThe pool manager contract.
poolIdPoolIdThe ID of the pool.
tickint16The tick to retrieve the bitmap for.

Returns

NameTypeDescription
tickBitmapuint256The bitmap of the tick.

getPositionInfo

Retrieves the position information of a pool without needing to calculate the positionId.

Corresponds to pools[poolId].positions[positionId]

function getPositionInfo(
IPoolManager manager,
PoolId poolId,
address owner,
int24 tickLower,
int24 tickUpper,
bytes32 salt
) internal view returns (uint128 liquidity, uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128);

Parameters

NameTypeDescription
managerIPoolManager
poolIdPoolIdThe ID of the pool.
owneraddressThe owner of the liquidity position.
tickLowerint24The lower tick of the liquidity range.
tickUpperint24The upper tick of the liquidity range.
saltbytes32The bytes32 randomness to further distinguish position state.

Returns

NameTypeDescription
liquidityuint128The liquidity of the position.
feeGrowthInside0LastX128uint256The fee growth inside the position for token0.
feeGrowthInside1LastX128uint256The fee growth inside the position for token1.

getPositionInfo

Retrieves the position information of a pool at a specific position ID.

Corresponds to pools[poolId].positions[positionId]

function getPositionInfo(IPoolManager manager, PoolId poolId, bytes32 positionId)
internal
view
returns (uint128 liquidity, uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128);

Parameters

NameTypeDescription
managerIPoolManagerThe pool manager contract.
poolIdPoolIdThe ID of the pool.
positionIdbytes32The ID of the position.

Returns

NameTypeDescription
liquidityuint128The liquidity of the position.
feeGrowthInside0LastX128uint256The fee growth inside the position for token0.
feeGrowthInside1LastX128uint256The fee growth inside the position for token1.

getPositionLiquidity

Retrieves the liquidity of a position.

Corresponds to pools[poolId].positions[positionId].liquidity. More gas efficient for just retrieiving liquidity as compared to getPositionInfo

function getPositionLiquidity(IPoolManager manager, PoolId poolId, bytes32 positionId)
internal
view
returns (uint128 liquidity);

Parameters

NameTypeDescription
managerIPoolManagerThe pool manager contract.
poolIdPoolIdThe ID of the pool.
positionIdbytes32The ID of the position.

Returns

NameTypeDescription
liquidityuint128The liquidity of the position.

getFeeGrowthInside

Calculate the fee growth inside a tick range of a pool

pools[poolId].feeGrowthInside0LastX128 in Position.State is cached and can become stale. This function will calculate the up to date feeGrowthInside

function getFeeGrowthInside(IPoolManager manager, PoolId poolId, int24 tickLower, int24 tickUpper)
internal
view
returns (uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128);

Parameters

NameTypeDescription
managerIPoolManagerThe pool manager contract.
poolIdPoolIdThe ID of the pool.
tickLowerint24The lower tick of the range.
tickUpperint24The upper tick of the range.

Returns

NameTypeDescription
feeGrowthInside0X128uint256The fee growth inside the tick range for token0.
feeGrowthInside1X128uint256The fee growth inside the tick range for token1.

_getPoolStateSlot

function _getPoolStateSlot(PoolId poolId) internal pure returns (bytes32);

_getTickInfoSlot

function _getTickInfoSlot(PoolId poolId, int24 tick) internal pure returns (bytes32);

_getPositionInfoSlot

function _getPositionInfoSlot(PoolId poolId, bytes32 positionId) internal pure returns (bytes32);
Helpful?