transactionMined.js 910 B

12345678910111213141516171819202122232425262728293031323334
  1. // From https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
  2. function transactionMined (txnHash, interval) {
  3. interval = interval || 500;
  4. const transactionReceiptAsync = function (txnHash, resolve, reject) {
  5. try {
  6. const receipt = web3.eth.getTransactionReceipt(txnHash);
  7. if (receipt === null) {
  8. setTimeout(function () {
  9. transactionReceiptAsync(txnHash, resolve, reject);
  10. }, interval);
  11. } else {
  12. resolve(receipt);
  13. }
  14. } catch (e) {
  15. reject(e);
  16. }
  17. };
  18. if (Array.isArray(txnHash)) {
  19. return Promise.all(txnHash.map(hash =>
  20. web3.eth.getTransactionReceiptMined(hash, interval)
  21. ));
  22. } else {
  23. return new Promise(function (resolve, reject) {
  24. transactionReceiptAsync(txnHash, resolve, reject);
  25. });
  26. }
  27. }
  28. web3.eth.transactionMined = transactionMined;
  29. module.exports = {
  30. transactionMined,
  31. };