|
pragma solidity ^0.4.2; |
|
|
|
//Contract defining the modifier onlyOwner |
|
//Needed to whitelist Poll participants |
|
contract owned { |
|
address public owner; |
|
|
|
function owned() { |
|
owner = msg.sender; |
|
} |
|
|
|
modifier onlyOwner { |
|
if (msg.sender != owner) throw; |
|
_; |
|
} |
|
} |
|
|
|
|
|
//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; |
|
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 _title, uint _votelimit) { |
|
p.owner = msg.sender; |
|
p.title = _title; |
|
p.votelimit = _votelimit; |
|
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 (p.status != true || approvedAccount[msg.sender] != true) { |
|
return false; |
|
} |
|
|
|
p.numVotes += 1; |
|
NewVote(choice); |
|
|
|
// if votelimit reached, end poll |
|
if (p.votelimit > 0) { |
|
endPoll(); |
|
} |
|
return true; |
|
} |
|
|
|
//when time or vote limit is reached, set the poll status to false |
|
function endPoll() returns (bool) { |
|
if (p.numVotes < p.votelimit) { |
|
return false; |
|
} |
|
p.status = false; |
|
return true; |
|
} |
|
|
|
} |