@hubbleprotocol/farms-sdk
Version:
72 lines • 4.61 kB
JavaScript
"use strict";
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeKlendFarmForReserve = initializeKlendFarmForReserve;
const web3_js_1 = require("@solana/web3.js");
const klend_sdk_1 = require("@kamino-finance/klend-sdk");
const types_1 = require("@kamino-finance/klend-sdk/dist/idl_codegen/types");
const utils_1 = require("./utils");
const utils_2 = require("../utils");
const Farms_1 = require("../Farms");
const types_2 = require("../rpc_client/types");
function initializeKlendFarmForReserve(reserve, kind, multisigToExecute, pendingAdmin, mode, priorityFeeMultiplier) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const admin = process.env.ADMIN;
const rpc = process.env.RPC;
const klendProgramId = new web3_js_1.PublicKey(process.env.KLEND_PROGRAM_ID);
const farmsId = (0, utils_2.getFarmsProgramId)(rpc);
const env = (0, utils_1.initializeClient)(rpc, admin, farmsId, mode === "multisig");
if (mode === "multisig" && !multisigToExecute) {
throw new Error("Cannot initailize Klend farm without as multisig without multisig pubkey");
}
const globalConfig = new web3_js_1.PublicKey(process.env.FARMS_GLOBAL_CONFIG);
const farmClient = new Farms_1.Farms(env.provider.connection);
const reserveState = yield klend_sdk_1.Reserve.fetch(env.provider.connection, reserve, klendProgramId);
if (!reserveState) {
throw new Error("Reserve does not exist");
}
const [lendingMarketAuthority, _] = (0, klend_sdk_1.lendingMarketAuthPda)(reserveState.lendingMarket, klendProgramId);
const lendingMarketOwner = (_a = (yield klend_sdk_1.LendingMarket.fetch(env.provider.connection, reserveState.lendingMarket, klendProgramId))) === null || _a === void 0 ? void 0 : _a.lendingMarketOwner;
const SIZE_FARM_STATE = 8336;
const farmState = web3_js_1.Keypair.generate();
const createFarmIx = web3_js_1.SystemProgram.createAccount({
fromPubkey: env.initialOwner.publicKey,
newAccountPubkey: farmState.publicKey,
space: SIZE_FARM_STATE,
lamports: yield env.provider.connection.getMinimumBalanceForRentExemption(SIZE_FARM_STATE),
programId: farmsId,
});
if (mode !== "simulate") {
const createFarmSig = yield farmClient.processTxn(env.initialOwner, [createFarmIx], "execute", priorityFeeMultiplier, "", [farmState], env.web3Client);
mode !== "multisig" &&
console.log("Transaction signature: " + createFarmSig);
}
const initIx = (0, klend_sdk_1.initFarmsForReserve)({
mode: types_1.ReserveFarmKind.fromDecoded({ [kind]: "" }).discriminator,
}, {
lendingMarketOwner,
lendingMarket: reserveState.lendingMarket,
lendingMarketAuthority,
reserve,
farmsProgram: farmsId,
farmsGlobalConfig: new web3_js_1.PublicKey(globalConfig),
farmState: farmState.publicKey,
farmsVaultAuthority: (0, utils_2.getFarmAuthorityPDA)(farmsId, farmState.publicKey),
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
systemProgram: web3_js_1.SystemProgram.programId,
}, klendProgramId);
const updateFarmPendingAdminIx = yield farmClient.updateFarmConfigIx(mode === "multisig" ? multisigToExecute : env.initialOwner.publicKey, farmState.publicKey, web3_js_1.PublicKey.default, new types_2.FarmConfigOption.UpdatePendingFarmAdmin(), pendingAdmin, -1, (0, utils_2.getFarmsProgramId)("mainnet"), true);
const sig = yield farmClient.processTxn(env.initialOwner, [initIx, updateFarmPendingAdminIx], mode, priorityFeeMultiplier, "", [], env.web3Client);
mode !== "multisig" && console.log("Transaction signature: " + sig);
});
}
//# sourceMappingURL=init_klend_farm.js.map