@orca-so/whirlpools-sdk
Version:
Typescript SDK to interact with Orca's Whirlpool program.
72 lines • 3.63 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeSwapStep = computeSwapStep;
const bn_js_1 = __importDefault(require("bn.js"));
const public_1 = require("../../types/public");
const bit_math_1 = require("./bit-math");
const token_math_1 = require("./token-math");
function computeSwapStep(amountRemaining, feeRate, currLiquidity, currSqrtPrice, targetSqrtPrice, amountSpecifiedIsInput, aToB) {
let initialAmountFixedDelta = tryGetAmountFixedDelta(currSqrtPrice, targetSqrtPrice, currLiquidity, amountSpecifiedIsInput, aToB);
let amountCalc = amountRemaining;
if (amountSpecifiedIsInput) {
const result = bit_math_1.BitMath.mulDiv(amountRemaining, public_1.FEE_RATE_MUL_VALUE.sub(new bn_js_1.default(feeRate)), public_1.FEE_RATE_MUL_VALUE, 128);
amountCalc = result;
}
let nextSqrtPrice = initialAmountFixedDelta.lte(amountCalc)
? targetSqrtPrice
: (0, token_math_1.getNextSqrtPrice)(currSqrtPrice, currLiquidity, amountCalc, amountSpecifiedIsInput, aToB);
let isMaxSwap = nextSqrtPrice.eq(targetSqrtPrice);
let amountUnfixedDelta = getAmountUnfixedDelta(currSqrtPrice, nextSqrtPrice, currLiquidity, amountSpecifiedIsInput, aToB);
let amountFixedDelta = !isMaxSwap || initialAmountFixedDelta.exceedsMax()
? getAmountFixedDelta(currSqrtPrice, nextSqrtPrice, currLiquidity, amountSpecifiedIsInput, aToB)
: initialAmountFixedDelta.value();
let amountIn = amountSpecifiedIsInput ? amountFixedDelta : amountUnfixedDelta;
let amountOut = amountSpecifiedIsInput
? amountUnfixedDelta
: amountFixedDelta;
if (!amountSpecifiedIsInput && amountOut.gt(amountRemaining)) {
amountOut = amountRemaining;
}
let feeAmount;
if (amountSpecifiedIsInput && !isMaxSwap) {
feeAmount = amountRemaining.sub(amountIn);
}
else {
const feeRateBN = new bn_js_1.default(feeRate);
feeAmount = bit_math_1.BitMath.mulDivRoundUp(amountIn, feeRateBN, public_1.FEE_RATE_MUL_VALUE.sub(feeRateBN), 128);
}
return {
amountIn,
amountOut,
nextPrice: nextSqrtPrice,
feeAmount,
};
}
function getAmountFixedDelta(currSqrtPrice, targetSqrtPrice, currLiquidity, amountSpecifiedIsInput, aToB) {
if (aToB === amountSpecifiedIsInput) {
return (0, token_math_1.getAmountDeltaA)(currSqrtPrice, targetSqrtPrice, currLiquidity, amountSpecifiedIsInput);
}
else {
return (0, token_math_1.getAmountDeltaB)(currSqrtPrice, targetSqrtPrice, currLiquidity, amountSpecifiedIsInput);
}
}
function tryGetAmountFixedDelta(currSqrtPrice, targetSqrtPrice, currLiquidity, amountSpecifiedIsInput, aToB) {
if (aToB === amountSpecifiedIsInput) {
return (0, token_math_1.tryGetAmountDeltaA)(currSqrtPrice, targetSqrtPrice, currLiquidity, amountSpecifiedIsInput);
}
else {
return (0, token_math_1.tryGetAmountDeltaB)(currSqrtPrice, targetSqrtPrice, currLiquidity, amountSpecifiedIsInput);
}
}
function getAmountUnfixedDelta(currSqrtPrice, targetSqrtPrice, currLiquidity, amountSpecifiedIsInput, aToB) {
if (aToB === amountSpecifiedIsInput) {
return (0, token_math_1.getAmountDeltaB)(currSqrtPrice, targetSqrtPrice, currLiquidity, !amountSpecifiedIsInput);
}
else {
return (0, token_math_1.getAmountDeltaA)(currSqrtPrice, targetSqrtPrice, currLiquidity, !amountSpecifiedIsInput);
}
}
//# sourceMappingURL=swap-math.js.map