Skip to main content
Helpful?

LiquidityAmounts

The LiquidityAmounts library provides functions for computing liquidity amounts from token amounts and prices in Uniswap V4.

Key Concept: sqrtPriceX96

sqrtPriceX96 represents the square root of the price ratio of token1 to token0, multiplied by 2^96. This representation allows for precise price calculations across a wide range of values while using fixed-point arithmetic. It's more efficient than using ticks for intermediate calculations, as it avoids frequent conversions between prices and ticks.

Functions

getLiquidityForAmount0

function getLiquidityForAmount0(uint160 sqrtPriceAX96, uint160 sqrtPriceBX96, uint256 amount0)
internal
pure
returns (uint128 liquidity)

Computes the amount of liquidity received for a given amount of token0 and price range.

Param NameTypeDescription
sqrtPriceAX96uint160Square root of price at first tick boundary
sqrtPriceBX96uint160Square root of price at second tick boundary
amount0uint256The amount of token0 being sent in

Returns the amount of liquidity received.

getLiquidityForAmount1

function getLiquidityForAmount1(uint160 sqrtPriceAX96, uint160 sqrtPriceBX96, uint256 amount1)
internal
pure
returns (uint128 liquidity)

Computes the amount of liquidity received for a given amount of token1 and price range.

Param NameTypeDescription
sqrtPriceAX96uint160Square root of price at first tick boundary
sqrtPriceBX96uint160Square root of price at second tick boundary
amount1uint256The amount of token1 being sent in

Returns the amount of liquidity received.

getLiquidityForAmounts

function getLiquidityForAmounts(
uint160 sqrtPriceX96,
uint160 sqrtPriceAX96,
uint160 sqrtPriceBX96,
uint256 amount0,
uint256 amount1
) internal pure returns (uint128 liquidity)

Computes the maximum amount of liquidity received for given amounts of token0 and token1, the current pool prices, and the prices at the tick boundaries.

Param NameTypeDescription
sqrtPriceX96uint160Current square root price of the pool
sqrtPriceAX96uint160Square root of price at first tick boundary
sqrtPriceBX96uint160Square root of price at second tick boundary
amount0uint256The amount of token0 being sent in
amount1uint256The amount of token1 being sent in

Returns the maximum amount of liquidity received.

getAmount0ForLiquidity

function getAmount0ForLiquidity(uint160 sqrtPriceAX96, uint160 sqrtPriceBX96, uint128 liquidity)
internal
pure
returns (uint256 amount0)

Computes the amount of token0 for a given amount of liquidity and a price range.

Param NameTypeDescription
sqrtPriceAX96uint160Square root of price at first tick boundary
sqrtPriceBX96uint160Square root of price at second tick boundary
liquidityuint128The liquidity being valued

Returns the amount of token0.

getAmount1ForLiquidity

function getAmount1ForLiquidity(uint160 sqrtPriceAX96, uint160 sqrtPriceBX96, uint128 liquidity)
internal
pure
returns (uint256 amount1)

Computes the amount of token1 for a given amount of liquidity and a price range.

Param NameTypeDescription
sqrtPriceAX96uint160Square root of price at first tick boundary
sqrtPriceBX96uint160Square root of price at second tick boundary
liquidityuint128The liquidity being valued

Returns the amount of token1.

getAmountsForLiquidity

function getAmountsForLiquidity(
uint160 sqrtPriceX96,
uint160 sqrtPriceAX96,
uint160 sqrtPriceBX96,
uint128 liquidity
) internal pure returns (uint256 amount0, uint256 amount1)

Computes the token0 and token1 value for a given amount of liquidity, the current pool prices, and the prices at the tick boundaries.

Param NameTypeDescription
sqrtPriceX96uint160Current square root price of the pool
sqrtPriceAX96uint160Square root of price at first tick boundary
sqrtPriceBX96uint160Square root of price at second tick boundary
liquidityuint128The liquidity being valued

Returns the amount of token0 and token1.

Helpful?