Blockchain Will Completely Revolutionize The Way We Vote
Making Elections Transparent and Fair; Using a Voting Dapp.
Think about what happens in an election. You get your ballot out, and you pick your candidate. It seems simple, doesn’t it? 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)
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 their 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. To make matters worse, 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 runs the risk of 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 maintaining a real-time poll. A system available to everyone, no risk of votes being tampered with, and where the identity of everyone is anonymous. That’s where blockchain comes in. In fact, I’m going to show you how you could host an election by building your very own voting decentralized application.
Building A Voting Dapp
Before we get into making the application, let’s define some basic terms:
- 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
- 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 users privacy, their lack of censorship, and their flexibility
What You’ll Need
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 IDE’s, 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).
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.
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 ties into the eventVote function on step #5, which ensures that 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
Once the contract is deployed, we can start developing the frontend of the application!
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 bytecode into the blockchain under a specific address known as the 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 are expecting.
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.
Step #5: Set Up The Index.js File
The most important parts of the index.js file are web3-eth-contract because that’s what allows us to interact with the smart contract, and public/index.html, which will enable 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 be leaving a link to the code at the end of the article to make things easier.
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 the 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 highly recommend you test it with secondary accounts on your computer, either by importing or creating new accounts through your Metamask wallet.
So how did it go for you? Were you able to successfully follow along?
If you’re seeking some more clarification behind the code or just want to see the dApp inaction, check out this video I made where I walk you through the entire project.