@hubbleprotocol/farms-sdk
Version:
139 lines • 7.21 kB
JavaScript
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.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");
function refreshAllKlendObligationFarmsFromFileCommand(market, file) {
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 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 = yield 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 = (yield 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 = yield 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) {
yield Promise.all(promises);
promises = [];
}
}
yield (0, klend_sdk_1.sleep)(10);
});
}
const executeWithoutAwait = (env, obligation, i, kaminoAction, lut, len) => __awaiter(void 0, void 0, void 0, function* () {
let numRetries = 5;
let retry = 0;
while (retry < numRetries) {
try {
const action = yield kaminoAction;
const sig = yield (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;
yield (0, klend_sdk_1.sleep)(1000);
}
}
return null;
});
const sendTransactionFromAction = (c_1, kaminoAction_1, liquidator_1, lookupTables_1, ...args_1) => __awaiter(void 0, [c_1, kaminoAction_1, liquidator_1, lookupTables_1, ...args_1], void 0, function* (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;
function sendAndConfirmTransactionV0(c_1, payer_1, instructions_1, lookupTables_1, signers_1) {
return __awaiter(this, arguments, void 0, function* (c, payer, instructions, lookupTables, signers, withDescription = "", logger = console.log) {
const blockhash = yield 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 = yield c.sendTransaction(tx, {
preflightCommitment: "processed",
skipPreflight: true,
});
logger(`Transaction Hash: ${withDescription} ${sig}`);
}
catch (e) {
logger(e);
}
const status = yield confirmTx(c, sig, blockhash);
return sig;
});
}
function confirmTx(connection, txHash, txnBlockhash) {
return __awaiter(this, void 0, void 0, function* () {
return connection.confirmTransaction({
blockhash: txnBlockhash.blockhash,
lastValidBlockHeight: txnBlockhash.lastValidBlockHeight,
signature: txHash,
}, "confirmed");
});
}
function buildRefreshObligationTxns(kaminoMarket, payer, obligation) {
return __awaiter(this, void 0, void 0, function* () {
const kaminoObligation = yield 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 = yield klend_sdk_1.KaminoAction.initialize("refreshObligation", "0", firstKaminoReserve === null || firstKaminoReserve === void 0 ? void 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
;