Skip to main content

Command Palette

Search for a command to run...

Voting?? On Solidity??

Published
3 min read
Here we go Again
Once upon a time I registered on HashNode about a year ago and my account ever since has been eating dust, but its time now. The revival begins.

So why the sudden change of heart?

I find myself having a great excuse to go ahead and write a blog finaly.

What is it?
Thanks to TGP Karnataka for conducting 3 day workshop about Blockchain, Cryptography, Ether, Solidity and related topics and also giving post work related to it which is going through and "Reading the Code" for a Voting Smart Contract and hence summarising your understanding after a day of learning solidity and going through a code snippet in the same.

Just a heads up for the readers I may use a few refereces realted to JAVA here to depict my understanding of the code I went through since its the language I have been using for a while and the concepts can be very easily related just like any other Object Oriented Programming language.


About the Code

So whats it about?

In layman terms and from what I understand its code written to stimulate an election environment in the most basic way possible.

It introduces the concepts of candidates, voters, votes, election time period, checks and verifications, permissions available for voters and candidates all of this in the form of a smart contract using very beginner friendly solidity syntax understandable to anyone with a little bit of knowledge and is attainable in a short while.

Skeleton or structure of the Code

  • The starting part of the code contains a lot of state variables, structures, enums, maps and events. In simple words the elements that will be used all throughout this code or contract.

  • The around the middle its structured with multiple modifiers which act as checks for the functions written further after which also exists a constructor to initialise the state variables.

  • The comes the code towards the end which is basically the most important part of the code and where all the main logic is written. Yes, the functions that define how the flow this code is supposed to be.

Details about the contract elements

  1. There are various state variables to help with this programs's logic such as owner, start and end time of the elections, maps that has the key values pairs for candidate id and information, structures to define candidates, voters, enum for the state of the elections, counter variable for the count of candidates and events which are triggered when needed.

  2. Modifiers

    1. onlyOwner: Only the elections controller who is overseeing the process can make use of these functions.

    2. onlyDuringVotingPeriod: Elections state should be in progress.

    3. onlyAfterVotingPeriod: After the Election state is set to completed.

    4. hasNotVoted: the candidates who have not voted.

  3. Functions

    1. startVoting: checks if owner, signals start of the voting period by enum state change.

    2. endVoting: checks if owner, signals end of the voting period by enum state change, emits the winning candidate.

    3. addCandidate: increases the candidate counter and adds the candidate to the map.

    4. vote: takes in the candidate to vote, and emits that the vote is casted.

getWinningCandidate: calculates the winning candidate by looping over the map and returns it.


The Contract in question

Utilities used

  • Remix IDE

  • Etherium Blockchain

  • Solidity programming language