LCP

Introduction to Voting Contracts

Voting is defined as formally choosing something/someone or giving your opinion on something. In Blockchain, a voting contract can be used to achieve the same functionality. In this blog, we will see what are Voting Contracts, their functions, and how they can be implemented.

A voting contract provides the users two choices, out of which users can choose one and vote for it. In the contract, the owner has the right to create a poll for a certain period and provide choices for voting for that poll. In the poll, the owner will provide a description for the users to see and cast their votes. The poll will run for a given time period, during which users can cast their votes. After the period for that poll has ended, voting rights will be stopped, and users will no longer be able to vote for that poll.

After a poll has ended, the owner must end the poll manually to be able to create a new poll. Once the owner manually ends a poll, he can again create a new poll by providing the necessary information.

Voting Contracts Functionality

Let us take a look at the basic functions that are offered by the voting smart contract.

FunctionDescriptionAccessibility
createPollThis function is used to create a new pollOwner-only
resetPoll

This function is used to reset all the previous poll information.

This can also forcefully end a Poll

Owner-only
castVoteThis function is used to cast a vote for the preferred choicePublic
getWinnerThis function will return the winning choicePublic
getPollStatusThis will return the status of the ongoing pollPublic

Voting Contracts Working

Let’s take a deep dive into how the voting contract works:

Variables used in the Voting contract are:

  • description: Description of a Poll
  • votesA: Count for the true choice
  • votesB: Count for the false choice
  • pollStatus: The current poll status
  • pollDuration: The duration of a poll in days
  • pollStartDate: The timestamp when the poll started

Create a Poll:

The very first step is to create a Poll. The owner of the contract creates a poll by calling the createPoll function. This function takes in a description of the Poll and the duration for which this poll should run.

   function createPoll(string memory pollDescription, uint256 durationInDays) public onlyOwner {
       require(pollStatus == 0, "An poll is already running");
       require(durationInDays undefined 0, "The voting period cannot be 0.");
       description = pollDescription;
       pollStatus = 1;
       pollStartDate = block.timestamp;
       pollDuration = durationInDays;
   }

We need to make sure that the duration of the poll is greater than 0 and that no poll is running at the moment. When we create a poll, the description and duration provided in the function is set to the description and pollDuration variables.

The pollStartDate is set to the current date using block.timestamp, which gives the date and time at which the transaction went through and the poll status is changed to 1.

Casting a Vote:

Any user including the owner has the right to vote until a poll is running and the time of that poll has not run out. If a user votes true, a vote is added to voteA, and for false, a vote is added to voteB.

   function castVote(bool vote) public {
       require(pollStatus == 1, "No poll is running");
       require(block.timestamp undefined pollStartDate + (pollDuration * 1 days), "Poll has already ended");
       if(vote){
           votesA++;
       }else{
           votesB++;
       }
   }

Get Poll Status:

At any moment users can check whether a poll is running or not using this function.

   function getPollStatus() public view returns(string memory){
       if(pollStatus == 1){
           return "A poll is running.";
       }else{
           return "No poll is running.";
       }
   }

Get Winner:

While the poll is in a running state, users can check which option is winning.

This function makes sure that a poll is running and that poll has not ended. It returns a message based on the number of votes.

 function getWinner() public view returns(string memory){
       require(pollStatus == 1, "No poll is running");
       require(block.timestamp undefined pollStartDate + (pollDuration * 1 days), "Poll has already ended");
       if(votesA undefined votesB){
           return "A is winning";
       }else if(votesA == votesB){
           return "Its a tie!";
       }else{
           return "B is winning";
       }
   }

Resetting a Poll:

This function can be called either after a poll has ended or if a poll is running and needs a forceful termination of that poll. This function resets every parameter that is used at the time of the poll creation.

   function resetPoll() public onlyOwner {
       description = "";
       pollStatus = 0;
       pollStartDate = 0;
       pollDuration = 0;
   }

End Note

These are the basic functions that are used in a Voting contract. There is an advanced variant of this contract that offers features like Staking where users have to stake X amount of ERC20 tokens in order to get X votes which they use in the contract.

Check out the complete code on our GitHub Repository.


We, at Seaflux, are Blockchain enthusiasts who are helping enterprises worldwide. Have a query or want to discuss Blockchain projects? Schedule a meeting with us here, we'll be happy to talk to you!

Jay Mehta - Director of Engineering
Jay Mehta

Director of Engineering

Claim Your No-Cost Consultation!

Let's Connect