smart contract for voting app

under the hood
Smart contracts are the way to store and fetch data from the blockchain network. It is a syntax for writing code to deal with the functionality of storing and fetching data from the blockchain network. In today’s story, I’m developing a smart contract that allows users to vote for a candidate. Apart from this, we will also provide the total votes obtained by the candidate.

to start
I will not go into detail about how to install and run the project and how to compile etc. Let’s bring the logic to the table instead of creating a setup, I’ll add a github repository to deal with the setup.

Logic

  • Each user can vote once using their address
  • Keep track of the candidate list (whoever is standing in the election)
  • Keep track of total votes polled by each candidate

smart contract
3 ways to define voting smart contract –

  • Count the total votes polled by each candidate
  • Verify whether the candidate is valid or not.
  • Once the vote is done, add the votes to the voting count of the candidates.

data structure
We only need to store 2 things, one is the one who is standing in the election and the other is how many votes he got. Of course, you can store more values ​​such as who voted for whom, find the voting timestamp and so on.

mapping(address => uint256) public votesReceived
// votesReceived is an object storing candidate votes counts
address[] public candidateList
// candidateList is an array of candidates address standing in the election
enter fullscreen mode

exit fullscreen mode

If you consider that these things become easy and the reason behind we defined the candidate list as a data structure mapping over an array to O(1) time and more technical votes to be counted.

methods
We need to add votes and calculate total votes as per requirement.

vote count
To fetch vote count you need candidate’s address and using our map data structure we will get total votes in O(1) time.

function fetchVotes(address memory candidate) view returns(uint){
 return candidateList[candidate];
}
enter fullscreen mode

exit fullscreen mode

vote add
To add votes we will simply increase the vote count of the candidate by 1 because each user can have only one vote.

function addVote(address candidate) public{
 return votesReceived[candidate] +=1;
}
enter fullscreen mode

exit fullscreen mode

validation
To verify if the user is voting for the correct candidate, we will only check if the candidate the user is voting for is available in the candidate list, if not, the candidate who is voting is not a valid candidate.

function validCandidate(){
   for(uint i= 0; i < candidateList.length; i++){
     if(candidateList[i] == candidate){
       return isCandidateValid = true;
     }else {
       return isCandidateValid = false;
    }
 };
enter fullscreen mode

exit fullscreen mode

The logic part is finished, the whole code of the smart contract is summarized for down voting.

voting.sol git code

You can deal with the likes of HardHat or Truffle, both of which are environments/frameworks for writing and deploying smart contracts. What matters is the logic part and the data structure and that is why I am going into a lot more detail on environment setup, plugins and migrations.

conclusion
That’s all for today, your next task might be to figure out how to build a UI around it and connect our smart contract with the UI using ether.js and web3.js.
I will cover new story on the same, so stay tuned.

Have a nice day till next time

keep growing
Shrey
i hate reading

Leave a Comment