Book Image

Ethereum Cookbook

By : Manoj P R
Book Image

Ethereum Cookbook

By: Manoj P R

Overview of this book

Ethereum and Blockchain will change the way software is built for business transactions. Most industries have been looking to leverage these new technologies to gain efficiencies and create new business models and opportunities. The Ethereum Cookbook covers various solutions such as setting up Ethereum, writing smart contracts, and creating tokens, among others. You’ll learn about the security vulnerabilities, along with other protocols of Ethereum. Once you have understood the basics, you’ll move on to exploring various design decisions and tips to make your application scalable and secure. In addition to this, you’ll work with various Ethereum packages such as Truffle, Web3, and Ganache. By the end of this book, you’ll have comprehensively grasped the Ethereum principles and ecosystem.
Table of Contents (13 chapters)

Working with the JavaScript console

The geth command-line utility has an inbuilt JavaScript Runtime Environment (JSRE). This JavaScript console exposes all web3js objects and methods. You can use this JSRE as a REPL (Read, Execute, Print, Loop) console.

In this recipe, we will learn how to connect and use the JSRE in interactive (console) and non-interactive (script) modes.

Getting ready

You should have the geth command-line tool installed on your system to test this recipe. You can either start a node along with the console or connect to an already existing node.

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.

There's more...

You can also execute JavaScript commands in a non-interactive way. There are two different approaches to doing this:

  1. 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.

  1. 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