2018-03-28 02:16:51 +00:00
|
|
|
pragma solidity ^0.4.21;
|
2017-11-28 03:33:25 +00:00
|
|
|
|
2018-03-29 19:09:30 +00:00
|
|
|
import "../common/Controlled.sol";
|
|
|
|
import "../token/MiniMeTokenInterface.sol";
|
2018-03-17 16:50:33 +00:00
|
|
|
import "./DelegationProxyInterface.sol";
|
2018-05-22 10:46:29 +00:00
|
|
|
import "./TrustNetworkInterface.sol";
|
2017-12-20 01:06:19 +00:00
|
|
|
|
2017-11-28 03:33:25 +00:00
|
|
|
/**
|
|
|
|
* @title ProposalManager
|
|
|
|
* @author Ricardo Guilherme Schmidt (Status Research & Development GmbH)
|
2018-03-29 19:09:30 +00:00
|
|
|
* Store the proposals, votes and tabulate results for Democracy
|
2017-11-28 03:33:25 +00:00
|
|
|
*/
|
2018-05-22 10:46:29 +00:00
|
|
|
contract ProposalManager is Controlled {
|
|
|
|
event ProposalSet(bytes32 indexed topic, uint256 proposalId, bytes32 txHash);
|
|
|
|
event ProposalResult(uint256 proposalId, uint8 finalResult);
|
|
|
|
|
|
|
|
MiniMeTokenInterface public token;
|
|
|
|
TrustNetworkInterface public trustNet;
|
|
|
|
uint256 public tabulationBlockDelay;
|
|
|
|
Proposal[] public proposals;
|
|
|
|
|
2018-06-21 17:48:19 +00:00
|
|
|
uint public quorumPercentage;
|
|
|
|
|
2018-05-22 10:46:29 +00:00
|
|
|
struct Proposal {
|
|
|
|
bytes32 topic;
|
|
|
|
bytes32 txHash;
|
|
|
|
|
|
|
|
uint blockStart;
|
|
|
|
uint voteBlockEnd;
|
|
|
|
|
|
|
|
address[] voters;
|
|
|
|
mapping(address => Vote) voteMap;
|
|
|
|
|
|
|
|
mapping(address => bool) tabulated;
|
|
|
|
mapping(uint8 => uint256) results;
|
|
|
|
Vote result;
|
|
|
|
uint256 lastTabulationTimestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Vote {
|
|
|
|
Null,
|
|
|
|
Reject,
|
|
|
|
Approve
|
|
|
|
}
|
2018-03-29 19:09:30 +00:00
|
|
|
|
2018-05-22 10:46:29 +00:00
|
|
|
constructor(
|
|
|
|
MiniMeTokenInterface _token,
|
|
|
|
TrustNetworkInterface _trustNet
|
2018-03-29 19:09:30 +00:00
|
|
|
)
|
|
|
|
public
|
|
|
|
{
|
2017-11-28 03:33:25 +00:00
|
|
|
trustNet = _trustNet;
|
2018-05-22 10:46:29 +00:00
|
|
|
token = _token;
|
2018-06-22 19:40:20 +00:00
|
|
|
tabulationBlockDelay = 7 days;
|
2018-06-21 17:48:19 +00:00
|
|
|
quorumPercentage = 50;
|
2018-05-22 10:46:29 +00:00
|
|
|
|
2017-11-28 03:33:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 19:09:30 +00:00
|
|
|
function addProposal(
|
|
|
|
bytes32 _topic,
|
2018-06-21 17:48:19 +00:00
|
|
|
bytes32 _txHash,
|
|
|
|
uint blocksUntilVotingStart,
|
|
|
|
uint voteDuration
|
2018-03-29 19:09:30 +00:00
|
|
|
)
|
|
|
|
public
|
|
|
|
returns (uint proposalId)
|
|
|
|
{
|
|
|
|
proposalId = proposals.length++;
|
2018-06-21 17:48:19 +00:00
|
|
|
|
2018-03-29 19:09:30 +00:00
|
|
|
Proposal storage p = proposals[proposalId];
|
2017-11-28 03:33:25 +00:00
|
|
|
|
2018-03-17 01:55:57 +00:00
|
|
|
p.topic = _topic;
|
|
|
|
p.txHash = _txHash;
|
2017-11-28 03:33:25 +00:00
|
|
|
|
2018-06-21 17:48:19 +00:00
|
|
|
p.blockStart = block.number + blocksUntilVotingStart; //will be replaced by configurations
|
|
|
|
p.voteBlockEnd = p.blockStart + voteDuration; //dummy value
|
2018-05-22 10:46:29 +00:00
|
|
|
emit ProposalSet(_topic, proposalId, _txHash);
|
2017-11-28 03:33:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 19:09:30 +00:00
|
|
|
function voteProposal(uint _proposalId, Vote _vote)
|
|
|
|
public
|
|
|
|
{
|
|
|
|
Proposal storage proposal = proposals[_proposalId];
|
2017-11-28 03:33:25 +00:00
|
|
|
require(block.number >= proposal.blockStart);
|
2018-05-22 10:46:29 +00:00
|
|
|
require(block.number <= proposal.voteBlockEnd);
|
2018-03-11 10:11:46 +00:00
|
|
|
proposal.voteMap[msg.sender] = _vote;
|
2018-06-21 17:48:19 +00:00
|
|
|
proposal.voters.push(msg.sender);
|
2017-11-28 03:33:25 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:46:58 +00:00
|
|
|
function getProposalCount()
|
|
|
|
public
|
|
|
|
view
|
|
|
|
returns (uint256)
|
|
|
|
{
|
|
|
|
return proposals.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
function exists(uint _proposalId)
|
|
|
|
public
|
|
|
|
view
|
|
|
|
returns (bool) {
|
|
|
|
return proposals.length != 0 && proposals[_proposalId].blockStart != 0;
|
|
|
|
}
|
|
|
|
|
2018-03-29 19:09:30 +00:00
|
|
|
function tabulateVote(uint _proposalId, address _delegator)
|
|
|
|
public
|
|
|
|
{
|
|
|
|
Proposal storage proposal = proposals[_proposalId];
|
2018-03-17 01:55:57 +00:00
|
|
|
require(block.number > proposal.voteBlockEnd);
|
2018-05-22 10:46:29 +00:00
|
|
|
require(!proposal.tabulated[_delegator]);
|
|
|
|
proposal.tabulated[_delegator] = true;
|
2018-03-17 01:55:57 +00:00
|
|
|
Vote _vote = proposal.voteMap[_delegator];
|
|
|
|
if(_vote == Vote.Null) {
|
2018-05-22 10:46:29 +00:00
|
|
|
address delegate = trustNet.getVoteDelegation(proposal.topic).delegationOfAt(_delegator, proposal.voteBlockEnd);
|
2018-03-17 01:55:57 +00:00
|
|
|
_vote = proposal.voteMap[delegate];
|
2017-11-28 03:33:25 +00:00
|
|
|
}
|
2018-03-11 10:11:46 +00:00
|
|
|
|
|
|
|
if (_vote == Vote.Reject || _vote == Vote.Approve) {
|
2018-03-17 16:50:33 +00:00
|
|
|
proposal.results[uint8(_vote)] += token.balanceOfAt(_delegator, proposal.voteBlockEnd);
|
2017-11-28 03:33:25 +00:00
|
|
|
}
|
2018-05-22 10:46:29 +00:00
|
|
|
proposal.lastTabulationTimestamp = block.timestamp;
|
2018-03-17 01:55:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:46:58 +00:00
|
|
|
|
2018-06-21 17:48:19 +00:00
|
|
|
function getProposalResultsByVote(uint _proposalId, uint8 vote)
|
|
|
|
public
|
|
|
|
view
|
|
|
|
returns (uint256){
|
|
|
|
return proposals[_proposalId].results[vote];
|
|
|
|
}
|
|
|
|
|
2018-03-29 19:09:30 +00:00
|
|
|
function finalResult(uint _proposalId)
|
|
|
|
public
|
|
|
|
{
|
|
|
|
Proposal storage proposal = proposals[_proposalId];
|
2018-05-22 10:46:29 +00:00
|
|
|
require(proposal.lastTabulationTimestamp + tabulationBlockDelay > block.number);
|
2018-03-29 19:09:30 +00:00
|
|
|
require(proposal.result == Vote.Null);
|
2018-05-22 10:46:29 +00:00
|
|
|
uint256 totalTokens = token.totalSupplyAt(proposal.voteBlockEnd);
|
2018-03-17 01:55:57 +00:00
|
|
|
uint256 approvals = proposal.results[uint8(Vote.Approve)];
|
2018-06-21 17:48:19 +00:00
|
|
|
|
|
|
|
uint256 approvalQuorum = (totalTokens * quorumPercentage / 100);
|
|
|
|
|
2018-05-22 10:46:29 +00:00
|
|
|
if(approvals >= approvalQuorum) {
|
2018-03-29 19:09:30 +00:00
|
|
|
proposal.result = Vote.Approve;
|
|
|
|
} else {
|
|
|
|
proposal.result = Vote.Reject;
|
|
|
|
}
|
2018-05-22 10:46:29 +00:00
|
|
|
emit ProposalResult(_proposalId, uint8(proposal.result));
|
2018-03-17 01:55:57 +00:00
|
|
|
}
|
2018-03-28 02:16:51 +00:00
|
|
|
|
2018-06-21 17:48:19 +00:00
|
|
|
function setQuorum(uint _percentage)
|
|
|
|
public
|
|
|
|
onlyController {
|
|
|
|
require(_percentage > 0 && _percentage <= 100);
|
|
|
|
quorumPercentage = _percentage;
|
|
|
|
}
|
2018-06-22 19:40:20 +00:00
|
|
|
|
|
|
|
function setTabulationBlockDelay(uint256 _tabulationBlockDelay)
|
|
|
|
public
|
|
|
|
onlyController
|
|
|
|
{
|
|
|
|
tabulationBlockDelay = _tabulationBlockDelay;
|
|
|
|
}
|
2018-05-22 10:46:29 +00:00
|
|
|
|
2018-06-21 17:48:19 +00:00
|
|
|
function hasVotesRecorded(uint256 _proposalId)
|
|
|
|
external
|
|
|
|
view
|
|
|
|
returns (bool)
|
|
|
|
{
|
|
|
|
return proposals[_proposalId].voters.length > 0;
|
|
|
|
}
|
2018-05-22 10:46:29 +00:00
|
|
|
|
|
|
|
function getProposalFinalResult(
|
|
|
|
uint256 _proposalId
|
|
|
|
)
|
|
|
|
external
|
|
|
|
view
|
|
|
|
returns (uint8)
|
|
|
|
{
|
|
|
|
|
|
|
|
return uint8(proposals[_proposalId].result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getProposal(uint _proposalId)
|
|
|
|
external
|
|
|
|
view
|
|
|
|
returns (
|
|
|
|
bytes32 topic,
|
|
|
|
bytes32 txHash,
|
2018-06-21 17:48:19 +00:00
|
|
|
bool approved
|
2018-05-22 10:46:29 +00:00
|
|
|
)
|
|
|
|
{
|
2018-06-21 17:48:19 +00:00
|
|
|
Proposal memory p = proposals[_proposalId];
|
|
|
|
return (p.topic, p.txHash, p.result == Vote.Approve);
|
|
|
|
}
|
2018-05-30 14:32:38 +00:00
|
|
|
|
2018-06-21 17:48:19 +00:00
|
|
|
function isVotingAvailable(uint _proposalId) public view returns (bool){
|
|
|
|
Proposal memory p = proposals[_proposalId];
|
|
|
|
return p.voteBlockEnd > block.number && p.result == Vote.Null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVoteInfo(uint _proposalId, address voter)
|
|
|
|
public
|
|
|
|
view
|
|
|
|
returns (uint8 vote, uint256 tokens)
|
2018-06-03 16:08:53 +00:00
|
|
|
{
|
|
|
|
Proposal storage p = proposals[_proposalId];
|
|
|
|
|
2018-06-21 17:48:19 +00:00
|
|
|
vote = uint8(p.voteMap[voter]);
|
|
|
|
tokens = token.balanceOfAt(voter, p.voteBlockEnd);
|
|
|
|
}
|
2018-05-22 10:46:29 +00:00
|
|
|
|
|
|
|
function offchainTabulateVoteResult(uint256 _proposalId)
|
|
|
|
external
|
|
|
|
view
|
|
|
|
returns (uint256[] votes)
|
|
|
|
{
|
|
|
|
Proposal memory proposal = proposals[_proposalId];
|
|
|
|
uint256 len = proposal.voters.length;
|
|
|
|
votes = new uint256[](4);
|
|
|
|
for(uint256 i = 0; i < len; i++) {
|
|
|
|
address voter = proposal.voters[i];
|
|
|
|
uint8 voteIndex = uint8(proposals[_proposalId].voteMap[voter]);
|
|
|
|
votes[voteIndex] += trustNet.getVoteDelegation(proposal.topic).influenceOfAt(proposal.voters[i], token, proposal.voteBlockEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-11-28 03:33:25 +00:00
|
|
|
}
|