2

I added the following code for implementing a wait logic after deploying the contract and before calling the functions from the contract. This is same as mentioned in some other posts in Stackexchange.

// await sleep trick
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

// We need to wait until any miner has included the transaction
// in a block to get the address of the contract
async function waitBlock() {
  while (true) {
    let receipt = web3.eth.getTransactionReceipt(transferfunds.transactionHash);
    if (receipt && receipt.contractAddress) {
      console.log("Your contract has been deployed at http://testnet.etherscan.io/address/" + receipt.contractAddress);
      console.log("Note that it might take 30 - 90 sceonds for the block to propagate befor it's visible in etherscan.io");
      break;
    }
    console.log("Waiting a mined block to include your contract... currently in block " + web3.eth.blockNumber);
    await sleep(4000);
  }
  return;
}

While loading the script to Geth for testing, it returned the following error. enter image description here

The error displayed points to the => from the sleep(). How can we get rid of this error ?

6

It looks like your code is written in ES6, but Geth does not support ES6 syntax. Geth uses otto as its JavaScript VM, which only supports ES5.

You'll need to translate your code to ES5 first. Something like this (completely untested):

function waitBlock(callback) {
  function innerWaitBlock() {
    var receipt = web3.eth.getTransactionReceipt(transferfunds.transactionHash);
    if (receipt && receipt.contractAddress) {
      console.log("Your contract has been deployed at http://testnet.etherscan.io/address/" + receipt.contractAddress);
      console.log("Note that it might take 30 - 90 sceonds for the block to propagate befor it's visible in etherscan.io");
    } else {
      console.log("Waiting a mined block to include your contract... currently in block " + web3.eth.blockNumber);
      setTimeout(innerWaitBlock, 4000);
    }
  }
  innerWaitBlock();
}

waitBlock(function () {
  // do stuff here now that the contract has been deployed
});
  • Thanks. I found that the code is not mowing past setTimeout :( – vzurd Dec 20 '17 at 20:17
  • Can you explain what you mean by "not [moving] past setTimeout?" Are you seeing an error of some sort? Or something else? – user19510 Dec 20 '17 at 20:20
  • It was not writing any logs after that. I am still working on it. it could be some other coding error I made. Let me get back to you. – vzurd Dec 20 '17 at 21:09
  • It seems that while executing, the control is coming inside the function and goes back to the main program where it was called from, regardless of completion of setTimeout. I used the same logic and wrote using while and a wait function (which runs a for loop till the required delay is over). It is working as expected now. – vzurd Dec 20 '17 at 21:28
  • Yes, you'll need to use a callback (to replace the ES6 async/await) if you want to run code only after waitBlock finishes. I've edited my code to reflect that. – user19510 Dec 20 '17 at 21:36

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.