6

I want to create contract in go-ethereum using Solidity, I started with "Hello world" program.

I create a private network:

$geth --genesis private.json --networkid 1234 --datadir /home/user/Documents/genesisdata  console

In here, I make a new account then I coded follow link https://www.ethereum.org/greeter.

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

And I got: undefined

When I code without var like this:

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

I got:

"contract mortal { address owner; function mortal() { owner = msg.sender; } function kill() { if (msg.sender == owner) selfdestruct(owner); } } contract greeter is mortal { string greeting; function greeter(string _greeting) public { greeting = _greeting; } function greet() returns (string) { return greeting; } function test() returns (string){ return greeting; } }"

So I continute code without var, I compile:

greeter = greeterContract.new(_greeting,{from:web3.eth.accounts[0], data: greeterCompiled.greeter.code, gas: 210000}, 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("Error:>>>>>>>>>>>>>");
console.log(e);
}

})

untill:

greeter.greet();

TypeError: 'greet' is not a function at :1:1

I don't know, how to solve it.

Please show me the problem that I make in this process.

Thanks.

2

2 Answers 2

4

It does not seem that you compiled the source? You just declared a variable holding the source code. That's the main problem.

Also, apparently, your function greet() is not declared with the keyword constant. You'll therefore have to call it with greeter.greet.call()

PS : no need to do this complicated stuff to create your development chain. Just run get --dev --mine --minerthreads 1

4
  • Yes, I do, I compiled the source and that is the result. I try: greeter.greet.call() and got "TypeError: Cannot access member 'call' of undefined at <anonymous>:1:1". #bortzmeyer
    – Ajk Thu
    Commented Jul 22, 2016 at 9:26
  • May be you do compile but it is not shown in your question. You should edit it for completeness.
    – bortzmeyer
    Commented Jul 22, 2016 at 9:33
  • And, after you compiled, you also have install the new contract, and gives the adress to the variable you want to use. This is clearly explained in the tutorial ethereum.org/greeter after the compilation step.
    – bortzmeyer
    Commented Jul 22, 2016 at 9:36
  • I wil check it again, thank you very much! @bortzmeyer
    – Ajk Thu
    Commented Jul 22, 2016 at 9:41
2

var greeterSource=... is correct and Javascript throws this undefined at you every time. Just ignore it.
You can see that the variable was set correctly by typing greeterSource again. You need to compile your contract with solc in order to use it, as the tutoral shows.

2
  • I follow step by step in tutoral and I compiled with solc.
    – Ajk Thu
    Commented Jul 22, 2016 at 9:31
  • could you have any idea or suggestion for my issue? I tried and strictly follow the tutorial. But my result always "TypeError: 'greet' is not a function at :1:1". Please help me
    – Ajk Thu
    Commented Jul 25, 2016 at 2:47

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.