Forked from xavierlepretre/getTransactionReceiptMined.js
Created
April 19, 2018 12:16
-
-
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
This file contains hidden or 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
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