UNPKG

@kamino-finance/farms-sdk

Version:
122 lines 5.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendTransactionFromAction = exports.LENDING_LUT = void 0; exports.refreshAllKlendObligationFarmsFromFileCommand = refreshAllKlendObligationFarmsFromFileCommand; exports.sendAndConfirmTransactionV0 = sendAndConfirmTransactionV0; exports.confirmTx = confirmTx; const klend_sdk_1 = require("@kamino-finance/klend-sdk"); const web3_js_1 = require("@solana/web3.js"); const utils_1 = require("./utils"); const utils_2 = require("../utils"); exports.LENDING_LUT = new web3_js_1.PublicKey("284iwGtA9X9aLy3KsyV8uT2pXLARhYbiSi5SiM2g47M2"); async function refreshAllKlendObligationFarmsFromFileCommand(market, file) { const admin = process.env.ADMIN; const rpc = process.env.RPC; const klendProgramId = new web3_js_1.PublicKey(process.env.KLEND_PROGRAM_ID || ""); const env = (0, utils_1.initializeClient)(rpc, admin, (0, utils_2.getFarmsProgramId)(rpc), false); const c = env.provider.connection; const lendingMarket = new web3_js_1.PublicKey(market); const kaminoMarket = await klend_sdk_1.KaminoMarket.load(c, lendingMarket, 450, klendProgramId); if (!kaminoMarket) { throw new Error("Kamino market not found"); } const fs = require("fs"); const rawData = fs.readFileSync(file, "utf8"); const obligationAddresses = JSON.parse(rawData); let startAt = 0; let stopAt = obligationAddresses.length; let maxConcurrent = 30; let promises = []; let lut = (await c.getAddressLookupTable(exports.LENDING_LUT)).value; for (let i = startAt; i < stopAt; i++) { const obligationAddress = new web3_js_1.PublicKey(obligationAddresses[i]); // console.log("Obligation", obligationAddress.toString()); const obligation = await kaminoMarket.getObligationByAddress(obligationAddress); const kaminoAction = buildRefreshObligationTxns(kaminoMarket, env.initialOwner.publicKey, obligationAddress); promises.push(executeWithoutAwait(env, obligationAddress, i, kaminoAction, lut, obligationAddresses.length)); if (promises.length >= maxConcurrent || i === obligationAddresses.length - 1 || i === stopAt - 1) { await Promise.all(promises); promises = []; } } await (0, klend_sdk_1.sleep)(10); } const executeWithoutAwait = async (env, obligation, i, kaminoAction, lut, len) => { let numRetries = 5; let retry = 0; while (retry < numRetries) { try { const action = await kaminoAction; const sig = await (0, exports.sendTransactionFromAction)(env.provider.connection, action, env.initialOwner, [lut], "Refreshed Obligation " + i + " " + obligation.toString() + " out of " + len); return sig; } catch (e) { console.log("Obligation error", i, obligation.toString(), e); retry += 1; await (0, klend_sdk_1.sleep)(1000); } } return null; }; const sendTransactionFromAction = async (c, kaminoAction, liquidator, lookupTables, withDescription = "") => { const ixs = [ ...kaminoAction.setupIxs, ...kaminoAction.lendingIxs, ...kaminoAction.cleanupIxs, ]; return sendAndConfirmTransactionV0(c, liquidator, ixs, lookupTables, [], withDescription, (str) => console.log(str)); }; exports.sendTransactionFromAction = sendTransactionFromAction; async function sendAndConfirmTransactionV0(c, payer, instructions, lookupTables, signers, withDescription = "", logger = console.log) { const blockhash = await c.getLatestBlockhash("confirmed"); const messageV0 = new web3_js_1.TransactionMessage({ payerKey: payer.publicKey, recentBlockhash: blockhash.blockhash, instructions, }).compileToV0Message(lookupTables); const tx = new web3_js_1.VersionedTransaction(messageV0); tx.sign([payer, ...signers]); let sig; try { sig = await c.sendTransaction(tx, { preflightCommitment: "processed", skipPreflight: true, }); logger(`Transaction Hash: ${withDescription} ${sig}`); } catch (e) { logger(e); } const status = await confirmTx(c, sig, blockhash); return sig; } async function confirmTx(connection, txHash, txnBlockhash) { return connection.confirmTransaction({ blockhash: txnBlockhash.blockhash, lastValidBlockHeight: txnBlockhash.lastValidBlockHeight, signature: txHash, }, "confirmed"); } async function buildRefreshObligationTxns(kaminoMarket, payer, obligation) { const kaminoObligation = await klend_sdk_1.KaminoObligation.load(kaminoMarket, obligation); if (!kaminoObligation) { throw new Error(`Obligation ${obligation.toBase58()} not found`); } const firstReserve = kaminoObligation.state.deposits.find((d) => !d.depositReserve.equals(web3_js_1.PublicKey.default)).depositReserve; const firstKaminoReserve = kaminoMarket.getReserveByAddress(firstReserve); if (!firstKaminoReserve) { throw new Error(`Reserve ${firstReserve.toBase58()} not found`); } const obligationType = (0, klend_sdk_1.getObligationTypeFromObligation)(kaminoMarket, kaminoObligation); const axn = await klend_sdk_1.KaminoAction.initialize("refreshObligation", "0", firstKaminoReserve?.getLiquidityMint(), kaminoObligation.state.owner, kaminoMarket, obligationType, kaminoMarket.programId); axn.addRefreshObligation(payer); return axn; } //# sourceMappingURL=refresh_all_klend_user_obligation_farms_from_file.js.map