SqrtPriceMath
Contains the math that uses square root of price as a Q64.96 and liquidity to compute deltas
Functions
getNextSqrtPriceFromAmount0RoundingUp
function getNextSqrtPriceFromAmount0RoundingUp(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amount,
bool add
) internal pure returns (uint160)
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:
Name | Type | Description |
---|---|---|
sqrtPX96 | uint160 | The starting price, i.e. before accounting for the token0 delta |
liquidity | uint128 | The amount of usable liquidity |
amount | uint256 | How much of token0 to add or remove from virtual reserves |
add | bool | Whether to add or remove the amount of token0 |
Return Values:
Type | Description |
---|---|
uint160 | price after adding or removing amount, depending on add |
getNextSqrtPriceFromAmount1RoundingDown
function getNextSqrtPriceFromAmount1RoundingDown(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amount,
bool add
) internal pure returns (uint160)
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:
Name | Type | Description |
---|---|---|
sqrtPX96 | uint160 | The starting price, i.e., before accounting for the token1 delta |
liquidity | uint128 | The amount of usable liquidity |
amount | uint256 | How much of token1 to add, or remove, from virtual reserves |
add | bool | Whether to add, or remove, the amount of token1 |