UNPKG

@kaiachain/web3js-ext

Version:
51 lines (47 loc) 2.66 kB
"use strict"; /* This file is part of web3.js. web3.js is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. web3.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see <http://www.gnu.org/licenses/>. */ // Taken from https://github.com/web3/web3.js/blob/v4.3.0/packages/web3-eth/src/utils/try_send_transaction.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.trySendTransaction = trySendTransaction; const web3_errors_1 = require("web3-errors"); const web3_utils_1 = require("web3-utils"); // eslint-disable-next-line import/no-cycle const reject_if_block_timeout_js_1 = require("./reject_if_block_timeout.js"); /** * An internal function to send a transaction or throws if sending did not finish during the timeout during the blocks-timeout. * @param web3Context - the context to read the configurations from * @param sendTransactionFunc - the function that will send the transaction (could be sendTransaction or sendRawTransaction) * @param transactionHash - to be used inside the exception message if there will be any exceptions. * @returns the Promise<string> returned by the `sendTransactionFunc`. */ async function trySendTransaction(web3Context, sendTransactionFunc, transactionHash) { const [timeoutId, rejectOnTimeout] = (0, web3_utils_1.rejectIfTimeout)(web3Context.transactionSendTimeout, new web3_errors_1.TransactionSendTimeoutError({ numberOfSeconds: web3Context.transactionSendTimeout / 1000, transactionHash, })); const [rejectOnBlockTimeout, blockTimeoutResourceCleaner] = await (0, reject_if_block_timeout_js_1.rejectIfBlockTimeout)(web3Context, transactionHash); try { // If an error happened here, do not catch it, just clear the resources before raising it to the caller function. return await Promise.race([ sendTransactionFunc(), // this is the function that will send the transaction rejectOnTimeout, // this will throw an error on Transaction Send Timeout rejectOnBlockTimeout, // this will throw an error on Transaction Block Timeout ]); } finally { clearTimeout(timeoutId); blockTimeoutResourceCleaner.clean(); } }