Created
September 18, 2016 10:31
-
-
Save elenadimitrova/1dfc3fc9147ae9839fc12d28569c92b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 "EternalStorage.sol"; | |
library VotingLibrary { | |
modifier ensurePollStatus(address _storageContract, uint256 pollId, uint256 pollStatus){ | |
var currentStatus = EternalStorage(_storageContract).getUIntValue(sha3("Poll", pollId, "status")); | |
if (pollStatus != currentStatus) { throw; } | |
_ | |
} | |
/// @notice Creates a new Poll | |
/// @param description The poll description or question to vote on | |
function createPoll(address _storageContract, string description) | |
returns (bool) { | |
// Infer the next pollId form incrementing the current Poll count | |
uint256 pollCount = EternalStorage(_storageContract).getUIntValue(sha3("PollCount")); | |
uint256 pollId = pollCount + 1; | |
EternalStorage(_storageContract).setStringValue(sha3("Poll", pollId, "description"), description); | |
EternalStorage(_storageContract).setUIntValue(sha3("PollCount"), pollCount + 1); | |
return true; | |
} | |
/// @notice For a poll with id 'pollId', adds a new voting option up to a max of 4 | |
/// Note: Once poll has status of 'open', no options can be added to it | |
/// @param pollOptionDescription The option description, e.g. "Yes" vote | |
function addPollOption(address _storageContract, uint256 pollId, string pollOptionDescription) | |
ensurePollStatus(_storageContract, pollId, 0) | |
returns (bool) | |
{ | |
var pollOptionCount = EternalStorage(_storageContract).getUIntValue(sha3("Poll", pollId, "OptionsCount")); | |
if (pollOptionCount >= 4) { return false; } | |
EternalStorage(_storageContract).setStringValue(sha3("Poll", pollId, "option", pollOptionCount + 1), pollOptionDescription); | |
EternalStorage(_storageContract).setUIntValue(sha3("Poll", pollId, "OptionsCount"), pollOptionCount + 1); | |
return true; | |
} | |
/// @notice Opens the poll for voting with immediate effect, for the given duration | |
/// @param pollDuration hours from now (effectively the opening time), the poll remains open for voting | |
function openPoll(address _storageContract, uint256 pollId, uint256 pollDuration) | |
ensurePollStatus(_storageContract, pollId, 0) | |
returns (bool) | |
{ | |
// Ensure there are at least 2 vote options, before a poll can open | |
var pollOptionCount = EternalStorage(_storageContract).getUIntValue(sha3("Poll", pollId, "OptionsCount")); | |
if (pollOptionCount < 2) { return false; } | |
EternalStorage(_storageContract).setUIntValue(sha3("Poll", pollId, "startTime"), now); | |
EternalStorage(_storageContract).setUIntValue(sha3("Poll", pollId, "closeTime"), now + pollDuration * 1 hours); | |
EternalStorage(_storageContract).setUIntValue(sha3("Poll", pollId, "status"), 1); | |
return true; | |
} | |
/// @notice Resolves the poll voting results | |
/// Note: Any votes which haven't been resolved will not count in the final poll results resolved here | |
function resolvePoll(address _storageContract, uint256 pollId) | |
ensurePollStatus(_storageContract, pollId, 1) | |
returns (bool) | |
{ | |
var startTime = EternalStorage(_storageContract).getUIntValue(sha3("Poll", pollId, "startTime")); | |
var endTime = EternalStorage(_storageContract).getUIntValue(sha3("Poll", pollId, "closeTime")); | |
var resolutionTime = endTime + (endTime - startTime); //TODO: Think about this time period. | |
if (now < resolutionTime) { return false; } | |
EternalStorage(_storageContract).setUIntValue(sha3("Poll", pollId, "status"), uint256(2)); | |
return true; | |
} | |
[...] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment