In this recipe, we will learn how to connect and use the JSRE in interactive (console) and non-interactive (script) modes.
- 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
- 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
- Take a look at the
web3
object, which is available for you to interact with:
> web3
- 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.
- 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.
- 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
- You can play around with mining and its methods through the console:
> miner
- This console also has an option to monitor the transaction pool through the
txpool
object:
> txpool
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")
Note
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
.
You can also execute JavaScript commands in a non-interactive way. There are two different approaches to doing this:
- Use the
--exec
argument, which takes JavaScript as input. This will work with both the console
and attach
commands:
$ geth --exec "eth.accounts" attach http://localhost:8545
This will print the list of accounts stored in the current node.
- You can execute more complex scripts with the
loadScript
method. The path to your scripts folder can be specified with the --jspath
attribute:
$ geth --jspath "/home" --exec 'loadScript("sendTransaction.js")' attach http://localhost:8545