@kamino-finance/kliquidity-sdk
Version:
Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol
283 lines • 13.3 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpanderRebalanceTypeName = exports.DefaultSwapUnevenAllowed = exports.DefaultExpansionSizeBPS = exports.DefaultMaxNumberOfExpansions = void 0;
exports.getExpanderRebalanceFieldInfos = getExpanderRebalanceFieldInfos;
exports.getPositionRangeFromExpanderParams = getPositionRangeFromExpanderParams;
exports.getPositionResetRangeFromExpanderParams = getPositionResetRangeFromExpanderParams;
exports.getDefaultExpanderRebalanceFieldInfos = getDefaultExpanderRebalanceFieldInfos;
exports.readRawExpanderRebalanceParamsFromStrategy = readRawExpanderRebalanceParamsFromStrategy;
exports.readExpanderRebalanceParamsFromStrategy = readExpanderRebalanceParamsFromStrategy;
exports.readRawExpanderRebalanceStateFromStrategy = readRawExpanderRebalanceStateFromStrategy;
exports.readExpanderRebalanceStateFromStrategy = readExpanderRebalanceStateFromStrategy;
exports.readExpanderRebalanceFieldInfosFromStrategy = readExpanderRebalanceFieldInfosFromStrategy;
exports.deserializeExpanderRebalanceWithStateOverride = deserializeExpanderRebalanceWithStateOverride;
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 whirlpool_sdk_1 = require("@orca-so/whirlpool-sdk");
const bn_js_1 = __importDefault(require("bn.js"));
const utils_2 = require("./utils");
const math_utils_1 = require("./math_utils");
const meteora_1 = require("../utils/meteora");
exports.DefaultMaxNumberOfExpansions = new decimal_js_1.default(10);
exports.DefaultExpansionSizeBPS = new decimal_js_1.default(100);
exports.DefaultSwapUnevenAllowed = new decimal_js_1.default(1);
exports.ExpanderRebalanceTypeName = 'expander';
function getExpanderRebalanceFieldInfos(price, lowerPercentageBPS, upperPercentageBPS, resetLowerPercentageBPS, resetUpperPercentageBPS, expansionBPS, maxNumberOfExpansions, swapUnevenAllowed, enabled = true) {
const rebalanceType = {
label: consts_1.RebalanceTypeLabelName,
type: 'string',
value: exports.ExpanderRebalanceTypeName,
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 expansionBpsRebalanceFieldInfo = {
label: 'expansionBps',
type: 'number',
value: expansionBPS,
enabled,
};
const maxNumberOfExpansionsRebalanceFieldInfo = {
label: 'maxNumberOfExpansions',
type: 'number',
value: maxNumberOfExpansions,
enabled,
};
const swapUnevenAllowedFieldInfo = {
label: 'swapUnevenAllowed',
type: 'number',
value: swapUnevenAllowed,
enabled,
};
const { lowerPrice, upperPrice } = getPositionRangeFromExpanderParams(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 } = getPositionResetRangeFromExpanderParams(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,
expansionBpsRebalanceFieldInfo,
maxNumberOfExpansionsRebalanceFieldInfo,
swapUnevenAllowedFieldInfo,
lowerRangeRebalanceFieldInfo,
upperRangeRebalanceFieldInfo,
resetLowerRangeRebalanceFieldInfo,
resetUpperRangeRebalanceFieldInfo,
];
}
function getPositionRangeFromExpanderParams(price, lowerPriceDifferenceBPS, upperPriceDifferenceBPS) {
return (0, math_utils_1.getPriceRangeFromPriceAndDiffBPS)(price, lowerPriceDifferenceBPS, upperPriceDifferenceBPS);
}
function getPositionResetRangeFromExpanderParams(price, lowerPriceDifferenceBPS, upperPriceDifferenceBPS, resetLowerPriceDifferenceBPS, resetUpperPriceDifferenceBPS) {
return (0, math_utils_1.getResetRangeFromPriceAndDiffBPS)(price, lowerPriceDifferenceBPS, upperPriceDifferenceBPS, resetLowerPriceDifferenceBPS, resetUpperPriceDifferenceBPS);
}
function getDefaultExpanderRebalanceFieldInfos(price) {
return getExpanderRebalanceFieldInfos(price, CreationParameters_1.DefaultLowerPercentageBPSDecimal, CreationParameters_1.DefaultUpperPercentageBPSDecimal, CreationParameters_1.DefaultLowerPercentageBPSDecimal, CreationParameters_1.DefaultUpperPercentageBPSDecimal, CreationParameters_1.DefaultLowerPercentageBPSDecimal, exports.DefaultMaxNumberOfExpansions, exports.DefaultSwapUnevenAllowed);
}
function readRawExpanderRebalanceParamsFromStrategy(rebalanceRaw) {
const paramsBuffer = Buffer.from(rebalanceRaw.params);
const params = {};
params['lowerRangeBps'] = new decimal_js_1.default(paramsBuffer.readUInt16LE(0));
params['upperRangeBps'] = new decimal_js_1.default(paramsBuffer.readUInt16LE(2));
params['lowerResetRatioBps'] = new decimal_js_1.default(paramsBuffer.readUInt16LE(4));
params['upperResetRatioBps'] = new decimal_js_1.default(paramsBuffer.readUInt16LE(6));
params['expansionBps'] = new decimal_js_1.default(paramsBuffer.readUInt16LE(8));
params['maxNumberOfExpansions'] = new decimal_js_1.default(paramsBuffer.readUInt16LE(10));
params['swapUnevenAllowed'] = new decimal_js_1.default(paramsBuffer.readUInt8(12));
return params;
}
function readExpanderRebalanceParamsFromStrategy(rebalanceRaw) {
const paramsBuffer = Buffer.from(rebalanceRaw.params);
const lowerRangeBps = new decimal_js_1.default(paramsBuffer.readUInt16LE(0));
const upperRangeBps = new decimal_js_1.default(paramsBuffer.readUInt16LE(2));
const lowerResetRatioBps = new decimal_js_1.default(paramsBuffer.readUInt16LE(4));
const upperResetRatioBps = new decimal_js_1.default(paramsBuffer.readUInt16LE(6));
const expansionBps = new decimal_js_1.default(paramsBuffer.readUInt16LE(8));
const maxNumberOfExpansions = new decimal_js_1.default(paramsBuffer.readUInt16LE(10));
const swapUnevenAllowed = new decimal_js_1.default(paramsBuffer.readUInt8(12));
const lowerBpsRebalanceFieldInfo = {
label: 'lowerRangeBps',
type: 'number',
value: lowerRangeBps,
enabled: true,
};
const upperBpsRebalanceFieldInfo = {
label: 'upperRangeBps',
type: 'number',
value: upperRangeBps,
enabled: true,
};
const resetLowerBpsRebalanceFieldInfo = {
label: 'resetLowerRangeBps',
type: 'number',
value: lowerResetRatioBps,
enabled: true,
};
const resetUpperBpsRebalanceFieldInfo = {
label: 'resetUpperRangeBps',
type: 'number',
value: upperResetRatioBps,
enabled: true,
};
const expansionBpsRebalanceFieldInfo = {
label: 'expansionBps',
type: 'number',
value: expansionBps,
enabled: true,
};
const maxNumberOfExpansionsRebalanceFieldInfo = {
label: 'maxNumberOfExpansions',
type: 'number',
value: maxNumberOfExpansions,
enabled: true,
};
const swapUnevenAllowedFieldInfo = {
label: 'swapUnevenAllowed',
type: 'number',
value: swapUnevenAllowed,
enabled: true,
};
return [
lowerBpsRebalanceFieldInfo,
upperBpsRebalanceFieldInfo,
resetLowerBpsRebalanceFieldInfo,
resetUpperBpsRebalanceFieldInfo,
expansionBpsRebalanceFieldInfo,
maxNumberOfExpansionsRebalanceFieldInfo,
swapUnevenAllowedFieldInfo,
];
}
function readRawExpanderRebalanceStateFromStrategy(rebalanceRaw) {
const stateBuffer = Buffer.from(rebalanceRaw.state);
const state = {};
state['initialPoolPrice'] = new decimal_js_1.default((0, utils_1.readBigUint128LE)(stateBuffer, 0).toString());
state['expansionCount'] = new decimal_js_1.default(stateBuffer.readUInt16LE(16));
return state;
}
function readExpanderRebalanceStateFromStrategy(dex, tokenADecimals, tokenBDecimals, rebalanceRaw) {
const params = readRawExpanderRebalanceParamsFromStrategy(rebalanceRaw);
const lowerRangeBps = params['lowerRangeBps'];
const upperRangeBps = params['upperRangeBps'];
const lowerResetRatioBps = params['lowerResetRatioBps'];
const upperResetRatioBps = params['upperResetRatioBps'];
const expansionBps = params['expansionBps'];
const state = readRawExpanderRebalanceStateFromStrategy(rebalanceRaw);
const initialPriceX64 = state['initialPoolPrice'];
const expansionCount = state['expansionCount'];
let initialPrice;
if (dex == 'ORCA') {
initialPrice = (0, whirlpool_sdk_1.sqrtPriceX64ToPrice)(new bn_js_1.default(initialPriceX64.toString()), tokenADecimals, tokenBDecimals);
}
else if (dex == 'RAYDIUM') {
initialPrice = (0, whirlpool_sdk_1.sqrtPriceX64ToPrice)(new bn_js_1.default(initialPriceX64.toString()), tokenADecimals, tokenBDecimals);
}
else if (dex == 'METEORA') {
initialPrice = (0, meteora_1.getPriceFromQ64Price)(new decimal_js_1.default(initialPriceX64.toString()), tokenADecimals, tokenBDecimals);
}
else {
throw new Error(`Unknown DEX ${dex}`);
}
const lowerRangeFactorBPS = lowerRangeBps.add(expansionBps.mul(expansionCount));
const upperRangeFactorBPS = upperRangeBps.add(expansionBps.mul(expansionCount));
const { lowerPrice, upperPrice } = (0, math_utils_1.getPriceRangeFromPriceAndDiffBPS)(initialPrice, lowerRangeFactorBPS, upperRangeFactorBPS);
const { lowerPrice: lowerResetPrice, upperPrice: upperResetPrice } = (0, math_utils_1.getResetRangeFromPriceAndDiffBPS)(initialPrice, lowerRangeFactorBPS, upperRangeFactorBPS, lowerResetRatioBps, upperResetRatioBps);
const lowerRangeRebalanceFieldInfo = {
label: 'rangePriceLower',
type: 'number',
value: lowerPrice,
enabled: false,
};
const upperRangeRebalanceFieldInfo = {
label: 'rangePriceUpper',
type: 'number',
value: upperPrice,
enabled: false,
};
const resetLowerRangeRebalanceFieldInfo = {
label: 'resetPriceLower',
type: 'number',
value: lowerResetPrice,
enabled: false,
};
const resetUpperRangeRebalanceFieldInfo = {
label: 'resetPriceUpper',
type: 'number',
value: upperResetPrice,
enabled: false,
};
return [
lowerRangeRebalanceFieldInfo,
upperRangeRebalanceFieldInfo,
resetLowerRangeRebalanceFieldInfo,
resetUpperRangeRebalanceFieldInfo,
];
}
function readExpanderRebalanceFieldInfosFromStrategy(price, rebalanceRaw) {
const paramsBuffer = Buffer.from(rebalanceRaw.params);
const lowerRangeBps = new decimal_js_1.default(paramsBuffer.readUInt16LE(0));
const upperRangeBps = new decimal_js_1.default(paramsBuffer.readUInt16LE(2));
const lowerResetRatioBps = new decimal_js_1.default(paramsBuffer.readUInt16LE(4));
const upperResetRatioBps = new decimal_js_1.default(paramsBuffer.readUInt16LE(6));
const expansionBps = new decimal_js_1.default(paramsBuffer.readUInt16LE(8));
const maxNumberOfExpansions = new decimal_js_1.default(paramsBuffer.readUInt16LE(10));
const swapUnevenAllowed = new decimal_js_1.default(paramsBuffer.readUInt8(12));
return getExpanderRebalanceFieldInfos(price, lowerRangeBps, upperRangeBps, lowerResetRatioBps, upperResetRatioBps, expansionBps, maxNumberOfExpansions, swapUnevenAllowed);
}
function deserializeExpanderRebalanceWithStateOverride(dex, tokenADecimals, tokenBDecimals, price, rebalanceRaw) {
const stateFields = readExpanderRebalanceStateFromStrategy(dex, tokenADecimals, tokenBDecimals, rebalanceRaw);
const fields = readExpanderRebalanceFieldInfosFromStrategy(price, rebalanceRaw);
return (0, utils_2.upsertManyRebalanceFieldInfos)(fields, stateFields);
}
//# sourceMappingURL=expanderRebalance.js.map