@kamino-finance/kliquidity-sdk
Version:
Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol
149 lines • 7.39 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.PricePercentageRebalanceTypeName = void 0;
exports.getPricePercentageRebalanceFieldInfos = getPricePercentageRebalanceFieldInfos;
exports.getPositionRangeFromPercentageRebalanceParams = getPositionRangeFromPercentageRebalanceParams;
exports.getDefaultPricePercentageRebalanceFieldInfos = getDefaultPricePercentageRebalanceFieldInfos;
exports.readPricePercentageRebalanceParamsFromStrategy = readPricePercentageRebalanceParamsFromStrategy;
exports.readRawPricePercentageRebalanceStateFromStrategy = readRawPricePercentageRebalanceStateFromStrategy;
exports.readPricePercentageRebalanceStateFromStrategy = readPricePercentageRebalanceStateFromStrategy;
exports.deserializePricePercentageRebalanceFromOnchainParams = deserializePricePercentageRebalanceFromOnchainParams;
exports.deserializePricePercentageRebalanceWithStateOverride = deserializePricePercentageRebalanceWithStateOverride;
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");
const meteora_1 = require("../utils/meteora");
exports.PricePercentageRebalanceTypeName = 'pricePercentage';
function getPricePercentageRebalanceFieldInfos(poolPrice, lowerPercentageBPS, upperPercentageBPS, enabled = true) {
const rebalanceType = {
label: consts_1.RebalanceTypeLabelName,
type: 'string',
value: exports.PricePercentageRebalanceTypeName,
enabled,
};
const lowerBpsRebalanceFieldInfo = {
label: 'lowerRangeBps',
type: 'number',
value: lowerPercentageBPS,
enabled,
};
const upperBpsRebalanceFieldInfo = {
label: 'upperRangeBps',
type: 'number',
value: upperPercentageBPS,
enabled,
};
const { lowerPrice, upperPrice } = getPositionRangeFromPercentageRebalanceParams(poolPrice, lowerPercentageBPS, upperPercentageBPS);
const lowerRangeRebalanceFieldInfo = {
label: 'rangePriceLower',
type: 'number',
value: lowerPrice,
enabled: false,
};
const upperRangeRebalanceFieldInfo = {
label: 'rangePriceUpper',
type: 'number',
value: upperPrice,
enabled: false,
};
return [
rebalanceType,
lowerBpsRebalanceFieldInfo,
upperBpsRebalanceFieldInfo,
lowerRangeRebalanceFieldInfo,
upperRangeRebalanceFieldInfo,
];
}
function getPositionRangeFromPercentageRebalanceParams(price, lowerPercentageBPS, upperPercentageBPS) {
return (0, math_utils_1.getPriceRangeFromPriceAndDiffBPS)(price, lowerPercentageBPS, upperPercentageBPS);
}
function getDefaultPricePercentageRebalanceFieldInfos(price) {
const fieldInfos = getPricePercentageRebalanceFieldInfos(price, CreationParameters_1.DefaultLowerPercentageBPSDecimal, CreationParameters_1.DefaultUpperPercentageBPSDecimal);
return fieldInfos;
}
function readPricePercentageRebalanceParamsFromStrategy(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,
};
return [lowerBpsRebalanceFieldInfo, upperBpsRebalanceFieldInfo];
}
function readRawPricePercentageRebalanceStateFromStrategy(rebalanceRaw) {
const stateBuffer = Buffer.from(rebalanceRaw.state);
const lowerRangeRebalanceFieldInfo = {
label: 'rangePriceLower',
type: 'number',
value: new decimal_js_1.default((0, utils_1.readBigUint128LE)(stateBuffer, 0).toString()),
enabled: false,
};
const upperRangeRebalanceFieldInfo = {
label: 'rangePriceUpper',
type: 'number',
value: new decimal_js_1.default((0, utils_1.readBigUint128LE)(stateBuffer, 16).toString()),
enabled: false,
};
return [lowerRangeRebalanceFieldInfo, upperRangeRebalanceFieldInfo];
}
function readPricePercentageRebalanceStateFromStrategy(dex, tokenADecimals, tokenBDecimals, rebalanceRaw) {
const stateBuffer = Buffer.from(rebalanceRaw.state);
const lowerSqrtPriceX64 = (0, utils_1.readBigUint128LE)(stateBuffer, 0).toString();
const upperSqrtPriceX64 = (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 if (dex == 'METEORA') {
lowerPrice = (0, meteora_1.getPriceFromQ64Price)(new decimal_js_1.default(lowerSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals);
upperPrice = (0, meteora_1.getPriceFromQ64Price)(new decimal_js_1.default(upperSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals);
}
else {
throw new Error(`Unknown DEX ${dex}`);
}
const lowerRangeRebalanceFieldInfo = {
label: 'rangePriceLower',
type: 'number',
value: lowerPrice,
enabled: false,
};
const upperRangeRebalanceFieldInfo = {
label: 'rangePriceUpper',
type: 'number',
value: upperPrice,
enabled: false,
};
return [lowerRangeRebalanceFieldInfo, upperRangeRebalanceFieldInfo];
}
function deserializePricePercentageRebalanceFromOnchainParams(price, rebalanceRaw) {
const params = readPricePercentageRebalanceParamsFromStrategy(rebalanceRaw);
const lowerRangeBPS = new decimal_js_1.default(params.find((param) => param.label == 'lowerRangeBps')?.value);
const upperRangeBPS = new decimal_js_1.default(params.find((param) => param.label == 'upperRangeBps')?.value);
return getPricePercentageRebalanceFieldInfos(price, lowerRangeBPS, upperRangeBPS);
}
function deserializePricePercentageRebalanceWithStateOverride(dex, tokenADecimals, tokenBDecimals, price, rebalanceRaw) {
const stateFields = readPricePercentageRebalanceStateFromStrategy(dex, tokenADecimals, tokenBDecimals, rebalanceRaw);
const fields = deserializePricePercentageRebalanceFromOnchainParams(price, rebalanceRaw);
return (0, utils_2.upsertManyRebalanceFieldInfos)(fields, stateFields);
}
//# sourceMappingURL=pricePercentageRebalance.js.map