How to do it...

  1. Start the Ethereum console with the console or attach subcommand. The console command starts the geth node and opens the JavaScript console along with it. The attach command is used to connect to an existing node. Here, we will connect to an already running node that you started earlier:
$ geth attach http://localhost:8545
  1. If you are starting a new node and would like to see the log information, start the node with this:
$ geth --verbosity 5 console 2>> /tmp/eth-node.log

Too many logs can pollute your console. To avoid this, start the node with a specific verbosity value:

$ geth --verbosity 0 console
  1. Take a look at the web3 object, which is available for you to interact with:
> web3
  1. For managing the node, you can make use of the admin API. For the list of supported admin operations, run the following command:
> admin

You can check the current node information with this:

> admin.nodeInfo

This will return an object with properties such as enode, protocols, ports, name, and other details related to the current node, as displayed here:

To see the list of peers connected to the current node, use the peers object:

> admin.peers

You also have access to methods that can help you enable or disable RPC/WS.

  1. For handling Ethereum blockchain-related tasks, use the eth object. Run the following command to see the methods supported by it:
> eth

You can check the latest block number (block height) with this:

> eth.blockNumber

You have an option to read the contents of a block. You can pass any block number as a parameter:

> eth.getBlock(301)

In the following screenshot, you can see the block number, difficulty, gas details, miner, hash, transactions, and much more:

eth also has methods related to accounts, transactions, and contracts. We will talk more about those later in this book.

  1. To manage Ethereum accounts, you can make use of the personal method. It has options for creating an account, unlocking the account, sending/signing a transaction, and so on:
> personal
  1. You can play around with mining and its methods through the console:
> miner
  1. This console also has an option to monitor the transaction pool through the txpool object:
> txpool
  1. web3 also offers some generic methods to help you with the interaction. Some of the examples include conversion of values, encoding, and hashing. One such example to convert ether to Wei is given here:
> web3.toWei(1, "ether")
Since it is a JavaScript console, you have complete ECMA5 functionality (Go Ethereum uses Otto JS VM, which is a JS interpreter written in Go). You can declare variables, use control structures, define new methods, and even use any of setInterval, clearInterval, setTimeout, and clearTimeout.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.162.37