Decentralized Voting System

Mir AbdulHaseeb
CodeX
Published in
4 min readOct 17, 2021

An election system hosted on blockchain.

Photo by Element5 Digital on Unsplash

Introduction

Here’s a decentralized voting system that allows (registered) voters to vote (registered) candidates and when the voting has ended announces a winner. To get registered the candidate has to pay some fee (100 wei). The owner of the contract adds the candidate to the contract to be voted.

The code is written in Solidity. There are unit test written in Python and the framework used to test the contract is brownie. You can visit this link if you are looking to choose a framework.

For demonstration, the UI that will be used throughout this article is remix. Remix IDE is a great tool that let’s you run your solidity code in the browser.

Features

Following are the key features of this contract:

  • Add candidates once they pay the fee
  • Register voters
  • Announce the winner at the end of election
  • The owner of the contract can withdraw the fees once voting has ended

Demonstration

When you open remix IDE you’ll find some smart contracts under contracts folder. These files end with .sol extension. You can make a new file by selecting the Create New File icon. Choose a name for the file and end it with .sol extension i.e. “Election.sol”. Copy the content of this file and paste it into the file you just created.

How it looks in remix IDE

Navigate to deploy and run transactions. Type a name for the election system and hit deploy.

Now under deployed contracts section you’ll find the contract you just deployed. You’ll find some buttons and input fields to interact with the contract.

Note: They may not be in the same order as the flow of the application.

Let’s start interacting with the contract.

Adding Candidates

Candidates are required to pay a fee of 100 wei to get registered. Select a different account than the one used to deploy the contract i.e. the owner. For me it’s “0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2”. Type 100 in value field and make sure the unit next to it is wei. Press registerCandidate button.

Candidate pays 100 wei to get registered

Now select the owner’s address and in the addCandidate input field paste the candidate’s address who just paid the fee and also type his name. You can either type the inputs in a single line (“0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2”, “Dave”) or press that arrow symbol next to addCandidate button to enter the information in separate fields as shown below:

Add Candidate

Press transact to add the candidate. Repeat these steps to add more candidates.

Registering Voters

To register a voter you’ll need to enter the voter’s address in registerVoter field. Make sure you select the owner’s address before pressing registerVoter button.

Register voter

You can add as many voters as you want.

Start Vote

Once we have the candidates and voters registered we can go ahead and start our voting. Press the startVote button to set the state to voting.

Vote Casting

To cast vote make sure you select a voter’s address from account section. Enter the candidate’s address you want to vote and press the vote button.

Cast Vote

End Vote

We can end the voting to get the winner of the election. Press endVote to set the state to ended.

Announce Winner

To get the winner press announceWinner button. You’ll see the winner’s address.

Announce Winner

Withdraw Registration Funds

To get the registration fees from the contract select owner’s address in the account section and press withdrawRegistrationFunds button. This will transfer all the funds to the owner’s address.

Conclusion

We’ve seen the contract in action with remix IDE. The next step should be to learn how to interact with this contract using a Smart Contract Development framework. A framework is a powerful tool that will let you write test cases and scripts that give you more flexibility and customization. You can choose between Truffle, HardHat and Brownie.

Hope this serves as a good learning project. Here’s the link to the code. Enjoy!

--

--