Skip to main content
Contains the math that uses square root of price as a Q64.96 and liquidity to compute deltas

Functions

getNextSqrtPriceFromAmount0RoundingUp

Gets the next sqrt price given a delta of token0 Always rounds up, because in the exact output case (increasing price) we need to move the price at least far enough to get the desired output amount, and in the exact input case (decreasing price) we need to move the price less in order to not send too much output. The most precise formula for this is liquidity * sqrtPX96 / (liquidity +- amount * sqrtPX96), if this is impossible because of overflow, we calculate liquidity / (liquidity / sqrtPX96 +- amount).

Parameters:

Return Values:

getNextSqrtPriceFromAmount1RoundingDown

Gets the next sqrt price given a delta of token1 Always rounds down, because in the exact output case (decreasing price) we need to move the price at least far enough to get the desired output amount, and in the exact input case (increasing price) we need to move the price less in order to not send too much output. The formula we compute is within <1 wei of the lossless version: sqrtPX96 +- amount / liquidity

Parameters:

Return Values:

getNextSqrtPriceFromInput

Gets the next sqrt price given an input amount of token0 or token1 Throws if price or liquidity are 0, or if the next price is out of bounds

Parameters:

Return Values:

getNextSqrtPriceFromOutput

Gets the next sqrt price given an output amount of token0 or token1 Throws if price or liquidity are 0 or the next price is out of bounds

Parameters:

Return Values:

getAmount0Delta

Gets the amount0 delta between two prices Calculates liquidity / sqrt(lower) - liquidity / sqrt(upper), i.e. liquidity * (sqrt(upper) - sqrt(lower)) / (sqrt(upper) * sqrt(lower))

Parameters:

Return Values:

getAmount1Delta

Gets the amount1 delta between two prices Calculates liquidity * (sqrt(upper) - sqrt(lower))

Parameters:

Return Values:

getAmount0Delta

Helper that gets signed token0 delta

Parameters:

Return Values:

getAmount1Delta

Helper that gets signed token1 delta

Parameters:

Return Values: