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
pragma solidity 0.5.11;
contract VotingForTopper {
address owner;
string public purpose;
struct Voter{
bool authorized;
bool voted;
}
uint totalVotes;
uint teamA;
uint teamB;
uint teamC;
mapping(address=>Voter) info;
constructor(
string memory _name) public {
purpose = _name;
owner = msg.sender;
}
modifier ownerOn() {
require(msg.sender==owner);
_;
}
function authorize(
address _person) ownerOn public {
info[_person].authorized= true ;
}
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++;
}
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++;
}
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;
}
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" ;
}
}
}
|