4

I tried deploying a greeter contract as shown here , I thought the deployment is suceess but i am unable to get the address of the contract, it always shows undefined.

When I tried to execute the greet method it also shows undefined

pls check the attached image.

  • geth version 1.5.3-stable-978737f5
  • solidity version - 0.4.6

I also did unlocking the account and waited for over 10 mins to let it mine.

below are the steps I did to compile and deploy the contract.

var greeterSource1 = 'contract mortal { address owner; function mortal() { owner = msg.sender; } function kill() { if (msg.sender == owner) selfdestruct(owner); } } contract greeter1 is mortal { string greeting; function greeter(string _greeting) public { greeting = _greeting; } function greet() constant returns (string) { return greeting; } }'

var greeterCompiled1 = web3.eth.compile.solidity(greeterSource1)

var _greeting1 = "Hello World!"

var greeterContract1 = web3.eth.contract(greeterCompiled1.greeter1.info.abiDefinition)

var greeter1 = greeterContract1.new(_greeting1,{from:web3.eth.accounts[0], data: greeterCompiled1.greeter1.code, gas: 300000}, 
function(e, contract) {

    if(!e) {
        if(!contract.address) {
            console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");

        } else {
            console.log("Contract mined! Address: " + contract.address);
            console.log(contract);
        }
    }
})

Any help is greatly appreciated. enter image description here

  • Hey. If possible, please avoid posting screenshots of code or terminal outputs. This will never show up on search engines and wont help users with the same issue like you have. Thanks. – Waqar Lim Dec 22 '16 at 11:40
2

You can only call functions after the transaction has been mined. So inside your callback not just after calling the new function since that'll return instantly.

Add some code in your callback to check for errors, when it's mined you can call the function. You might want to check out Truffle framework which makes life a bit easier.

  • i did wait for more that 10 min to get the contract mined with no luck.I think the deployment failed. how to check logs, i am connecting via putty to my ethereum transaction node. – mahesh gupta Dec 7 '16 at 12:44
  • it should be in the error argument. The 'e' in your callback. Pending transactions can be viewed if you connect with geth (geth attach) and then view eth.pendingTransactions. You could also try by adding a gasprice when you create the contract. – BennyM Dec 7 '16 at 12:54
  • found out the reason, it says my account is locked. so i unlocked the account before executing the contract deployment statement. do i need to unlock the account in every ssh session? because i did unlock in my previous session. – mahesh gupta Dec 8 '16 at 8:52
  • Yes you do. If you are just testing you could use this client instead: github.com/ethereumjs/testrpc – BennyM Dec 8 '16 at 8:56
1

thanks to benny, I found the issue by changing the above code to log the error

 var greeter1 =
 greeterContract1.new(_greeting1,{from:web3.eth.accounts[0], data:
 greeterCompiled1.greeter1.code, gas: 300000}, function(e, contract){
 if(!e) {
   if(!contract.address) {
     console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
   } else {
     console.log("Contract mined! Address: " + contract.address);
     console.log(contract);
   }     
}   else{   console.log(e);     
     }  
     })

Issue is that my account is locked, unlocked using below statement personal.unlockAccount(eth.accounts[0],"yourpassword").

After executing the deployment statement you will get 2 responses 1. It says your contract is being mined, few seconds later 2. Address to your contract Now I got my address to the contract.

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.