Created
January 13, 2019 22:15
-
-
Save Shritesh99/21e21a34a60d4d5433b3b315c5bff592 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.2+commit.1df8f40c.js&optimize=false&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
// Solidity Version 0.4.22 <0.6.0 | |
// Election contract | |
contract Elections{ | |
// Events | |
event AddedParty(uint id); | |
event PartyVotes(uint votes); | |
/////////////////////////////////////////////////////////////////////////////////////// | |
// Party Struct | |
struct Party{ | |
uint id; | |
} | |
//////////////////////////////////////////////////////////////////////////////////////// | |
// Global variables | |
address superChairPerson; | |
address[] chairpersons; | |
address[] campaigns; | |
uint[] partyIDs; | |
mapping(uint => Party) parties; | |
///////////////////////////////////////////////////////////////////////////////////////// | |
// Modifieres | |
modifier restricted(){ | |
require(msg.sender == superChairPerson); | |
_; | |
} | |
//////////////////////////////////////////////////////////////////////////////////////// | |
// Constructor | |
constructor(address superChairpersonAddress) public { | |
superChairPerson = superChairpersonAddress; | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
// Chairperson Queries (geter and setter) | |
function getChairperson() public view returns(address) { | |
return superChairPerson; | |
} | |
function setChairperson(address superChairPersonNew)public restricted{ | |
superChairPerson = superChairPersonNew; | |
} | |
//////////////////////////////////////////////////////////////////////////////////////// | |
// Party Queries | |
function addParty(uint id) public restricted{ | |
parties[id] = Party(id); | |
partyIDs.push(id); | |
emit AddedParty(id); | |
} | |
function updateParty(uint id, uint newID)public restricted{ | |
parties[newID] = parties[id]; | |
removeParty(id); | |
} | |
function getParties()public view restricted returns(uint[] memory){ | |
return (partyIDs); | |
} | |
function removeParty(uint id) public restricted{ | |
for (uint i=0;i<partyIDs.length;i++){ | |
if(partyIDs[i] == id){ | |
delete partyIDs[i]; | |
} | |
} | |
delete parties[id]; | |
} | |
function getNumOfParties() public view restricted returns(uint) { | |
return partyIDs.length; | |
} | |
function getPartyVotesByCampaign(uint id,address campaign)public restricted returns(uint){ | |
// Suscribe for event PartyVotes | |
ElectionCampaign campaign = ElectionCampaign(campaign); | |
return (campaign.getPartyVotes(id)); | |
} | |
function getPartyVotes(uint id,address campaign)public restricted returns(uint){ | |
// Suscribe for event PartyVotes | |
uint votes; | |
for(uint i = 0; i<campaigns.length; i++){ | |
votes += getPartyVotesByCampaign(id, campaigns[i]); | |
} | |
emit PartyVotes(votes); | |
} | |
function getPartyCandidatesByCampaign(uint id,address campaign)public view restricted returns(uint[] memory){ | |
ElectionCampaign campaign = ElectionCampaign(campaign); | |
return (campaign.getPartyCandidates(id)); | |
} | |
function getPartyCandidatesCount(uint id)public view restricted returns(uint){ | |
uint count; | |
for(uint i = 0; i<campaigns.length; i++){ | |
count += ElectionCampaign(campaigns[i]).getPartyCandidatesCount(id); | |
} | |
return (count); | |
} | |
function getPartyCandidates(uint id)public view restricted returns(uint[] memory){ | |
uint length = getPartyCandidatesCount(id); | |
uint[] memory ar = new uint[](length); | |
uint count=0; | |
for(uint j = 0; j<campaigns.length; j++){ | |
for(uint k=0;k < ElectionCampaign(campaigns[j]).getPartyCandidatesCount(id);k++){ | |
ar[count] = ElectionCampaign(campaigns[j]).getPartyCandidates(id)[k]; | |
count++; | |
} | |
} | |
return (ar); | |
} | |
//////////////////////////////////////////////////////////////////////////////////////// | |
// Campaign Queries | |
function crateElectionCampaign(address chairperson) public restricted returns(address){ | |
chairpersons.push(chairperson); | |
ElectionCampaign campaign = new ElectionCampaign(chairperson,superChairPerson); | |
campaigns.push(address(campaign)); | |
return (address(campaign)); | |
} | |
function getElectionCampaigns() public restricted view returns(address[] memory){ | |
return (campaigns); | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////////////////// | |
// Election Campaign contract | |
contract ElectionCampaign{ | |
// Events | |
event AddedCandidate(uint id); | |
event CandidateVotes(uint votes); | |
/////////////////////////////////////////////////////////////////////////////////////// | |
// Candidate Stuct | |
struct Candidate{ | |
uint id; | |
uint partyID; | |
uint votes; | |
} | |
//////////////////////////////////////////////////////////////////////////////////////// | |
//Voter Stuct | |
struct Voter { | |
uint id; | |
uint toCandidate; | |
bool voted; | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////// | |
// Global variables | |
address chairperson; | |
address superChairperson; | |
uint votersCount; | |
uint[] candidateIDs; | |
mapping (uint => Candidate)public candidates; | |
mapping (uint => Voter)public voters; | |
mapping (uint => uint[])public partyCandidates; | |
/////////////////////////////////////////////////////////////////////////////////////////// | |
// Modifieres | |
modifier restricted(){ | |
require(msg.sender == chairperson || msg.sender == superChairperson); | |
_; | |
} | |
//////////////////////////////////////////////////////////////////////////////////////////// | |
// Constructor | |
constructor(address chairpersonAddress, address superChairpersonAddress) public { | |
chairperson = chairpersonAddress; | |
superChairperson = superChairpersonAddress; | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
// SuperChairperson Queries | |
function getSuperChairperson() public view returns(address) { | |
return superChairperson; | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
// Chairperson Queries | |
function getChairperson() public view returns(address) { | |
return chairperson; | |
} | |
function setChairperson(address chairpersonNew)public restricted{ | |
chairperson = chairpersonNew; | |
} | |
///////////////////////////////////////////////////////////////////////////////////////////// | |
// Candidate Queries | |
function addCandidate(uint id, uint partyID) public restricted{ | |
candidates[id] = Candidate(id,partyID,0); | |
candidateIDs.push(id); | |
partyCandidates[partyID].push(id); | |
emit AddedCandidate(id); | |
} | |
function updateCandidate(uint id, uint newID) public restricted{ | |
candidates[newID] = candidates[id]; | |
removeCandidate(id); | |
} | |
function getCandidates() public view restricted returns(uint[] memory){ | |
return (candidateIDs); | |
} | |
function removeCandidate(uint id) public restricted{ | |
for (uint i =0;i<candidateIDs.length;i++){ | |
if(candidateIDs[i] == id){ | |
delete candidateIDs[i]; | |
} | |
} | |
delete candidates[id]; | |
} | |
function getNumOfCandidates() public view restricted returns(uint) { | |
return candidateIDs.length; | |
} | |
function getCandidateVotes(uint id) public restricted{ | |
// Suscribe for event CandidateVotes | |
emit CandidateVotes(candidates[id].votes); | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////// | |
function getPartyVotes(uint partyID)public view restricted returns(uint){ | |
uint votes; | |
for(uint i = 0; i < partyCandidates[partyID].length ; i++){ | |
votes += candidates[partyCandidates[partyID][i]].votes; | |
} | |
return (votes); | |
} | |
function getPartyCandidates(uint partyID)public view restricted returns(uint[] memory){ | |
return (partyCandidates[partyID]); | |
} | |
function getPartyCandidatesCount(uint partyID)public view restricted returns(uint){ | |
return (partyCandidates[partyID].length); | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////// | |
// Voter Queries | |
function getNumOfVoters() public view restricted returns(uint) { | |
return votersCount; | |
} | |
function getVote(uint id)public view returns(uint){ | |
return (voters[id].toCandidate); | |
} | |
//////////////////////////////////////////////////////////////////////////////////////////////// | |
// Actual Vote Function | |
function vote(uint id, uint candidateID) public restricted{ | |
if(!voters[id].voted){ | |
voters[id] = Voter(id,candidateID,true); | |
candidates[candidateID].votes++; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "./ballot.sol"; | |
contract test3 { | |
Ballot ballotToTest; | |
function beforeAll () public { | |
ballotToTest = new Ballot(2); | |
} | |
function checkWinningProposal () public { | |
ballotToTest.vote(1); | |
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal"); | |
} | |
function checkWinninProposalWithReturnValue () public view returns (bool) { | |
return ballotToTest.winningProposal() == 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment