[Ethereum] How to estimate gas at contract creation/deployment to private ethereum blockchain

gas-estimatego-ethereumremixsolidity

What I have done
a) When I use web3.eth.estimateGas to estimate the cost of a contract creation constructor with no parameters, the estimate is correct.

b) If the contract is already deployed then estimating the gas cost of a contract function with parameters, it works fine. (contract.myMethod.estimateGas() using the web3 api )

Issue
a) When I estimate gas in a contract on contract creation time (contractObject.new) with a parameterized constructor then it gives an incorrect estimation of gas cost. (web3.eth.estimateGas of web3 api)

What I want
a) When I estimate gas with multiple parameter constructor of contract then it should estimate correct gas.
(contractObject.new for calling constructor)

b) Browser-soldity gives right gas estimation of contract with parametrized constructor –> before contract create/deploy (like transaction cost or execution cost, how can I use their algorithm with web3 api to estimate gas correct way?)

Best Answer

Try using .getData().

.getData() returns the encoded parameters of a function in order to send the transaction manually. You can then stick this in web3.eth.estimateGas() (the one on web3.eth, not on a given method.) to simulate sending the transaction.

Here's an untested example, but hopefully it can help you on your way:

var contractData = contractObject.new.getData(someparam, another, {data: contractBytecode});
var estimate = web3.eth.estimateGas({data: contractData})

References:

web3.eth.estimateGas()

An example of using .getData() (It's the fourth option.)

Related Topic