0

When I run the code line below:

let gasEstimate = web3.eth.estimateGas({from:myAddress, data: bytecode})

got and error:

> Error: Node error: {"code":-32000,"message":"gas required exceeds allowance (8000000) or always failing transaction"}

How fix it? many thanks.

3
  • are you missing await ? let gasEstimate = await web3.eth.estimateGas({from:myAddress, data: bytecode})
    – Majd TL
    Commented Jun 20, 2019 at 10:35
  • It's because your transaction is going to fail. Did you try to deploy in the remix? Commented Jun 20, 2019 at 10:52
  • I can deploy the contract with constant gas, I want to use the estimated gas, but when I uncomment the two lines. there is always an error as above said. The code used: const main = async () => { await web3.eth.personal.unlockAccount(myAddress, "123456", 600); //let gasEstimate = await web3.eth.estimateGas({from:myAddress, data: bytecode}); //console.log(gasEstimate); await new web3.eth.Contract(abi) .deploy({data: bytecode, arguments: ['TestToken', 'Token', 18, '1000000000000000000000']}) .send({ from:myAddress, gas: 6000000 }); }
    – user53451
    Commented Jun 20, 2019 at 11:18

1 Answer 1

1

I hope this will work for you,

const contract = new web3.eth.Contract(abi);

const estimateGas = await contract.deploy({
  data: bytecode
}).estimateGas();

contract.deploy({
  data: bytecode,
  arguments: ['TestToken', 'Token', 18, '1000000000000000000000']
})
.send({
  from: yourAddress,
  gas: estimateGas,
})
.on('error', (error) => {
  console.log(error)
})
.on('transactionHash', (transactionHash) => {
  console.log(transactionHash)
});

2
  • It works well.Thanks for your timely help! One comment is that under the line 4 "data: bytecode, ", "arguments: ['TestToken', 'Token', 18, '1000000000000000000000']" should be added, otherwise it can not pass the grammar check.
    – user53451
    Commented Jun 21, 2019 at 1:51
  • A question about contract deployment, I notice remix's generated code to deploy contract is as below:var TestToken = TestTokenContract.new( _name, _symbol, _decimals, _totalSupply, { from: web3.eth.accounts[0], data: '0xAAAAA...', gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } }) what's the difference between it and method you mentioned above?
    – user53451
    Commented Jun 26, 2019 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.