@kamino-finance/kliquidity-sdk
Version:
Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol
210 lines • 8.91 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.MeteoraService = void 0;
exports.computeMeteoraFee = computeMeteoraFee;
const kit_1 = require("@solana/kit");
const decimal_js_1 = __importDefault(require("decimal.js"));
const utils_1 = require("../utils");
const accounts_1 = require("../@codegen/meteora/accounts");
const programId_1 = require("../@codegen/meteora/programId");
const meteora_1 = require("../utils/meteora");
const pubkeys_1 = require("../constants/pubkeys");
class MeteoraService {
_rpc;
_meteoraProgramId;
constructor(rpc, meteoraProgramId = programId_1.PROGRAM_ID) {
this._rpc = rpc;
this._meteoraProgramId = meteoraProgramId;
}
getMeteoraProgramId() {
return this._meteoraProgramId;
}
async getPool(poolAddress) {
return await accounts_1.LbPair.fetch(this._rpc, poolAddress);
}
async getPosition(position) {
return await accounts_1.PositionV2.fetch(this._rpc, position);
}
async getMeteoraPools() {
const rawPools = await this._rpc
.getProgramAccounts(programId_1.PROGRAM_ID, {
commitment: 'confirmed',
filters: [{ dataSize: 904n }],
})
.send();
const pools = [];
for (let i = 0; i < rawPools.length; i++) {
try {
const lbPair = accounts_1.LbPair.decode(Buffer.from(rawPools[i].account.data));
pools.push({ pool: lbPair, key: rawPools[i].pubkey });
}
catch (e) {
console.log(e);
}
}
return pools;
}
async getStrategyMeteoraPoolAprApy(strategy) {
const position = await this.getPosition(strategy.position);
const pool = await this.getPool(strategy.pool);
const decimalsX = strategy.tokenAMintDecimals.toNumber();
const decimalsY = strategy.tokenBMintDecimals.toNumber();
let priceLower = new decimal_js_1.default(0);
let priceUpper = new decimal_js_1.default(0);
if (position && pool) {
const priceRange = (0, utils_1.getMeteoraPriceLowerUpper)(position.lowerBinId, position.upperBinId, pool.binStep, decimalsX, decimalsY);
priceLower = priceRange.priceLower;
priceUpper = priceRange.priceUpper;
}
let priceRange = { priceLower, poolPrice: new decimal_js_1.default(0), priceUpper, strategyOutOfRange: true };
if (pool && position) {
priceRange = (0, utils_1.getStrategyPriceRangeMeteora)(priceLower, priceUpper, pool.activeId, pool.binStep, decimalsX, decimalsY);
}
if (priceRange.strategyOutOfRange) {
return {
...priceRange,
rewardsApy: [],
rewardsApr: [],
feeApy: utils_1.ZERO,
feeApr: utils_1.ZERO,
totalApy: utils_1.ZERO,
totalApr: utils_1.ZERO,
};
}
// TODO: fix this
const totalApr = new decimal_js_1.default(0);
const feeApr = new decimal_js_1.default(0);
const rewardsApr = [new decimal_js_1.default(0)];
return {
totalApr,
totalApy: (0, utils_1.aprToApy)(totalApr, 365),
feeApr,
feeApy: (0, utils_1.aprToApy)(feeApr, 365),
rewardsApr,
rewardsApy: rewardsApr.map((x) => (0, utils_1.aprToApy)(x, 365)),
...priceRange,
};
}
// strongly recommended to pass lowestTick and highestTick because fetching the lowest and highest existent takes very long
async getMeteoraLiquidityDistribution(poolKey, keepOrder = true, lowestTick, highestTick) {
// trick the linter
(() => {
return { keepOrder, highestTick, lowestTick };
})();
//TODO: fix this
const pool = await this.getPool(poolKey);
if (!pool) {
// if the pool doesn't exist, return empty distribution
return {
currentPrice: new decimal_js_1.default(0),
currentTickIndex: 0,
distribution: [],
};
}
const currentTickIndex = pool.activeId;
const tokenXDecimals = await (0, utils_1.getMintDecimals)(this._rpc, pool.tokenXMint);
const tokenYDecimals = await (0, utils_1.getMintDecimals)(this._rpc, pool.tokenYMint);
const currentPrice = (0, meteora_1.getPriceOfBinByBinIdWithDecimals)(currentTickIndex, pool.binStep, tokenXDecimals, tokenYDecimals);
// TODO: add actual distribution
return {
currentPrice,
currentTickIndex,
distribution: [],
};
}
async getMeteoraPositionAprApy(poolPubkey, priceLower, priceUpper) {
const pool = await this.getPool(poolPubkey);
if (!pool) {
return {
priceLower: utils_1.ZERO,
priceUpper: utils_1.ZERO,
poolPrice: utils_1.ZERO,
strategyOutOfRange: true,
rewardsApy: [],
rewardsApr: [],
feeApy: utils_1.ZERO,
feeApr: utils_1.ZERO,
totalApy: utils_1.ZERO,
totalApr: utils_1.ZERO,
};
}
const tokenXDecimals = await (0, utils_1.getMintDecimals)(this._rpc, pool.tokenXMint);
const tokenYDecimals = await (0, utils_1.getMintDecimals)(this._rpc, pool.tokenYMint);
const priceRange = (0, utils_1.getStrategyPriceRangeMeteora)(priceLower, priceUpper, pool.activeId, pool.binStep, tokenXDecimals, tokenYDecimals);
if (priceRange.strategyOutOfRange) {
return {
...priceRange,
rewardsApy: [],
rewardsApr: [],
feeApy: utils_1.ZERO,
feeApr: utils_1.ZERO,
totalApy: utils_1.ZERO,
totalApr: utils_1.ZERO,
};
}
const totalApr = new decimal_js_1.default(0);
const feeApr = new decimal_js_1.default(0);
const rewardsApr = [new decimal_js_1.default(0)];
return {
totalApr,
totalApy: (0, utils_1.aprToApy)(totalApr, 365),
feeApr,
feeApy: (0, utils_1.aprToApy)(feeApr, 365),
rewardsApr,
rewardsApy: rewardsApr.map((x) => (0, utils_1.aprToApy)(x, 365)),
...priceRange,
};
}
async getGenericPoolInfo(poolPubkey) {
const pool = await this.getPool(poolPubkey);
if (!pool) {
return {
dex: 'METEORA',
address: pubkeys_1.DEFAULT_PUBLIC_KEY,
tokenMintA: pubkeys_1.DEFAULT_PUBLIC_KEY,
tokenMintB: pubkeys_1.DEFAULT_PUBLIC_KEY,
price: new decimal_js_1.default(0),
feeRate: new decimal_js_1.default(0),
volumeOnLast7d: new decimal_js_1.default(0),
tvl: new decimal_js_1.default(0),
tickSpacing: new decimal_js_1.default(0),
positions: new decimal_js_1.default(0),
};
}
const tokenXDecimals = await (0, utils_1.getMintDecimals)(this._rpc, pool.tokenXMint);
const tokenYDecimals = await (0, utils_1.getMintDecimals)(this._rpc, pool.tokenYMint);
const price = (0, meteora_1.getPriceOfBinByBinIdWithDecimals)(pool.activeId, pool.binStep, tokenXDecimals, tokenYDecimals);
const poolInfo = {
dex: 'METEORA',
address: (0, kit_1.address)(poolPubkey),
tokenMintA: pool.tokenXMint,
tokenMintB: pool.tokenYMint,
price,
feeRate: computeMeteoraFee(pool),
// TODO: add these
volumeOnLast7d: new decimal_js_1.default(0),
tvl: new decimal_js_1.default(0),
tickSpacing: new decimal_js_1.default(pool.binStep),
// todo(Silviu): get real amount of positions
positions: new decimal_js_1.default(await this.getPositionsCountByPool(poolPubkey)),
};
return poolInfo;
}
async getPositionsCountByPool(pool) {
const rawPositions = await this._rpc
.getProgramAccounts(programId_1.PROGRAM_ID, {
commitment: 'confirmed',
filters: [{ dataSize: 8120n }, { memcmp: { bytes: pool, offset: 8n, encoding: 'base58' } }],
})
.send();
return rawPositions.length;
}
}
exports.MeteoraService = MeteoraService;
function computeMeteoraFee(pool) {
return new decimal_js_1.default(pool.parameters.baseFactor).mul(new decimal_js_1.default(pool.binStep)).div(new decimal_js_1.default(1e6));
}
//# sourceMappingURL=MeteoraService.js.map