Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Ethereum Cookbook

You're reading from   Ethereum Cookbook Over 100 recipes covering Ethereum-based tokens, games, wallets, smart contracts, protocols, and Dapps

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789133998
Length 404 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Manoj P R Manoj P R
Author Profile Icon Manoj P R
Manoj P R
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started FREE CHAPTER 2. Smart Contract Development 3. Interacting with the Contract 4. The Truffle Suite 5. Tokens and ICOs 6. Games and DAOs 7. Advanced Solidity 8. Smart Contract Security 9. Design Decisions 10. Other Protocols and Applications 11. Miscellaneous 12. Other Books You May Enjoy

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
You have been reading a chapter from
Ethereum Cookbook
Published in: Aug 2018
Publisher: Packt
ISBN-13: 9781789133998
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image