@kamino-finance/kliquidity-sdk
Version:
Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol
92 lines • 4.46 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
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;
exports.getPeriodicRebalanceRebalanceFieldInfos = getPeriodicRebalanceRebalanceFieldInfos;
exports.getPositionRangeFromPeriodicRebalanceParams = getPositionRangeFromPeriodicRebalanceParams;
exports.getDefaultPeriodicRebalanceFieldInfos = getDefaultPeriodicRebalanceFieldInfos;
exports.readPeriodicRebalanceRebalanceParamsFromStrategy = readPeriodicRebalanceRebalanceParamsFromStrategy;
exports.readPeriodicRebalanceRebalanceStateFromStrategy = readPeriodicRebalanceRebalanceStateFromStrategy;
exports.deserializePeriodicRebalanceFromOnchainParams = deserializePeriodicRebalanceFromOnchainParams;
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) {
const rebalanceType = {
label: consts_1.RebalanceTypeLabelName,
type: 'string',
value: exports.PeriodicRebalanceTypeName,
enabled,
};
const periodRebalanceFieldInfo = {
label: 'period',
type: 'number',
value: period,
enabled,
};
const lowerRangeBpsRebalanceFieldInfo = {
label: 'lowerRangeBps',
type: 'number',
value: lowerRangeBps,
enabled,
};
const upperRangeBpsRebalanceFieldInfo = {
label: 'upperRangeBps',
type: 'number',
value: upperRangeBps,
enabled,
};
const { lowerPrice, upperPrice } = getPositionRangeFromPeriodicRebalanceParams(price, lowerRangeBps, upperRangeBps);
const lowerRangeRebalanceFieldInfo = {
label: 'rangePriceLower',
type: 'number',
value: lowerPrice,
enabled: false,
};
const upperRangeRebalanceFieldInfo = {
label: 'rangePriceUpper',
type: 'number',
value: upperPrice,
enabled: false,
};
return [
rebalanceType,
periodRebalanceFieldInfo,
lowerRangeBpsRebalanceFieldInfo,
upperRangeBpsRebalanceFieldInfo,
lowerRangeRebalanceFieldInfo,
upperRangeRebalanceFieldInfo,
];
}
function getPositionRangeFromPeriodicRebalanceParams(price, lowerPercentageBPS, upperPercentageBPS) {
return (0, math_utils_1.getPriceRangeFromPriceAndDiffBPS)(price, lowerPercentageBPS, upperPercentageBPS);
}
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);
}
function readPeriodicRebalanceRebalanceParamsFromStrategy(rebalanceRaw) {
const paramsBuffer = Buffer.from(rebalanceRaw.params);
const 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;
}
function readPeriodicRebalanceRebalanceStateFromStrategy(rebalanceRaw) {
const stateBuffer = Buffer.from(rebalanceRaw.state);
const state = {};
state['lastRebalanceTimestamp'] = new decimal_js_1.default(stateBuffer.readBigUInt64LE(0).toString());
return state;
}
function deserializePeriodicRebalanceFromOnchainParams(price, rebalanceRaw) {
const params = readPeriodicRebalanceRebalanceParamsFromStrategy(rebalanceRaw);
return getPeriodicRebalanceRebalanceFieldInfos(price, params['period'], params['lowerRangeBps'], params['upperRangeBps']);
}
//# sourceMappingURL=periodicRebalance.js.map
;