@swaptoshi/dex-module
Version:
Klayr decentralized exchange (dex) on-chain module
98 lines • 6.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNextSqrtPriceFromAmount0RoundingUp = getNextSqrtPriceFromAmount0RoundingUp;
exports.getNextSqrtPriceFromAmount1RoundingDown = getNextSqrtPriceFromAmount1RoundingDown;
exports.getNextSqrtPriceFromInput = getNextSqrtPriceFromInput;
exports.getNextSqrtPriceFromOutput = getNextSqrtPriceFromOutput;
exports.getAmount0DeltaHelper = getAmount0DeltaHelper;
exports.getAmount1DeltaHelper = getAmount1DeltaHelper;
exports.getAmount0Delta = getAmount0Delta;
exports.getAmount1Delta = getAmount1Delta;
const int_1 = require("../int");
const FixedPoint96 = require("./fixed_point_96");
const FullMath = require("./full_math");
const UnsafeMath = require("./unsafe_math");
function getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amount, add) {
if (int_1.Uint256.from(amount).eq(0))
return sqrtPX96;
const numerator1 = int_1.Uint256.from(liquidity).shl(parseInt(FixedPoint96.RESOLUTION, 10));
if (add) {
const product = int_1.Uint256.from(amount).mul(sqrtPX96);
if (product.div(amount).eq(sqrtPX96)) {
const denominator = numerator1.add(product);
if (denominator.gte(numerator1))
return FullMath.mulDivRoundingUp(numerator1.toString(), sqrtPX96, denominator.toString());
}
return int_1.Uint160.from(UnsafeMath.divRoundingUp(numerator1.toString(), numerator1.div(sqrtPX96).add(amount).toString())).toString();
}
const product = int_1.Uint256.from(amount).mul(sqrtPX96);
if (product.div(amount).eq(sqrtPX96) && numerator1.gt(product)) {
const denominator = numerator1.sub(product);
return int_1.Uint160.from(FullMath.mulDivRoundingUp(numerator1.toString(), sqrtPX96, denominator.toString())).toString();
}
throw new Error('denominator overflow');
}
function getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amount, add) {
if (add) {
const quotient = int_1.Uint256.from(amount).lte(int_1.Uint160.MAX)
? int_1.Uint256.from(amount).shl(parseInt(FixedPoint96.RESOLUTION, 10)).div(liquidity)
: int_1.Uint256.from(FullMath.mulDiv(amount, FixedPoint96.Q96, liquidity));
return int_1.Uint160.from(int_1.Uint256.from(sqrtPX96).add(quotient).toString()).toString();
}
const quotient = int_1.Uint256.from(amount).lte(int_1.Uint160.MAX)
? int_1.Uint256.from(UnsafeMath.divRoundingUp(int_1.Uint256.from(amount).shl(parseInt(FixedPoint96.RESOLUTION, 10)).toString(), liquidity))
: int_1.Uint256.from(FullMath.mulDivRoundingUp(amount, FixedPoint96.Q96, liquidity));
if (int_1.Uint256.from(sqrtPX96).lte(quotient)) {
throw new Error('sqrtPX96 is less than equal to quotient');
}
return int_1.Uint160.from(sqrtPX96).sub(quotient).toString();
}
function getNextSqrtPriceFromInput(sqrtPX96, liquidity, amountIn, zeroForOne) {
if (int_1.Uint160.from(sqrtPX96).lte(0))
throw new Error('sqrtPX96 must be positive');
if (int_1.Uint128.from(liquidity).lte(0))
throw new Error('liquidity must be positive');
return zeroForOne ? getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true) : getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true);
}
function getNextSqrtPriceFromOutput(sqrtPX96, liquidity, amountOut, zeroForOne) {
if (int_1.Uint160.from(sqrtPX96).lte(0))
throw new Error('sqrtPX96 must be positive');
if (int_1.Uint128.from(liquidity).lte(0))
throw new Error('liquidity must be positive');
return zeroForOne ? getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false) : getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false);
}
function getAmount0DeltaHelper(_sqrtRatioAX96, _sqrtRatioBX96, liquidity, roundUp) {
let sqrtRatioAX96 = _sqrtRatioAX96;
let sqrtRatioBX96 = _sqrtRatioBX96;
if (int_1.Uint160.from(sqrtRatioAX96).gt(sqrtRatioBX96))
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
const numerator1 = int_1.Uint256.from(liquidity).shl(parseInt(FixedPoint96.RESOLUTION, 10)).toString();
const numerator2 = int_1.Uint256.from(sqrtRatioBX96).sub(sqrtRatioAX96).toString();
if (int_1.Uint256.from(sqrtRatioAX96).lte(0))
throw new Error('sqrtRatioAX96 must be positive');
return roundUp
? UnsafeMath.divRoundingUp(FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96), sqrtRatioAX96)
: int_1.Uint256.from(FullMath.mulDiv(numerator1, numerator2, sqrtRatioBX96))
.div(sqrtRatioAX96)
.toString();
}
function getAmount1DeltaHelper(_sqrtRatioAX96, _sqrtRatioBX96, liquidity, roundUp) {
let sqrtRatioAX96 = _sqrtRatioAX96;
let sqrtRatioBX96 = _sqrtRatioBX96;
if (int_1.Uint160.from(sqrtRatioAX96).gt(sqrtRatioBX96))
[sqrtRatioAX96, sqrtRatioBX96] = [sqrtRatioBX96, sqrtRatioAX96];
return roundUp
? FullMath.mulDivRoundingUp(liquidity, int_1.Uint160.from(sqrtRatioBX96).sub(sqrtRatioAX96).toString(), FixedPoint96.Q96)
: FullMath.mulDiv(liquidity, int_1.Uint160.from(sqrtRatioBX96).sub(sqrtRatioAX96).toString(), FixedPoint96.Q96);
}
function getAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity) {
return int_1.Int256.from(liquidity).lt(0)
? int_1.Int256.from(`-${getAmount0DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, int_1.Int256.from(liquidity).mul(-1).toString(), false)}`).toString()
: int_1.Int256.from(getAmount0DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, liquidity, true)).toString();
}
function getAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity) {
return int_1.Int256.from(liquidity).lt(0)
? int_1.Int256.from(`-${getAmount1DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, int_1.Int256.from(liquidity).mul(-1).toString(), false)}`).toString()
: int_1.Int256.from(getAmount1DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, liquidity, true)).toString();
}
//# sourceMappingURL=sqrt_price_math.js.map