Introduction to the Voting DApp – Logic, Entities, and Interactions

Welcome to the first article in our series on building a decentralized voting application (DApp) using the Ethereum network! If you're new to blockchain or DApps, don't worry—this guide is designed to be beginner-friendly while still diving into the technical details. By the end of this series, you'll have a fully functional voting DApp that demonstrates how blockchain can revolutionize trust and transparency in elections.
In this article, we’ll lay the foundation by exploring the logic behind our voting DApp, the key entities involved, how they interact, and the access permissions for each entity. Let’s dive in!
The Big Picture: What Does This Voting DApp Do?
Imagine a world where voting is secure, transparent, and tamper-proof. Our voting DApp achieves this by leveraging the Ethereum blockchain. Here’s what it does at a high level:
Create Elections : An admin can create an election with a name, description, start time, and end time.
Register Candidates : Candidates can be registered for specific elections.
Register Voters : Eligible voters (18+ years old) can register themselves.
Cast Votes : Registered voters can cast their votes during the active election period.
End Elections : Once the election ends, the system determines the winner based on the highest number of votes.
All of this happens on the blockchain, ensuring that no one can tamper with the results or manipulate the process.
Key Entities in the Voting DApp
To understand how this DApp works, let’s break it down into its core components—or entities . These entities are like the characters in our story, each playing a specific role:
1. Owner (Admin)
The owner is the person who deploys the smart contract and manages the voting system.
Responsibilities:
Create elections.
End elections and declare winners.
Access Permissions:
- Only the owner can call functions like
createElection
andendElection
. This is enforced using theonlyOwner
modifier in the smart contract.
- Only the owner can call functions like
2. Elections
Elections are the backbone of the DApp. Each election has:
A unique ID.
A name and description.
Start and end times (to ensure voting happens within a specific timeframe).
A status (
isActive
) to indicate whether the election is currently running.Counters for candidates and voters.
Example:
Election Name: “School President Election”
Description: “Vote for your favorite candidate to lead the student council.”
Start Time: October 15, 2023, 9:00 AM
End Time: October 15, 2023, 5:00 PM
3. Candidates
Candidates are individuals running for office in a specific election.
Each candidate has:
A unique ID.
A name, party affiliation, and personal information.
A vote count to track how many votes they’ve received.
An association with the election they’re running in.
Example:
Candidate Name: “Alice Johnson”
Party Affiliation: “Green Future Party”
Personal Info: “Passionate about sustainability and education.”
4. Voters
Voters are the people who participate in the election by casting their votes.
Each voter has:
A unique Ethereum address (used as their identity).
Personal details like name, age, and gender.
A registration status (
isRegistered
) to ensure only eligible voters can participate.A mapping to track whether they’ve voted in a specific election (
hasVoted
).
Example:
Voter Name: “John Doe”
Age: 22
Gender: Male
Ethereum Address:
0x123...abc
How Do These Entities Interact?
Now that we know who the players are, let’s see how they work together:
Creating an Election
The owner creates an election by calling the
createElection
function. They provide details like the name, description, start time, and end time.Example Interaction:
createElection("School President Election", "Vote for your favorite candidate!", 1697385600, 1697407200);
- This creates an election scheduled from October 15, 2023, 9:00 AM to 5:00 PM.
Registering Candidates
Candidates can be registered for a specific election using the
registerCandidate
function.Example Interaction:
registerCandidate(1, "Alice Johnson", "Green Future Party", "Passionate about sustainability.");
- This registers Alice as a candidate for Election ID 1.
Registering Voters
Voters register themselves by calling the
registerVoter
function. They must be at least 18 years old.Example Interaction:
registerVoter("John Doe", 22, "Male");
- This registers John as a voter.
Casting Votes
During the active election period, registered voters can cast their votes by calling the
castVote
function.Example Interaction:
castVote(1, 1);
- This records John’s vote for Candidate ID 1 in Election ID 1.
Ending the Election
After the election ends, the owner calls the
endElection
function to calculate the winner.Example Interaction:
endElection(1);
- This ends Election ID 1 and declares the winner based on the highest vote count.
Access Control: Who Can Do What?
One of the most important aspects of any voting system is ensuring that only authorized users can perform specific actions. Here’s how access is controlled in our DApp:
Only the Owner :
Can create elections (
createElection
).Can end elections (
endElection
).
Any User :
- Can register themselves as a voter (
registerVoter
), provided they meet the age requirement.
- Can register themselves as a voter (
During Registration Period :
- Candidates can be registered for an election (
registerCandidate
).
- Candidates can be registered for an election (
During Active Election Period :
- Registered voters can cast their votes (
castVote
), but only once per election.
- Registered voters can cast their votes (
After the Election Ends :
- The winner is calculated automatically when the owner ends the election.
Why Build This on Ethereum?
You might be wondering, “Why use Ethereum for voting?” Great question! Here’s why:
Transparency : Every action (creating elections, registering candidates, casting votes) is recorded on the blockchain and can be audited by anyone.
Immutability : Once data is written to the blockchain, it cannot be altered. This ensures the integrity of the voting process.
Decentralization : There’s no central authority controlling the system, reducing the risk of fraud or manipulation.
Security : Ethereum’s robust infrastructure makes it difficult for bad actors to tamper with the system.
Wrapping Up
Congratulations—you now have a clear understanding of the logic, entities, and interactions in our voting DApp! In the next article, we’ll roll up our sleeves and dive into the Solidity code to develop the smart contract that powers this system.
Your Task : Take a moment to think about how this voting system could be applied in real-world scenarios. Could it be used for school elections, community decisions, or even corporate governance? Share your thoughts in the comments below!
Stay tuned for Article 2, where we’ll write and explain the Solidity smart contract step by step. See you there! 🚀
Subscribe to my newsletter
Read articles from chainyblock directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

chainyblock
chainyblock
👋 Hi, We are ChainyBlock, a passionate advocate for blockchain technology and its transformative potential. With a background in software engineering and cybersecurity, We've spent a lot of time exploring how decentralized systems can reshape industries, foster trust, and create a more inclusive future. 🎯 What Drives Me? I believe that understanding complex technologies like blockchain shouldn’t be reserved for experts—it should be accessible to everyone. That’s why I’m here: to break down the fundamentals of Web3, cryptocurrencies, smart contracts, and decentralized applications into simple, actionable insights. Whether you’re a beginner or a seasoned learner, my goal is to help you navigate this rapidly evolving space with confidence. 💭 Dreams for the Future: I dream of a world where blockchain technology enables secure, transparent, and efficient systems for everyone—regardless of location or background. Through education and collaboration, I hope to inspire others to embrace the possibilities of Web3 and contribute to this global movement. 🌟 Let’s Connect: Feel free to reach out if you’d like to discuss blockchain innovations, collaborate on projects, or share ideas. Together, we can build a smarter, decentralized future. 🌐💡