Smart contract for voting app
Tags: blockchain, web3, webdev, programming
Published: 2022-06-17 09:07:31

Smart contract to cast a vote

**Under the Hood**
Smart contracts are the way to store and fetch data from the blockchain network. It's a syntax of writing code to deal with functionalities of storing and fetching data in and from the blockchain network. In today’s story, I will be developing a smart contract to allow users to vote for the candidate. In addition, we will also provide the total votes received by the candidate.

**Getting Started**
I will not go into detail about how to set up the project and how to run and compile and so on. Let's bring the logic to the table instead of creating the setup, I will add the github repository to deal with the setup.

**Logic**
Every user can vote once using his/her address
Keep the track of the candidate list (whoever is standing in the election)
Keep the track of the total votes received by each candidate
Smart Contract
3 methods to define the voting smart contract -

Calculate the total votes received by each and every candidate
Validate if the candidate is valid or not.
Add vote to the candidates voting count once the vote is done.

**Data structure**
We need only 2 things to store, one is who is standing in the election and the second is how many votes he/she has received. Of course, you can store more values such as who has voted to whom, trace 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
```

If you understand these things become easy and the reason behind why we have defined candidate list as the mapping data structure over an array is to fetch the votes count in no time more technical in O(1) time.

**Methods**
We need to add the votes and return the total votes count as per as requirements.

**Votes count**
To fetch the votes count you need the candidate address and using our map data structure we will fetch the total votes in O(1) time.

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

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

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

**Validation**
To validate if the user is voting for the right candidate we will simply check if the candidate user is voting for is available in the candidate list, if not then the voting candidate is not the valid candidate.

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

The logic part is over, below is the entire code gist of the smart contract for voting.

[Code repository](https://gist.github.com/shreyvijayvargiya/f77e3ccfbb2f4d8196ff172fbb5c6640#file-voting-sol)

You can deal with hardhat or truffle choice is your both of them are the environment/framework to write and deploy smart contracts. What matter is the logic part and data structure and that is why I am going into too much detail on environment setup, plugins and migration.

**Conclusion**
That’s it for today, your next job might be how to create a UI around this and connect our smart contract with the UI using ether.js and web3.js. I will cover the new story on the same, so stay tuned and have a good day, People.



Our website [iHateReading](www.ihatereading.in) || [Youtube](https://www.youtube.com/channel/UC6I-Tz6QwYbJpoIK7l0NtXA) || [Twitter](https://twitter.com/treyvijay)