4

I'm starting to learn Solidity and how to interact with smart contracts within a Dapp, and I would like to know how can I log/handle errors from the contract in my Dapp.

For example, in the Dapp I have a button, when you click it, it runs a function in the contract, I have a require() there with a specific message: "Wait 15m", basically, the user can only click this button once every 15 minutes.

The button interaction is fired in a try/catch, if the user already clicked the button, so the next time will catch the error. My problem is the error that I'm getting in the console (to debug), it is like:

Error: transaction failed (transactionHash="0xe95c970115f5fe55202d04c2e0cd83c2ff67d3653f5397d7739764d685249da6", transaction={"hash":"0xe95c970115f5fe55202d04c2e0cd83c2ff67d3653f5397d7739764d685249da6","type":2,"accessList":null,"blockHash":null,"blockNumber":null,"transactionIndex":null,"confirmations":0,"from":"0xc6682cDC60a1e4603D051e48A2970E4859C62521","gasPrice":{"type":"BigNumber","hex":"0x59682f11"},"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"}......

It is just a string with this bunch of code, without the error message specified in the require() which is the thing that is firing the error. This is the transaction in Rinkeby.

So, I imagine, in the future, I could have many functions in my contract with many require() with different messages. How can I handle these errors and show valuable information to the user in the UI? Is there any function that could help to get the proper messages?

I'm using: Ethers - React

Thanks in advance for any help!

Rod
  • 349
  • 3
  • 10
  • Have you tryed converting the error to string? Try using error.toString(); – NuMa Jan 16 '22 at 19:52
  • @NuMa I will try that and see what I can get. Thanks! – Rod Jan 16 '22 at 22:27
  • Once asked the same type of question here: https://stackoverflow.com/questions/62493511/require-statement-returns-transaction-revert-error-instead-of-provided-custom-er Hope this helps you in some way – Taimoor Jan 16 '22 at 23:21
  • @Taimoor Thanks! I read that question, but I wanted to ask this because maybe now exists the possibility to return the errors within `require()` functions. – Rod Jan 17 '22 at 01:03
  • @NuMa Converting the error to string in the way that you suggested throws me in the console: `[object Object]` – Rod Jan 17 '22 at 08:07

1 Answers1

1

To extract error message from revert and require in the contract, please use provider.call to retrieve the error data. The following code works well with alchemy provider and goerli net. (ethers.js version 5.6.8)

const alchemyProvider = new ethers.providers.AlchemyProvider(
  (network = "goerli"),
  API_KEY
)

const tx = await alchemyProvider.getTransaction("0x5479d838aa66169470a61a8347727b75264e1d5d68d6749e5bd59b44d525dc21");
const code = await alchemyProvider.call(
      {
          to: tx.to,
          from: tx.from,
          nonce: tx.nonce,
          gasLimit: tx.gasLimit,
          gasPrice: tx.gasPrice,
          data: tx.data,
          value: tx.value,
          chainId: tx.chainId,
          type: tx.type ?? undefined,
          accessList: tx.accessList,
      },
      tx.blockNumber,
  )

  const decodedError = SimpleAuction.interface.parseError(code)
  console.log(decodedError)
  console.log(`Transaction failed: ${decodedError.name}`)

The transaction info in etherscan which error encountered during contract revert execution. So the "code" is detailed and raw error info, and can be parsed and fromatted by interface. The output of error showed in the following.

ErrorDescription {
  args: [
    BigNumber { _hex: '0x01', _isBigNumber: true },
    highestBid: BigNumber { _hex: '0x01', _isBigNumber: true }
  ],
  errorFragment: {
    type: 'error',
    name: 'BidNotHighEnough',
    inputs: [ [ParamType] ],
    _isFragment: true,
    constructor: [Function: ErrorFragment] {
      from: [Function (anonymous)],
      fromObject: [Function (anonymous)],
      fromString: [Function (anonymous)],
      isErrorFragment: [Function (anonymous)]
    },
    format: [Function (anonymous)]
  },
  name: 'BidNotHighEnough',
  signature: 'BidNotHighEnough(uint256)',
  sighash: '0x4e12c1bb'
}

The "BidNotHighEnough" is an reverted error defined in the contract. So that all the detailed erro info.

Gitcoins
  • 11
  • 1