1

I am trying to return error messages from solidity. I have this contract:

pragma solidity ^0.4.23;
contract Simple {
    function arithmetics(uint a, uint b){           
                     require(a % 2 == 0, "Even value required.");             
                    b = b * 3; 
            }
}

When I test in remix, it's returning me the following error message:

transact to Simple.arithmetics errored: VM error: revert. revert The transaction has been reverted to the initial state. Reason provided by the contract: "Even value required.". Debug the transaction to get more information.

However, when I tried calling it from my dApp using web3.js I am not getting an error message, only this:

Transaction ran out of gas.

2 Answers 2

1

You can find the error message tracing the transaction. For example, using geth and I assume your node is running at http://127.0.0.1:8545:

1- Open a console

geth attach http://127.0.0.1:8545

2- Trace transaction

debug.traceTransaction(<your TX hash here>)

You should get a transaction trace. If your geth node wasn't started with debug enabled, you can just do an HTTP request like:

curl -X POST --data '{"jsonrpc":"2.0","method":"debug_traceTransaction","params":["<your TX hash here>"],"id":1}' http://127.0.0.1

Now that you have the trace, you can prettify the JSON to see it better. At the very bottom you will find the last instruction and the EVM state (and other things). The last instruction should be a REVERT opcode. Then, go to the MEMORY section, and you can convert it to ascii to find the error message. For example, in bash this command would convert from hex to ascii:

echo <your HEX here> | xxd -r -p

0
1

For web3, this is a work under development and not possible yet. You can follow this thread : https://github.com/ethereum/web3.js/issues/1707

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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