UNPKG

@kaiachain/web3js-ext

Version:
51 lines (47 loc) 2.73 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/watch_transaction_by_pooling.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.watchTransactionByPolling = void 0; const web3_eth_1 = require("web3-eth"); const web3_rpc_methods_1 = require("web3-rpc-methods"); const web3_utils_1 = require("web3-utils"); /** * This function watches a Transaction by subscribing to new heads. * It is used by `watchTransactionForConfirmations`, in case the provider does not support subscription. * And it is also used by `watchTransactionBySubscription`, as a fallback, if the subscription failed for any reason. */ const watchTransactionByPolling = ({ web3Context, transactionReceipt, transactionPromiEvent, returnFormat, }) => { // Having a transactionReceipt means that the transaction has already been included // in at least one block, so we start with 1 let confirmations = 1; const intervalId = setInterval(() => { (async () => { if (confirmations >= web3Context.transactionConfirmationBlocks) { clearInterval(intervalId); } const nextBlock = await web3_rpc_methods_1.ethRpcMethods.getBlockByNumber(web3Context.requestManager, (0, web3_utils_1.numberToHex)(BigInt(transactionReceipt.blockNumber) + BigInt(confirmations)), false); if (nextBlock?.hash) { confirmations += 1; transactionPromiEvent.emit("confirmation", { confirmations: (0, web3_utils_1.format)({ format: "uint" }, confirmations, returnFormat), receipt: (0, web3_utils_1.format)(web3_eth_1.transactionReceiptSchema, transactionReceipt, returnFormat), latestBlockHash: (0, web3_utils_1.format)({ format: "bytes32" }, nextBlock.hash, returnFormat), }); } })(); }, web3Context.transactionReceiptPollingInterval ?? web3Context.transactionPollingInterval); }; exports.watchTransactionByPolling = watchTransactionByPolling;