9

I am using a public node as my web3 privider, so I must sign all transactions manually. This includes transactions that create contracts.

Currently, I use the following method to deploy contracts.

/* I create the contract first. */
var contractAbi = ... (I load this from a file.)
var contractBytecode = ... (I load this from another file.)
var contract = new web3.eth.Contract(contractAbi);

/* Then I get the data encoded with constructor parameters. */
var contractDeployInfo = contract.deploy({
  data: contractBytecode,
  arguments: [constructorParam1, constructorParam2]
});

/* Data field of the transaction. */
var transactionData = contractDeployInfo.encodeABI();

/* Then I estimate the gas, using the provided method. */
contractDeployInfo.estimateGas().then(gas => {
    // Use this gas estimate to create raw transaction, 
    // sign it, send it, and so on.
})

The transaction does go through, and the contract is deployed.

However, the gas estimate returned by contract.deploy().estimateGas() only returns gas that is enough to send the contract through to the miners, but not enough to actually execute the contract's constructor.

As a result, I am left with a blank contract with no data in the contract address that is specified in the transaction receipt.

Should I calculate the gas estimate of the contract's constructor, and add that to the gas limit in my transactions?

This(How to estimate gas at contract creation/deployment to private ethereum blockchain) “duplicate” question uses an older version of Web3 library. Also I was referring to the newly introduced deploy function.

  • Can you give more detail about "var contractBytecode = ... (I load this from another file.)". Also, can you show on Etherscan, the transaction that "failed"? – Xavier Leprêtre B9lab Jun 25 '18 at 9:58
  • 1
    Just to clear some things up. You can't really be left with a blank contract that fails to run the constructor, that whole transaction of deploying and running the constructor has failed. All transactions that can be successfully sent to the network will return a receipt, this doesn't mean they will succeed when ran. These failed transactions are also mined, so the gas used to prove that will be lost. It seems like the contract has an error in it, causing a bad estimate and an ultimate failed deploy. – blackops Sep 17 '18 at 17:58
1

You can deploy the contract in your development testrpc, and check the gas for that transaction.

const tx = await MyContract.new();
const gasUsed = tx.receipt.gasUsed;

Hope this helps.

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.