UNPKG

@marinade.finance/kamino-sdk

Version:
86 lines 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.upsertManyRebalanceFieldInfos = exports.upsertRebalanceFieldInfo = exports.getRebalanceMethodFromRebalanceFields = exports.rebalanceTypeToRebalanceMethod = exports.getRebalanceTypeFromRebalanceFields = void 0; const RebalanceType_1 = require("../kamino-client/types/RebalanceType"); const CreationParameters_1 = require("../utils/CreationParameters"); const driftRebalance_1 = require("./driftRebalance"); const expanderRebalance_1 = require("./expanderRebalance"); const manualRebalance_1 = require("./manualRebalance"); const periodicRebalance_1 = require("./periodicRebalance"); const pricePercentageRebalance_1 = require("./pricePercentageRebalance"); const pricePercentageWithResetRebalance_1 = require("./pricePercentageWithResetRebalance"); const takeProfitRebalance_1 = require("./takeProfitRebalance"); function getRebalanceTypeFromRebalanceFields(rebalanceFieldInfos) { let rebalanceTypeField = rebalanceFieldInfos.find((field) => field.label === 'rebalanceType'); if (!rebalanceTypeField) { throw new Error('Rebalance type field not found'); } switch (rebalanceTypeField.value) { case manualRebalance_1.ManualRebalanceTypeName: return new RebalanceType_1.Manual(); case pricePercentageRebalance_1.PricePercentageRebalanceTypeName: return new RebalanceType_1.PricePercentage(); case pricePercentageWithResetRebalance_1.PricePercentageWithResetRebalanceTypeName: return new RebalanceType_1.PricePercentageWithReset(); case driftRebalance_1.DriftRebalanceTypeName: return new RebalanceType_1.Drift(); case takeProfitRebalance_1.TakeProfitRebalanceTypeName: return new RebalanceType_1.TakeProfit(); case periodicRebalance_1.PeriodicRebalanceTypeName: return new RebalanceType_1.PeriodicRebalance(); case expanderRebalance_1.ExpanderRebalanceTypeName: return new RebalanceType_1.Expander(); default: throw new Error(`Invalid rebalance type ${rebalanceTypeField.value}`); } } exports.getRebalanceTypeFromRebalanceFields = getRebalanceTypeFromRebalanceFields; function rebalanceTypeToRebalanceMethod(rebalanceType) { switch (rebalanceType.kind) { case RebalanceType_1.Manual.kind: return CreationParameters_1.ManualRebalanceMethod; case RebalanceType_1.PricePercentage.kind: return CreationParameters_1.PricePercentageRebalanceMethod; case RebalanceType_1.PricePercentageWithReset.kind: return CreationParameters_1.PricePercentageWithResetRangeRebalanceMethod; case RebalanceType_1.Drift.kind: return CreationParameters_1.DriftRebalanceMethod; case RebalanceType_1.TakeProfit.kind: return CreationParameters_1.TakeProfitMethod; case RebalanceType_1.PeriodicRebalance.kind: return CreationParameters_1.PeriodicRebalanceMethod; case RebalanceType_1.Expander.kind: return CreationParameters_1.ExpanderMethod; default: throw new Error(`Invalid rebalance type ${rebalanceType}`); } } exports.rebalanceTypeToRebalanceMethod = rebalanceTypeToRebalanceMethod; function getRebalanceMethodFromRebalanceFields(rebalanceFieldInfos) { let rebalanceType = getRebalanceTypeFromRebalanceFields(rebalanceFieldInfos); return rebalanceTypeToRebalanceMethod(rebalanceType); } exports.getRebalanceMethodFromRebalanceFields = getRebalanceMethodFromRebalanceFields; function upsertRebalanceFieldInfo(rebalanceFieldInfos, newFieldInfo) { let newRebalanceFieldInfoIndex = rebalanceFieldInfos.findIndex((fieldInfo) => fieldInfo.label === newFieldInfo.label); // if the field is not found, add it if (newRebalanceFieldInfoIndex === -1) { return [...rebalanceFieldInfos, newFieldInfo]; } else { // if the field is found, update it let newRebalanceFieldInfos = [...rebalanceFieldInfos]; newRebalanceFieldInfos[newRebalanceFieldInfoIndex] = newFieldInfo; return newRebalanceFieldInfos; } } exports.upsertRebalanceFieldInfo = upsertRebalanceFieldInfo; function upsertManyRebalanceFieldInfos(rebalanceFieldInfos, newFieldInfos) { let updatedFieldInfos = [...rebalanceFieldInfos]; for (let newFieldInfo of newFieldInfos) { updatedFieldInfos = upsertRebalanceFieldInfo(updatedFieldInfos, newFieldInfo); } return updatedFieldInfos; } exports.upsertManyRebalanceFieldInfos = upsertManyRebalanceFieldInfos; //# sourceMappingURL=utils.js.map