@alcorexchange/alcor-swap-sdk
Version:
## Installation **npm** ``` npm i @alcorexchange/alcor-swap-sdk ``` **yarn** ``` yarn add @alcorexchange/alcor-swap-sdk ``` ## Usage ### Import:
105 lines (104 loc) • 5.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SqrtPriceMath = void 0;
var _internalConstants = require("../internalConstants");
var _fullMath = require("./fullMath");
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function multiplyIn128(x, y) {
const product = x * y;
return product & _internalConstants.MaxUint128;
}
function addIn128(x, y) {
const sum = x + y;
return sum & _internalConstants.MaxUint128;
}
let SqrtPriceMath = exports.SqrtPriceMath = /*#__PURE__*/function () {
/**
* Cannot be constructed.
*/
function SqrtPriceMath() {
_classCallCheck(this, SqrtPriceMath);
}
return _createClass(SqrtPriceMath, null, [{
key: "getAmountADelta",
value: function getAmountADelta(sqrtRatioLX64, sqrtRatioUX64, liquidity, roundUp) {
if (sqrtRatioLX64 > sqrtRatioUX64) {
[sqrtRatioLX64, sqrtRatioUX64] = [sqrtRatioUX64, sqrtRatioLX64];
}
const numerator1 = liquidity << 64n;
const numerator2 = sqrtRatioUX64 - sqrtRatioLX64;
if (roundUp) {
const mul1 = _fullMath.FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtRatioUX64);
return _fullMath.FullMath.mulDivRoundingUp(mul1, _internalConstants.ONE, sqrtRatioLX64);
} else {
const mul1 = numerator1 * numerator2 / sqrtRatioUX64;
return mul1 / sqrtRatioLX64;
}
}
}, {
key: "getAmountBDelta",
value: function getAmountBDelta(sqrtRatioLX64, sqrtRatioUX64, liquidity, roundUp) {
if (sqrtRatioLX64 > sqrtRatioUX64) {
[sqrtRatioLX64, sqrtRatioUX64] = [sqrtRatioUX64, sqrtRatioLX64];
}
const diff = sqrtRatioUX64 - sqrtRatioLX64;
if (roundUp) {
return _fullMath.FullMath.mulDivRoundingUp(liquidity, diff, _internalConstants.Q64);
} else {
return liquidity * diff / _internalConstants.Q64;
}
}
}, {
key: "getNextSqrtPriceFromInput",
value: function getNextSqrtPriceFromInput(sqrtPX64, liquidity, amountIn, zeroForOne) {
// Убраны invariant для ускорения, если данные валидны
return zeroForOne ? this.getNextSqrtPriceFromAmountARoundingUp(sqrtPX64, liquidity, amountIn, true) : this.getNextSqrtPriceFromAmountBRoundingDown(sqrtPX64, liquidity, amountIn, true);
}
}, {
key: "getNextSqrtPriceFromOutput",
value: function getNextSqrtPriceFromOutput(sqrtPX64, liquidity, amountOut, zeroForOne) {
// Убраны invariant для ускорения, если данные валидны
return zeroForOne ? this.getNextSqrtPriceFromAmountBRoundingDown(sqrtPX64, liquidity, amountOut, false) : this.getNextSqrtPriceFromAmountARoundingUp(sqrtPX64, liquidity, amountOut, false);
}
}, {
key: "getNextSqrtPriceFromAmountARoundingUp",
value: function getNextSqrtPriceFromAmountARoundingUp(sqrtPX64, liquidity, amount, add) {
if (amount === _internalConstants.ZERO) return sqrtPX64;
const numerator1 = liquidity << 64n;
if (add) {
const product = multiplyIn128(amount, sqrtPX64);
if (product / amount === sqrtPX64) {
const denominator = addIn128(numerator1, product);
if (denominator >= numerator1) {
return _fullMath.FullMath.mulDivRoundingUp(numerator1, sqrtPX64, denominator);
}
}
const adjustedDenominator = numerator1 / sqrtPX64 + amount;
return _fullMath.FullMath.mulDivRoundingUp(numerator1, _internalConstants.ONE, adjustedDenominator);
} else {
const product = multiplyIn128(amount, sqrtPX64);
// Убрана invariant, предполагаем, что numerator1 > product
const denominator = numerator1 - product;
return _fullMath.FullMath.mulDivRoundingUp(numerator1, sqrtPX64, denominator);
}
}
}, {
key: "getNextSqrtPriceFromAmountBRoundingDown",
value: function getNextSqrtPriceFromAmountBRoundingDown(sqrtPX64, liquidity, amount, add) {
if (add) {
const quotient = amount <= _internalConstants.MaxUint128 ? (amount << 64n) / liquidity : amount * _internalConstants.Q64 / liquidity;
return sqrtPX64 + quotient;
} else {
const quotient = _fullMath.FullMath.mulDivRoundingUp(amount, _internalConstants.Q64, liquidity);
// Убрана invariant, предполагаем, что sqrtPX64 > quotient
return sqrtPX64 - quotient;
}
}
}]);
}();