@kaiachain/web3js-ext
Version:
web3.js extension for kaiachain blockchain
92 lines (88 loc) • 4.39 kB
JavaScript
;
/*
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_subscription.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.watchTransactionBySubscription = void 0;
const web3_eth_1 = require("web3-eth");
const web3_utils_1 = require("web3-utils");
const watch_transaction_by_pooling_js_1 = require("./watch_transaction_by_pooling.js");
/**
* This function watches a Transaction by subscribing to new heads.
* It is used by `watchTransactionForConfirmations`, in case the provider supports subscription.
*/
const watchTransactionBySubscription = ({ web3Context, transactionReceipt, transactionPromiEvent, returnFormat, }) => {
// The following variable will stay true except if the data arrived,
// or if watching started after an error had occurred.
let needToWatchLater = true;
let lastCaughtBlockHash;
setImmediate(() => {
web3Context.subscriptionManager
?.subscribe("newHeads")
.then((subscription) => {
subscription.on("data", async (newBlockHeader) => {
needToWatchLater = false;
if (!newBlockHeader?.number ||
// For some cases, the on-data event is fired couple times for the same block!
// This needs investigation but seems to be because of multiple `subscription.on('data'...)` even this should not cause that.
lastCaughtBlockHash === newBlockHeader?.parentHash) {
return;
}
lastCaughtBlockHash = newBlockHeader?.parentHash;
const confirmations = BigInt(newBlockHeader.number) -
BigInt(transactionReceipt.blockNumber) +
BigInt(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" }, newBlockHeader.parentHash, returnFormat),
});
if (confirmations >= web3Context.transactionConfirmationBlocks) {
await web3Context.subscriptionManager?.removeSubscription(subscription);
}
});
subscription.on("error", async () => {
await web3Context.subscriptionManager?.removeSubscription(subscription);
needToWatchLater = false;
(0, watch_transaction_by_pooling_js_1.watchTransactionByPolling)({
web3Context,
transactionReceipt,
transactionPromiEvent,
returnFormat,
});
});
})
.catch(() => {
needToWatchLater = false;
(0, watch_transaction_by_pooling_js_1.watchTransactionByPolling)({
web3Context,
transactionReceipt,
transactionPromiEvent,
returnFormat,
});
});
});
// Fallback to polling if tx receipt didn't arrived in "blockHeaderTimeout" [10 seconds]
setTimeout(() => {
if (needToWatchLater) {
(0, watch_transaction_by_pooling_js_1.watchTransactionByPolling)({
web3Context,
transactionReceipt,
transactionPromiEvent,
returnFormat,
});
}
}, web3Context.blockHeaderTimeout * 1000);
};
exports.watchTransactionBySubscription = watchTransactionBySubscription;