SqrtPriceMath
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:
Name | Type | Description |
---|---|---|
| uint160 | The starting price, i.e. before accounting for the token0 delta |
| uint128 | The amount of usable liquidity |
| uint256 | How much of token0 to add or remove from virtual reserves |
| 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
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 |
---|---|---|
| uint160 | The starting price, i.e., before accounting for the token1 delta |
| uint128 | The amount of usable liquidity |
| uint256 | How much of token1 to add, or remove, from virtual reserves |
| bool | Whether to add, or remove, the amount of token1 |
Return Values:
Type | Description |
---|---|
uint160 | price after adding or removing |
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:
Name | Type | Description |
---|---|---|
| uint160 | The starting price, i.e., before accounting for the input amount |
| uint128 | The amount of usable liquidity |
| uint256 | How much of token0, or token1, is being swapped in |
| bool | Whether the amount in is token0 or token1 |
Return Values:
Name | Type | Description |
---|---|---|
| uint160 | The price after adding the input amount to token0 or token1 |
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:
Name | Type | Description |
---|---|---|
| uint160 | The starting price before accounting for the output amount |
| uint128 | The amount of usable liquidity |
| uint256 | How much of token0, or token1, is being swapped out |
| bool | Whether the amount out is token0 or token1 |
Return Values:
Name | Type | Description |
---|---|---|
| uint160 | The price after removing the output amount of token0 or token1 |
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:
Name | Type | Description |
---|---|---|
| uint160 | A sqrt price |
| uint160 | Another sqrt price |
| uint128 | The amount of usable liquidity |
| bool | Whether to round the amount up or down |
Return Values:
Name | Type | Description |
---|---|---|
| uint256 | Amount of token0 required to cover a position of size liquidity between the two passed prices |
getAmount1Delta
Gets the amount1 delta between two prices
Calculates liquidity * (sqrt(upper) - sqrt(lower))
Parameters:
Name | Type | Description |
---|---|---|
| uint160 | A sqrt price |
| uint160 | Another sqrt price |
| uint128 | The amount of usable liquidity |
| bool | Whether to round the amount up, or down |
Return Values:
Name | Type | Description |
---|---|---|
| uint256 | Amount of token1 required to cover a position of size liquidity between the two passed prices |
getAmount0Delta
Helper that gets signed token0 delta
Parameters:
Name | Type | Description |
---|---|---|
| uint160 | A sqrt price |
| uint160 | Another sqrt price |
| int128 | The change in liquidity for which to compute the amount0 delta |
Return Values:
Name | Type | Description |
---|---|---|
| int256 | Amount of token0 corresponding to the passed liquidityDelta between the two prices |
getAmount1Delta
Helper that gets signed token1 delta
Parameters:
Name | Type | Description |
---|---|---|
| uint160 | A sqrt price |
| uint160 | Another sqrt price |
| int128 | The change in liquidity for which to compute the amount1 delta |
Return Values:
Name | Type | Description |
---|---|---|
| int256 | Amount of token1 corresponding to the passed liquidityDelta between the two prices |
Last updated