5

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
2

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:

user@Kumquat:~$ geth --testnet account new
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase: 
Repeat Passphrase: 
Address: {730b87e78b07fb2bf1b1e8b127b3353d08d72706}

Then start geth --testnet on the Testnet with the --mine --minerthreads 1 --unlock 0 parameter with the console command. You will have to enter in your password to unlock the account:

user@Kumquat:~$ geth --testnet --mine --minerthreads 1 --unlock 0 console 
...
Unlocking account 0 | Attempt 1/3
Passphrase:
> I0615 01:37:11.434270 ethash.go:291] Generating DAG: 0%
I0615 01:37:19.280918 ethash.go:291] Generating DAG: 1%
...
I0615 01:40:09.629009 core/blockchain.go:959] imported 256 block(s) (0 queued 0 ignored) including 120 txs in 2.822940573s. #1039346 [534c4f03 / c9f46493]
I0615 01:40:09.723141 ethash.go:291] Generating DAG: 17%
...
I0615 01:59:43.271478 core/blockchain.go:959] imported 256 block(s) (0 queued 0 ignored) including 366 txs in 6.860619737s. #1113842 [cb034c71 / 909241a8]
I0615 01:59:48.964948 ethash.go:291] Generating DAG: 100%
I0615 01:59:48.969898 ethash.go:276] Done generating DAG for epoch 34, it took 22m39.495183713s
I0615 01:59:50.226735 core/blockchain.go:959] imported 256 block(s) (0 queued 0 ignored) including 401 txs in 6.953559841s. #1114098 [2f30678a / aa5b5fd1]

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:

> eth.syncing
{
  currentBlock: 1123788,
  highestBlock: 1133372,
  knownStates: 0,
  pulledStates: 0,
  startingBlock: 1024655
}
> I0615 02:02:03.312407 core/blockchain.go:959] imported 256 block(s) (0 queued 0 ignored) including 306 txs in 8.307949518s. #1123826 [e9d853f5 / a549429b]

The blockchain has now finished syncing. Let's check the mining status and our coinbase account:

I0615 02:09:36.554320 miner/worker.go:555] commit new work on block 1133511 with 0 txs & 0 uncles. Took 293.439µs
I0615 02:09:38.061406 core/blockchain.go:959] imported 1 block(s) (0 queued 0 ignored) including 0 txs in 8.141851ms. #1133511 [140a530f / 140a530f]
I0615 02:09:38.061848 miner/worker.go:555] commit new work on block 1133512 with 0 txs & 0 uncles. Took 210.852µs
> eth.mining
true
> web3.fromWei(eth.getBalance(eth.accounts[0]).toNumber(), "ether")
"28.902244797526348"

In a separate window, run top from your command line and you should see geth utilising 100+% CPU

I'm using the source code listed in Deploying the Greeter contract via the geth CLI is not registering in my private blockchain.

> 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; } }'
undefined
> var greeterCompiled = web3.eth.compile.solidity(greeterSource)
I0615 02:16:00.290143 common/compiler/solidity.go:114] solc, the solidity compiler commandline interface
Version: 0.3.4-0/RelWithDebInfo-Linux/g++/Interpreter

path: /usr/bin/solc
undefined

Let's check the compiled information:

> greeterCompiled
{
  greeter: {
    code: "0x606060405260405161023e38038061023e8339810160405280510160008054600160a060020a031916331790558060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10609f57805160ff19168380011785555b50608e9291505b8082111560cc57600081558301607d565b50505061016e806100d06000396000f35b828001600101855582156076579182015b82811115607657825182600050559160200191906001019060b0565b509056606060405260e060020a600035046341c0e1b58114610026578063cfae321714610068575b005b6100246000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101375760005473ffffffffffffffffffffffffffffffffffffffff16ff5b6100c9600060609081526001805460a06020601f6002600019610100868816150201909416939093049283018190040281016040526080828152929190828280156101645780601f1061013957610100808354040283529160200191610164565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b565b820191906000526020600020905b81548152906001019060200180831161014757829003601f168201915b505050505090509056",
    info: {
      abiDefinition: [{...}, {...}, {...}],
      compilerOptions: "--bin --abi --userdoc --devdoc --add-std --optimize -o /tmp/solc736903870",
      compilerVersion: "0.3.4",
      developerDoc: {
        methods: {}
      },
      language: "Solidity",
      languageVersion: "0.3.4",
      source: "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; } }",
      userDoc: {
        methods: {}
      }
    }
  },
  mortal: {
    code: "0x606060405260008054600160a060020a03191633179055605c8060226000396000f3606060405260e060020a600035046341c0e1b58114601a575b005b60186000543373ffffffffffffffffffffffffffffffffffffffff90811691161415605a5760005473ffffffffffffffffffffffffffffffffffffffff16ff5b56",
    info: {
      abiDefinition: [{...}, {...}],
      compilerOptions: "--bin --abi --userdoc --devdoc --add-std --optimize -o /tmp/solc736903870",
      compilerVersion: "0.3.4",
      developerDoc: {
        methods: {}
      },
      language: "Solidity",
      languageVersion: "0.3.4",
      source: "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; } }",
      userDoc: {
        methods: {}
      }
    }
  }
}

Send the transaction to insert the contract code into the blockchain. You will see that the contract transaction has been successfully mined.

> var _greeting = "Hello World!" 
undefined
> var greeterContract = web3.eth.contract(greeterCompiled.greeter.info.abiDefinition);
undefined
> var greeter = greeterContract.new(_greeting, {from: eth.accounts[0], data: greeterCompiled.greeter.code, gas: 4000000}, 
  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);
      }
    } 
  })
Contract transaction send: TransactionHash: 0xcad6fb4afbf49a77fbd5622435ae010d0665b54f9eda18ad50858effe98f23f5 waiting to be mined...
undefined
Contract mined! Address: 0x170ab13569f1c8c9f99918b7f220ce88d1bc4beb
[object Object]

We can now call the greet() function:

> greeter.greet();
"Hello World!"
  • Why do you write "you don't have to mine the Testnet blockchain as another miner will pick up you transaction and include in another block"? Isn't this only true if you supply enough gas for the transaction to execute? – TMOTTM Jun 14 '16 at 20:13
  • Works, don't know what's different this time. greeterCompiled seems to be more extensive than before.. Need to check. – TMOTTM Jun 14 '16 at 20:39
  • Great. When your account is locked, the greeterContract.new command does not return the "Contract transaction send" message. Even if you are mining yourself, you have to supply enough gas as each VM instruction costs a fixed amount of gas. Some info on gas at ethereum.stackexchange.com/questions/52/…. – Bokky WeAreAllAssange PooBah Jun 14 '16 at 21:00
  • Sure, i know I need to supply gas for my contract to be mined. But I got the ether from faucet.ma.cx:3000 which perhaps is a bit easier. – TMOTTM Jun 15 '16 at 15:46
  • I'm getting an error TypeError: Cannot access member 'info' of undefined when doing the step var greeterContract = web3.eth.contract(greeterCompiled.greeter.info.abiDefinition); – Jus12 Feb 10 '17 at 13:13
1

Because you did not add it.

greeter.at("0xXXXXXXXX")

Where XXXX is the address displayed in the console. Then, the greeter will have an address.

If you missed the message in the console, you can still retrieve it by querying the transaction. If you don't know the transaction address, you should examine the account's address. You can do it from the console or on the Testnet explorer, http://testnet.etherscan.io

  • I found the transactions where I sent myself 4 times 1 ether from faucet. Can you give an example on how I would examine my accounts address on the console? I'm not sure I understand what you mean. greeter.at gives me a type error. – TMOTTM Jun 14 '16 at 4:02
0

You need to sync testnet blockchain, and wait the last block before to see your transaction

look here https://testnet.etherscan.io

last block is BLOCK 1128505

you can type eth.blockNumber under your geth client to see if you are near this block

enjoy

Billy M.

  • Does not think it is his problem. (See my answer.) – bortzmeyer Jun 13 '16 at 22:35

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.