UNPKG

@hubbleprotocol/farms-sdk

Version:
224 lines 12.1 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.downloadAllFarmConfigs = downloadAllFarmConfigs; exports.getMarketsFromApiData = getMarketsFromApiData; exports.getFarmConfigType = getFarmConfigType; const utils_1 = require("../utils"); const utils_2 = require("./utils"); const Farms_1 = require("../Farms"); const fs = __importStar(require("fs")); const web3_js_1 = require("@solana/web3.js"); const klend_sdk_1 = require("@kamino-finance/klend-sdk"); const accounts_1 = require("../rpc_client/accounts"); const decimal_js_1 = __importDefault(require("decimal.js")); const types_1 = require("../rpc_client/types"); const kliquidity_sdk_1 = require("@kamino-finance/kliquidity-sdk"); function downloadAllFarmConfigs(targetPath) { return __awaiter(this, void 0, void 0, function* () { const admin = process.env.ADMIN; const rpc = process.env.RPC; const klendProgramId = new web3_js_1.PublicKey(process.env.KLEND_PROGRAM_ID); const lendingMarkets = yield getMarketsFromApiData(klendProgramId); const env = (0, utils_2.initializeClient)(rpc, admin, (0, utils_1.getFarmsProgramId)(rpc), false); const farmClient = new Farms_1.Farms(env.provider.connection); const allFarms = yield farmClient.getAllFarmStates(); const fetchedFarmsForStratsAndReserves = new klend_sdk_1.PublicKeySet([]); // Download for lending based on API markets - all reserve farms for (const market of lendingMarkets) { const kaminoMarket = yield klend_sdk_1.KaminoMarket.load(env.provider.connection, market.key, 450, klendProgramId); if (!kaminoMarket) { throw new Error("Kamino market not found"); } fs.mkdirSync(`${targetPath}/lending/${market.marketName}`, { recursive: true, }); for (const reserve of kaminoMarket.reserves.values()) { if (!reserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)) { fetchedFarmsForStratsAndReserves.add(reserve.state.farmCollateral); const farmStateCollateral = yield accounts_1.FarmState.fetch(env.provider.connection, reserve.state.farmCollateral); if (farmStateCollateral) { const farmConfig = getFarmConfigType(reserve.state.farmCollateral, farmStateCollateral, { type: "reserve", reserve: reserve.address, market: market.key, strategy: undefined, }); fs.writeFileSync(`${targetPath}/lending/${market.marketName}/${reserve.symbol}-Collateral.json`, JSON.stringify(farmConfig, null, 2)); } else { console.log("Could not fetch farm", reserve.state.farmCollateral.toBase58()); } } if (!reserve.state.farmDebt.equals(web3_js_1.PublicKey.default)) { fetchedFarmsForStratsAndReserves.add(reserve.state.farmDebt); const farmStateDebt = yield accounts_1.FarmState.fetch(env.provider.connection, reserve.state.farmDebt); if (farmStateDebt) { const farmConfig = getFarmConfigType(reserve.state.farmDebt, farmStateDebt, { type: "reserve", reserve: reserve.address, market: market.key, strategy: undefined, }); fs.writeFileSync(`${targetPath}/lending/${market.marketName}/${reserve.symbol}-Debt.json`, JSON.stringify(farmConfig, null, 2)); } else { console.log("Could not fetch farm", reserve.state.farmDebt.toBase58()); } } } } // Download for yvaults all strategy farms const kamino = new kliquidity_sdk_1.Kamino("mainnet-beta", env.provider.connection); let strategies = yield kamino.getAllStrategiesWithFilters({ strategyCreationStatus: "LIVE", }); fs.mkdirSync(`${targetPath}/yvaults/`, { recursive: true }); for (const strategy of strategies) { const farmAddress = strategy.strategy.farm; if (!farmAddress.equals(web3_js_1.PublicKey.default)) { fetchedFarmsForStratsAndReserves.add(farmAddress); const farmState = yield accounts_1.FarmState.fetch(env.provider.connection, farmAddress); if (farmState) { const farmConfig = getFarmConfigType(farmAddress, farmState, { type: "strategy", reserve: undefined, market: undefined, strategy: strategy.address, }); // in case strategy is not set on farm side, we override value so we set on next upsert farmConfig.strategyId = strategy.address; fs.writeFileSync(`${targetPath}/yvaults/${strategy.address.toBase58()}.json`, JSON.stringify(farmConfig, null, 2)); } else { console.log("Could not fetch farm", farmAddress.toBase58()); } } } // Download all standalone farms fs.mkdirSync(`${targetPath}/standaloneFarms/`, { recursive: true }); for (const farmAndKey of allFarms) { // skip farms already downloaded as part of reserves or strategies if (fetchedFarmsForStratsAndReserves.contains(farmAndKey.key)) { continue; } const farmConfig = getFarmConfigType(farmAndKey.key, farmAndKey.farmState, { type: "standalone", reserve: undefined, market: undefined, strategy: undefined, }); fs.writeFileSync(`${targetPath}/standaloneFarms/${farmAndKey.key.toBase58()}.json`, JSON.stringify(farmConfig, null, 2)); } }); } function getMarketsFromApiData(programId) { return __awaiter(this, void 0, void 0, function* () { const markets = []; yield (0, klend_sdk_1.getMarketsFromApi)(programId, "API").then(function (response) { for (const marketData of response) { markets.push({ marketName: marketData.description.replace(" ", "-"), key: new web3_js_1.PublicKey(marketData.lendingMarket), }); } }); return markets; }); } function getRewardType(rewardTypeNumber) { switch (rewardTypeNumber) { case types_1.RewardType.Proportional.discriminator: return types_1.RewardType.Proportional.kind; case types_1.RewardType.Constant.discriminator: return types_1.RewardType.Constant.kind; default: throw new Error(`Invalid reward type: ${rewardTypeNumber}`); } } function getFarmConfigType(farmKey, farmState, farmMetadata) { return { farmMetadata, farmPubkey: farmKey, stakingTokenMint: farmState.token.mint, withdrawAuthority: farmState.withdrawAuthority, globalConfig: farmState.globalConfig, strategyId: farmState.strategyId, // reserve farm depositCapAmount: new decimal_js_1.default(farmState.depositCapAmount.toString()).toNumber(), rewards: farmState.rewardInfos .map((rewardInfo) => { if (!rewardInfo.token.mint.equals(web3_js_1.PublicKey.default)) { return { rewardTokenMint: rewardInfo.token.mint, rewardType: getRewardType(rewardInfo.rewardType), rewardPerSecondDecimals: rewardInfo.rewardsPerSecondDecimals, minClaimDurationSeconds: new decimal_js_1.default(rewardInfo.minClaimDurationSeconds.toString()).toNumber(), rewardCurve: rewardInfo.rewardScheduleCurve.points .map((point) => { if (new decimal_js_1.default(point.rewardPerTimeUnit.toString()).toNumber() !== 0 || point.tsStart.toString() !== klend_sdk_1.U64_MAX) { return { startTs: new decimal_js_1.default(point.tsStart.toString()).toNumber(), rps: new decimal_js_1.default(point.rewardPerTimeUnit.toString()).toNumber(), }; } }) .filter((point) => point !== undefined), rewardAvailable: (0, utils_1.lamportsToCollDecimal)(new decimal_js_1.default(rewardInfo.rewardsAvailable.toString()), rewardInfo.token.decimals.toNumber()) .floor() .toNumber(), rewardToTopUp: 0, rewardToTopUpDurationDays: 0, }; } }) .filter((rewardInfoConfig) => rewardInfoConfig !== undefined), pendingFarmAdmin: farmState.pendingFarmAdmin, scopePrices: farmState.scopePrices, scopePriceOracleId: farmState.scopeOraclePriceId.toString(), scopeOracleMaxAge: new decimal_js_1.default(farmState.scopeOracleMaxAge.toString()).toNumber(), lockingMode: new decimal_js_1.default(farmState.lockingMode.toString()).toNumber(), lockingStart: new decimal_js_1.default(farmState.lockingStartTimestamp.toString()).toNumber(), lockingDuration: new decimal_js_1.default(farmState.lockingDuration.toString()).toNumber(), lockingEarlyWithdrawalPenaltyBps: new decimal_js_1.default(farmState.lockingEarlyWithdrawalPenaltyBps.toString()).toNumber(), depositWarmupPeriod: farmState.depositWarmupPeriod, withdrawCooldownPeriod: farmState.withdrawalCooldownPeriod, slashedAmountSpillAddress: farmState.slashedAmountSpillAddress, }; } //# sourceMappingURL=download_all_farm_configs.js.map