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.
Best Answer
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 keywordconstant
. You'll therefore have to call it withgreeter.greet.call()
PS : no need to do this complicated stuff to create your development chain. Just run
get --dev --mine --minerthreads 1