Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch 1 #4

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## In the Voting folder we have impelemented FPTP and Two Round Voting System using Solidity
74 changes: 74 additions & 0 deletions Voting/FPTP.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
pragma solidity >=0.4.25 <0.7.0;

contract FPTP {
event VeiwNETA(address[] NETA_list);
address private chairperson;
constructor() public {
chairperson = msg.sender;
}

modifier isChairperson() {
require(msg.sender == chairperson, "Caller is not owner");
_;
}
address[] private voter_list;
address[] public NETA_list;
mapping(address => bool) public NETA_approved;
address public winner;
mapping(address => uint256) public vote_cnt;
struct Voter {
bool approved;
bool voted;
address neta;
}

mapping(address => Voter) public voters;

function voter_registration() public{
voters[msg.sender].voted = false;
voters[msg.sender].approved = false;
voter_list.push(msg.sender);
}

function approve_voter() public isChairperson{
for(uint i=0;i<voter_list.length;i++){
if(voters[voter_list[i]].voted == false){
voters[voter_list[i]].approved=true;
}
}
}

function NETA_registration() public{
NETA_list.push(msg.sender);
}
function NETA_approve() public isChairperson{
for(uint i=0;i<NETA_list.length;i++){
NETA_approved[NETA_list[i]] = true;
vote_cnt[NETA_list[i]] = 0;
}
}
function veiwAll_NETA() public{
emit VeiwNETA(NETA_list);
}

function voteYour_NETA(address NETA) public{
if(voters[msg.sender].voted == false && NETA_approved[NETA] == true ){vote_cnt[NETA]+=1;voters[msg.sender].voted = true;}
}

function find_winner() public isChairperson{
uint mini=0;
for(uint i=0;i<NETA_list.length;i++){
if(mini<vote_cnt[NETA_list[i]]){
mini=vote_cnt[NETA_list[i]];
winner=NETA_list[i];
}
}
}
function view_winner() public view returns(address){
return winner;
}
function view_chairperson() public view returns(address){
return chairperson;
}

}
28 changes: 28 additions & 0 deletions Voting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## First Past the Post Voting implementation using Solidity
In this assignment we implemented this voting system with funtionalities like **approving voters, approving candidates, viewing candidates**

### How the voting system runs-
1. The chairperson deploys the contract. He is made the owner of the contract.
2. Various voters and candidated registers through funtion *voter_registration()* and *NETA_registration*.
3. The chairperson approves the candidates and votes through function *approve_voter()* and *NETA_approve()*
4. The voters can see all the available candidates with help of *veiwAll_NETA()* function and can vote them through *voteYour_NETA()* function
5. Now the chairperson runs *find_winner()* function and the result is stored in **winner** variable which anyone can view.
6. Every voter can view the number of votes gained by each candidate.

### Main variables

1. NETA_list- Stores address of all candidates.
1. voter_list - Stores address of all voters.
1. voters - Its a mapping from addresses of different voters to Voter.
1. Voter - Its a map from address of an voter to keep a track of if he approved or not, if he already voted or not.
1. Neta_approved - Its a boolean array that keep track if the candidate is approved by chairperson or not.
1. vote_cnt - Its a map from address of candidates to the number of votes they get.


Since it was not very clear to us about what to keep private and what to keep public (like should the voters be public or private etc) so I declared them according to my ease.


## Two Round Voting System
The two-round system (also known as the second ballot, runoff voting or ballotage) is a voting method used to elect a single candidate, where voters cast a single vote for their preferred candidate. The election proceeds to a second round only if in the first round no candidate has received a simple majority (more than 50%) of votes cast, or at least some other prescribed percentage. In the second round, usually only the two candidates who received the most votes in the first round, or those candidates who received above a prescribed proportion of the votes, would be candidates in the second round. Any remaining candidate is free to withdraw from the second round.

**This is similar to FPTP but here there is change in checking the winner. First its checked if a candidate has gained more then half votes then he directly wins. If not then We find the 2 best candidates and the voters vote again between these 2 candidates. Now the cnadidates with more votes wins**
133 changes: 133 additions & 0 deletions Voting/TwoRoundVoting.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
pragma solidity >=0.4.25 <0.7.0;

contract Two_round_voting {
string private s="Time for round 2";
event VeiwNETA(address[] NETA_list);
event round2(string s);
//event debug(address NETA_li,uint max_vote,bool flag,uint next_NETA_cnt);


address private chairperson;
constructor() public {
chairperson = msg.sender;
}

modifier isChairperson() {
require(msg.sender == chairperson, "Caller is not owner");
_;
}

address[] private voter_list;
address[] public NETA_list;
mapping(address => bool) public NETA_approved;
address[] public best_NETA;
address public winner;
mapping(address => uint256) public vote_cnt;
struct Voter {
bool approved;
bool voted;
address neta;
}

mapping(address => Voter) public voters;

function voter_registration() public{
voters[msg.sender].voted = false;
voters[msg.sender].approved = false;
voter_list.push(msg.sender);
}

function approve_voter() public isChairperson{
for(uint i=0;i<voter_list.length;i++){
if(voters[voter_list[i]].voted == false){
voters[voter_list[i]].approved=true;
}
}
}

function NETA_registration() public{
NETA_list.push(msg.sender);
}
function NETA_approve() public isChairperson{
for(uint i=0;i<NETA_list.length;i++){
NETA_approved[NETA_list[i]] = true;
vote_cnt[NETA_list[i]] = 0;
}
}
function veiwAll_NETA() public{
emit VeiwNETA(NETA_list);
}

function voteYour_NETA(address NETA) public{
if(voters[msg.sender].voted == false && NETA_approved[NETA] == true ){vote_cnt[NETA]+=1;voters[msg.sender].voted = true;}
}

bool public ifMajority = false;
address public best_candidate;
address public next_NETA;
uint total_count=0;
function find_winner_1st_round() public isChairperson{

uint max_vote=0;

for(uint i=0;i<NETA_list.length;i++){
total_count+=vote_cnt[NETA_list[i]];
}

ifMajority = false;
for(uint i=0;i<NETA_list.length;i++){
if(vote_cnt[NETA_list[i]]>max_vote){max_vote=vote_cnt[NETA_list[i]]; best_candidate=NETA_list[i];}

if(vote_cnt[NETA_list[i]]*2 > (total_count)){
ifMajority = true;
winner=NETA_list[i];
}
}
best_NETA.push(best_candidate);
if(ifMajority == false){
emit round2(s);

uint next_NETA_cnt=0;
bool flag = false;
for(uint i=0;i<NETA_list.length;i++){
//emit debug(NETA_list[i],max_vote,flag,next_NETA_cnt);
if(vote_cnt[NETA_list[i]] == max_vote && flag == false){flag = true;}
else if(vote_cnt[NETA_list[i]] == max_vote){next_NETA=NETA_list[i];next_NETA_cnt=max_vote;}
else if(vote_cnt[NETA_list[i]] > next_NETA_cnt){next_NETA=NETA_list[i];next_NETA_cnt=vote_cnt[NETA_list[i]];}
}
best_NETA.push(next_NETA);
vote_cnt[next_NETA]=0;
vote_cnt[best_candidate]=0;
for(uint i=0;i<NETA_list.length;i++){NETA_approved[NETA_list[i]]=false;}
NETA_approved[next_NETA]=true;
NETA_approved[best_candidate]=true;
}
}
function view_best_NETA(uint i) public returns(address ){
if(i<2)return best_NETA[i];
}


function round2_preparation() public isChairperson{
for(uint i=0;i<voter_list.length;i++){
voters[voter_list[i]].voted =false;
}
}

function round2_voting(address NETA) public{
if(voters[msg.sender].voted == false && NETA_approved[NETA] == true ){vote_cnt[NETA]+=1;voters[msg.sender].voted=true;}
}

function find_winner_2nd_round() public isChairperson{
if(vote_cnt[best_candidate]>vote_cnt[next_NETA]){winner=best_candidate;}
else{winner=next_NETA;}
}

function view_winner() public view returns(address){
return winner;
}
function view_chairperson() public view returns(address){
return chairperson;
}

}