What is Decentralized Voting Application (DApps)?

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 authoried 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";
          }
      }
    }

chevron_right





My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.