In our previous session, we created an Ethereum account and performed mining. Now, let's start by checking the account balance.
Checking Your Account Balance
First, let's verify the account status. After mining in the last session, the balance of the coinbase address we configured has increased.
> eth.getBalance(eth.accounts[0])
80000000000000000000Note that the unit here is Wei, not ETH. If you've used Ethereum before, you might recognize this unit from transaction details.
Understanding Wei and Ether Conversions
- 1 ETH = 10ยนโธ Wei
- Use
web3.fromWei()to convert Wei to Ether. This function takes two arguments: the value to convert and the target unit (e.g., "ether", "kwei", or "tether"). - For reverse conversions, use
web3.toWei().
> web3.fromWei(1e18, "ether")
"1"
> web3.fromWei(1e3, "kwei")
"1"
> web3.fromWei(1e30, "tether")
"1"
> web3.toWei(1, "kwei")
"1000"Now, let's check the balance in Ether:
> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
80Success! Our balance is 80 ETH.
Sending a Transaction
Now that we've confirmed our balance, let's send a transaction. Currently, we have two accounts:
> eth.accounts
["0xae9abfc6eca2b25d579be8649d4f232f80d3bd46", "0x46c001f57b55abbe9086d595e15cbf7dc3a9b5b2"]We'll send 10 ETH from the first account to the second using eth.sendTransaction(). Specify:
from: Sender addressto: Recipient addressvalue: Amount to send (in Wei)
Unlocking the Account
Before sending, unlock the account using personal.unlockAccount():
> personal.unlockAccount(eth.accounts[0])
Unlock account 0xae9abfc6eca2b25d579be8649d4f232f80d3bd46
Passphrase:
trueNow, execute the transaction:
> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(10, "ether")})
"0x29a48a49a140c0774b6876d343dda6df4af71b00bd515e22f6c9c08d1f4a7a00"The returned hash confirms the transaction was submitted. However, the balance hasn't changed yet because the transaction needs to be mined.
Mining the Transaction
Since mining was stopped earlier, restart it to confirm the transaction:
> miner.start(1)
null
> eth.mining
trueCheck the transaction status:
> eth.getTransaction("0x29a48a49a140c0774b6876d343dda6df4af71b00bd515e22f6c9c08d1f4a7a00")
{
blockNumber: 17,
// ... (other details)
}After mining, stop the miner:
> miner.stop()
null
> eth.mining
falseVerifying Balances
Check the recipient's balance:
> eth.getBalance(eth.accounts[1])
10000000000000000000
> web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")
10The sender's balance now reflects mining rewards:
> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
85Transaction Receipt
For detailed gas usage, use eth.getTransactionReceipt():
> eth.getTransactionReceipt("0x29a48a49a140c0774b6876d343dda6df4af71b00bd515e22f6c9c08d1f4a7a00")
{
gasUsed: 21000,
// ... (other details)
}FAQ
1. Why is my balance still in Wei?
Ethereum defaults to Wei for precision. Use web3.fromWei() to convert to ETH.
2. How do I unlock my account?
Run personal.unlockAccount(address) and enter your passphrase.
3. Why hasn't my transaction gone through?
Transactions require mining. Start mining with miner.start().
4. What is gasUsed in the receipt?
It shows the computational effort (in gas) required for the transaction.
5. Can I send fractions of ETH?
Yes! Specify the value in Wei (e.g., web3.toWei(0.1, "ether")).
๐ Learn more about Ethereum transactions
This guide covers balance checks, sending transactions, and mining in Go Ethereum. For advanced topics like smart contracts, stay tuned for Part 4!