Skip to content

Instantly share code, notes, and snippets.

Last active February 5, 2023 03:26
  • Star 36 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
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(
oneTxHash => () => self.getTransactionReceiptMined(oneTxHash, interval)));
} else if (typeof txHash === "string") {
return transactionReceiptRetry();
} else {
throw new Error("Invalid Type: " + txHash);
Copy link

Copy link

roo2 commented Oct 12, 2017

Hey thanks very much for publishing this, I want to use it in my project but would you please add a default license statement to your profile? Under copyright law we're legally not allowed to use your code without one! A license can be put anywhere public here's an example

Copy link

alexwagg commented Nov 1, 2017

Thank you very much for this. Planning to use this as a work around for faulty truffle event watching behavior. Ok to use this in my code correct? Just wanted to make sure! :)

Copy link

Maybe this is a stupid question that doesn't apply to Ethereum but with this code how would one check that the TX hadn't been orphaned or something like that? Wouldn't it be better to have a minimum confirmation that needs to be satisfied for a TX to be confirmed, after which the promise resolves. Is this detail already handled by the wallet?

Copy link

Hello. Great function! I have implemented a minor improvement to use block limit while searching. Have a look, please.

Copy link

Hello. Great idea, but I got the following problem:

Unhandled Promise rejection: Invalid JSON RPC response: {"id":358,"jsonrpc":"2.0","error":{"code":-32603}} ; Zone: ; Task: Promise.then ; Value: Error: Invalid JSON RPC response: {"id":358,"jsonrpc":"2.0","error":{"code":-32603}}
at Object.InvalidResponse

Copy link

If I do:

await web3.eth.getTransaction( txn_id );

It will wait till it is mined - correct?

Copy link

@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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment