@alcorexchange/alcor-swap-sdk
Version:
**npm** ``` npm i @alcorexchange/alcor-swap-sdk ``` **yarn** ``` yarn add @alcorexchange/alcor-swap-sdk ``` ## Usage ### Import:
84 lines (82 loc) • 5.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SwapMath = void 0;
var _internalConstants = require("../internalConstants");
var _fullMath = require("./fullMath");
var _sqrtPriceMath = require("./sqrtPriceMath");
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); }
const MAX_FEE = BigInt(10) ** BigInt(6);
// Cache for fee factors - feePips values are limited (100, 500, 3000, 10000)
const feeFactorCache = new Map();
const feePipsCache = new Map();
function getFeeFactor(feePips) {
let cached = feeFactorCache.get(feePips);
if (cached === undefined) {
cached = MAX_FEE - getFeePipsBigInt(feePips);
feeFactorCache.set(feePips, cached);
}
return cached;
}
function getFeePipsBigInt(feePips) {
let cached = feePipsCache.get(feePips);
if (cached === undefined) {
cached = BigInt(feePips);
feePipsCache.set(feePips, cached);
}
return cached;
}
let SwapMath = exports.SwapMath = /*#__PURE__*/function () {
/**
* Cannot be constructed.
*/
function SwapMath() {
_classCallCheck(this, SwapMath);
}
return _createClass(SwapMath, null, [{
key: "computeSwapStep",
value: function computeSwapStep(sqrtRatioCurrentX64, sqrtRatioTargetX64, liquidity, amountRemaining, feePips) {
const zeroForOne = sqrtRatioCurrentX64 >= sqrtRatioTargetX64;
const exactIn = amountRemaining >= _internalConstants.ZERO;
let sqrtRatioNextX64;
let amountIn;
let amountOut;
let feeAmount;
if (exactIn) {
const feeFactor = getFeeFactor(feePips);
const amountRemainingLessFee = amountRemaining * feeFactor / MAX_FEE;
const deltaIn = zeroForOne ? _sqrtPriceMath.SqrtPriceMath.getAmountADelta(sqrtRatioTargetX64, sqrtRatioCurrentX64, liquidity, true) : _sqrtPriceMath.SqrtPriceMath.getAmountBDelta(sqrtRatioCurrentX64, sqrtRatioTargetX64, liquidity, true);
if (amountRemainingLessFee >= deltaIn) {
sqrtRatioNextX64 = sqrtRatioTargetX64;
amountIn = deltaIn;
} else {
sqrtRatioNextX64 = _sqrtPriceMath.SqrtPriceMath.getNextSqrtPriceFromInput(sqrtRatioCurrentX64, liquidity, amountRemainingLessFee, zeroForOne);
amountIn = zeroForOne ? _sqrtPriceMath.SqrtPriceMath.getAmountADelta(sqrtRatioNextX64, sqrtRatioCurrentX64, liquidity, true) : _sqrtPriceMath.SqrtPriceMath.getAmountBDelta(sqrtRatioCurrentX64, sqrtRatioNextX64, liquidity, true);
}
amountOut = zeroForOne ? _sqrtPriceMath.SqrtPriceMath.getAmountBDelta(sqrtRatioNextX64, sqrtRatioCurrentX64, liquidity, false) : _sqrtPriceMath.SqrtPriceMath.getAmountADelta(sqrtRatioCurrentX64, sqrtRatioNextX64, liquidity, false);
feeAmount = sqrtRatioNextX64 !== sqrtRatioTargetX64 ? amountRemaining - amountIn : _fullMath.FullMath.mulDivRoundingUp(amountIn, getFeePipsBigInt(feePips), feeFactor);
} else {
const deltaOut = zeroForOne ? _sqrtPriceMath.SqrtPriceMath.getAmountBDelta(sqrtRatioTargetX64, sqrtRatioCurrentX64, liquidity, false) : _sqrtPriceMath.SqrtPriceMath.getAmountADelta(sqrtRatioCurrentX64, sqrtRatioTargetX64, liquidity, false);
const amountRemainingNegative = amountRemaining * _internalConstants.NEGATIVE_ONE;
if (amountRemainingNegative >= deltaOut) {
sqrtRatioNextX64 = sqrtRatioTargetX64;
amountOut = deltaOut;
} else {
sqrtRatioNextX64 = _sqrtPriceMath.SqrtPriceMath.getNextSqrtPriceFromOutput(sqrtRatioCurrentX64, liquidity, amountRemainingNegative, zeroForOne);
amountOut = zeroForOne ? _sqrtPriceMath.SqrtPriceMath.getAmountBDelta(sqrtRatioNextX64, sqrtRatioCurrentX64, liquidity, false) : _sqrtPriceMath.SqrtPriceMath.getAmountADelta(sqrtRatioCurrentX64, sqrtRatioNextX64, liquidity, false);
}
amountIn = zeroForOne ? _sqrtPriceMath.SqrtPriceMath.getAmountADelta(sqrtRatioNextX64, sqrtRatioCurrentX64, liquidity, true) : _sqrtPriceMath.SqrtPriceMath.getAmountBDelta(sqrtRatioCurrentX64, sqrtRatioNextX64, liquidity, true);
if (amountOut > amountRemainingNegative) {
amountOut = amountRemainingNegative;
}
feeAmount = _fullMath.FullMath.mulDivRoundingUp(amountIn, getFeePipsBigInt(feePips), getFeeFactor(feePips));
}
return [sqrtRatioNextX64, amountIn, amountOut, feeAmount];
}
}]);
}();