Skip to main content

LiquidityAmounts

Provides functions for computing liquidity amounts from token amounts and prices

Functions

getLiquidityForAmount0

  function getLiquidityForAmount0(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint256 amount0
) internal returns (uint128 liquidity)

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

Calculates amount0 (sqrt(upper) sqrt(lower)) / (sqrt(upper) - sqrt(lower))

Parameters:

NameTypeDescription
sqrtRatioAX96uint160A sqrt price representing the first tick boundary
sqrtRatioBX96uint160A sqrt price representing the second tick boundary
amount0uint256The amount0 being sent in

Return Values:

NameTypeDescription
liquidityuint128The amount of returned liquidity

getLiquidityForAmount1

  function getLiquidityForAmount1(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint256 amount1
) internal returns (uint128 liquidity)

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

Calculates amount1 / (sqrt(upper) - sqrt(lower)).

Parameters:

NameTypeDescription
sqrtRatioAX96uint160A sqrt price representing the first tick boundary
sqrtRatioBX96uint160A sqrt price representing the second tick boundary
amount1uint256The amount1 being sent in

Return Values:

NameTypeDescription
liquidityuint128The amount of returned liquidity

getLiquidityForAmounts

  function getLiquidityForAmounts(
uint160 sqrtRatioX96,
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint256 amount0,
uint256 amount1
) internal returns (uint128 liquidity)

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

Parameters:

NameTypeDescription
sqrtRatioX96uint160A sqrt price representing the current pool prices
sqrtRatioAX96uint160A sqrt price representing the first tick boundary
sqrtRatioBX96uint160A sqrt price representing the second tick boundary
amount0uint256The amount of token0 being sent in
amount1uint256The amount of token1 being sent in

Return Values:

NameTypeDescription
liquidityuint128The maximum amount of liquidity received

getAmount0ForLiquidity

  function getAmount0ForLiquidity(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity
) internal returns (uint256 amount0)

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

Parameters:

NameTypeDescription
sqrtRatioAX96uint160A sqrt price representing the first tick boundary
sqrtRatioBX96uint160A sqrt price representing the second tick boundary
liquidityuint128The liquidity being valued

Return Values:

NameTypeDescription
amount0uint256The amount of token0

getAmount1ForLiquidity

  function getAmount1ForLiquidity(
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity
) internal returns (uint256 amount1)

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

Parameters:

NameTypeDescription
sqrtRatioAX96uint160A sqrt price representing the first tick boundary
sqrtRatioBX96uint160A sqrt price representing the second tick boundary
liquidityuint128The liquidity being valued

Return Values:

NameTypeDescription
amount1uint256The amount of token1

getAmountsForLiquidity

  function getAmountsForLiquidity(
uint160 sqrtRatioX96,
uint160 sqrtRatioAX96,
uint160 sqrtRatioBX96,
uint128 liquidity
) internal 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

Parameters:

NameTypeDescription
sqrtRatioX96uint160A sqrt price representing the current pool prices
sqrtRatioAX96uint160A sqrt price representing the first tick boundary
sqrtRatioBX96uint160A sqrt price representing the second tick boundary
liquidityuint128The liquidity being valued

Return Values:

NameTypeDescription
amount0uint256The amount of token0
amount1uint256The amount of token1