2

I am trying to deploy a contract in Ethereum testnet, with the web3 API via my geth node. The contract is deployed correctly and I can see the address in geth and verify it exists on etherscan. However I can't get the contract address working in my javascript. My code is:

var myContractReturned = web3.eth.contract(abi).new({data: code,gas:500000}, function (err, contract) {
  if(err) {
    console.error(err);
    return;
  } else if(contract.address){
    console.log('ContractAddress: ' + contract.address);
    document.getElementById('status').innerText = 'Mined!';
  }else{
    console.log('ContractAddress: ' + contract.address);
  }
});

In the If statement the only part that is executed is the "else" and contract.address returns undefined.

1

This part:

... new({data: code,gas:500000}, function (err, contract) { if(err) {

Should probably be:

... new({data: code,gas:500000}, function (err, txnHash) {
      if(err) {

When you send that, you get a transaction hash.

The hash means the transaction was sent to the network for verification. It's an unconfirmed transaction. No results yet.

The trick is to wait for the transaction to be mined. This might help: Optimal way of waiting for transactions to be mined in geth

Hope it helps.

| improve this answer | |
  • Thank you for your input! Indeed txnHash returns the transaction hash but I also need the contract address. I tried the code provided in the link (ECMAScript 2016 version of waiting a contract to be mined) both inside the "else" and after it but in both cases but all I get is Waiting a mined block to include your contract... currently in block 587791 and then the next block 587792, 587793 and so on. This happens indefinitely. – PetrosM Mar 4 '17 at 13:06
  • This is my favorite way, It uses promises, so the style is more like truffle, but if you look at the web3 extension it might give some ideas. gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6 – Rob Hitchens Mar 4 '17 at 14:13
  • console.log(web3.eth.getTransactionReceiptMined(txnHash.transactionHash)); gets stuck at [[PromiseStatus]] : "Pending" and never changes. I just added the getTransactionReceiptMined function and added the console log command above. Am I doing anything wrong? Note that I am using txnHash.transactionHash because I changed the contract to txnHash like you indicated above – PetrosM Mar 4 '17 at 18:14
  • Make sure someone's mining? – Rob Hitchens Mar 4 '17 at 18:18
  • Something weird is happening with geth. Even though it seems like geth is synced with the latest block (syncing only 1 block at a time) it is still way behind the last block on etherscan. Yesterday, for some reason it started syncing correctly (latest blocks) and I got the contract address and everything. Today I get the same problem. On Geth I am running geth --rpc --rpcapi="db,eth,net,web3,personal,web3" --rpcaddr="localhost" --rpcport="8545" --rpccorsdomain="http://localhost:3000" --testnet I read somewhere about another attack on ropsten might be happening now. Is this the case? – PetrosM Mar 5 '17 at 10:12

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.