SqrtPriceMath

Compute deltas based on liquidity and square root price data.

GitHub File: sqrtPriceMath.ts

Constructor

Private constructor that cannot be constructed.

Methods

getAmount0Unlock() - public static

Returns the amount of token0 required to initialize the pool.

Parameters

ParamsTypeDescription

sqrtRatioInitX96

The square root price of the pool upon initialization, denominated in Q notation.

Returns

TypeDescription

The amount of token0 required to initialize the pool.


getAmount1Unlock() - public static

Returns the amount of token1 required to initialize the pool.

Parameters

ParamsTypeDescription

sqrtRatioInitX96

The square root price of the pool upon initialization, denominated in Q notation.

Returns

TypeDescription

The amount of token1 required to initialize the pool.


getAmount0Delta() - public static

Gets the liquidity delta between two token0 prices.

Parameters

ParamsTypeDescription

sqrtRatioAX96

A sqrt price of token0.

sqrtRatioBX96

Another sqrt price of token0.

liquidity

The amount of usable liquidity.

roundUp

boolean

true = Result is rounded up to the nearest integer. false = Result is not rounded down to the ne

Returns

TypeDescription

Amount of token0 required to cover a position of size liquidity between the two passed prices.


getAmount1Delta() - public static

Gets the liquidity delta between two token1 prices.

Parameters

ParamsTypeDescription

sqrtRatioAX96

A sqrt price of token0.

sqrtRatioBX96

Another sqrt price of token0.

liquidity

The amount of usable liquidity.

roundUp

boolean

true = Result is rounded up to the nearest integer. false = Result is not rounded down to the ne

Returns

TypeDescription

Amount of token1 required to cover a position of size liquidity between the two passed prices.


getNextSqrtPriceFromInput() - public static

Gets the next sqrt price given an input amount of token0 or token1.

Parameters

ParamsTypeDescription

sqrtPX96

A sqrt price of token0.

liquidity

The amount of usable liquidity.

amounIn

The amount of tokens being swapped in.

zeroForOne

boolean

true = Amount in is in token1. false = Amount in is in token0.

Returns

TypeDescription

The price after removing the output amount of token0 or token1


getNextSqrtPriceFromOutput() - public static

Gets the next sqrt price given an output amount of token0 or token1.

Parameters

ParamsTypeDescription

sqrtPX96

A sqrt price of token0.

liquidity

The amount of usable liquidity.

amountOut

The amount of tokens being swapped out.

zeroForOne

boolean

true = Amount out is in token1. false = Amount out is in token0.

Returns

TypeDescription

The price after removing the output amount of token0 or token1


getNextSqrtPriceFromAmount0RoundingUp() - private static

Gets the next sqrt price given a delta of token0. The token0 price is rounded up to ensure that the virtual reserves never falls below 0 and the output amount is always achieved.

Parameters

ParamsTypeDescription

sqrtPX96

The starting price of of token0.

liquidity

The amount of usable liquidity.

amount

The amount of token0 to add or remove from virtual reserves.

add

boolean

true = Amount is added to the virtual reserve. false = Amount is removed from the virtual reserve.

Returns

TypeDescription

The token0 price after adding or removing the amount specified.


getNextSqrtPriceFromAmount1RoundingDown() - private static

Gets the next sqrt price given a delta of token1. The token1 price is rounded down to ensure that the virtual reserves never falls below 0 and the output amount is always achieved.

Parameters

ParamsTypeDescription

sqrtPX96

The starting price of of token1.

liquidity

The amount of usable liquidity.

amount

The amount of token0 to add or remove from virtual reserves.

add

boolean

true = Amount is added to the virtual reserve. false = Amount is removed from the virtual reserve.

Returns

TypeDescription

The token1 price after adding or removing the amount specified.

Last updated