Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save VanijaDev/94481514a8f677ad7e27bb0edb8f0895 to your computer and use it in GitHub Desktop.
Save VanijaDev/94481514a8f677ad7e27bb0edb8f0895 to your computer and use it in GitHub Desktop.
Get the Promise of an Ethereum transaction receipt when it is finally mined
module.exports = function getTransactionReceiptMined(txHash, interval, blockLimit) {
const self = this;
var count = 0;
var blocks = blockLimit;
const transactionReceiptAsync = function (resolve, reject) {
if (count > blocks) {
reject('Contract transaction couldn\'t be found after ', blocks, ' blocks');
return;
}
self.getTransactionReceipt(txHash, (error, receipt) => {
if (error) {
reject(error);
} else if (receipt == null) {
setTimeout(
() => transactionReceiptAsync(resolve, reject),
interval ? interval : 500);
} else {
resolve(receipt);
}
});
count++;
};
if (Array.isArray(txHash)) {
return Promise.all(txHash.map(
oneTxHash => self.getTransactionReceiptMined(oneTxHash, interval)));
} else if (typeof txHash === "string") {
return new Promise(transactionReceiptAsync);
} 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