LiquidityAmounts

Git Source

Authors: Aloe Labs, Inc., Modified from Uniswap

Provides functions for computing liquidity amounts from token amounts and prices

Functions

getAmountsForLiquidity

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

function getAmountsForLiquidity(
    uint160 sqrtRatioX96,
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint128 liquidity
) internal pure returns (uint256 amount0, uint256 amount1);

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

Returns

NameTypeDescription
amount0uint256The amount of token0
amount1uint256The amount of token1

getValuesOfLiquidity

Computes the value of each portion of the liquidity in terms of token1

Each return value can fit in a uint192 if necessary

function getValuesOfLiquidity(
    uint160 sqrtRatioX96,
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint128 liquidity
) internal pure returns (uint256 value0, uint256 value1);

Parameters

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

Returns

NameTypeDescription
value0uint256The value of amount0 underlying liquidity, in terms of token1
value1uint256The amount of token1

getValueOfLiquidity

Computes the value of the liquidity in terms of token1

The return value can fit in a uint192 if necessary

function getValueOfLiquidity(
    uint160 sqrtRatioX96,
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint128 liquidity
) internal pure returns (uint256);

Parameters

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

Returns

NameTypeDescription
<none>uint256The value of the underlying liquidity, in terms of token1

_getAmount0ForLiquidity

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

function _getAmount0ForLiquidity(
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint128 liquidity
) private pure returns (uint256 amount0);

Parameters

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

Returns

NameTypeDescription
amount0uint256The amount of token0. Will fit in a uint224 if you need it to

_getAmount1ForLiquidity

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

function _getAmount1ForLiquidity(
    uint160 sqrtRatioAX96,
    uint160 sqrtRatioBX96,
    uint128 liquidity
) private pure returns (uint256 amount1);

Parameters

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

Returns

NameTypeDescription
amount1uint256The amount of token1. Will fit in a uint192 if you need it to