How to build a dapp on a private Ethereum network : Part — 2

Amey
Coinmonks
5 min readMar 12, 2020

--

This is a series of 5 articles for developing a decentralized application (dapp) on a network of 2 private Ethereum nodes without using any third-party APIs or apps (such as Infura, MetaMask, etc.). This tutorial covers the essential elements of what I learn during my research project at CSIR-CEERI.

In the previous article (part-1), I covered sections I-III from the table of contents given below.

If you want to skip to a particular section, you can scroll down to the All series links section at the end of this article.

Recently released — “Part 5 : Section VIII-IX

Table of Contents

Here, you can find a list of the sections and subsections that I have divided this tutorial into :

I. Creating the project

II. Installing prerequisites

III. Configuring the network

→ IV. Designing the smart contract

V. Setting-up the Truffle project

VI. Launching the network

VII. Building the web app

VIII. Testing the dapp

IX. Modifying the dapp

The terms “smart contract” and “blockchain” are viral. One of the top buzzwords. Everyone has heard them, but few know what they really are. Fear no more, it will take you at most 20 minutes to be one of those few.

In part-2, I will explain the simplest way to design and test the smart contract for our application (section IV). You can refer to the table of contents mentioned below to understand the flow of this development process.

IV. Designing the Smart Contract

The best way to construct and test a smart contract is the online Remix IDE. It has an interactive interface and is well-equipped to simulate a live Ethereum environment. For more information, refer to the Remix IDE docs.

a) Contract code

The sections circled correspond to the 3 steps mentioned below
  • Open the Remix IDE and click on the “Solidity” button under the “Environment” heading in the “Home” tab.
  • Under the “Solidity Compiler” menu, choose the 0.4.25+commit.59dbf8f1 option from the “Compiler” drop-down list and select the “Auto Compile” tick box.
  • Click on “New File” from the “Home” tab. Type helloworld.sol as the file name. A new tab opens up in the form of an editor window. Type in the following code :

Pro-tip : To increase the font size of the editor where the code is typed in, click inside this area and press Ctrl and “+” on your keyboard.

  • The contract should compile automatically. You can also click on the “Compile helloworld.sol” button under the “Solidity compiler” menu OR hit Ctrl + S (see figure below).
The symbol circled on the left panel is the “Solidity compiler” menu

The contract used above is self-explanatory. Besides, comments have also been used to ease understanding of its working.

Apart from functions and variables for the 3 key functionalities mentioned before (just under the title Dapp Development), some additional functions need to be explicitly programmed as Solidity does not have predefined libraries or modules (here, append).

  • substring : This enables the programmer to append (join) 2 strings together.

b) Testing the contract

After constructing the contract, testing it is necessary to ensure it is free of bugs and errors since the smart contract is the core component that enables and simplifies communication between the geth console (i.e. the user) and the blockchain.

  • Under the “Deploy and Run Transactions” menu, select the “JavaScript VM” option from the “Environment” section. The JS VM will execute all transactions in a sandbox blockchain in the web browser. When you reload the page, a new blockchain is started and the old one is not be saved, i.e. information does not persist (see figure below).
The symbol circled on the left panel is the “Deploy and Run Transactions” menu
  • Then click on the “Deploy” button to generate an API of the smart contract. The drop-down arrow of this instance reveals buttons corresponding to each public function and variable of the smart contract, by clicking which you can call the corresponding function/variable.
Observe the input parameter and output used for this instance

To test whether the contract works as intended, do as follows :

  1. Enter your name in double quotes (“YourName”) in thestring _name string _name field and click on the “inputUser” button.
  2. Click on “dispUser” button and verify the function output.
The actions performed above can also be cross-checked by using the console window that opens after you click on the circled button

Pro-tip : Clicking inside any of the rectangles shown below will display detailed information about the corresponding action. Go ahead and try it out!

This shows the confirmation for the transaction that deployed the smart contract
This shows the transactions that : (1) Changed the value of the “name” variable when “inputUser” button is clicked, and (2) Displayed the output as “My name is…” when “dispUser” button is clicked

This concludes section IV of this tutorial. The other sections are being typed in as you read this article.

In the next part, we will learn how to set-up a private Ethereum network of 2 nodes using geth : How to build a dapp on a private Ethereum network : Part — 3

P.S. — Clap 10 times if you liked the article! Comment below to let me know your thoughts or if you want to share some hacks.

I will be publishing more such interesting articles shortly. Follow me on Medium or stalk me on Twitter to stay tuned.

All series links

In case you want to skip ahead and jump onto a specific section, you can use the links below for reference. Refer to the Table of Contents below to match a section with its corresponding topic.

Part-1 : Section I-III

Part-2 : Section IV

Part-3 : Section V-VI

Part 4 : Section VII

Part 5 : Section VIII-IX

Get Best Software Deals Directly In Your Inbox

--

--

Amey
Coinmonks

A product afficianado and an endorser of decentralization | Everything about me is at ameys.in