Trying to parse the transaction result, for example the exact gas used by a transaction. How can I do it in javascript?

The below is not working btw, any idea?

return metaInstance.deposit({ from: accounts[0], value: web3.toWei(5, "ether") }).then(function (tx) {
  var rcpt = web3.eth.getTransactionReceipt(tx).then(function (rcpt) {

Your example doesn't work because the transaction hasn't been mined yet by the time you call getTransactionReceipt(tx). The transaction hash is known before mining, while the receipt only after. If you want to wait for the transaction to be mined and then get the receipt, use web3 1.0 or truffle-contract. Both provide nice wrappers that resolve a promise with the transaction receipt upon mining of the tx. (Although still in beta I recommend web3).


If you're using truffle already, the above is not the problem but you're getting confused with the js promise i think. Try:

     return metaInstance.deposit({ from: accounts[0], value: web3.toWei(5, "ether") }).then(function (tx) {
       return web3.eth.getTransactionReceipt(tx);
     }).then((_rcpt) => {
| improve this answer | |
  • I thought in truffle unit tests, it is using instant mining! Is it not the case? Why I'm asking is all other steps underneath it depending on this transaction succeeds, including a check balance. – Jimson Kannanthara James Mar 6 '18 at 3:33
  • yup, this was from a truffle unit test. btw, the issue was some mis-understanding about the params. – Jimson Kannanthara James Mar 6 '18 at 18:55

The transaction response is not the hash, hash needs to be taken from the response. See below.

btw, this works only in truffle test cases, in realworld scenarios, like @theo-port said, we need to wait for the mining to complete to get the transaction receipt.

      return metaInstance.deposit({ from: accounts[0], value: web3.toWei(5, "ether") }).then(function (resp) {
        var rcpt = web3.eth.getTransactionReceipt(resp.tx);

        console.log("cumulativeGasUsed: " + rcpt.cumulativeGasUsed);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.