Question:

Catching errors with gas issues

Benjamin: 2 weeks ago

I am working on the frontend where I use try / catch to find errors and give appropriate messages to users. My smart contract has a payable function and I was at the point of catching errors regarding gas usage.

uint[] public theArray;




function myFunction(uint bigNumber) payable public {
         if(msg.value == 0.01 ether){
         theArray.push(bigNumber);}
else {if (msg.value == .05 ether){
            for (int i; i<50;i++)
            theArray.push(bigNumber);

        }
else { 
    revert();
}
}}

Now when I try to catch the errors, the same error can have two different 'outputs' (err.message). The examples:

Using MetaMask on Rinkeby testing network

If I send 0.05 ETH with 21,000 gas or even 21,001 (minimum amount) the error I am getting is:

Returned error: Error: Error: [ethjs-rpc] rpc error with payload

However if I send 0.05 ETH with 25,000 gas (not enough gas so it still fails) the error I am getting is:

Transaction has been reverted by the EVM

Could anyone explain me why if I increase the gas amount the error changes?

Another thing I noticed is that if I send an invalid amount such as 0.04 I am also getting

Transaction has been reverted by the EVM

Answer:
William: 2 weeks ago

Base on my observation and understanding. Might not be the best explanation out there.

  1. If I send 0.05 ETH with 21,000 gas or even 21,001 (minimum amount) the error I am getting is:

    Returned error: Error: Error: [ethjs-rpc] rpc error with payload

-For this error it may have been because of metamask itself, maybe play with the gasPrice and/or gasLimit. You can refer here (https://ethereum.stackexchange.com/questions/52274/error-error-ethjs-rpc-rpc-error-with-payload-id6223335088050-jsonrpc) or here (https://github.com/MetaMask/metamask-extension/issues/2690) for better understanding.


  1. However if I send 0.05 ETH with 25,000 gas (not enough gas so it still fails) the error I am getting is:

Transaction has been reverted by the EVM

Could anyone explain me why if I increase the gas amount the error changes?

-For this error, I ran the code and find that possible reason is the transaction happens and being proceeded. The parameters satisfies this part of the code. However in the midst of the process the gas ran out while the for loop is running which eats up most of the gas you have provided. Maybe try to increase the gas limit and this will be mined without error.

if (msg.value == .05 ether){
        for (int i; i<50;i++)
        theArray.push(bigNumber);

    }

  1. Another thing I noticed is that if I send an invalid amount such as 0.04 I am also getting

Transaction has been reverted by the EVM

With the last scenario the parameter simply just did not satisfy any of the condition that is why it is going straight to the last else which revert() it.