Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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(txHash.map(
oneTxHash => () => self.getTransactionReceiptMined(oneTxHash, interval)));
} else if (typeof txHash === "string") {
return transactionReceiptRetry();
} else {
throw new Error("Invalid Type: " + txHash);
}
};
@xavierlepretre

This comment has been minimized.

Copy link
Owner Author

commented Aug 19, 2016

sequentialPromise is found here: https://gist.github.com/xavierlepretre/0e24b1dc300e5aaea20c87e3d3039d29
Example of use in a test:

  web3.eth.getTransactionReceiptMined = require("getTransactionReceiptMined.js");

  it("should transfer coins", function() {

    let meta;
    const account_one = accounts[0];
    const account_two = accounts[1];

    return MetaCoin.deployed()
      .then(function (instance) {
      	meta = instance;
        return meta.sendCoin.call(account_two, 6, { from: account_one });
      })
      .then(function (sufficient) {
        if (!sufficient) {
          throw "Insufficient coins";
        }
        return meta.sendCoin.sendTransaction(account_two, 6, { from: account_one });
      })
      .then(function (txHash) {
        return web3.eth.getTransactionReceiptMined(txHash);
      })
      .then(function (receipt) {
        assert.strictEqual(receipt.logs.length, 1, "Should have emitted an event");
        return meta.getBalance.call(account_two);
      })
      .then(function (balance) {
        assert.strictEqual(balance.toNumber(), 6, "Should have received 6 coins");
      });

  });
@xavierlepretre

This comment has been minimized.

Copy link
Owner Author

commented Oct 6, 2016

Or with arrays:

web3.eth.getTransactionReceiptMined = require("getTransactionReceiptMined.js");

it("should transfer coins", function() {

  let meta;
  const account_one = accounts[0];
  const account_two = accounts[1];
  const account_three = accounts[2];

  return MetaCoin.deployed()
    .then(function(instance) {
      meta = instance;
      return meta.sendCoin.call(account_two, 6, { from: account_one });
    })
    .then(function (sufficient) {
      if (!sufficient) {
        throw "Insufficient coins";
      }
      return Promise.all([
        meta.sendCoin.sendTransaction(account_two, 6, { from: account_one }),
        meta.sendCoin.sendTransaction(account_three, 4, { from: account_one })
      ]);
    })
    .then(function (txHashes) {
      return web3.eth.getTransactionReceiptMined(txHashes);
    })
    .then(function (receipts) {
      assert.strictEqual(receipts[0].logs.length, 1, "Should have emitted an event");
      assert.strictEqual(receipts[1].logs.length, 1, "Should have emitted an event");
      return meta.getBalance.call(account_two);
    })
    .then(function (balance) {
      assert.equal(balance, 6, "Should have received 6 coins");
    });

});
@Physes

This comment has been minimized.

Copy link

commented Jun 21, 2017

I'm curious why this begins with web3.eth.getTransactionReceiptMined. getTransactionReceiptMined as far as I know is not a web3 method. Can you explain this?

@xavierlepretre

This comment has been minimized.

Copy link
Owner Author

commented Jul 17, 2017

It does not exist. That's why we add it to web3.eth. The purpose of this gist is to add this function under this name.

@xavierlepretre

This comment has been minimized.

@roo2

This comment has been minimized.

Copy link

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 https://gist.github.com/rwaldron/1056460

@alexwagg

This comment has been minimized.

Copy link

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! :)

@robertsdotpm

This comment has been minimized.

Copy link

commented Mar 22, 2018

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?

@VanijaDev

This comment has been minimized.

Copy link

commented Apr 19, 2018

Hello. Great function! I have implemented a minor improvement to use block limit while searching. Have a look, please.
https://gist.github.com/VanijaDev/94481514a8f677ad7e27bb0edb8f0895

@nogueiradalmeida

This comment has been minimized.

Copy link

commented May 17, 2018

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

@prographo

This comment has been minimized.

Copy link

commented Feb 7, 2019

If I do:

await web3.eth.getTransaction( txn_id );

It will wait till it is mined - correct?

@perfectmak

This comment has been minimized.

Copy link

commented May 10, 2019

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.