UNPKG

@kaiachain/web3js-ext

Version:
65 lines (61 loc) 3.31 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_for_confirmations.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.watchTransactionForConfirmations = void 0; const web3_errors_1 = require("web3-errors"); const web3_eth_1 = require("web3-eth"); const web3_utils_1 = require("web3-utils"); const web3_validator_1 = require("web3-validator"); const watch_transaction_by_pooling_js_1 = require("./watch_transaction_by_pooling.js"); const watch_transaction_by_subscription_js_1 = require("./watch_transaction_by_subscription.js"); function watchTransactionForConfirmations(web3Context, transactionPromiEvent, transactionReceipt, transactionHash, returnFormat) { if ((0, web3_validator_1.isNullish)(transactionReceipt) || (0, web3_validator_1.isNullish)(transactionReceipt.blockHash)) { throw new web3_errors_1.TransactionMissingReceiptOrBlockHashError({ receipt: transactionReceipt, blockHash: (0, web3_utils_1.format)({ format: "bytes32" }, transactionReceipt?.blockHash, returnFormat), transactionHash: (0, web3_utils_1.format)({ format: "bytes32" }, transactionHash, returnFormat), }); } if (!transactionReceipt.blockNumber) { throw new web3_errors_1.TransactionReceiptMissingBlockNumberError({ receipt: transactionReceipt }); } // As we have the receipt, it's the first confirmation that tx is accepted. // @ts-ignore: web3.js has the same error transactionPromiEvent.emit("confirmation", { confirmations: (0, web3_utils_1.format)({ format: "uint" }, 1, returnFormat), receipt: (0, web3_utils_1.format)(web3_eth_1.transactionReceiptSchema, transactionReceipt, returnFormat), latestBlockHash: (0, web3_utils_1.format)({ format: "bytes32" }, transactionReceipt.blockHash, returnFormat), }); // so a subscription for newBlockHeaders can be made instead of polling const provider = web3Context.requestManager.provider; if (provider && "supportsSubscriptions" in provider && provider.supportsSubscriptions()) { (0, watch_transaction_by_subscription_js_1.watchTransactionBySubscription)({ web3Context, transactionReceipt, transactionPromiEvent, returnFormat, }); } else { (0, watch_transaction_by_pooling_js_1.watchTransactionByPolling)({ web3Context, transactionReceipt, transactionPromiEvent, returnFormat, }); } } exports.watchTransactionForConfirmations = watchTransactionForConfirmations;