UNPKG

@firefly-exchange/library-sui

Version:

Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui

92 lines (91 loc) 4.14 kB
"use strict"; /** * This file incorporates code from cetus-clmm-sui-sdk by CetusProtocol, * licensed under the Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * which can be found at https://github.com/CetusProtocol/cetus-clmm-sui-sdk/blob/main/LICENSE */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUpperSqrtPriceFromCoinB = exports.getLowerSqrtPriceFromCoinB = exports.getUpperSqrtPriceFromCoinA = exports.getLowerSqrtPriceFromCoinA = exports.SwapUtils = void 0; const bn_js_1 = __importDefault(require("bn.js")); const utils_1 = require("./utils"); const constants_1 = require("./constants"); class SwapUtils { /** * Retrieves the default sqrt price limit for a swap operation. * * @param a2b - Set to true for an A to B swap, or false for a B to A swap. * @returns The default sqrt price limit for the specified swap direction. */ static getDefaultSqrtPriceLimit(a2b) { return new bn_js_1.default(a2b ? constants_1.MIN_SQRT_PRICE : constants_1.MAX_SQRT_PRICE); } /** * Retrieves the default values for the otherAmountThreshold parameter in a swap. * * @param amountSpecifiedIsInput - Indicates the direction of the swap. * @returns The default values for the otherAmountThreshold in the swap. */ static getDefaultOtherAmountThreshold(amountSpecifiedIsInput) { return amountSpecifiedIsInput ? utils_1.ZERO : utils_1.U64_MAX; } } exports.SwapUtils = SwapUtils; /** * Calculates the lower sqrt price based on the amount of token A. * * @param amount - The amount of tokens the user wants to swap. * @param liquidity - The current pool liquidity. * @param sqrtPriceX64 - The pool's current sqrt price. * @returns The calculated lower sqrt price (LowesqrtPriceX64). */ function getLowerSqrtPriceFromCoinA(amount, liquidity, sqrtPriceX64) { const numerator = liquidity.mul(sqrtPriceX64).shln(64); const denominator = liquidity.shln(64).add(amount.mul(sqrtPriceX64)); // always round up return utils_1.MathUtil.divRoundUp(numerator, denominator); } exports.getLowerSqrtPriceFromCoinA = getLowerSqrtPriceFromCoinA; /** * Calculates the upper sqrt price based on the amount of token A. * * @param amount - The amount of tokens the user wants to swap. * @param liquidity - The current pool liquidity. * @param sqrtPriceX64 - The pool's current sqrt price. * @returns The calculated upper sqrt price (UpperSqrtPriceX64). */ function getUpperSqrtPriceFromCoinA(amount, liquidity, sqrtPriceX64) { const numerator = liquidity.mul(sqrtPriceX64).shln(64); const denominator = liquidity.shln(64).sub(amount.mul(sqrtPriceX64)); // always round up return utils_1.MathUtil.divRoundUp(numerator, denominator); } exports.getUpperSqrtPriceFromCoinA = getUpperSqrtPriceFromCoinA; /** * Calculates the lower sqrt price based on the amount of coin B. * * @param amount - The amount of coins the user wants to swap. * @param liquidity - The current pool liquidity. * @param sqrtPriceX64 - The pool's current sqrt price. * @returns The calculated lower sqrt price (LowerSqrtPriceX64). */ function getLowerSqrtPriceFromCoinB(amount, liquidity, sqrtPriceX64) { // always round down(rounding up a negative number) return sqrtPriceX64.sub(utils_1.MathUtil.divRoundUp(amount.shln(64), liquidity)); } exports.getLowerSqrtPriceFromCoinB = getLowerSqrtPriceFromCoinB; /** * Calculates the upper sqrt price based on the amount of coin B. * * @param amount - The amount of coins the user wants to swap. * @param liquidity - The current pool liquidity. * @param sqrtPriceX64 - The pool's current sqrt price. * @returns The calculated upper sqrt price (UpperSqrtPriceX64). */ function getUpperSqrtPriceFromCoinB(amount, liquidity, sqrtPriceX64) { // always round down (rounding up a negative number) return sqrtPriceX64.add(amount.shln(64).div(liquidity)); } exports.getUpperSqrtPriceFromCoinB = getUpperSqrtPriceFromCoinB;