@angleprotocol/sdk
Version:
SDK for Angle DApps
210 lines • 12.4 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLiquidityForAmounts = exports.getLiquidityForAmount1 = exports.getLiquidityForAmount0 = exports.getAmountsForLiquidity = exports.getAmount1ForLiquidity = exports.getAmount0ForLiquidity = exports.getTickAtSqrtRatio = exports.getSqrtRatioAtTick = void 0;
const ethers_1 = require("ethers");
const jsbi_1 = __importDefault(require("jsbi"));
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
const MIN_TICK = -887272;
const MAX_TICK = -MIN_TICK;
const MIN_SQRT_RATIO = jsbi_1.default.BigInt('4295128739');
const MAX_SQRT_RATIO = jsbi_1.default.BigInt('1461446703485210103287273052203988822378723970342');
function mulShift(val, mulBy) {
return jsbi_1.default.signedRightShift(jsbi_1.default.multiply(val, jsbi_1.default.BigInt(mulBy)), jsbi_1.default.BigInt(128));
}
const Q32 = jsbi_1.default.exponentiate(jsbi_1.default.BigInt(2), jsbi_1.default.BigInt(32));
const Q96 = jsbi_1.default.exponentiate(jsbi_1.default.BigInt(2), jsbi_1.default.BigInt(96));
const MaxUint256 = jsbi_1.default.BigInt((_a = ethers_1.ethers.constants.MaxUint256) === null || _a === void 0 ? void 0 : _a.toString());
const ZERO = jsbi_1.default.BigInt(0);
const ONE = jsbi_1.default.BigInt(1);
function getSqrtRatioAtTick(tick) {
(0, tiny_invariant_1.default)(tick >= MIN_TICK && tick <= MAX_TICK && Number.isInteger(tick), 'TICK');
const absTick = tick < 0 ? tick * -1 : tick;
let ratio = (absTick & 0x1) != 0 ? jsbi_1.default.BigInt('0xfffcb933bd6fad37aa2d162d1a594001') : jsbi_1.default.BigInt('0x100000000000000000000000000000000');
if ((absTick & 0x2) != 0)
ratio = mulShift(ratio, '0xfff97272373d413259a46990580e213a');
if ((absTick & 0x4) != 0)
ratio = mulShift(ratio, '0xfff2e50f5f656932ef12357cf3c7fdcc');
if ((absTick & 0x8) != 0)
ratio = mulShift(ratio, '0xffe5caca7e10e4e61c3624eaa0941cd0');
if ((absTick & 0x10) != 0)
ratio = mulShift(ratio, '0xffcb9843d60f6159c9db58835c926644');
if ((absTick & 0x20) != 0)
ratio = mulShift(ratio, '0xff973b41fa98c081472e6896dfb254c0');
if ((absTick & 0x40) != 0)
ratio = mulShift(ratio, '0xff2ea16466c96a3843ec78b326b52861');
if ((absTick & 0x80) != 0)
ratio = mulShift(ratio, '0xfe5dee046a99a2a811c461f1969c3053');
if ((absTick & 0x100) != 0)
ratio = mulShift(ratio, '0xfcbe86c7900a88aedcffc83b479aa3a4');
if ((absTick & 0x200) != 0)
ratio = mulShift(ratio, '0xf987a7253ac413176f2b074cf7815e54');
if ((absTick & 0x400) != 0)
ratio = mulShift(ratio, '0xf3392b0822b70005940c7a398e4b70f3');
if ((absTick & 0x800) != 0)
ratio = mulShift(ratio, '0xe7159475a2c29b7443b29c7fa6e889d9');
if ((absTick & 0x1000) != 0)
ratio = mulShift(ratio, '0xd097f3bdfd2022b8845ad8f792aa5825');
if ((absTick & 0x2000) != 0)
ratio = mulShift(ratio, '0xa9f746462d870fdf8a65dc1f90e061e5');
if ((absTick & 0x4000) != 0)
ratio = mulShift(ratio, '0x70d869a156d2a1b890bb3df62baf32f7');
if ((absTick & 0x8000) != 0)
ratio = mulShift(ratio, '0x31be135f97d08fd981231505542fcfa6');
if ((absTick & 0x10000) != 0)
ratio = mulShift(ratio, '0x9aa508b5b7a84e1c677de54f3e99bc9');
if ((absTick & 0x20000) != 0)
ratio = mulShift(ratio, '0x5d6af8dedb81196699c329225ee604');
if ((absTick & 0x40000) != 0)
ratio = mulShift(ratio, '0x2216e584f5fa1ea926041bedfe98');
if ((absTick & 0x80000) != 0)
ratio = mulShift(ratio, '0x48a170391f7dc42444e8fa2');
if (tick > 0)
ratio = jsbi_1.default.divide(MaxUint256, ratio);
return jsbi_1.default.greaterThan(jsbi_1.default.remainder(ratio, Q32), ZERO) ? jsbi_1.default.add(jsbi_1.default.divide(ratio, Q32), ONE) : jsbi_1.default.divide(ratio, Q32);
}
exports.getSqrtRatioAtTick = getSqrtRatioAtTick;
const TWO = jsbi_1.default.BigInt(2);
const POWERS_OF_2 = [128, 64, 32, 16, 8, 4, 2, 1].map((pow) => [pow, jsbi_1.default.exponentiate(TWO, jsbi_1.default.BigInt(pow))]);
function mostSignificantBit(x) {
(0, tiny_invariant_1.default)(jsbi_1.default.greaterThan(x, ZERO), 'ZERO');
(0, tiny_invariant_1.default)(jsbi_1.default.lessThanOrEqual(x, MaxUint256), 'MAX');
let msb = 0;
for (const [power, min] of POWERS_OF_2) {
if (jsbi_1.default.greaterThanOrEqual(x, min)) {
x = jsbi_1.default.signedRightShift(x, jsbi_1.default.BigInt(power));
msb += power;
}
}
return msb;
}
function getTickAtSqrtRatio(sqrtRatioX96) {
(0, tiny_invariant_1.default)(jsbi_1.default.greaterThanOrEqual(sqrtRatioX96, MIN_SQRT_RATIO) && jsbi_1.default.lessThan(sqrtRatioX96, MAX_SQRT_RATIO), 'SQRT_RATIO');
const sqrtRatioX128 = jsbi_1.default.leftShift(sqrtRatioX96, jsbi_1.default.BigInt(32));
const msb = mostSignificantBit(sqrtRatioX128);
let r;
if (jsbi_1.default.greaterThanOrEqual(jsbi_1.default.BigInt(msb), jsbi_1.default.BigInt(128))) {
r = jsbi_1.default.signedRightShift(sqrtRatioX128, jsbi_1.default.BigInt(msb - 127));
}
else {
r = jsbi_1.default.leftShift(sqrtRatioX128, jsbi_1.default.BigInt(127 - msb));
}
let log_2 = jsbi_1.default.leftShift(jsbi_1.default.subtract(jsbi_1.default.BigInt(msb), jsbi_1.default.BigInt(128)), jsbi_1.default.BigInt(64));
for (let i = 0; i < 14; i++) {
r = jsbi_1.default.signedRightShift(jsbi_1.default.multiply(r, r), jsbi_1.default.BigInt(127));
const f = jsbi_1.default.signedRightShift(r, jsbi_1.default.BigInt(128));
log_2 = jsbi_1.default.bitwiseOr(log_2, jsbi_1.default.leftShift(f, jsbi_1.default.BigInt(63 - i)));
r = jsbi_1.default.signedRightShift(r, f);
}
const log_sqrt10001 = jsbi_1.default.multiply(log_2, jsbi_1.default.BigInt('255738958999603826347141'));
const tickLow = jsbi_1.default.toNumber(jsbi_1.default.signedRightShift(jsbi_1.default.subtract(log_sqrt10001, jsbi_1.default.BigInt('3402992956809132418596140100660247210')), jsbi_1.default.BigInt(128)));
const tickHigh = jsbi_1.default.toNumber(jsbi_1.default.signedRightShift(jsbi_1.default.add(log_sqrt10001, jsbi_1.default.BigInt('291339464771989622907027621153398088495')), jsbi_1.default.BigInt(128)));
return tickLow === tickHigh ? tickLow : jsbi_1.default.lessThanOrEqual(getSqrtRatioAtTick(tickHigh), sqrtRatioX96) ? tickHigh : tickLow;
}
exports.getTickAtSqrtRatio = getTickAtSqrtRatio;
const getAmount0ForLiquidity = (lowerTick, upperTick, liquidity) => {
const sqrtRatioAX96 = getSqrtRatioAtTick(lowerTick);
const sqrtRatioBX96 = getSqrtRatioAtTick(upperTick);
return _getAmount0ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
};
exports.getAmount0ForLiquidity = getAmount0ForLiquidity;
const _getAmount0ForLiquidity = (sqrtRatioAX96, sqrtRatioBX96, liquidity) => {
if (jsbi_1.default.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
sqrtRatioAX96 = sqrtRatioBX96;
sqrtRatioBX96 = sqrtRatioAX96;
}
let JSBIRes = jsbi_1.default.multiply(jsbi_1.default.BigInt(liquidity === null || liquidity === void 0 ? void 0 : liquidity.shl(96).toString()), jsbi_1.default.subtract(sqrtRatioBX96, sqrtRatioAX96));
JSBIRes = jsbi_1.default.divide(JSBIRes, sqrtRatioBX96);
JSBIRes = jsbi_1.default.divide(JSBIRes, sqrtRatioAX96);
const res = ethers_1.BigNumber.from(JSBIRes === null || JSBIRes === void 0 ? void 0 : JSBIRes.toString());
return res;
};
const getAmount1ForLiquidity = (lowerTick, upperTick, liquidity) => {
const sqrtRatioAX96 = getSqrtRatioAtTick(lowerTick);
const sqrtRatioBX96 = getSqrtRatioAtTick(upperTick);
return _getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
};
exports.getAmount1ForLiquidity = getAmount1ForLiquidity;
const _getAmount1ForLiquidity = (sqrtRatioAX96, sqrtRatioBX96, liquidity) => {
if (jsbi_1.default.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
sqrtRatioAX96 = sqrtRatioBX96;
sqrtRatioBX96 = sqrtRatioAX96;
}
let JSBIRes = jsbi_1.default.multiply(jsbi_1.default.BigInt(liquidity === null || liquidity === void 0 ? void 0 : liquidity.toString()), jsbi_1.default.subtract(sqrtRatioBX96, sqrtRatioAX96));
JSBIRes = jsbi_1.default.divide(JSBIRes, Q96);
const res = ethers_1.BigNumber.from(JSBIRes === null || JSBIRes === void 0 ? void 0 : JSBIRes.toString());
return res;
};
const getAmountsForLiquidity = (sqrtRatioX96String, lowerTick, upperTick, liquidity) => {
let sqrtRatioAX96 = getSqrtRatioAtTick(lowerTick);
let sqrtRatioBX96 = getSqrtRatioAtTick(upperTick);
const sqrtRatioX96 = jsbi_1.default.BigInt(sqrtRatioX96String === null || sqrtRatioX96String === void 0 ? void 0 : sqrtRatioX96String.toString());
if (jsbi_1.default.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
sqrtRatioAX96 = sqrtRatioBX96;
sqrtRatioBX96 = sqrtRatioAX96;
}
let amount0 = ethers_1.BigNumber.from(0);
let amount1 = ethers_1.BigNumber.from(0);
if (jsbi_1.default.greaterThan(sqrtRatioAX96, sqrtRatioX96)) {
amount0 = _getAmount0ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
}
else if (jsbi_1.default.greaterThan(sqrtRatioBX96, sqrtRatioX96)) {
amount0 = _getAmount0ForLiquidity(sqrtRatioX96, sqrtRatioBX96, liquidity);
amount1 = _getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioX96, liquidity);
}
else {
amount1 = _getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
}
return [amount0, amount1];
};
exports.getAmountsForLiquidity = getAmountsForLiquidity;
const getLiquidityForAmount0 = (lowerTick, upperTick, amount0) => {
let sqrtRatioAX96 = getSqrtRatioAtTick(lowerTick);
let sqrtRatioBX96 = getSqrtRatioAtTick(upperTick);
if (jsbi_1.default.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
sqrtRatioAX96 = sqrtRatioBX96;
sqrtRatioBX96 = sqrtRatioAX96;
}
const intermediate = jsbi_1.default.divide(jsbi_1.default.multiply(sqrtRatioAX96, sqrtRatioBX96), Q96);
let JSBIRes = jsbi_1.default.multiply(jsbi_1.default.BigInt(amount0 === null || amount0 === void 0 ? void 0 : amount0.toString()), intermediate);
JSBIRes = jsbi_1.default.divide(JSBIRes, jsbi_1.default.subtract(sqrtRatioBX96, sqrtRatioAX96));
return ethers_1.BigNumber.from(JSBIRes === null || JSBIRes === void 0 ? void 0 : JSBIRes.toString());
};
exports.getLiquidityForAmount0 = getLiquidityForAmount0;
const getLiquidityForAmount1 = (lowerTick, upperTick, amount1) => {
let sqrtRatioAX96 = getSqrtRatioAtTick(lowerTick);
let sqrtRatioBX96 = getSqrtRatioAtTick(upperTick);
if (jsbi_1.default.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
sqrtRatioAX96 = sqrtRatioBX96;
sqrtRatioBX96 = sqrtRatioAX96;
}
let JSBIRes = jsbi_1.default.multiply(jsbi_1.default.BigInt(amount1 === null || amount1 === void 0 ? void 0 : amount1.toString()), Q96);
JSBIRes = jsbi_1.default.divide(JSBIRes, jsbi_1.default.subtract(sqrtRatioBX96, sqrtRatioAX96));
return ethers_1.BigNumber.from(JSBIRes === null || JSBIRes === void 0 ? void 0 : JSBIRes.toString());
};
exports.getLiquidityForAmount1 = getLiquidityForAmount1;
const getLiquidityForAmounts = (tick, lowerTick, upperTick, amount0, amount1) => {
let sqrtRatioAX96 = getSqrtRatioAtTick(lowerTick);
let sqrtRatioBX96 = getSqrtRatioAtTick(upperTick);
const sqrtRatioX96 = getSqrtRatioAtTick(tick);
if (jsbi_1.default.greaterThan(sqrtRatioAX96, sqrtRatioBX96)) {
sqrtRatioAX96 = sqrtRatioBX96;
sqrtRatioBX96 = sqrtRatioAX96;
}
if (jsbi_1.default.greaterThan(sqrtRatioAX96, sqrtRatioX96)) {
return (0, exports.getLiquidityForAmount0)(lowerTick, upperTick, amount0);
}
else if (jsbi_1.default.greaterThan(sqrtRatioBX96, sqrtRatioX96)) {
const liquidity0 = (0, exports.getLiquidityForAmount0)(tick, upperTick, amount0);
const liquidity1 = (0, exports.getLiquidityForAmount1)(lowerTick, tick, amount1);
return (liquidity0 === null || liquidity0 === void 0 ? void 0 : liquidity0.gt(liquidity1)) ? liquidity1 : liquidity0;
}
else {
return (0, exports.getLiquidityForAmount1)(lowerTick, upperTick, amount1);
}
};
exports.getLiquidityForAmounts = getLiquidityForAmounts;
//# sourceMappingURL=uniswap.js.map