SwapMath

Handles the swap maths within ticks.

GitHub File: swapMath.ts

Constructor

Private constructor that cannot be constructed.

Methods

computeSwapStepPromm() - public static

Returns the price and token amounts resulting from an Elastic swap.

Parameters

ParamsTypeDescription

sqrtRatioCurrentX96

The current square root price of the pool denominated in Q notation.

sqrtRatioTargetX96

The target square root price of the pool following the swap denominated in Q notation. Direction of the swap is inferred by comparing the target price against the current pool price.

liquidity

The amount of usable liquidity.

amountRemaining

Remaining amount of tokens that needs to be swapped in or out.

feePips

The swap fee taken from the input amount, denominated in hundredths of bips.

exactIn

boolean

true = Swap logic is based on exact in. false = Swap logic is based on exact out.

zeroForOne

boolean

true = Swap is from token0 to token1. false = Swap is from token1 to token0.

Returns

TypeDescription

An array consisting of the following value by index: 0 = The square root price after the swap. 1 = The amount of input tokens for the swap. 2 = The amount of output tokens from the swap. 3 = The liquidity delta due to the swap.


calcReachAmount() - public static

Returns the amount of tokens used in the swap in order to reach the target price.

Parameters

ParamsTypeDescription

sqrtRatioCurrentX96

The current square root price of the pool denominated in Q notation.

sqrtRatioTargetX96

The target square root price of the pool following the swap denominated in Q notation. Direction of the swap is inferred by comparing the target price against the current pool price.

liquidity

The amount of usable liquidity.

feePips

The swap fee taken from the input amount, denominated in hundredths of bips.

exactIn

boolean

true = Swap logic is based on exact in. false = Swap logic is based on exact out.

zeroForOne

boolean

true = Swap is from token0 to token1. false = Swap is from token1 to token0.

Returns

TypeDescription

The amount of tokens used in the swap in order to reach the target price.


calcReturnedAmount() - public static

Returns the output token amount from the swap.

Parameters

ParamsTypeDescription

sqrtRatioCurrentX96

The current square root price of the pool denominated in Q notation.

sqrtRatioTargetX96

The target square root price of the pool following the swap denominated in Q notation. Direction of the swap is inferred by comparing the target price against the current pool price.

liquidity

The amount of usable liquidity.

deltaL

The liquidity delta due to the swap.

feePips

The swap fee taken from the input amount, denominated in hundredths of bips.

exactIn

boolean

true = Swap logic is based on exact in. false = Swap logic is based on exact out.

zeroForOne

boolean

true = Swap is from token0 to token1. false = Swap is from token1 to token0.

Returns

TypeDescription

Swap output token amount.


calcIncrementalLiquidity() - public static

Returns the incremental liquidity in the case where the resulting pool price after the swap is non-zero (i.e. target price was reached within the pool).

Parameters

ParamsTypeDescription

sqrtRatioCurrentX96

The current square root price of the pool denominated in Q notation.

sqrtRatioTargetX96

The target square root price of the pool following the swap denominated in Q notation. Direction of the swap is inferred by comparing the target price against the current pool price.

liquidity

The amount of usable liquidity.

absAmount

The absolute amount of tokens used for the swap.

exactIn

boolean

true = Swap logic is based on exact in. false = Swap logic is based on exact out.

zeroForOne

boolean

true = Swap is from token0 to token1. false = Swap is from token1 to token0.

Returns

TypeDescription

The incremental liquidity amount following a swap.


estimateIncrementalLiquidity() - public static

Returns the estimated incremental liquidity in the case where the resulting pool price after the swap is zero (i.e. target price is outside the pool).

Parameters

ParamsTypeDescription

absAmount

The absolute amount of tokens used for the swap.

liquidity

The amount of usable liquidity.

sqrtRatioCurrentX96

The current square root price of the pool denominated in Q notation.

feePips

The swap fee taken from the input amount, denominated in hundredths of bips.

exactIn

boolean

true = Swap logic is based on exact in. false = Swap logic is based on exact out.

zeroForOne

boolean

true = Swap is from token0 to token1. false = Swap is from token1 to token0.

Returns

TypeDescription

The estimated incremental liquidity amount following a swap.


calcFinalPrice() - public static

Returns the final price following the swap.

Parameters

ParamsTypeDescription

absAmount

The absolute amount of tokens used for the swap.

liquidity

The amount of usable liquidity.

deltaL

The liquidity delta due to the swap.

sqrtRatioCurrentX96

The current square root price of the pool denominated in Q notation.

feePips

The swap fee taken from the input amount, denominated in hundredths of bips.

exactIn

boolean

true = Swap logic is based on exact in. false = Swap logic is based on exact out.

zeroForOne

boolean

true = Swap is from token0 to token1. false = Swap is from token1 to token0.

Returns

TypeDescription

The final price following the swap.

Last updated