UNPKG

@kamino-finance/kliquidity-sdk

Version:

Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol

230 lines 12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PricePercentageWithResetRebalanceTypeName = void 0; exports.getPricePercentageWithResetRebalanceFieldInfos = getPricePercentageWithResetRebalanceFieldInfos; exports.getPositionRangeFromPricePercentageWithResetParams = getPositionRangeFromPricePercentageWithResetParams; exports.getPositionResetRangeFromPricePercentageWithResetParams = getPositionResetRangeFromPricePercentageWithResetParams; exports.getDefaultPricePercentageWithResetRebalanceFieldInfos = getDefaultPricePercentageWithResetRebalanceFieldInfos; exports.readPricePercentageWithResetRebalanceParamsFromStrategy = readPricePercentageWithResetRebalanceParamsFromStrategy; exports.readRawPricePercentageWithResetRebalanceStateFromStrategy = readRawPricePercentageWithResetRebalanceStateFromStrategy; exports.readPricePercentageWithResetRebalanceStateFromStrategy = readPricePercentageWithResetRebalanceStateFromStrategy; exports.deserializePricePercentageWithResetRebalanceFromOnchainParams = deserializePricePercentageWithResetRebalanceFromOnchainParams; exports.deserializePricePercentageWithResetRebalanceWithStateOverride = deserializePricePercentageWithResetRebalanceWithStateOverride; 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 whirlpools_core_1 = require("@orca-so/whirlpools-core"); const utils_2 = require("./utils"); const math_utils_1 = require("./math_utils"); const meteora_1 = require("../utils/meteora"); exports.PricePercentageWithResetRebalanceTypeName = 'pricePercentageWithReset'; function getPricePercentageWithResetRebalanceFieldInfos(price, lowerPercentageBPS, upperPercentageBPS, resetLowerPercentageBPS, resetUpperPercentageBPS, enabled = true) { const rebalanceType = { label: consts_1.RebalanceTypeLabelName, type: 'string', value: exports.PricePercentageWithResetRebalanceTypeName, enabled, }; const lowerBpsRebalanceFieldInfo = { label: 'lowerRangeBps', type: 'number', value: lowerPercentageBPS, enabled, }; const upperBpsRebalanceFieldInfo = { label: 'upperRangeBps', type: 'number', value: upperPercentageBPS, enabled, }; const resetLowerBpsRebalanceFieldInfo = { label: 'resetLowerRangeBps', type: 'number', value: resetLowerPercentageBPS, enabled, }; const resetUpperBpsRebalanceFieldInfo = { label: 'resetUpperRangeBps', type: 'number', value: resetUpperPercentageBPS, enabled, }; const { lowerPrice, upperPrice } = getPositionRangeFromPricePercentageWithResetParams(price, lowerPercentageBPS, upperPercentageBPS); const lowerRangeRebalanceFieldInfo = { label: 'rangePriceLower', type: 'number', value: lowerPrice, enabled: false, }; const upperRangeRebalanceFieldInfo = { label: 'rangePriceUpper', type: 'number', value: upperPrice, enabled: false, }; const { lowerPrice: resetLowerPrice, upperPrice: resetUpperPrice } = getPositionResetRangeFromPricePercentageWithResetParams(price, lowerPercentageBPS, upperPercentageBPS, resetLowerPercentageBPS, resetUpperPercentageBPS); const resetLowerRangeRebalanceFieldInfo = { label: 'resetPriceLower', type: 'number', value: resetLowerPrice, enabled: false, }; const resetUpperRangeRebalanceFieldInfo = { label: 'resetPriceUpper', type: 'number', value: resetUpperPrice, enabled: false, }; return [ rebalanceType, lowerBpsRebalanceFieldInfo, upperBpsRebalanceFieldInfo, resetLowerBpsRebalanceFieldInfo, resetUpperBpsRebalanceFieldInfo, lowerRangeRebalanceFieldInfo, upperRangeRebalanceFieldInfo, resetLowerRangeRebalanceFieldInfo, resetUpperRangeRebalanceFieldInfo, ]; } function getPositionRangeFromPricePercentageWithResetParams(price, lowerPercentageBPS, upperPercentageBPS) { return (0, math_utils_1.getPriceRangeFromPriceAndDiffBPS)(price, lowerPercentageBPS, upperPercentageBPS); } function getPositionResetRangeFromPricePercentageWithResetParams(price, lowerPercentageBPS, upperPercentageBPS, resetLowerPercentageBPS, resetUpperPercentageBPS) { return (0, math_utils_1.getResetRangeFromPriceAndDiffBPS)(price, lowerPercentageBPS, upperPercentageBPS, resetLowerPercentageBPS, resetUpperPercentageBPS); } function getDefaultPricePercentageWithResetRebalanceFieldInfos(price) { const fieldInfos = getPricePercentageWithResetRebalanceFieldInfos(price, CreationParameters_1.DefaultLowerPercentageBPSDecimal, CreationParameters_1.DefaultUpperPercentageBPSDecimal, CreationParameters_1.DefaultLowerPercentageBPSDecimal, CreationParameters_1.DefaultUpperPercentageBPSDecimal); return fieldInfos; } function readPricePercentageWithResetRebalanceParamsFromStrategy(rebalanceRaw) { const paramsBuffer = Buffer.from(rebalanceRaw.params); const lowerBpsRebalanceFieldInfo = { label: 'lowerRangeBps', type: 'number', value: new decimal_js_1.default(paramsBuffer.readUint16LE(0)), enabled: true, }; const upperBpsRebalanceFieldInfo = { label: 'upperRangeBps', type: 'number', value: new decimal_js_1.default(paramsBuffer.readUint16LE(2)), enabled: true, }; const resetLowerBpsRebalanceFieldInfo = { label: 'resetLowerRangeBps', type: 'number', value: new decimal_js_1.default(paramsBuffer.readUint16LE(4)), enabled: true, }; const resetUpperBpsRebalanceFieldInfo = { label: 'resetUpperRangeBps', type: 'number', value: new decimal_js_1.default(paramsBuffer.readUint16LE(6)), enabled: true, }; return [ lowerBpsRebalanceFieldInfo, upperBpsRebalanceFieldInfo, resetLowerBpsRebalanceFieldInfo, resetUpperBpsRebalanceFieldInfo, ]; } function readRawPricePercentageWithResetRebalanceStateFromStrategy(rebalanceRaw) { const stateBuffer = Buffer.from(rebalanceRaw.state); const lowerRangeRebalanceFieldInfo = { label: 'lastRebalanceLowerResetPoolPrice', type: 'number', value: new decimal_js_1.default((0, utils_1.readBigUint128LE)(stateBuffer, 0).toString()), enabled: false, }; const upperRangeRebalanceFieldInfo = { label: 'lastRebalanceUpperResetPoolPrice', type: 'number', value: new decimal_js_1.default((0, utils_1.readBigUint128LE)(stateBuffer, 16).toString()), enabled: false, }; return [lowerRangeRebalanceFieldInfo, upperRangeRebalanceFieldInfo]; } function readPricePercentageWithResetRebalanceStateFromStrategy(dex, tokenADecimals, tokenBDecimals, rebalanceRaw) { const stateBuffer = Buffer.from(rebalanceRaw.state); const params = readPricePercentageWithResetRebalanceParamsFromStrategy(rebalanceRaw); const lowerRangeBps = new decimal_js_1.default(params.find((param) => param.label == 'lowerRangeBps')?.value.toString()); const upperRangeBps = new decimal_js_1.default(params.find((param) => param.label == 'upperRangeBps')?.value.toString()); const resetLowerRangeBps = new decimal_js_1.default(params.find((param) => param.label == 'resetLowerRangeBps')?.value.toString()); const resetUpperRangeBps = new decimal_js_1.default(params.find((param) => param.label == 'resetUpperRangeBps')?.value.toString()); const lowerResetSqrtPriceX64 = (0, utils_1.readBigUint128LE)(stateBuffer, 0).toString(); const upperResetSqrtPriceX64 = (0, utils_1.readBigUint128LE)(stateBuffer, 16).toString(); let lowerResetPrice, upperResetPrice; if (dex == 'ORCA') { lowerResetPrice = new decimal_js_1.default((0, whirlpools_core_1.sqrtPriceToPrice)(BigInt(lowerResetSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals)); upperResetPrice = new decimal_js_1.default((0, whirlpools_core_1.sqrtPriceToPrice)(BigInt(upperResetSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals)); } else if (dex == 'RAYDIUM') { lowerResetPrice = new decimal_js_1.default((0, whirlpools_core_1.sqrtPriceToPrice)(BigInt(lowerResetSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals)); upperResetPrice = new decimal_js_1.default((0, whirlpools_core_1.sqrtPriceToPrice)(BigInt(upperResetSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals)); } else if (dex == 'METEORA') { lowerResetPrice = (0, meteora_1.getPriceFromQ64Price)(new decimal_js_1.default(lowerResetSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals); upperResetPrice = (0, meteora_1.getPriceFromQ64Price)(new decimal_js_1.default(upperResetSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals); } else { throw new Error(`Unknown DEX ${dex}`); } const resetLowerFactor = resetLowerRangeBps.mul(lowerRangeBps).div(CreationParameters_1.FullBPSDecimal); const resetUpperFactor = resetUpperRangeBps.mul(upperRangeBps).div(CreationParameters_1.FullBPSDecimal); const lowerPositionPrice = lowerResetPrice .mul(CreationParameters_1.FullBPSDecimal.sub(lowerRangeBps)) .div(CreationParameters_1.FullBPSDecimal.sub(resetLowerFactor)); const upperPositionPrice = upperResetPrice .mul(CreationParameters_1.FullBPSDecimal.add(upperRangeBps)) .div(CreationParameters_1.FullBPSDecimal.add(resetUpperFactor)); const lowerBpsRebalanceFieldInfo = { label: 'rangePriceLower', type: 'number', value: lowerPositionPrice, enabled: true, }; const upperBpsRebalanceFieldInfo = { label: 'rangePriceUpper', type: 'number', value: upperPositionPrice, enabled: true, }; const resetLowerBpsRebalanceFieldInfo = { label: 'resetPriceLower', type: 'number', value: lowerResetPrice, enabled: true, }; const resetUpperBpsRebalanceFieldInfo = { label: 'resetPriceUpper', type: 'number', value: upperResetPrice, enabled: true, }; return [ lowerBpsRebalanceFieldInfo, upperBpsRebalanceFieldInfo, resetLowerBpsRebalanceFieldInfo, resetUpperBpsRebalanceFieldInfo, ]; } function deserializePricePercentageWithResetRebalanceFromOnchainParams(price, rebalanceRaw) { const params = readPricePercentageWithResetRebalanceParamsFromStrategy(rebalanceRaw); const lowerRangeBps = new decimal_js_1.default(params.find((param) => param.label == 'lowerRangeBps')?.value.toString()); const upperRangeBps = new decimal_js_1.default(params.find((param) => param.label == 'upperRangeBps')?.value.toString()); const lowerResetRangeBps = new decimal_js_1.default(params.find((param) => param.label == 'resetLowerRangeBps')?.value.toString()); const upperResetRangeBps = new decimal_js_1.default(params.find((param) => param.label == 'resetUpperRangeBps')?.value.toString()); return getPricePercentageWithResetRebalanceFieldInfos(price, lowerRangeBps, upperRangeBps, lowerResetRangeBps, upperResetRangeBps); } function deserializePricePercentageWithResetRebalanceWithStateOverride(dex, tokenADecimals, tokenBDecimals, price, rebalanceRaw) { const stateFields = readPricePercentageWithResetRebalanceStateFromStrategy(dex, tokenADecimals, tokenBDecimals, rebalanceRaw); const fields = deserializePricePercentageWithResetRebalanceFromOnchainParams(price, rebalanceRaw); return (0, utils_2.upsertManyRebalanceFieldInfos)(fields, stateFields); } //# sourceMappingURL=pricePercentageWithResetRebalance.js.map