UNPKG

@marinade.finance/kamino-sdk

Version:
144 lines 7.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deserializePricePercentageRebalanceWithStateOverride = exports.deserializePricePercentageRebalanceFromOnchainParams = exports.readPricePercentageRebalanceStateFromStrategy = exports.readRawPricePercentageRebalanceStateFromStrategy = exports.readPricePercentageRebalanceParamsFromStrategy = exports.getDefaultPricePercentageRebalanceFieldInfos = exports.getPositionRangeFromPercentageRebalanceParams = exports.getPricePercentageRebalanceFieldInfos = exports.PricePercentageRebalanceTypeName = void 0; const decimal_js_1 = __importDefault(require("decimal.js")); const CreationParameters_1 = require("../utils/CreationParameters"); const consts_1 = require("./consts"); const utils_1 = require("../utils"); const utils_2 = require("./utils"); const whirlpool_sdk_1 = require("@orca-so/whirlpool-sdk"); const bn_js_1 = __importDefault(require("bn.js")); const math_utils_1 = require("./math_utils"); exports.PricePercentageRebalanceTypeName = 'pricePercentage'; function getPricePercentageRebalanceFieldInfos(poolPrice, lowerPercentageBPS, upperPercentageBPS, enabled = true) { let rebalanceType = { label: consts_1.RebalanceTypeLabelName, type: 'string', value: exports.PricePercentageRebalanceTypeName, enabled, }; let lowerBpsRebalanceFieldInfo = { label: 'lowerRangeBps', type: 'number', value: lowerPercentageBPS, enabled, }; let upperBpsRebalanceFieldInfo = { label: 'upperRangeBps', type: 'number', value: upperPercentageBPS, enabled, }; let { lowerPrice, upperPrice } = getPositionRangeFromPercentageRebalanceParams(poolPrice, lowerPercentageBPS, upperPercentageBPS); let lowerRangeRebalanceFieldInfo = { label: 'rangePriceLower', type: 'number', value: lowerPrice, enabled: false, }; let upperRangeRebalanceFieldInfo = { label: 'rangePriceUpper', type: 'number', value: upperPrice, enabled: false, }; return [ rebalanceType, lowerBpsRebalanceFieldInfo, upperBpsRebalanceFieldInfo, lowerRangeRebalanceFieldInfo, upperRangeRebalanceFieldInfo, ]; } exports.getPricePercentageRebalanceFieldInfos = getPricePercentageRebalanceFieldInfos; function getPositionRangeFromPercentageRebalanceParams(price, lowerPercentageBPS, upperPercentageBPS) { return (0, math_utils_1.getPriceRangeFromPriceAndDiffBPS)(price, lowerPercentageBPS, upperPercentageBPS); } exports.getPositionRangeFromPercentageRebalanceParams = getPositionRangeFromPercentageRebalanceParams; function getDefaultPricePercentageRebalanceFieldInfos(price) { let fieldInfos = getPricePercentageRebalanceFieldInfos(price, CreationParameters_1.DefaultLowerPercentageBPSDecimal, CreationParameters_1.DefaultUpperPercentageBPSDecimal); return fieldInfos; } exports.getDefaultPricePercentageRebalanceFieldInfos = getDefaultPricePercentageRebalanceFieldInfos; function readPricePercentageRebalanceParamsFromStrategy(rebalanceRaw) { let paramsBuffer = Buffer.from(rebalanceRaw.params); let lowerBpsRebalanceFieldInfo = { label: 'lowerRangeBps', type: 'number', value: new decimal_js_1.default(paramsBuffer.readUint16LE(0)), enabled: true, }; let upperBpsRebalanceFieldInfo = { label: 'upperRangeBps', type: 'number', value: new decimal_js_1.default(paramsBuffer.readUint16LE(2)), enabled: true, }; return [lowerBpsRebalanceFieldInfo, upperBpsRebalanceFieldInfo]; } exports.readPricePercentageRebalanceParamsFromStrategy = readPricePercentageRebalanceParamsFromStrategy; function readRawPricePercentageRebalanceStateFromStrategy(rebalanceRaw) { let stateBuffer = Buffer.from(rebalanceRaw.state); let lowerRangeRebalanceFieldInfo = { label: 'rangePriceLower', type: 'number', value: new decimal_js_1.default((0, utils_1.readBigUint128LE)(stateBuffer, 0).toString()), enabled: false, }; let upperRangeRebalanceFieldInfo = { label: 'rangePriceUpper', type: 'number', value: new decimal_js_1.default((0, utils_1.readBigUint128LE)(stateBuffer, 16).toString()), enabled: false, }; return [lowerRangeRebalanceFieldInfo, upperRangeRebalanceFieldInfo]; } exports.readRawPricePercentageRebalanceStateFromStrategy = readRawPricePercentageRebalanceStateFromStrategy; function readPricePercentageRebalanceStateFromStrategy(dex, tokenADecimals, tokenBDecimals, rebalanceRaw) { let stateBuffer = Buffer.from(rebalanceRaw.state); let lowerSqrtPriceX64 = new decimal_js_1.default((0, utils_1.readBigUint128LE)(stateBuffer, 0).toString()); let upperSqrtPriceX64 = new decimal_js_1.default((0, utils_1.readBigUint128LE)(stateBuffer, 16).toString()); let lowerPrice, upperPrice; if (dex == 'ORCA') { lowerPrice = (0, whirlpool_sdk_1.sqrtPriceX64ToPrice)(new bn_js_1.default(lowerSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals); upperPrice = (0, whirlpool_sdk_1.sqrtPriceX64ToPrice)(new bn_js_1.default(upperSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals); } else if (dex == 'RAYDIUM') { lowerPrice = (0, whirlpool_sdk_1.sqrtPriceX64ToPrice)(new bn_js_1.default(lowerSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals); upperPrice = (0, whirlpool_sdk_1.sqrtPriceX64ToPrice)(new bn_js_1.default(upperSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals); } else { throw new Error(`Unknown DEX ${dex}`); } let lowerRangeRebalanceFieldInfo = { label: 'rangePriceLower', type: 'number', value: lowerPrice, enabled: false, }; let upperRangeRebalanceFieldInfo = { label: 'rangePriceUpper', type: 'number', value: upperPrice, enabled: false, }; return [lowerRangeRebalanceFieldInfo, upperRangeRebalanceFieldInfo]; } exports.readPricePercentageRebalanceStateFromStrategy = readPricePercentageRebalanceStateFromStrategy; function deserializePricePercentageRebalanceFromOnchainParams(price, rebalanceRaw) { let params = readPricePercentageRebalanceParamsFromStrategy(rebalanceRaw); let lowerRangeBPS = new decimal_js_1.default(params.find((param) => param.label == 'lowerRangeBps')?.value); let upperRangeBPS = new decimal_js_1.default(params.find((param) => param.label == 'upperRangeBps')?.value); return getPricePercentageRebalanceFieldInfos(price, lowerRangeBPS, upperRangeBPS); } exports.deserializePricePercentageRebalanceFromOnchainParams = deserializePricePercentageRebalanceFromOnchainParams; function deserializePricePercentageRebalanceWithStateOverride(dex, tokenADecimals, tokenBDecimals, price, rebalanceRaw) { const stateFields = readPricePercentageRebalanceStateFromStrategy(dex, tokenADecimals, tokenBDecimals, rebalanceRaw); let fields = deserializePricePercentageRebalanceFromOnchainParams(price, rebalanceRaw); return (0, utils_2.upsertManyRebalanceFieldInfos)(fields, stateFields); } exports.deserializePricePercentageRebalanceWithStateOverride = deserializePricePercentageRebalanceWithStateOverride; //# sourceMappingURL=pricePercentageRebalance.js.map