I'm working on the contracts tutorial. Here's the code (including contract):
var greeterSource = 'contract mortal { address owner; function mortal() { owner = msg.sender; } function kill() { if (msg.sender == owner) suicide(owner); } } contract greeter is mortal { string greeting; function greeter(string _greeting) public { greeting = _greeting; } function greet() constant returns (string) { return greeting; } }'
var greeterCompiled = web3.eth.compile.solidity(greeterSource)
var _greeting = "Hello World!"
var greeterContract = web3.eth.contract(greeterCompiled.greeter.info.abiDefinition);
var greeter = greeterContract.new(_greeting,{from:web3.eth.accounts[0], data: greeterCompiled.greeter.code, gas: 10000000000000}, 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);
}
}
})
On the top line, the contract source code is written without line breaks.
I have 4 ether for testnet:
> eth.getBalance(eth.coinbase)
4000000000000000000
You're welcome to send me some more:
> eth.accounts
["0x425434d3a0f5da79d60769f2400896f0d85b41ec"]
In the geth-1.4.6
console, I import the complete script from above using
> loadScript("/home/work/projects/contracts/tutorial_greeter/setup_greeter.js")
true
Compiling works, but why does it not have an address even when a number of blocks have been mined in the meanwhile?
> greeter
{
abi: [{
constant: false,
inputs: [],
name: "kill",
outputs: [],
type: "function"
}, {
constant: true,
inputs: [],
name: "greet",
outputs: [{...}],
type: "function"
}, {
inputs: [{...}],
type: "constructor"
}],
address: undefined,
transactionHash: null
Best Answer
Here's a step-by-step guide of deploying and running Greeter on Testnet.
I you do not already have a coinbase account, run the following command:
Then start
geth --testnet
on the Testnet with the--mine --minerthreads 1 --unlock 0
parameter with theconsole
command. You will have to enter in your password to unlock the account:The DAG has now been created and but the blockchain is still being downloaded - from the
imported 256 block(s)
message above.Note that you don't have to mine the Testnet blockchain, as another miner (if there is one) will pick up your transaction and include your transaction in a mined block.
You can check the syncing status using the following command:
The blockchain has now finished syncing. Let's check the mining status and our coinbase account:
In a separate window, run
top
from your command line and you should seegeth
utilising 100+% CPUI'm using the source code listed in Deploying the Greeter contract via the geth CLI is not registering in my private blockchain.
Let's check the compiled information:
Send the transaction to insert the contract code into the blockchain. You will see that the contract transaction has been successfully mined.
We can now call the
greet()
function: