9

I am trying to use the estimateGas function in Web3 to estimate how much gas it would cost to run a function in my contract. As an example of how this would normally be used, in geth I can run a command like the following and get something back (21001):

web3.eth.estimateGas({from: eth.accounts[0], to: "0xEDA8A2E1dfA5B93692D2a9dDF833B6D7DF6D5f93", amount: web3.toWei(1, "ether")})

However, running the same command on a webpage with web3 (eg. trying the following):

console.log(web3.eth.estimateGas({from: eth.accounts[0], to: "0xEDA8A2E1dfA5B93692D2a9dDF833B6D7DF6D5f93", amount: web3.toWei(1, "ether")}))

Will result in this error:

eth is not defined

I am having trouble getting this to work in any case. For example, say in my contract I have the following function:

function SetMessage (bytes32 _message) returns (bool success) {
    message = _message;
    return true;
    }

Which may be executed in Web3 as follows:

MyContract.deployed().then(function (contractInstance) {    
      return contractInstance.SetMessage(_message, { gas: 200000, from: web3.eth.accounts[0] })
    })

What would be the exact syntax to estimate the gas of sending a transaction to this function?

2 Answers 2

7

Try the below code in a browser. Note that account selection is done with web3.eth.accounts[0] instead of eth.accounts[0].

console.log(web3.eth.estimateGas({from: web3.eth.accounts[0], to: "0xEDA8A2E1dfA5B93692D2a9dDF833B6D7DF6D5f93", amount: web3.toWei(1, "ether")}))
1
  • 1
    Thanks - I'm still having trouble estimating gas cost of a specific function in a contract though (eg. for a contract I've deployed MyContract, and a function SetMessage which takes one parameter as in my post)
    – ZhouW
    Commented Jul 19, 2017 at 8:35
3

You probably did not instantiate an instance of web3, which is not necessary in the geth console but required for using web3 in the browser. Instantiating an instance of web3 requires that you specify a provider, for which you have multiple options (connecting to a locally running node, using MetaMask, a testRPC, etc).

The Ethereum docs recommend this pattern:

if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider);
} else {
  // set the provider you want from Web3.providers
  web3 = new Web3(new 
  Web3.providers.HttpProvider("http://localhost:8545"));
}

Or as a concrete example using testRPC:

  • install ethereumjs-testrpc
  • instantiate an instance of web3:

    const TestRPC = require('ethereumjs-testrpc')

    web3 = new Web3(TestRPC.provider())

Each of the providers additionally includes configuration options so you can do things like pre-fund certain accounts with test ether, set the mining difficulty, etc.

1
  • I've initiated web3 and can use other web3 functions.
    – ZhouW
    Commented Jul 18, 2017 at 12:35

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.