Skip to main content

SwapMath

Contains methods for computing the result of a swap within a single tick price range, i.e., a single tick.

Functions

computeSwapStep

  function computeSwapStep(
uint160 sqrtRatioCurrentX96,
uint160 sqrtRatioTargetX96,
uint128 liquidity,
int256 amountRemaining,
uint24 feePips
) internal pure returns (uint160 sqrtRatioNextX96, uint256 amountIn, uint256 amountOut, uint256 feeAmount)

Computes the result of swapping some amount in, or amount out, given the parameters of the swap

The fee, plus the amount in, will never exceed the amount remaining if the swap's amountSpecified is positive

Parameters:

NameTypeDescription
sqrtRatioCurrentX96uint160The current sqrt price of the pool
sqrtRatioTargetX96uint160The price that cannot be exceeded, from which the direction of the swap is inferred
liquidityuint128The usable liquidity
amountRemainingint256How much input or output amount is remaining to be swapped in/out
feePipsuint24The fee taken from the input amount, expressed in hundredths of a bip

Return Values:

NameTypeDescription
sqrtRatioNextX96uint160The price after swapping the amount in/out, not to exceed the price target
amountInuint256The amount to be swapped in, of either token0 or token1, based on the direction of the swap
amountOutuint256The amount to be received, of either token0 or token1, based on the direction of the swap
feeAmountuint256The amount of input that will be taken as a fee