UNPKG

@swaptoshi/dex-module

Version:

Klayr decentralized exchange (dex) on-chain module

53 lines 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.computeSwapStep = computeSwapStep; const int_1 = require("../int"); const FullMath = require("./full_math"); const SqrtPriceMath = require("./sqrt_price_math"); function computeSwapStep(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, amountRemaining, feePips) { let sqrtRatioNextX96 = int_1.Uint160.from(0); let amountIn = int_1.Uint256.from(0); let amountOut = int_1.Uint256.from(0); let feeAmount = int_1.Uint256.from(0); const zeroForOne = int_1.Uint160.from(sqrtRatioCurrentX96).gte(sqrtRatioTargetX96); const exactIn = int_1.Int256.from(amountRemaining).gte(0); if (exactIn) { const amountRemainingLessFee = int_1.Uint256.from(FullMath.mulDiv(int_1.Uint256.from(amountRemaining).toString(), int_1.Uint24.from(1000000).sub(feePips).toString(), '1000000')); amountIn = zeroForOne ? int_1.Uint256.from(SqrtPriceMath.getAmount0DeltaHelper(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, true)) : int_1.Uint256.from(SqrtPriceMath.getAmount1DeltaHelper(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, true)); if (amountRemainingLessFee.gte(amountIn)) sqrtRatioNextX96 = int_1.Uint160.from(sqrtRatioTargetX96); else sqrtRatioNextX96 = int_1.Uint160.from(SqrtPriceMath.getNextSqrtPriceFromInput(sqrtRatioCurrentX96, liquidity, amountRemainingLessFee.toString(), zeroForOne)); } else { amountOut = zeroForOne ? int_1.Uint256.from(SqrtPriceMath.getAmount1DeltaHelper(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, false)) : int_1.Uint256.from(SqrtPriceMath.getAmount0DeltaHelper(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, false)); if (int_1.Uint256.from(0).sub(amountRemaining).gte(amountOut)) sqrtRatioNextX96 = int_1.Uint160.from(sqrtRatioTargetX96); else sqrtRatioNextX96 = int_1.Uint160.from(SqrtPriceMath.getNextSqrtPriceFromOutput(sqrtRatioCurrentX96, liquidity, int_1.Uint256.from(0).sub(amountRemaining).toString(), zeroForOne)); } const max = sqrtRatioNextX96.eq(sqrtRatioTargetX96); if (zeroForOne) { amountIn = max && exactIn ? amountIn : int_1.Uint256.from(SqrtPriceMath.getAmount0DeltaHelper(sqrtRatioNextX96.toString(), sqrtRatioCurrentX96, liquidity, true)); amountOut = max && !exactIn ? amountOut : int_1.Uint256.from(SqrtPriceMath.getAmount1DeltaHelper(sqrtRatioNextX96.toString(), sqrtRatioCurrentX96, liquidity, false)); } else { amountIn = max && exactIn ? amountIn : int_1.Uint256.from(SqrtPriceMath.getAmount1DeltaHelper(sqrtRatioCurrentX96, sqrtRatioNextX96.toString(), liquidity, true)); amountOut = max && !exactIn ? amountOut : int_1.Uint256.from(SqrtPriceMath.getAmount0DeltaHelper(sqrtRatioCurrentX96, sqrtRatioNextX96.toString(), liquidity, false)); } if (!exactIn && amountOut.gt(int_1.Uint256.from(0).sub(amountRemaining))) { amountOut = int_1.Uint256.from(0).sub(amountRemaining); } if (exactIn && !sqrtRatioNextX96.eq(sqrtRatioTargetX96)) { feeAmount = int_1.Uint256.from(amountRemaining).sub(amountIn); } else { feeAmount = int_1.Uint256.from(FullMath.mulDivRoundingUp(amountIn.toString(), feePips, int_1.Uint24.from(1000000).sub(feePips).toString())); } return [sqrtRatioNextX96.toString(), amountIn.toString(), amountOut.toString(), feeAmount.toString()]; } //# sourceMappingURL=swap_math.js.map