0% found this document useful (0 votes)
17 views36 pages

Build Your Ethereum DApp Step-by-Step

This document outlines the steps to build an Ethereum decentralized application (DApp), starting with setting up a private Ethereum network using the go-ethereum (geth) client. It details the creation of a smart contract for polling functionality, deployment of the contract, and interaction with it through a front-end web application using the web3 library. The document also includes specific commands and configurations for initializing and running nodes on the private network.

Uploaded by

nagarjunht00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views36 pages

Build Your Ethereum DApp Step-by-Step

This document outlines the steps to build an Ethereum decentralized application (DApp), starting with setting up a private Ethereum network using the go-ethereum (geth) client. It details the creation of a smart contract for polling functionality, deployment of the contract, and interaction with it through a front-end web application using the web3 library. The document also includes specific commands and configurations for initializing and running nodes on the private network.

Uploaded by

nagarjunht00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Building an Ethereum DApp

MODULE--5
• The first step will be to set up a private Ethereum network.

• Then, for hosting the business logic and poll results, we will create a
smart contract that will be deployed on this private Ethereum
network.

• To interact with this smart contract, we will create a front-end web


application using the web3 library.
DApp development will have the following
steps:
1) Setting up a private Ethereum network

2) Creating a smart contract for polling functionality

3) Deploying the smart contract to the private network

4) Creating a front-end web app to interact with the smart

contract
Setting Up a Private Ethereum Network
• Ethereum client is an application that implements the Ethereum
blockchain protocol.

• There are many Ethereum clients available on the Internet today

• The popular one is go-ethereum, also known as geth.


INSTALL GO-ETHEREUM (GETH)
• To install geth, we will get the geth executable installer from the official source
[Link] .

• Download the installer package for your platform and install geth on your local machine.

(You can also choose to install geth on a remote (cloud-hosted) server/virtual


machine)

• Once geth is successfully installed on your local machine, we can check the installation by
running the following command in your terminal/command prompt.
geth version
• Depending on your platform OS and the geth version you have installed,
this command should give an output similar to the following:
• Geth
Version: 1.7.3-stable Git Commit:
4bb3c89d44e372e6a9ab85a8be0c9345265c763a Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9 Operating System: linux GOPATH=
GOROOT=/usr/lib/go-1.9
1) CREATE GETH DATA DIRECTORY
create a directory ---mkdir mygeth
2) CREATE A GETH ACCOUNT
We need this account to create our smart contracts and transactions
later in the DApp development.
We can create a new account using the following command:
sudo geth account new --datadir < path to the data directory we
created in the previous step >
sudo geth account new --datadir /mygeth
Note: We are using sudo to avoid any permission issues
• When you run this command, the prompt will ask for a password to
lock this account.
• Enter and confirm the password and then your geth account will be
created.
• Make sure to remember the password you entered; it will be needed
to unlock the account later to sign transactions.
• The address of this account will be shown on the screen.
baf735f889d603f0ec6b1030c91d9033e60525c3
CREATE GENESIS. J SON CONFIGURATION FILE
• After installing geth and creating a new account, the next step is to define the
genesis configuration for our private network.
• Now we have Genesis block that acts as the starting point of the blockchain, and
all transactions and blocks are validated against the genesis block.
• For our private network, we will have a custom genesis block and hence a custom
genesis configuration.
• This configuration defines some key values for the blockchain like difficulty level,
gas limit for blocks, etc.
• The genesis configuration for Ethereum has the following format as a JSON
object
Each of the keys of this object is a configuration value that drives the network
{
"config": {
"chainId": 3792,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"difficulty": "2000",
"gasLimit": "2100000", "alloc": {
"baf735f889d603f0ec6b1030c91d9033e6052 5c3": { "balance":
"9000000000000000000" }
}
}
• The JSON object is primarily constituted by a config section having values
specific to chainId and block numbers.

The important parameter to note here is the chainId, which represents the
identifier of the blockchain and helps prevent replay attacks.

• For our private chain, we have opted for a random chainId 3792.

The next important parameter is the difficulty.

• This value defines how difficult it will be to mine a new block.

• This value is much higher in the Ethereum main network, but for private
networks we can choose a relatively smaller value.
Then there is gasL imit.

• This is the total gas limit for a block and not a transaction.

• A higher value generally means more transactions in each block.

Finally, we have the alloc section.

• Using this configuration, we can prefund Ethereum accounts with the


value in wei.
RUN THE FIRST NODE OF THE PRIVATE
NETWORK
Copy the JSON from the previous step and save it as a file named
[Link].

For simplicity, we can save the file in the same directory that we are using as
the data directory for geth.

First, we need to initialize geth with the genesis. json. This initialization is
needed to set the custom genesis configuration for our private network.
• CD to the directory where we have saved the genesis. j son file is
cd mygeth
• The following command will initialize geth with the custom
configuration we have defined.
sudo geth --datadir "/mygeth" init [Link]
geth will confirm the custom genesis configuration set-up with the
output in the following
• Next, we need to run geth using the following command and the
parameters,

sudo geth --datadir "/mygeth" --networkid 8956

--ipcdisable—port 30307—rpc --rpcapi "eth, web3, personal, net,


miner, admin, debug" -- rpcport 8507 --mine –minerthreads = 1--
etherbase=0xbaf735f889d603f0ec6b1030c91d9033e6 0525c3
The parameters that we gave to the geth command
• datadir: This is to specify the data directory just like we did in the previous
steps
• networkid: This is the identifier of the network, which differentiates our
private blockchain with other Ethereum networks. This is similar to the
chainId.
• ipcdisable: With this parameter we have disabled the interprocess
communication port for geth so that while running multiple geth instances
(nodes) on the same local machine we should not encounter any conflicting
issues
• port: We have selected a custom value for the port to interact with geth.

• rpc, --rpcapi, --rpcport: These three parameters define the configuration


for the RPC API exposed by geth, we want
eth,web3,personal,net,miner,admin,debug geth APIs exposed over RPC;
and we want to run it on a custom port 8507 and enable it.

• mine – minerthreads – etherbase : With these three parameters we are


instructing geth to start this node as a miner node, limit the miner process
threads to only one and send the mining rewards to the Ethereum account
that we created
INFO [02-11| [Link] U DP listener
upself=enode://e03b50e9b1b2579904f2bbdff7dd082
6bd4e4eb2e225c1d1cb1a765195474d7418f3e8fbfeefd
55bd85722973d17626f0e53208c62e38d1099bb583e702
b3b48@[::]:30307
• This contains the address of the node we just started.
• To connect other nodes to this node we will need this address.
• Note the [::] before the port number we defined in the command.
• It can be replaced with the local host IP address if we are running the
other node on the same machine, or else replace it with the external
IP address of the machine.
• Since we are going to run the other network node on the same
machine we will replace it with the localhost IP address.

• So, the address of the first node will finally be—

enode://e03b50e9b1b2579904f2bbdff7dd0826bd4e4e
b2e225c1d1cb1a765195474d7418f3e8fbfeefd55bd857
22973d17626f0e53208c62e38d1099bb583e702b3b48@
[Link]:30307
RUN THE SECOND NODE OF THE NETWORK
• To run another node, first of all we need another directory that can be set as the
data directory of the second node
• Let us create one.
mkdir mygeth2
• we will initialize this node also with the same [Link] configuration we
created for the first node.
• sudo geth --datadir "/mygeth2" init [Link]
we will get a similar output as we got for the first node
• For running the second node, we will pass a few different parameters to
the geth command.
• This second node will not run as a miner, so we will skip the last three
parameters from the command that we gave to the first node.
• Also, we want to expose the geth console while running this node, so we
will add a parameter for that.
• The command for running the second node will be

sudo geth --datadir "/mygeth2" --networkid 8956 --ipcdisable --port 30308


--rpc --rpcapi "eth,web3,personal,net,miner,admin,debug" -- rpcport 8508
console

• Here the data directory and ports have been changed.

• At this time, both our geth nodes are running but they do not know about
each other.

• If we run the [Link] command on the geth console of the second


node, we will get an empty array as the result
• This means that the nodes are not connected to each other.
• To connect this, we will send the [Link]() command on the
geth console of the second node with the node address of the first
node as the parameter.
• To run this command in the second node’s geth console.

[Link]("enode://e03b50e9b1b2579904f2bbd
ff7dd0826bd4e4eb2e225c1d1cb1a765195474d7418f3e
8fbfeefd55bd85722973d17626f0e53208c62e38d1099b
b583e702b3b48@[Link]:30307")

• As we run this command on the second node, it returns true.


• Also, after a few seconds it starts synchronization with the first node.
• The following screen shot shows this output from the console of the
second node

A440588_1_En_6_Fig7_HTML.jpg

• Both our nodes are now connected and our private Ethereum
network is up.
• To further verify this, we can run the [Link] command again on
the second node
Creating the Smart Contract
• We can move on to the next step of creating a smart contract for the
polling functionality of our DApp.
• We will then deploy this contract to our private network.
• The Solidity code snippet shows the smart contract that have coded
for the polling functionality.
pragma solidity ^ 0.4.19;
contract Poll {
event Voted(
address _ voter,
uint _ value
);
mapping(address =>uint) public votes;
string pollSubject = "Should coffee be
made tax free? Pass 1 for yes OR 2 for no in the vote function.";
function getPoll() constant public returns (string) {
return pollSubject;
}
function vote(uint selection) public { Voted([Link], selection);
require (votes[[Link]] == 0);
require (selection > 0 && selection
3;
votes[[Link]] = selection;
}
}
The name of the contract is Poll.

The 1st line of code is

event Voted(

address _ voter, uint _ value

);

Here we are declaring a smart contract event that takes two


parameters: one is of the type of Ethereum address and another is of
the type of unsigned integer.
Next, we have

mapping(address =>uint) public votes;

• It declares a mapping of Ethereum addresses and unsigned integers.

• This is the data store where we will be storing the voters’ addresses and their chosen
value for the vote.

Then we have

string pollSubject = "Should coffee be made tax free? Pass 1 for yes OR 2 for no in the vote
function”.

function getPoll() constant public returns (string) {

return pollSubject;

}
• Here we first declares a string for the polling subject.

• In this we are asking a question of the voters.

• And then we have a function that can return the value of this string
so that voters can query what the poll is about.

• And finally, we have the function that implements the voting


functionality.
function vote(uint selection) public { Voted([Link], selection);
require (votes[[Link]] == 0);
require (selection >0 && selection < 3);
votes[[Link]] = selection;
}
• First, as soon as we enter this function, we are raising the voted event
we created with the values of the sender’s address (voter) and the
value he has chosen.
• Next, we are limiting one vote per voter by checking if the value of
the vote is zero for the corresponding address in the mapping.
• The require statement is used to check conditions based on user
inputs
• And then we are also limiting, by using the require statement, the
value of the selection to either 1 or 2.

• 1 is a yes and 2 is a no.

• And we have passed these instructions in the poll Subject string so


that the voters know what to do.

“The code can be compiled using Remix and we can take ABI
(Contract Application Binary Interface) and byte code for the contract
so that we can deploy it to our private network”
Deploying the Smart Contract
1) SETTING UP WEB3 LIBRARY AND CONNECTION
• First we need to install the web3 library in a [Link] application.
npm install web3@1.0.0-beta.28
• After installation, first we initialize and instantiate the web3 instance
var Web3 = require('web3');
var web3 = new Web3(new
[Link]('[Link] 8507'));
• Port we are using is 8507, which is what we provided in the --rpcport
parameter when we set up the first node of our private network.

You might also like