Skip to main content

V4StateReader

Git Source

Author: Axicon Labs Limited, credit to Uniswap Labs under MIT License

A library to retrieve state information from Uniswap V4 pools via extsload.

Functions

getSqrtPriceX96

Retrieves the current sqrtPriceX96 from a Uniswap V4 pool.

function getSqrtPriceX96(IPoolManager manager, PoolId poolId) internal view returns (uint160 sqrtPriceX96);

Parameters

NameTypeDescription
managerIPoolManagerThe Uniswap V4 pool manager contract
poolIdPoolIdThe pool ID of the Uniswap V4 pool

Returns

NameTypeDescription
sqrtPriceX96uint160The current sqrtPriceX96 of the Uniswap V4 pool

getTick

Retrieves the current tick from a Uniswap V4 pool.

function getTick(IPoolManager manager, PoolId poolId) internal view returns (int24 tick);

Parameters

NameTypeDescription
managerIPoolManagerThe Uniswap V4 pool manager contract
poolIdPoolIdThe pool ID of the Uniswap V4 pool

Returns

NameTypeDescription
tickint24The current tick of the Uniswap V4 pool

getFeeGrowthInside

Calculates the fee growth that has occurred (per unit of liquidity) in the AMM/Uniswap for an option position's tick range.

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

Parameters

NameTypeDescription
managerIPoolManagerThe Uniswap V4 pool manager contract
idV4PoolIdThe pool ID of the Uniswap V4 pool
currentTickint24The current price tick in the AMM
tickLowerint24The lower tick of the option position leg (a liquidity chunk)
tickUpperint24The upper tick of the option position leg (a liquidity chunk)

Returns

NameTypeDescription
feeGrowthInside0X128uint256The fee growth in the AMM for currency0
feeGrowthInside1X128uint256The fee growth in the AMM for currency1

getFeeGrowthInsideLast

Diagrams shown for currency0, and applies for currency1 the same L = lowerTick, U = upperTick liquidity lowerOut0 (all fees collected in this price tick range for currency0) ▲ ◄──────────────^v───► (to MAX_TICK) │ │ upperOut0 │ ◄─────^v───► │ ┌────────┐ │ │ chunk │ │ │ │ └─────▲─────┴────────┴────────► price tick │ L U │ current tick liquidity ▲ upperOut0 │◄─^v─────────────────────► │ │ lowerOut0 ┌────────┐ │◄─^v───────────►│ chunk │ │ │ │ └────────────────┴────────┴─▲─────► price tick L U │ │ current tick

Retrieves the last stored feeGrowthInsideLast values for a unique Uniswap V4 position.

Corresponds to pools[poolId].positions[positionId] in manager.

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

Parameters

NameTypeDescription
managerIPoolManagerThe Uniswap V4 pool manager contract
poolIdPoolIdThe ID of the Uniswap V4 pool
positionIdbytes32The ID of the position, which is a hash of the owner, tickLower, tickUpper, and salt.

Returns

NameTypeDescription
feeGrowthInside0LastX128uint256The fee growth inside the position for currency0
feeGrowthInside1LastX128uint256The fee growth inside the position for currency1