1

Newbie. There is a go-ethereum method:

eth.estimateGas({from:'firstAccount', to:'secondAccount'})

that works well, but same method with contract address like:

eth.estimateGas({from:'firstAccount', to:'contractAddr'})

fails with error

gas required exceeds allowance or always failing transaction

I have looked into go-ethereum source code and it has the line, that contains proposal to use contract address as second parameter: https://github.com/ethereum/go-ethereum/blob/master/accounts/abi/bind/base.go#L221

The question is: is there any possibily to use eth.estimateGas with contract address as second parameter and how to avoid above error? Thank you.

Vadim Filin
  • 342
  • 3
  • 12
  • Forgot to mention: it is a private network, i have enough ether and contract has deployed and mined. – Vadim Filin Apr 08 '18 at 16:32
  • Passing encoded method as data works, but please note that if you're estimating ether transaction to contract you must also pass exact value in the estimate transaction object. – Deimantas Jul 04 '19 at 17:59

1 Answers1

2

You're not specifying what you're executing in the contract, so there's nothing to estimate. When you estimateGas for a transfer to an EOA account, there is no contract code to execute, so there is no message data to be sent as part of the transaction object. If you're estimating gas on a contract call, you need to include the data for the contract.

For example, if you want to estimate gas to setValue(2) method in this contract

pragma solidity ^0.4.19;

contract SimpleContract {
  uint256 _value;

  function setValue(uint256 value) public {
    _value = value;
  }
}

your call would be

var data = '552410770000000000000000000000000000000000000000000000000000000000000002';
eth.estimateGas({from: fromAccount, to: contractAddress, data});

The value for data comes from encoding the function signature and the parameter value(s). You can use a simple tool (like https://abi.hashex.org) to generate this. You just enter the function name along with the parameter argument types and their values, and it will generate the message data for you. You can also do this using web3js.

EDIT - I neglected to consider contracts with fallback functions. Executing estimateGas on a contract without passing in message data provide the estimate for contracts that have a fallback function. If the contract does not have a fallback function, the call will fail.

Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
  • Thank you again! So according to this commentary: https://github.com/ethereum/go-ethereum/blob/master/accounts/abi/bind/base.go#L179 - if contract doesn't have default method for contract funding above error will cause? – Vadim Filin Apr 08 '18 at 19:05
  • Yes. You raise a good point about the fallback function though. I'll add that to the answer. – Adam Kipnis Apr 08 '18 at 20:44