0

here is the code as explained in web3 doc(node:13887)

 web3.eth.estimateGas({data: '0x' + bytecode}).then(value=>{
 console.log('gasEstimate = ' + value);
 let MyContract = new web3.eth.Contract(abi,{
          from: publicKey,//user wallet 
          gasPrice: '10000000000000',
          gas: value
         });
 console.log('deploying contract...');
 MyContract.deploy().send(function(error, transactionHash){
  }).on('error', function(error){ })
    .on('transactionHash', function(transactionHash){ })
    .on('receipt', function(receipt){
     console.log(receipt.contractAddress) // contains the new contract address   })
    .on('confirmation', function(confirmationNumber, receipt){  })
    .then(function(newContractInstance){
    console.log(newContractInstance.options.address) // instance with the new contract address
   });
});

here is error message

UnhandledPromiseRejectionWarning: Error: Request failed with status code 522
at createError (/home/project/node_modules/axios/lib/core/createError.js:16:15)
at settle (/home/project/node_modules/axios/lib/core/settle.js:18:12)
at IncomingMessage.handleStreamEnd (/home/project/node_modules/axios/lib/adapters/http.js:201:11)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
(node:13887) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:13887) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

EDIT

Thanks to all this is working code

  web3.eth.estimateGas({data: '0x' + bytecode}).then(value=>{
  let MyContract = new web3.eth.Contract(abi,{
     from: publicKey,
     gasPrice: 1000000,
     gas: value
   });
  MyContract.deploy({
       data:  '0x' +   bytecode
       }).send({
               from: publicKey,
               gasPrice: 1000000,
               gas: value
      },function(error, transactionHash){})
        .on('error', function(error){ })
        .on('transactionHash', function(transactionHash){ })
        .on('receipt', function(receipt){})
        .on('confirmation', function(confirmationNumber, receipt){  })
        .then(function(newContractInstance){ });
        });
3
  • I think you need to replace MyContract.deploy() with (await MyContract.deploy())... And of course, declare the entire function async. May 17, 2018 at 8:59
  • @goodvibration the code is using the promises, look then
    – qbsp
    May 17, 2018 at 9:17
  • MyContract.deploy({data: '0x' + bytecode})
    – kherwa
    May 17, 2018 at 9:27

2 Answers 2

2

You forgot to send the information that are required in order to deploy the contract. As per doc you have to provide the bytecode and the arguments of the constructor of your contract to the function deploy.

myContract.deploy({
    data: '0x12345...',
    arguments: [123, 'My String']
})
.send({
    from: '0x1234567890123456789012345678901234567891',
    gas: 1500000,
    gasPrice: '30000000000000'
}, function(error, transactionHash){ ... })
.on('error', function(error){ ... })
.on('transactionHash', function(transactionHash){ ... })
.on('receipt', function(receipt){
   console.log(receipt.contractAddress) // contains the new contract address
})
.on('confirmation', function(confirmationNumber, receipt){ ... })
.then(function(newContractInstance){
    console.log(newContractInstance.options.address) // instance with the new contract address
});
2
  • I do have that part here let MyContract = new web3.eth.Contract(abi,{ from: publicKey, gasPrice: value, gas: '10000000000000' });
    – Albert
    May 17, 2018 at 9:19
  • I can't see any bytecode there. To deploy the contract in the blockchain you need to send the bytecode. You're just initializing an object with the abi interface
    – qbsp
    May 17, 2018 at 9:21
0

Status code 522 is HTTP connection timeout. Check if you have set the provider and created web3 instance correctly. Check on your internet status as well.

13
  • I do have proper internet as blockchain is syncing and web3 instance as I am sending ether to the creator so it will have ether for contract gas
    – Albert
    May 17, 2018 at 8:57
  • @goodvibration - that is the example from web3 docs
    – Albert
    May 17, 2018 at 9:14
  • You are right. I was confused on web3 interface at the moment.
    – viz
    May 17, 2018 at 9:17
  • Think @mirg is right
    – viz
    May 17, 2018 at 9:28

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.