UNPKG

@marinade.finance/kamino-sdk

Version:
92 lines 4.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deserializePeriodicRebalanceFromOnchainParams = exports.readPeriodicRebalanceRebalanceStateFromStrategy = exports.readPeriodicRebalanceRebalanceParamsFromStrategy = exports.getDefaultPeriodicRebalanceFieldInfos = exports.getPositionRangeFromPeriodicRebalanceParams = exports.getPeriodicRebalanceRebalanceFieldInfos = exports.PeriodicRebalanceTypeName = exports.DEFAULT_REBALANCE_PERIOD = exports.DEFAULT_UPPER_RANGE_PRICE_DIFF_BPS_PERIODIC_REBALANCE = exports.DEFAULT_LOWER_RANGE_PRICE_DIFF_BPS_PERIODIC_REBALANCE = void 0; const decimal_js_1 = __importDefault(require("decimal.js")); const consts_1 = require("./consts"); const math_utils_1 = require("./math_utils"); exports.DEFAULT_LOWER_RANGE_PRICE_DIFF_BPS_PERIODIC_REBALANCE = new decimal_js_1.default(500); exports.DEFAULT_UPPER_RANGE_PRICE_DIFF_BPS_PERIODIC_REBALANCE = new decimal_js_1.default(500); exports.DEFAULT_REBALANCE_PERIOD = new decimal_js_1.default(3600 * 24 * 3); // 3 days exports.PeriodicRebalanceTypeName = 'periodicRebalance'; function getPeriodicRebalanceRebalanceFieldInfos(price, period, // seconds lowerRangeBps, upperRangeBps, enabled = true) { let rebalanceType = { label: consts_1.RebalanceTypeLabelName, type: 'string', value: exports.PeriodicRebalanceTypeName, enabled, }; let periodRebalanceFieldInfo = { label: 'period', type: 'number', value: period, enabled, }; let lowerRangeBpsRebalanceFieldInfo = { label: 'lowerRangeBps', type: 'number', value: lowerRangeBps, enabled, }; let upperRangeBpsRebalanceFieldInfo = { label: 'upperRangeBps', type: 'number', value: upperRangeBps, enabled, }; let { lowerPrice, upperPrice } = getPositionRangeFromPeriodicRebalanceParams(price, lowerRangeBps, upperRangeBps); let lowerRangeRebalanceFieldInfo = { label: 'rangePriceLower', type: 'number', value: lowerPrice, enabled: false, }; let upperRangeRebalanceFieldInfo = { label: 'rangePriceUpper', type: 'number', value: upperPrice, enabled: false, }; return [ rebalanceType, periodRebalanceFieldInfo, lowerRangeBpsRebalanceFieldInfo, upperRangeBpsRebalanceFieldInfo, lowerRangeRebalanceFieldInfo, upperRangeRebalanceFieldInfo, ]; } exports.getPeriodicRebalanceRebalanceFieldInfos = getPeriodicRebalanceRebalanceFieldInfos; function getPositionRangeFromPeriodicRebalanceParams(price, lowerPercentageBPS, upperPercentageBPS) { return (0, math_utils_1.getPriceRangeFromPriceAndDiffBPS)(price, lowerPercentageBPS, upperPercentageBPS); } exports.getPositionRangeFromPeriodicRebalanceParams = getPositionRangeFromPeriodicRebalanceParams; function getDefaultPeriodicRebalanceFieldInfos(price) { return getPeriodicRebalanceRebalanceFieldInfos(price, exports.DEFAULT_REBALANCE_PERIOD, exports.DEFAULT_LOWER_RANGE_PRICE_DIFF_BPS_PERIODIC_REBALANCE, exports.DEFAULT_UPPER_RANGE_PRICE_DIFF_BPS_PERIODIC_REBALANCE); } exports.getDefaultPeriodicRebalanceFieldInfos = getDefaultPeriodicRebalanceFieldInfos; function readPeriodicRebalanceRebalanceParamsFromStrategy(rebalanceRaw) { let paramsBuffer = Buffer.from(rebalanceRaw.params); let params = {}; params['period'] = new decimal_js_1.default(paramsBuffer.readBigUint64LE(0).toString()); params['lowerRangeBps'] = new decimal_js_1.default(paramsBuffer.readUInt16LE(8)); params['upperRangeBps'] = new decimal_js_1.default(paramsBuffer.readUInt16LE(10)); return params; } exports.readPeriodicRebalanceRebalanceParamsFromStrategy = readPeriodicRebalanceRebalanceParamsFromStrategy; function readPeriodicRebalanceRebalanceStateFromStrategy(rebalanceRaw) { let stateBuffer = Buffer.from(rebalanceRaw.state); let state = {}; state['lastRebalanceTimestamp'] = new decimal_js_1.default(stateBuffer.readBigUint64LE(0).toString()); return state; } exports.readPeriodicRebalanceRebalanceStateFromStrategy = readPeriodicRebalanceRebalanceStateFromStrategy; function deserializePeriodicRebalanceFromOnchainParams(price, rebalanceRaw) { let params = readPeriodicRebalanceRebalanceParamsFromStrategy(rebalanceRaw); return getPeriodicRebalanceRebalanceFieldInfos(price, params['period'], params['lowerRangeBps'], params['upperRangeBps']); } exports.deserializePeriodicRebalanceFromOnchainParams = deserializePeriodicRebalanceFromOnchainParams; //# sourceMappingURL=periodicRebalance.js.map