@kamino-finance/kliquidity-sdk
Version:
Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol
99 lines • 5.61 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.TakeProfitRebalanceTypeName = exports.DEFAULT_DESTINATION_TOKEN = exports.DEFAULT_UPPER_RANGE_PRICE_DIFF_BPS = exports.DEFAULT_LOWER_RANGE_PRICE_DIFF_BPS = void 0;
exports.getTakeProfitRebalanceFieldsInfos = getTakeProfitRebalanceFieldsInfos;
exports.getPositionRangeFromTakeProfitParams = getPositionRangeFromTakeProfitParams;
exports.getDefaultTakeProfitRebalanceFieldsInfos = getDefaultTakeProfitRebalanceFieldsInfos;
exports.readTakeProfitRebalanceParamsFromStrategy = readTakeProfitRebalanceParamsFromStrategy;
exports.readTakeProfitRebalanceStateFromStrategy = readTakeProfitRebalanceStateFromStrategy;
exports.deserializeTakeProfitRebalanceFromOnchainParams = deserializeTakeProfitRebalanceFromOnchainParams;
const decimal_js_1 = __importDefault(require("decimal.js"));
const CreationParameters_1 = require("../utils/CreationParameters");
const utils_1 = require("../utils");
const whirlpool_sdk_1 = require("@orca-so/whirlpool-sdk");
const bn_js_1 = __importDefault(require("bn.js"));
const meteora_1 = require("../utils/meteora");
const lib_1 = require("@raydium-io/raydium-sdk-v2/lib");
exports.DEFAULT_LOWER_RANGE_PRICE_DIFF_BPS = new decimal_js_1.default(500);
exports.DEFAULT_UPPER_RANGE_PRICE_DIFF_BPS = new decimal_js_1.default(500);
exports.DEFAULT_DESTINATION_TOKEN = new decimal_js_1.default(1);
exports.TakeProfitRebalanceTypeName = 'takeProfit';
function getTakeProfitRebalanceFieldsInfos(lowerRangePrice, upperRangePrice, destinationToken, enabled = true) {
const rebalanceType = {
label: 'rebalanceType',
type: 'string',
value: exports.TakeProfitRebalanceTypeName,
enabled,
};
const lowerRangePriceRebalanceFieldInfo = {
label: 'rangePriceLower',
type: 'number',
value: lowerRangePrice,
enabled,
};
const upperRangePriceRebalanceFieldInfo = {
label: 'rangePriceUpper',
type: 'number',
value: upperRangePrice,
enabled,
};
const destinationTokenRebalanceFieldInfo = {
label: 'destinationToken',
type: 'number',
value: destinationToken,
enabled,
};
return [
rebalanceType,
lowerRangePriceRebalanceFieldInfo,
upperRangePriceRebalanceFieldInfo,
destinationTokenRebalanceFieldInfo,
];
}
function getPositionRangeFromTakeProfitParams(dex, tokenADecimals, tokenBDecimals, lowerSqrtPriceX64, upperSqrtPriceX64) {
if (dex == 'ORCA') {
const lowerPrice = (0, whirlpool_sdk_1.sqrtPriceX64ToPrice)(new bn_js_1.default(lowerSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals);
const upperPrice = (0, whirlpool_sdk_1.sqrtPriceX64ToPrice)(new bn_js_1.default(upperSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals);
return { lowerPrice, upperPrice };
}
else if (dex == 'RAYDIUM') {
const lowerPrice = (0, whirlpool_sdk_1.sqrtPriceX64ToPrice)(new bn_js_1.default(lowerSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals);
const upperPrice = (0, whirlpool_sdk_1.sqrtPriceX64ToPrice)(new bn_js_1.default(upperSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals);
return { lowerPrice, upperPrice };
}
else if (dex == 'METEORA') {
const lowerPrice = (0, meteora_1.getPriceFromQ64Price)(new decimal_js_1.default(lowerSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals);
const upperPrice = (0, meteora_1.getPriceFromQ64Price)(new decimal_js_1.default(upperSqrtPriceX64.toString()), tokenADecimals, tokenBDecimals);
return { lowerPrice, upperPrice };
}
else {
throw new Error(`Unknown DEX ${dex}`);
}
}
function getDefaultTakeProfitRebalanceFieldsInfos(price) {
const lowerPrice = price.mul(CreationParameters_1.FullBPSDecimal.sub(exports.DEFAULT_LOWER_RANGE_PRICE_DIFF_BPS)).div(CreationParameters_1.FullBPSDecimal);
const upperPrice = price.mul(CreationParameters_1.FullBPSDecimal.add(exports.DEFAULT_UPPER_RANGE_PRICE_DIFF_BPS)).div(CreationParameters_1.FullBPSDecimal);
return getTakeProfitRebalanceFieldsInfos(lowerPrice, upperPrice, price);
}
function readTakeProfitRebalanceParamsFromStrategy(tokenADecimals, tokenBDecimals, rebalanceRaw) {
const paramsBuffer = Buffer.from(rebalanceRaw.params);
const params = {};
params['lowerRangePrice'] = lib_1.SqrtPriceMath.sqrtPriceX64ToPrice(new bn_js_1.default((0, utils_1.readBigUint128LE)(paramsBuffer, 0).toString()), tokenADecimals, tokenBDecimals);
params['upperRangePrice'] = lib_1.SqrtPriceMath.sqrtPriceX64ToPrice(new bn_js_1.default((0, utils_1.readBigUint128LE)(paramsBuffer, 16).toString()), tokenADecimals, tokenBDecimals);
params['destinationToken'] = new decimal_js_1.default(paramsBuffer.readUint8(32));
return params;
}
function readTakeProfitRebalanceStateFromStrategy(rebalanceRaw) {
const stateBuffer = Buffer.from(rebalanceRaw.state);
const state = {};
state['step'] = new decimal_js_1.default(stateBuffer.readUInt8(0));
return state;
}
function deserializeTakeProfitRebalanceFromOnchainParams(tokenADecimals, tokenBDecimals, rebalanceRaw) {
const params = readTakeProfitRebalanceParamsFromStrategy(tokenADecimals, tokenBDecimals, rebalanceRaw);
return getTakeProfitRebalanceFieldsInfos(params['lowerRangePrice'], params['upperRangePrice'], params['destinationToken']);
}
//# sourceMappingURL=takeProfitRebalance.js.map