[Ethereum] way to capture the require / revert failure reason

revert-opcodesolidityweb3.pyweb3js

In my contract, I have a bunch of checking at the start of a public function. It looks something like this:

require ( prx < 1e18, "Value prx too high")
require ( prx != 0, "Value prx cannot be zero")
if (!validAmt(amt)) { revert("Value amt invalid"); }

Obviously I would like the user to see the failure and the reason text, is this possible in web3.js, or perhaps web3.py?

I know I can capture events, but I don't want to log events on these kind of failures.

Best Answer

In your web3 code, you can use a try/catch block, and parse the thrown error:

try {
  const receipt = await web3.methods.foo().send(...);
  ...
} catch (e) {
  //parse e here, the reason will be inside
}
Related Topic