1

Been struggling in finding a way to estimate gas for calling a function. I was getting the error - Error: VM Exception while processing transaction: revert locally on ganache and getting Error: The execution failed due to an exception. on Kovan.

Here's a sample solidity function:

enter image description here

This is what my code looks like:

sig := []byte("withdraw(address)")
methodID := crypto.Keccak256(sig)[:4]
toAddress := common.HexToAddress(myAddr)
paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)

var data []byte
data = append(data, methodID...)
data = append(data, paddedAddress...)

sendTo := common.HexToAddress(myAddress)

callMsg := ethereum.CallMsg{
        From:     Wallet.Address,
        To:       &sendTo,
        Gas:      0,
        GasPrice: big.NewInt(0),
        Value:    big.NewInt(0),
        Data:     data,
}
gasAmount, err := eth.EthClient.EstimateGas(context.Background(), callMsg)

Why am I getting the complaint - Error: VM Exception while processing transaction: revert

2 Answers 2

0

Invalid opcode is a generic EVM error, so there is a problem in your transaction.

Are you sure about the to address? I don't think is possible to send transaction to null address (even if you are only make a gas estimation).

Try to set a valid address in to field.

3
  • You are right about the to address needs to be a valid address. However, now I'm getting Error: VM Exception while processing transaction: revert. Commented Aug 13, 2020 at 7:52
  • Wich function are you calling? Seems an error from smart contract
    – Dibi91
    Commented Aug 13, 2020 at 8:08
  • I call a simple function from my custom contract, like the edited image above Commented Aug 13, 2020 at 9:31
0

Here's how I managed to solve the problem:

  1. Like Dibi91 mentioned, the to address has to be a valid address.
  2. Found this way on this PR, refer to the snippet below. https://github.com/ethereum/go-ethereum/pull/15768/files
var iargs []interface{}
iargs = append(iargs, "0x...") //fill out your arguments

input, err := Abi.Pack("withdraw", iargs...)

sendTo := common.HexToAddress(myAddress)

callMsg := ethereum.CallMsg{
        From:     Wallet.Address,
        To:       &sendTo,
        Gas:      0,
        GasPrice: big.NewInt(0),
        Value:    big.NewInt(0),
        Data:     input,
    }
gasLimit, err := eth.EthClient.EstimateGas(context.Background(), callMsg)
1
  • giving this error "cannot use string as type array as argument" Commented Jul 13, 2023 at 11:46

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.