Ethereum Network Interaction with Geth

Assuming you have followed along so far and finish the Provisioning section, now it’s time to talk to Ethereum using Geth.

Once Geth is up and in sync with the blockchain, you can attach to it with:

geth attach http://:8545
Welcome to the Geth JavaScript console!  
instance: Geth/v1.7.1-stable-05101641/linux-amd64/go1.9 
coinbase: 0x0e1e9ce68b3254461a95916c185c37dd65468ab5
at block: 1856113 (Thu, 12 Oct 2017 13:25:06 UTC)
modules: eth:1.0 net:1.0 personal:1.0 rpc:1.0 web3:1.0
>

A fresh installation of Geth won’t have any accounts yet:

> eth.accounts 
[]

You can create an account with the newAccount() function:

> web3.personal.newAccount() 
Passphrase:
Repeat passphrase:
"0x4ffd642a057ce33579a3ca638347b402b909f6d6"
>
> web3.personal.newAccount()
Passphrase:
Repeat passphrase:
"0xa3eebd575245e0bd51aa46b87b1ffc6a1689965a"

You can get the balance of any account with getBalance():

> eth.getBalance("0x4ffd642a057ce33579a3ca638347b402b909f6d6") 
0

You can also visit etherscan.io to inspect the address.

To get testnet Ether, you can simply ask a faucet for some:

curl -X POST  \   
-H "Content-Type: application/json" \
-d "{\"toWhom\":\"0x4ffd642a057ce33579a3ca638347b402b909f6d6\"}" \
https://ropsten.faucet.b9lab.com/tap
{   
"txHash" : "0x3e5bfb4bc071e4b0e70eedbc7b667ff6a5e7eb5397a21e16582b85848190ae98" }

You’ll get a transaction hash back and you can follow it on etherscan.

After the transaction is mined you will see the balance increase:

> eth.getBalance("0x4ffd642a057ce33579a3ca638347b402b909f6d6") 
500000000000000000

You can now send Ether to another account with:

eth.sendTransaction(   
{
from:"0x4ffd642a057ce33579a3ca638347b402b909f6d6",
to:"0xa3eebd575245e0bd51aa46b87b1ffc6a1689965a",
value:100000000000000000
}
)

Again, a transaction hash will come back and you can follow it on etherscan.

Once that transaction goes through we can see the balance of both accounts:

> eth.getBalance("0x4ffd642a057ce33579a3ca638347b402b909f6d6") 
399370000000000000
>
> eth.getBalance("0xa3eebd575245e0bd51aa46b87b1ffc6a1689965a")
100000000000000000

You might also want to wrap those large numbers in a fromWei() function:

web3.fromWei(
eth.getBalance(
"0x4ffd642a057ce33579a3ca638347b402b909f6d6"
),"ether"
)
0.39937

Notice that 0x4ffd doesn’t have exactly 4 Ether. This is because a small amount of Ether is spent on gas to fuel the transaction. In the next section we’ll deploy a contract and talk about gas.

This is an excerpt from Concurrence — Ethereum Network Interaction.

Read more about Concurrence here.