drizzle icon indicating copy to clipboard operation
drizzle copied to clipboard

cacheSend() doesn't catch MetaMask exceptions on invalid transactions

Open samajammin opened this issue 5 years ago • 8 comments

I'm trying to catch & display errors on invalid transaction inputs in my Drizzle project. When clicking to confirm an invalid transaction in the MetaMask window, MetaMack triggers a console error with the RPC payload & my contract code's revert statement in the error.message.

Using cacheSend, the MetaMask exception logs to the browser console but isn't caught in my code:

handleSubmit = async () => {
  try {
    console.log('attempting transaction...');
    const method = this.contract.methods[this.props.method];
    await method.cacheSend(...Object.values(this.state), {
      from: this.props.account
    });
  } catch (e) {
    console.log('This never logs...');
  }
};
image 2019-01-30 at 11 04 14 pm

But if I use the web3 send, it DOES catch the error:

handleSubmit = async () => {
  try {
    console.log('attempting transaction...');
    const method = this.contract.methods[this.props.method];
    await method(...Object.values(this.state)).send({
      from: this.props.account
    });
  } catch (e) {
    console.log('This logs! Now I can explain to the user why it failed.');
  }
};
image 2019-01-30 at 11 03 07 pm

It appears this MetaMask error is logging before the failed transaction hash is added into Drizzle's state, so I can't access the error there. Perhaps I don't fully grok the cacheSend method? How should I properly catch transaction errors with Drizzle?

Thanks!

samajammin avatar Jan 31 '19 07:01 samajammin

Hi @sbrichards, we've made some changes to the transactionStack which will probably help here. See the docs here, but the TL;DR is that a temporary ID will be pushed to transactionStack, which you can then use to access the error in the transactions object (using the temporary ID).

OnlyOneJMJQ avatar Feb 22 '19 16:02 OnlyOneJMJQ

@sbrichards Is this still an issue for you?

cacheSend uses promiEvents under the hood to observe the multiple stages of a transaction. When an error is detected its message is recorded to the transactions sub-tree of redux state. PromiEvent will catch and present an exception through the error listener, unlike web3 send.

TX_ERROR

cds-amal avatar Mar 15 '19 14:03 cds-amal

Thanks for the update @cds-amal - I will take a look ASAP, might be a week or two.

samajammin avatar Mar 18 '19 23:03 samajammin

@sbrichards Did you found the solution because I have the same problem

lundrimazemi avatar Apr 18 '19 02:04 lundrimazemi

@DiscRiskandBisque

Consider the following: You run a contract with invalid parameters, causing the Ethereum VM to throw an error. Gas is spent, and you receive a transaction hash.

Drizzle registers this transaction as successful, even though it failed!

As you can see from Etherscan, this transaction clearly failed https://ropsten.etherscan.io/tx/0xd73f7c608d041ada2b27e094e682b53e119c7a5b1d0c38b71273a465d22692ad

// But the transaction is registered as successful in drizzleState
drizzleState.transactions['0xd73f7c608d041ada2b27e094e682b53e119c7a5b1d0c38b71273a465d22692ad']

{
  "status": "success",
  "confirmations": [
    {
      "blockHash": "0x8bbb5212640465d5cdb53943c615fb970d2f90eb017575b922f028c1c44273d0",
      "blockNumber": 5727902,
      "contractAddress": null,
      "cumulativeGasUsed": 170757,
      "from": "0x8f9359c5f31c067eefb4569b938f97851bd6ea96",
      "gasUsed": 23757,
      "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
      "status": "0x0",
      "to": "0xc47d9b2a41f775e9588c1dfb18d839212e8e83b5",
      "transactionHash": "0xd73f7c608d041ada2b27e094e682b53e119c7a5b1d0c38b71273a465d22692ad",
      "transactionIndex": 7,
      "events": {}
    }
  ],
  "receipt": {
    "blockHash": "0x8bbb5212640465d5cdb53943c615fb970d2f90eb017575b922f028c1c44273d0",
    "blockNumber": 5727902,
    "contractAddress": null,
    "cumulativeGasUsed": 170757,
    "from": "0x8f9359c5f31c067eefb4569b938f97851bd6ea96",
    "gasUsed": 23757,
    "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "status": "0x0",
    "to": "0xc47d9b2a41f775e9588c1dfb18d839212e8e83b5",
    "transactionHash": "0xd73f7c608d041ada2b27e094e682b53e119c7a5b1d0c38b71273a465d22692ad",
    "transactionIndex": 7,
    "events": {}
  }
}

EDIT: Corrected "Truffle" to "Drizzle"

bergkvist avatar Jun 04 '19 05:06 bergkvist

Thanks for reporting this @bergkvist. Can you share the code/project so we can reproduce this issue?

cds-amal avatar Jun 04 '19 10:06 cds-amal

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 03 '19 13:08 stale[bot]

This issue has been closed, but can be re-opened if further comments indicate that the problem persists. Feel free to tag maintainers if there is no reply to further comments.

stale[bot] avatar Aug 10 '19 14:08 stale[bot]