0

I'm trying to obtain estimate gas for an erc20 token contract deployed over kovan network, just like this:

var getCode = web3.eth.getCode(tokenAddress)

var transactionObject = {
  from: address,
  to: tokenAddress,
  value: web3.utils.toWei(data.value.toString()),
  data: getCode
}

web3.eth.estimateGas(transactionObject)

The response:

Unhandled rejection Error: Returned error: Transaction execution error. at Object.ErrorResponse (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-core-helpers/src/errors.js:29:16) at /Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-core-requestmanager/src/index.js:137:36 at XMLHttpRequest.request.onreadystatechange (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-providers-http/src/index.js:64:13) at XMLHttpRequestEventTarget.dispatchEvent (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-providers-http/node_modules/xhr2/lib/xhr2.js:64:18) at XMLHttpRequest._setReadyState (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-providers-http/node_modules/xhr2/lib/xhr2.js:354:12) at XMLHttpRequest._onHttpResponseEnd (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-providers-http/node_modules/xhr2/lib/xhr2.js:509:12) at IncomingMessage.<anonymous> (/Users/rstorm/repos/audity/checkBalances/node_modules/web3/packages/web3-providers-http/node_modules/xhr2/lib/xhr2.js:469:24) at emitNone (events.js:110:20) at IncomingMessage.emit (events.js:207:7) at endReadableNT (_stream_readable.js:1059:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickDomainCallback (internal/process/next_tick.js:218:9)
1

Your data is almost certainly invalid. You're calling getCode, which gives you the bytecode of the contract, and then you're sending that code in your transaction. estimateGas requires a valid transaction, since it then essentially runs the transaction (without submitting it to the rest of the network) to measure the gas that's used.

If you have the ABI for the contract and are trying to estimate the gas usage for a particular method call, you can do something like this:

contract.methods.myMethod(...).estimateGas(function (error, gasAmount) {
    ...
  });

See https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-estimategas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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