@kamino-finance/klend-sdk
Version:
Typescript SDK for interacting with the Kamino Lending (klend) protocol
154 lines • 5.5 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Web3Client = void 0;
exports.signSendAndConfirmRawTransactionWithRetry = signSendAndConfirmRawTransactionWithRetry;
exports.sendAndConfirmRawTransactionWithRetry = sendAndConfirmRawTransactionWithRetry;
exports.isVersionedTransaction = isVersionedTransaction;
const web3_js_1 = require("@solana/web3.js");
const bs58_1 = __importDefault(require("bs58"));
class Web3Client {
_connection;
_sendConnection;
_sendConnectionsExtra;
_endpoint;
_env;
_chain;
constructor(endpoint) {
let chain;
if (typeof endpoint === 'string') {
if (chain === undefined) {
throw Error(`Invalid environment - ${endpoint}`);
}
}
else {
chain = endpoint;
}
this._chain = chain;
this._endpoint = chain.endpoint;
this._env = chain.name;
// use this connection to get data
this._connection = new web3_js_1.Connection(this._endpoint, {
commitment: 'recent',
wsEndpoint: chain.wsEndpoint,
confirmTransactionInitialTimeout: 120 * 1000,
});
// use this one to submit transactions
this._sendConnection = new web3_js_1.Connection(this._endpoint, {
commitment: 'confirmed',
wsEndpoint: chain.wsEndpoint,
confirmTransactionInitialTimeout: 120 * 1000,
});
this._sendConnectionsExtra = [this._sendConnection];
}
get endpoint() {
return this._endpoint;
}
get chain() {
return this._chain;
}
get env() {
return this._env;
}
get connection() {
return this._connection;
}
get sendConnection() {
return this._sendConnection;
}
get sendConnectionsExtra() {
return this._sendConnectionsExtra;
}
}
exports.Web3Client = Web3Client;
async function signSendAndConfirmRawTransactionWithRetry({ mainConnection, extraConnections = [], tx, signers, commitment = 'confirmed', sendTransactionOptions, }) {
tx.sign(signers);
return sendAndConfirmRawTransactionWithRetry({
mainConnection,
extraConnections,
tx,
commitment,
sendTransactionOptions,
});
}
async function sendAndConfirmRawTransactionWithRetry({ mainConnection, extraConnections = [], tx, commitment = 'confirmed', sendTransactionOptions, }) {
const signature = isVersionedTransaction(tx) ? bs58_1.default.encode(tx.signatures[0]) : tx.signatures?.toString();
console.log('Signature attempted: ', signature);
let confirmed = false;
const serialized = Buffer.from(tx.serialize());
const latestBlockHashAndContext = await mainConnection.getLatestBlockhashAndContext(commitment);
const defaultOptions = {
skipPreflight: true,
maxRetries: 0,
preflightCommitment: commitment,
};
if (!signature) {
throw new Error('Transaction is not signed. Refresh the page and try again');
}
// Listen for transaction confirmation
const waitForConfirmation = async (sig) => {
try {
const res = await mainConnection.confirmTransaction({
blockhash: latestBlockHashAndContext.value.blockhash,
lastValidBlockHeight: latestBlockHashAndContext.value.lastValidBlockHeight,
minContextSlot: latestBlockHashAndContext.context.slot,
signature: sig,
}, commitment);
confirmed = true;
return res;
}
catch (error) {
console.log(error);
return null;
}
};
// Send transaction and set interval to resend every X seconds
const sendTransaction = () => {
if (confirmed) {
return;
}
try {
mainConnection.sendRawTransaction(serialized, {
...defaultOptions,
...sendTransactionOptions,
minContextSlot: latestBlockHashAndContext.context.slot,
});
extraConnections.forEach((conn) => {
conn.sendRawTransaction(serialized, {
...defaultOptions,
...sendTransactionOptions,
minContextSlot: latestBlockHashAndContext.context.slot,
});
});
}
catch (error) {
console.log(error);
}
};
sendTransaction();
const res = await waitForConfirmation(signature);
if (res && res.value && res.value.err) {
const txDetails = await mainConnection.getTransaction(signature, {
maxSupportedTransactionVersion: 0,
commitment: 'confirmed',
});
if (txDetails) {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw {
err: txDetails.meta?.err,
logs: txDetails.meta?.logMessages || [],
signature,
tx,
};
}
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw { err: res.value.err, msg: res.value.err, signature, tx };
}
return signature;
}
function isVersionedTransaction(transaction) {
return 'version' in transaction;
}
//# sourceMappingURL=sendTransactionsUtils.js.map
;