-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poll.sol
94 lines (74 loc) · 2.14 KB
/
Poll.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
pragma solidity ^0.4.2;
//Contract defining to the modifier onlyOwner & allowing to transfer the Ownership
//Needed to whitelist Poll participants
contract owned {
address public owner;
function owned() {
owner = msg.sender;
}
modifier onlyOwner {
if (msg.sender != owner) throw;
_
}
function transferOwnership(address newOwner) onlyOwner {
owner = newOwner;
}
}
//Contract defining the Poll
contract NewPoll is owned {
//Maps an Address to a Boolean value to whitelist accounts
mapping (address => bool) public approvedAccount;
//defines the poll's properties
struct Poll {
address owner;
string title;
uint votelimit;
string options;
uint deadline;
bool status;
uint numVotes;
}
// event tracking of all votes
event NewVote(string votechoice);
event ApprovedAddress(address target, bool approved);
// declare a public poll called p
Poll public p;
//initiator function that stores the necessary poll information. Needs to have the same name as the contract!
function NewPoll(string _options, string _title, uint _votelimit, uint _deadline) {
p.owner = msg.sender;
p.options = _options;
p.title = _title;
p.votelimit = _votelimit;
p.deadline = _deadline;
p.status = true;
p.numVotes = 0;
}
//Function whitelisting poll paritcipants
function approveAddress(address target, bool approved) onlyOwner{
approvedAccount[target] = approved;
ApprovedAddress(target, approved);
}
//function for user vote. input is a string choice
function vote(string choice) returns (bool) {
if (msg.sender != p.owner || p.status != true || approvedAccount[msg.sender] != true) {
return false;
}
p.numVotes += 1;
NewVote(choice);
// if votelimit reached, end poll
if (p.votelimit > 0) {
if (p.numVotes >= p.votelimit) {
endPoll();
}
}
return true;
}
//when time or vote limit is reached, set the poll status to false
function endPoll() returns (bool) {
if (msg.sender != p.owner) {
return false;
}
p.status = false;
return true;
}
}