KyberSwap Docs
Search
K

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

Params
Type
Description
sqrtRatioCurrentX96
JSBI
The current square root price of the pool denominated in Q notation.
sqrtRatioTargetX96
JSBI
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
JSBI
The amount of usable liquidity.
amountRemaining
JSBI
Remaining amount of tokens that needs to be swapped in or out.
feePips
FeeAmount
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

Type
Description
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

Params
Type
Description
sqrtRatioCurrentX96
JSBI
The current square root price of the pool denominated in Q notation.
sqrtRatioTargetX96
JSBI
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
JSBI
The amount of usable liquidity.
feePips
FeeAmount
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

Type
Description
JSBI
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

Params
Type
Description
sqrtRatioCurrentX96
JSBI
The current square root price of the pool denominated in Q notation.
sqrtRatioTargetX96
JSBI
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
JSBI
The amount of usable liquidity.
deltaL
JSBI
The liquidity delta due to the swap.
feePips
FeeAmount
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

Type
Description
JSBI
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

Params
Type
Description
sqrtRatioCurrentX96
JSBI
The current square root price of the pool denominated in Q notation.
sqrtRatioTargetX96
JSBI
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
JSBI
The amount of usable liquidity.
absAmount
JSBI
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

Type
Description
JSBI
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

Params
Type
Description
absAmount
JSBI
The absolute amount of tokens used for the swap.
liquidity
JSBI
The amount of usable liquidity.
sqrtRatioCurrentX96
JSBI
The current square root price of the pool denominated in Q notation.
feePips
FeeAmount
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

Type
Description
JSBI
The estimated incremental liquidity amount following a swap.

calcFinalPrice() - public static

Returns the final price following the swap.

Parameters

Params
Type
Description
absAmount
JSBI
The absolute amount of tokens used for the swap.
liquidity
JSBI
The amount of usable liquidity.
deltaL
JSBI
The liquidity delta due to the swap.
sqrtRatioCurrentX96
JSBI
The current square root price of the pool denominated in Q notation.
feePips
FeeAmount
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

Type
Description
JSBI
The final price following the swap.