How to build a dapp on a private Ethereum network : Part — 2
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
- 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.
pragma solidity ^0.4.25;contract HelloWorld {
// Define a string variable
string name;
// Modifies the value of string variable (name)
function inputUser(string _name) public {
name = _name;
}
// Validated that the string variable (name) holds the updated value
function dispUser() public view returns(string){
return (append("Hello world! My name is ", name));
}
// Appends 2 strings a, b
function append(string a, string b) internal pure returns (string) {
return string(abi.encodePacked(a, b));
}
}
- 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 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).
- 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.
To test whether the contract works as intended, do as follows :
- Enter your name in double quotes (
“YourName”
) in thestring _namestring _name
field and click on the “inputUser” button. - Click on “dispUser” button and verify the function output.
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 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
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.