Skip to content

Commit 3c155e4

Browse files
authored
Initial commit
0 parents  commit 3c155e4

File tree

6 files changed

+1046
-0
lines changed

6 files changed

+1046
-0
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Ballot Contract
2+
3+
Demo smart contract to illustrate how voting may work on the blockchain
4+
5+
## Process
6+
7+
1. constructor
8+
2. addVoter()
9+
3. startVote()
10+
4. countVote()
11+

Reload-0.6s-200px.svg

+8
Loading

ballot.sol

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* @file ballot.sol
3+
*/
4+
5+
pragma solidity ^0.5.0;
6+
7+
contract Ballot {
8+
9+
struct vote{
10+
address voterAddress;
11+
bool choice;
12+
}
13+
14+
struct voter{
15+
string voterName;
16+
bool voted;
17+
}
18+
19+
uint private countResult = 0;
20+
uint public finalResult = 0;
21+
uint public totalVoter = 0;
22+
uint public totalVote = 0;
23+
address public ballotOfficialAddress;
24+
string public ballotOfficialName;
25+
string public proposal;
26+
27+
mapping(uint => vote) private votes;
28+
mapping(address => voter) public voterRegister;
29+
30+
enum State { Created, Voting, Ended }
31+
State public state;
32+
33+
//creates a new ballot contract
34+
constructor(
35+
string memory _ballotOfficialName,
36+
string memory _proposal) public {
37+
ballotOfficialAddress = msg.sender;
38+
ballotOfficialName = _ballotOfficialName;
39+
proposal = _proposal;
40+
41+
state = State.Created;
42+
}
43+
44+
45+
modifier condition(bool _condition) {
46+
require(_condition);
47+
_;
48+
}
49+
50+
modifier onlyOfficial() {
51+
require(msg.sender ==ballotOfficialAddress);
52+
_;
53+
}
54+
55+
modifier inState(State _state) {
56+
require(state == _state);
57+
_;
58+
}
59+
60+
event voterAdded(address voter);
61+
event voteStarted();
62+
event voteEnded(uint finalResult);
63+
event voteDone(address voter);
64+
65+
//add voter
66+
function addVoter(address _voterAddress, string memory _voterName)
67+
public
68+
inState(State.Created)
69+
onlyOfficial
70+
{
71+
voter memory v;
72+
v.voterName = _voterName;
73+
v.voted = false;
74+
voterRegister[_voterAddress] = v;
75+
totalVoter++;
76+
emit voterAdded(_voterAddress);
77+
}
78+
79+
//declare voting starts now
80+
function startVote()
81+
public
82+
inState(State.Created)
83+
onlyOfficial
84+
{
85+
state = State.Voting;
86+
emit voteStarted();
87+
}
88+
89+
//voters vote by indicating their choice (true/false)
90+
function doVote(bool _choice)
91+
public
92+
inState(State.Voting)
93+
returns (bool voted)
94+
{
95+
bool found = false;
96+
97+
if (bytes(voterRegister[msg.sender].voterName).length != 0
98+
&& !voterRegister[msg.sender].voted){
99+
voterRegister[msg.sender].voted = true;
100+
vote memory v;
101+
v.voterAddress = msg.sender;
102+
v.choice = _choice;
103+
if (_choice){
104+
countResult++; //counting on the go
105+
}
106+
votes[totalVote] = v;
107+
totalVote++;
108+
found = true;
109+
}
110+
emit voteDone(msg.sender);
111+
return found;
112+
}
113+
114+
//end votes
115+
function endVote()
116+
public
117+
inState(State.Voting)
118+
onlyOfficial
119+
{
120+
state = State.Ended;
121+
finalResult = countResult; //move result from private countResult to public finalResult
122+
emit voteEnded(finalResult);
123+
}
124+
}

0 commit comments

Comments
 (0)