Last active
February 5, 2023 03:26
-
-
Save xavierlepretre/88682e871f4ad07be4534ae560692ee6 to your computer and use it in GitHub Desktop.
Get the Promise of an Ethereum transaction receipt when it is finally mined
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Promise = require("bluebird"); | |
const sequentialPromise = require("./sequentialPromise.js"); | |
/** | |
* @param {!string | !Array.<!string>} txHash, a transaction hash or an array of transaction hashes. | |
* @param {Number} interval, in seconds. | |
* @returns {!Promise.<!object> | !Promise.<!Array.<!object>>} the receipt or an array of receipts. | |
*/ | |
module.exports = function getTransactionReceiptMined(txHash, interval) { | |
const self = this; | |
const transactionReceiptRetry = () => self.getTransactionReceipt(txHash) | |
.then(receipt => receipt != null | |
? receipt | |
: Promise.delay(interval ? interval : 500).then(transactionReceiptRetry)); | |
if (Array.isArray(txHash)) { | |
return sequentialPromise(txHash.map( | |
oneTxHash => () => self.getTransactionReceiptMined(oneTxHash, interval))); | |
} else if (typeof txHash === "string") { | |
return transactionReceiptRetry(); | |
} else { | |
throw new Error("Invalid Type: " + txHash); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@prographo, No, it will not wait till it is mined. If it is not mined yet, some of the fields (such as 'blockNumber') of the resolved transaction object would be null. See the document here: https://web3js.readthedocs.io/en/1.0/web3-eth.html#gettransaction