@kamino-finance/farms-sdk
Version:
71 lines • 4.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRewardsApyForStrategy = getRewardsApyForStrategy;
exports.getRewardsApyForReserve = getRewardsApyForReserve;
exports.getFarmIncentives = getFarmIncentives;
const kliquidity_sdk_1 = require("@kamino-finance/kliquidity-sdk");
const Farms_1 = require("../Farms");
const accounts_1 = require("../@codegen/farms/accounts");
const price_1 = require("./price");
const pubkey_1 = require("./pubkey");
const compat_1 = require("@solana/compat");
const compat_2 = require("./compat");
const klend_sdk_1 = require("@kamino-finance/klend-sdk");
async function getRewardsApyForStrategy(connection, legacyConnection, strategy) {
const admin = process.env.ADMIN;
const rpc = process.env.RPC;
const kaminoClient = new kliquidity_sdk_1.Kamino("mainnet-beta", connection, legacyConnection);
const strategyState = await kaminoClient.getStrategyByAddress(strategy);
if (!strategyState) {
throw new Error(`Strategy state not found for strategy: ${strategy}`);
}
const farm = strategyState.farm;
const stakedTokenMintDecimals = strategyState.sharesMintDecimals.toNumber();
const stakedTokenPrice = await kaminoClient.getStrategySharePrice(strategy);
const farmsClient = new Farms_1.Farms(connection);
console.log(`Farm: ${farm}`);
return await getFarmIncentives(farmsClient, farm, stakedTokenPrice, stakedTokenMintDecimals);
}
async function getRewardsApyForReserve(connection, legacyConnection, reserve) {
const reserveIncentives = {
collateralFarmIncentives: null,
debtFarmIncentives: null,
};
const reserveAccount = await klend_sdk_1.Reserve.fetch(legacyConnection, (0, compat_2.toLegacyPublicKey)(reserve));
if (!reserveAccount) {
throw new Error(`Reserve state not found for reserve: ${reserve}`);
}
const klendMarket = await klend_sdk_1.KaminoMarket.load(legacyConnection, reserveAccount.lendingMarket, 450);
if (!klendMarket) {
throw new Error(`Market state not found for market: ${reserveAccount.lendingMarket.toBase58()}`);
}
const kaminoReserve = klendMarket.getReserveByAddress((0, compat_2.toLegacyPublicKey)(reserve));
if (!kaminoReserve) {
throw new Error(`Strategy state not found for strategy: ${reserve}`);
}
const farmCollateral = (0, compat_1.fromLegacyPublicKey)(kaminoReserve.state.farmCollateral);
const farmDebt = (0, compat_1.fromLegacyPublicKey)(kaminoReserve.state.farmDebt);
const stakedTokenMintDecimals = kaminoReserve.getMintDecimals();
const reserveTokenPrice = await (0, price_1.getPriceForTokenMint)((0, compat_1.fromLegacyPublicKey)(kaminoReserve.getLiquidityMint()));
const reserveCtokenPrice = reserveTokenPrice.div(kaminoReserve.getCollateralExchangeRate());
console.log(`reserveCtokenPrice: ${reserveCtokenPrice}`);
const farmsClient = new Farms_1.Farms(connection);
if (farmCollateral !== pubkey_1.DEFAULT_PUBLIC_KEY) {
const farmIncentivesCollateral = await getFarmIncentives(farmsClient, farmCollateral, reserveCtokenPrice, 6);
reserveIncentives.collateralFarmIncentives = farmIncentivesCollateral;
}
if (farmDebt !== pubkey_1.DEFAULT_PUBLIC_KEY) {
const farmIncentivesDebt = await getFarmIncentives(farmsClient, farmDebt, reserveTokenPrice, stakedTokenMintDecimals);
reserveIncentives.debtFarmIncentives = farmIncentivesDebt;
}
return reserveIncentives;
}
async function getFarmIncentives(farmsClient, farm, stakedTokenPrice, stakedTokenMintDecimals) {
const farmState = await accounts_1.FarmState.fetch(farmsClient.getConnection(), farm, farmsClient.getProgramID());
if (!farmState) {
throw new Error(`Farm state not found for farm: ${farm}`);
}
const farmsIncentives = await farmsClient.calculateFarmIncentivesApy({ farmState, key: farm }, price_1.getPriceForTokenMint, stakedTokenPrice, stakedTokenMintDecimals);
return farmsIncentives;
}
//# sourceMappingURL=apy.js.map