Election voting DAPP using Smart contracts (Block Chain)- Part II

Nishant Vemulakonda
5 min readJun 14, 2021

Intro | Continued from PART I :

In the previous part (here), we discussed about need & pros of having a decentralised election voting setup.

In this part, we will discuss in detail, the working of the election voting decentralised app (Dapp) on a local Ethereum blockchain (Ganache).

“DAPP” (OR) A DECENTALISED APPLICATION :-

This is a fully decentralised app due on the following points:

1. The Network is decentralised (Runs on peer-to-peer network).

2. The data is decentralised as each NODE has a copy of latest ledger.

3. The code is decentralised as it lies on each Node of the blockchain as a deployed Smart Contract.

Working:

The Smart Contract is written with Solidity programming (<appname>.sol) & contains the business logic of the application.

The app.js is used for interacting with front end (UI) of web application.

Truffle suite provides following code files to build up the App UI: -

  • Index.html (default code for the web app front end)
  • app.js (boilerplate code for the app user interaction (UI) logic)

Some “Rules” for our local test Election setup:

  • Each eligible voter (an ETH address in this case) is allowed to vote only once in the election.
  • Only the Smart contract is able to add valid candidates in the election. (detailed below).
  • Reads on the (Ethereum) blockchain are free however, writes on the blockchain cost gas (transaction fee).

Each eligible voter (an ETH address in this case) is allowed to vote only once in the election. Using metadata, we create a mapping file for the voter accounts such as to allow only a single voting and restricts from double voting.

Only the Smart contract can add the valid candidates for an election.
Because the smart contract sits on the blockchain itself, every node has a copy of it and any imposter will not be able to add fake/invalid candidates as the consensus would fall through and not accept the fake candidate.

A feature added in the web app to show the eth address with which a user (voter) is logged in currently.

When the vote transaction is successfully submitted (cast), the UI reloads & shows the latest vote count and disables the candidate selection & voting button. (see image).

We model a (valid) candidate using a struct data structure :

1. Unique id (Id)

2. Candidate Name (name)

3. Votecount (Tracks # of votes received for each candidate)

A candidateCount variable as a counter to track # of Candidates in Mapping.

A private function addCandidates is defined in the code so that only the smart contract code is able to add candidates to the election.

Note: Making this function public would cause anyone to add any candidate to the election voting app leading to undesirable outcome.

A public function vote is defined to enable voting functionality for the users.

  • Record if a voter has voted (or not).
  • Update votes received for each candidate using a vote counter.

Metamask:

We would need a way for our Dapp, to connect to our blockchain & interact with the smart contract to let us cast a vote.

For this, Metamask (metamask.io) is used which is a web3 compliant wallet and is available as a chrome extension. Metamask lets us use the accounts provided in our blockchain (Ganache) to vote.

Tests:

Testing of the smart contract thoroughly so that it is bug free:

Reasons:

  • Smart contracts are immutable, once deployed cannot be changed.
  • Deploying smart contract costs fee & multiple deployment is not desirable.

Project uses tests built using Mocha test framework to check for:

  • Voter voting for the first time.
  • Voting for invalid candidates (candidates not listed in smart contract).
  • Double voting (voting multiple times).
  • Validate Candidate vote count.

Challenges:

Some challenges inherent to Dapps running on a blockchain network are listed below:-

Deployment of a smart contract requires gas (transaction fee) each time as Blockchain works on the principle of immutability.

  1. Gas fee is needed for each & every vote transaction & it can fluctuate.
  2. Ensure bug free smart contract code to reduce gas cost.

Summary:

Though this Election Dapp serves well for a basic use case, additional features & functionalities can be added to mimic a real case scenario:-

  • Timer can be added to run the election for a limited time only.
  • Feature to include & limit only pre-registered voters in the election.

The code repo for the Election voting Dapp project is available on my GitHub page or dappuniversity.

Notes:

  • The election Dapp runs on a local Ethereum blockchain built on Ganache and provides us 10 ETH addresses (accounts) to work with.
  • These accounts contain only fake ETH and do not actually cost us gas fee in the setup.
  • However, smart contract for any Dapp should be thoroughly tested on a test net before deployment on any live blockchain (main net).

Please clap, share & comment for more such useful articles in future.

--

--

Nishant Vemulakonda

Prolific Blockchain Smart Contract Developer, Experienced Data Scientist , forever Learner