Blockchain Will Completely Revolutionize The Way We Vote

Girik Narang
9 min readFeb 3, 2022

--

Making Elections Transparent and Fair; Using a Voting Dapp.

Photo by Arnaud Jaegers on Unsplash

Think about what happens in an election. You get your ballot out, and you pick your candidate. It seems simple. Yet most people never wonder where these votes go, who counts them or what even happens to them. As citizens, we barely know anything about our election process. The issue with the current system is that votes (whether electronic or on paper) all go through a centralized server or hub. With anything centralized, it's a lot more vulnerable since it isn't challenging to breach this centralized server and tamper with the votes.

In other words, centralized servers enable electoral fraud.

The U.S Presidential Election (2020)

Photo by NBC News

The year 2020 was one of the craziest in recent memory. But for Americans, it was a whole other story. Who knew that months of political conflict, protests, a pandemic, and an upcoming election would be a recipe for disaster?

However, after months of candidates battling it out, the country finally decided who its next president would be. But for the second election in a row, the winner was accused of tampering with the votes.

It became clear that people didn't have faith in security and transparency with how the current election system works. Every election, there is an abundance of controversy regarding the accuracy of the results.

Moreover, with the Covid-19 Pandemic, having people gather in large groups to vote is simply unsafe. The mail-in voting system was proven inefficient and significantly increased the time it takes for the entire election process to occur. In-person voting is inaccessible to citizens who cannot access transportation, those living abroad, or those employed by the military. Further complicating matters, ballots are counted by hand, which makes the whole process lengthy and often runs the risk of recounting ballots repeatedly to ensure the results are as accurate as possible. With online voting platforms, a person risks losing the anonymity that comes with voting in a democracy, and the security measures aren't enough to prevent a breach from occurring.

A simple hack can forever change the future of a country.

We need a new way of voting that's accurate, efficient, and constantly maintains a real-time poll. A system available to everyone, with no risk of votes being tampered with, and where the identity of everyone is anonymous. That's where blockchain comes in.

In fact, rather than explaining how the application works, I will show you how you could host an election by building your own decentralized voting application.

Building A Voting Dapp

Before we get into making the application, let's define some basic terms:

Photo by Christopher Gower on Unsplash

Smart Contracts

  • Smart contracts are self-executing digital contracts with the terms of the agreement between buyer and seller directly embedded into the code
  • They render transactions traceable, transparent, and irreversible
  • The most popular blockchain for running smart contracts is Ethereum

Decentralized Applications (dApps):

  • A dApp is a digital application that runs on a blockchain network of computers instead of relying on a single computer
  • They are entirely decentralized (free from the control and interference of a single authority)
  • Key benefits of dApps are their ability to safeguard user's privacy, their lack of censorship, and their flexibility
Traditional Web vs Decentralized App

What You'll Need

By Karolina Grabowska on Pexels

Metamask:

When deploying a smart contract (Ethereum Mainnet or Test Network), you will require funds to cover gas fees; and therefore need a wallet to store these funds. I highly recommend Metamask as your crypto wallet, although other options exist.

Remix — Ethereum IDE:

After trying multiple IDEs, I found Remix to be the best since there is no installation or need to open the terminal. I used Remix to write and deploy the smart contract (backend).

Replit:

Also, an online IDE which I used for the interactive portion (frontend) of the dApp.

Writing The Smart Contract (Backend)

Step #1: Setting up Your Workspace

Set up your remix workspace with a solidity file, and name it election.sol.

Step #2: Declaring License + Solidity Version

To start, I declared the license, which is an MIT license. When announcing the solidity version, I recommend going with the latest one (unless you are following an older tutorial or a feature you require is no longer available).

Step #3: Define A Contract

Next, define a contract using the "contract" statement.

Step #4: Define The Candidate Structure

Each candidate has an ID (integer), name (string) and vote count(integer).

Step #5: Mapping Candidates + Voters + Candidate Count

We need a place to store our candidates and save the candidate structure; we can do this with mapping. Using mapping in Solidity is similar to using a hash table or dictionary in any other language.

eventVote is explained in step #8

Step #6: Introduce Your Candidates

Now it's time for you to have fun with the election. To go along with the article's theme, I went with Trump and Biden, but you can use whatever you want.

Step #7: Add Candidates Function

This allows us to add additional candidates to the contract.

Step #8: Define Vote Function

This function ensures that only one vote per address can be cast, and when they vote, the vote counter adds only one vote to the current count. Then we can check whether an address has voted if it sends back a true. This section also ties into the eventVote function on step #5, which ensures you can only vote once and for a valid candidate.

Step #9: Return The Vote Count For A Candidate

This last function gives us the total vote count of a candidate.

Step #10: Test + Deploy Your Contract

Deploy your contract to the JavaScriptVM to play around with the voting features. Once satisfied, I recommend deploying to a test network of your choice. You could deploy it directly to the Ethereum Mainnet; however, gas fees are ridiculously high. If you want to see your application in action, use a test network instead (I used Rinkeby).

Once the contract is deployed, we can start developing the application's frontend!

Making It Interactive (Frontend)

Step #1: Set Up New Repl

Go to your Replit account and create a new repl with HTML/CSS/JS for the primary language.

Step #2: Create The Following Files

If you want to create each file as you progress, that's fine, but I recommend creating them beforehand to avoid confusion.

Step #3: Import The ABI (Application Binary Interface)

A smart contract is stored as bytecodes in the blockchain under a specific contact address. Well, the ABI is needed to access this bytecode and essentially tells you which functions you can call on and guarantees that the function will return data in the form you expect.

Once copied, paste it into the contract.js file.

Step #4: Import The Smart Contract Address

Next, we're copying our contract address from Remix into our contract.js file.

Your Smart Contract Address Won't Be the Same as Mine

Step #5: Set Up The Index.js File

The most important parts of the index.js file are web3-eth-contract because that allows us to interact with the smart contract and public/index.html, enabling us to connect with the HTML file.

Step #6: Set Up The Public/index.html File

At this point, you need to import a few scripts into the HTML file. However, I'll also leave a link to the code at the end of the article to make things easier.

You Can Find the Full Code for This Project in the Attached Video (End of Article)

Step #7: Create A Table For Candidates + Votes

Next, we create a table to display the candidates and votes using HTML. Depending on how you want your dApp to look, you could either copy the code below or add some of your elements to take it to the next level!

Step #8: Connect Metamask Function

We can connect a user's Metamask wallet address to the application using web3.js functions.

Step #9: Get Current Vote Count

Now we are essentially writing the function that takes the vote count values of each candidate and updates them in the table we created earlier.

Step #10: Get Current Account Function

Here we have a function to get the current account.

Step #11: Create Vote Function

When a vote is initiated, it will send a voting action to the blockchain through the smart contract and register your vote.

Step #12: Test Your Application

Finally, we have arrived near the end; it's time for you to test your dApp. To do this, enter node index.js in the shell and click the Run button at the top of the screen. Your dApp should launch and look similar to what I have below, depending on any changes you might have made in the HTML file.

At this point, the app should be up and running. I recommend you test it with secondary accounts on your computer by importing or creating new accounts through your Metamask wallet.

Newly Created Accounts Cannot Be Deleted!

So how did it go for you? Were you able to successfully follow along?

If you're seeking more clarification behind the code or want to see the dApp inaction, check out this video I made where I walk you through the entire project.

This project was modified from Dapp University’s How To Build An Ethereum Dapp tutorial. See the original Github project here:

Let's Connect!

If you enjoyed reading this article or learned something new, I’d love to connect on LinkedIn. If you’d like to stay updated on my recent articles or projects, you can subscribe to my monthly newsletter here!

--

--

Girik Narang
Girik Narang

Written by Girik Narang

just trying to solve problems.

No responses yet