Open In App

What is Decentralized Voting Application (DApps)?

Last Updated : 11 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Project Name is Decentralized Voting Application (DApps) which is built on Solidity Language. This Project showcases a lot of Solidity’s features. It implements a voting contract. Of course, the main problem of electronic voting is how to prevent to assign the duplicate Vote.

Some Important Concepts are: 

1. Contract: A contract is just like a class in Solidity which consists (its functions) and data (its state) that resides at a specific address on the Ethereum Blockchain. In each Contract, we can define State Variables, Methods, and Events, etc. A smart contract runs exactly as programmed without any possibility of downtime, censorship, fraud, and third-party interference.

2. Structure: The Structure is Collection of different type of Data Types same like C and C++, which is shown in the following example: 
 

struct Voter{
          bool authorized;
          bool voted;
      }

3. Mapping: Mapping is just like Hash tables It stores the value based on key. They cannot be used as parameters or return parameters of contract functions that are publicly visible.  You cannot iterate over mappings, i.e. you cannot enumerate their keys. It possible to implement a data structure on top of them and iterate over that. 

Example: 

mapping(address=>Voter) info; 

4. Modifier: Modifiers are used to easily change the behavior of a function. They can automatically check conditions before executing the functions. 
 

modifier ownerOn() {
        require(msg.sender==owner);
        _;
    }
function temaAF(address _address) public {
        require(!info[_address].voted, "already voted person"); //If already not vote
        require(info[_address].authorized, "You Have No Right for Vote");
        info[_address].voted = true;
        teamA++;
        totalVotes++;
      }

Explanation:

require(!info[_address].voted, "already voted person"); 

Firstly we need to verify that person is Voted Or Not. If Person is voted then stop to proceed in the code otherwise proceed rest of code.

Implementation in Solidity 

Solidity




// Solidity program to demonstrate
// DApps
 
pragma solidity 0.5.11;
 
// Smart Contract for the Voting application
contract VotingForTopper {
 
      // Refer to the owner
      address  owner;
 
      // Declaring the public variable 'purpose'
      // to demonstrate the purpose of voting
      string public purpose;
      
      // Defining a structure with boolean 
      // variables authorized and voted
      struct Voter{
          bool authorized; 
          bool voted;
      }
 
      // Declaring the unsigned integer
      // variables totalVotes, and for the
      //3 teams- A,B, and C
      uint  totalVotes;
      uint teamA;
      uint  teamB;
      uint teamC;
       
      // Creating a mapping for the total Votes
      mapping(address=>Voter) info;
 
      // Defining a constructor indicating
      // the purpose of voting
      constructor(
      string memory _name) public{
        purpose = _name;  
        owner = msg.sender;
      }
    
      // Defining a modifier to
      // verify the ownership
      modifier ownerOn() {
        require(msg.sender==owner);
        _;
    }
       
      // Defining a function to verify
      // the person is voted or not
      function authorize(
      address _person) ownerOn public {
        info[_person].authorized= true;
         
    }
    
      // Defining a function to check and
      // skip the code if the person is already
      // voted else allow to vote and
      // calculate totalvotes for team A  
      function temaAF(address _address) public {
        require(
        !info[_address].voted,
        "already voted person");
        require(
        info[_address].authorized,
        "You Have No Right for Vote");
        info[_address].voted = true;
        teamA++;
        totalVotes++;
      }
 
      // Defining a function to check
      // and skip the code if the person
      // is already voted else allow to vote
      // and calculate totalvotes for team B 
      function temaBF(address _address) public {
       require(
       !info[_address].voted,
       "already voted person");
        require(
        info[_address].authorized,
        "You Have No Right for Vote");
        teamB++;
        info[_address].voted = true;
        totalVotes++;
      }
 
      // Defining a function to check
      // and skip the code if the person
      // is already voted else allow to vote
      // and calculate totalvotes for team C  
      function temaCF(address _address) public returns(
      string memory){
        require(
        !info[_address].voted,
        "already voted person");
        require(
        info[_address].authorized,
        "You Have No Right for Vote");
        info[_address].voted = true;
        teamC++;
        totalVotes++;
        return("Thanks for Voting");
      }
 
      function totalVotesF() public view returns(uint){
          return totalVotes;
      }
 
      // Defining a function to announce
      // the result of voting and
      // the name of the winning team
      function resultOfVoting() public view returns(
      string memory){
          if(teamA>teamB){
              if(teamA>teamC){
                  return"A is Winning";
              }
              else if(teamC>teamA){
                  return "C is Winning"; } }
          else if(teamB>teamC) {
              return "B is Winning";
          }
          else if(
          teamA==teamB && teamA==teamC || teamB==teamC ){
              return "No One is Winning";
          }
      }
    }




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads