Reader small image

You're reading from  Mastering Blockchain.. - Third Edition

Product typeBook
Published inAug 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781839213199
Edition3rd Edition
Languages
Right arrow
Author (1)
Imran Bashir
Imran Bashir
author image
Imran Bashir

Imran Bashir has an M.Sc. in Information Security from Royal Holloway, University of London, and has a background in software development, solution architecture, infrastructure management, and IT service management. He is also a member of the Institute of Electrical and Electronics Engineers (IEEE) and the British Computer Society (BCS). Imran has extensive experience in both the public and financial sectors, having worked on large-scale IT projects in the public sector before moving to the financial services industry. Since then, he has worked in various technical roles for different financial companies in Europe's financial capital, London.
Read more about Imran Bashir

Right arrow

Introducing Web3

Web3 is a JavaScript library that can be used to communicate with an Ethereum node via RPC communication. Web3 works by exposing methods that have been enabled over RPC. This allows the development of user interfaces (UIs) that make use of the Web3 library in order to interact with contracts deployed over the blockchain.

In this chapter, we'll explore the Web3 API, and introduce some detailed examples of how smart contracts are written, tested, and deployed to the Ethereum blockchain. We will use various tools such as the Remix IDE and Ganache to develop and test smart contracts, and look at the methods used to deploy smart contracts to Ethereum test networks and private networks. The chapter will explore how HTML and JavaScript frontends can be developed to interact with smart contracts deployed on the blockchain, and introduce advanced libraries such as Drizzle. The topics we will cover are as follows:

  • Exploring Web3 with Geth
  • ...

Exploring Web3 with Geth

In order to expose the required APIs via geth, the following command can be used:

$ geth --datadir ~/etherprivate --networkid 786 --rpc --rpcapi "web3,net,eth,debug" --rpcport 8001 --rpccorsdomain http://localhost:7777

The --rpcapi flag in the preceding command allows the web3, eth, net, and debug methods. There are other APIs such as personal, miner, and admin that can be exposed by adding their names to this list.

Web3 is a powerful API and can be explored by attaching a geth instance. Later in this section, you will be introduced to the concepts and techniques of making use of Web3 via JavaScript/HTML frontends. The geth instance can be attached using the following command:

$ geth attach ~/etherprivate/geth.ipc

Once the geth JavaScript console is running, Web3 can be queried:

Figure 15.1: Web3 via geth.ipc

Now that we've introduced Web3, let's consider how the Remix IDE can be used to deploy...

Contract deployment

A simple contract can be deployed using Geth and interacted with using Web3 via the command-line interface (CLI) that geth provides (console or attach). The following are the steps to achieve that. As an example, the following source code will be used:

pragma solidity ^0.4.0;
contract valueChecker
{
    uint price=10;
    event valueEvent(bool returnValue);
    function Matcher (uint8 x) public returns (bool)
    {
        if (x>=price)
        {
            emit valueEvent(true);
            return true;
        }
    }
}

Run the geth client using the following command:

$ geth --datadir ~/etherprivate --networkid 786 --allow-insecure-unlock –rpc --rpcapi "web3,net,eth,debug,personal" --rpccorsdomain https://remix.ethereum.org --debug --vmdebug –nodiscover

or

$ geth --datadir ~/etherprivate --networkid 786 --allow-insecure-unlock --rpc --rpcapi "web3,net,eth,debug" --rpcport 8001 --rpccorsdomain "...

Interacting with contracts via frontends

It is desirable to interact with the contracts in a user-friendly manner via a webpage. It is possible to interact with the contracts using the web3.js library from HTML-/JS-/CSS-based webpages.

The HTML and JavaScript frontend

The HTML content can be served using any HTTP web server, whereas web3.js can connect via local RPC to the running Ethereum client (geth) and provide an interface to the contracts on the blockchain. This architecture can be visualized in the following diagram:

Figure 15.6: web3.js, frontend, and blockchain interaction architecture

If web3.js is not JavaScript frontend already installed, use these steps; otherwise, move on to the next section, Interacting with contracts via a web frontend.

Installing Web3.js JavaScript library

Web3, which we discussed earlier in this chapter, was looked at in the context of the Web3 API exposed by geth. In this section, we will introduce the Web3...

Development frameworks

There are various development frameworks now available for Ethereum. As seen in the examples discussed earlier, it can be quite time-consuming to deploy the contracts via manual means. This is where Truffle and similar frameworks such as Embark can be used to make the process simpler and quicker. We have chosen Truffle because it has a more active developer community and is currently the most widely used framework for Ethereum development. However, note that there is no best framework as all frameworks aim to provide methods to make development, testing, and deployment easier.

You can read more about Embark here: https://github.com/embark-framework/embark.

In the next section, you will be introduced to an example project to demonstrate the usage of the Truffle framework.

Using Truffle to develop a decentralized application

We discussed Truffle briefly in Chapter 14, Development Tools and Frameworks. In this section, we will see an...

Summary

This chapter started with the introduction of Web3. We explored various methods to develop smart contracts. Also, we saw how the contract can be tested and verified using local test blockchain before implementation on a public blockchain or private production blockchain.

We worked with various tools such as Ganache, the Geth client console, and the Remix IDE to develop, test, and deploy smart contracts. Moreover, the Truffle framework was also used to test and migrate smart contracts. We also explored how IPFS can be used to host the webpages that we created for our DApp, serving as the decentralized storage layer of the blockchain ecosystem.

In the bonus content for this chapter, which we strongly encourage you to use, we practiced the techniques we learned in this chapter, plus other advanced topics such as Drizzle, to create the frontends for DApps easily.

In the next chapter, we will introduce Serenity, Ethereum 2.0, which is the final version of...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Blockchain.. - Third Edition
Published in: Aug 2020Publisher: PacktISBN-13: 9781839213199
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Imran Bashir

Imran Bashir has an M.Sc. in Information Security from Royal Holloway, University of London, and has a background in software development, solution architecture, infrastructure management, and IT service management. He is also a member of the Institute of Electrical and Electronics Engineers (IEEE) and the British Computer Society (BCS). Imran has extensive experience in both the public and financial sectors, having worked on large-scale IT projects in the public sector before moving to the financial services industry. Since then, he has worked in various technical roles for different financial companies in Europe's financial capital, London.
Read more about Imran Bashir