@hubbleprotocol/farms-sdk
Version:
171 lines • 6.99 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());
});
};
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 {
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,
});
if (chain.name !== "localnet") {
this._sendConnectionsExtra = [
new web3_js_1.Connection(process.env.IRONFORGE_CLUSTER, {
commitment: "confirmed",
wsEndpoint: chain.wsEndpoint,
confirmTransactionInitialTimeout: 120 * 1000,
}),
];
}
else {
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;
const RETRY_INTERVAL = 2000;
function signSendAndConfirmRawTransactionWithRetry(_a) {
return __awaiter(this, arguments, void 0, function* ({ mainConnection, extraConnections = [], tx, signers, commitment = "confirmed", sendTransactionOptions, }) {
tx.sign(signers);
return sendAndConfirmRawTransactionWithRetry({
mainConnection,
extraConnections,
tx,
commitment,
sendTransactionOptions,
});
});
}
function sendAndConfirmRawTransactionWithRetry(_a) {
return __awaiter(this, arguments, void 0, function* ({ mainConnection, extraConnections = [], tx, commitment = "confirmed", sendTransactionOptions, }) {
var _b, _c, _d;
const signature = isVersionedTransaction(tx)
? bs58_1.default.encode(tx.signatures[0])
: (_b = tx.signatures) === null || _b === void 0 ? void 0 : _b.toString();
console.log("Signature attempted: ", signature);
let intervalId;
let confirmed = false;
const serialized = Buffer.from(tx.serialize());
const latestBlockHashAndContext = yield 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 = (sig) => __awaiter(this, void 0, void 0, function* () {
try {
const res = yield mainConnection.confirmTransaction({
blockhash: latestBlockHashAndContext.value.blockhash,
lastValidBlockHeight: latestBlockHashAndContext.value.lastValidBlockHeight,
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, Object.assign(Object.assign({}, defaultOptions), sendTransactionOptions));
extraConnections.forEach((conn) => {
conn.sendRawTransaction(serialized, Object.assign(Object.assign({}, defaultOptions), sendTransactionOptions));
});
}
catch (error) {
console.log(error);
}
};
sendTransaction();
intervalId = setInterval(() => {
sendTransaction();
}, RETRY_INTERVAL);
const res = yield waitForConfirmation(signature);
if (res && res.value && res.value.err) {
const txDetails = yield mainConnection.getTransaction(signature, {
maxSupportedTransactionVersion: 0,
commitment: "confirmed",
});
if (txDetails) {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw {
err: (_c = txDetails.meta) === null || _c === void 0 ? void 0 : _c.err,
logs: ((_d = txDetails.meta) === null || _d === void 0 ? void 0 : _d.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
;