Add voterIds to proposals #22

Merged
bumi merged 1 commits from features/add-voterids-to-proposals into master 2018-04-10 12:01:24 +00:00

View File

@ -16,11 +16,13 @@ contract Operator is Upgradeable {
bytes32 ipfsHash; bytes32 ipfsHash;
uint8 hashFunction; uint8 hashFunction;
uint8 hashSize; uint8 hashSize;
mapping (address => bool) votes; uint256[] voterIds;
mapping (uint256 => bool) votes;
bool exists; bool exists;
} }
Proposal[] public proposals; mapping(uint256 => Proposal) public proposals;
uint256 public proposalsCount;
event ProposalCreated(uint256 id, address creator, uint recipient, uint256 amount); event ProposalCreated(uint256 id, address creator, uint recipient, uint256 amount);
event ProposalVoted(uint256 id, address voter, uint256 totalVotes); event ProposalVoted(uint256 id, address voter, uint256 totalVotes);
@ -69,37 +71,52 @@ contract Operator is Upgradeable {
require(exists); require(exists);
} }
function proposalsCount() view public returns (uint) {
return proposals.length;
}
function addProposal(uint _recipient, uint256 _amount, bytes32 _ipfsHash, uint8 _hashFunction, uint8 _hashSize) public returns (uint256 proposalId) { function addProposal(uint _recipient, uint256 _amount, bytes32 _ipfsHash, uint8 _hashFunction, uint8 _hashSize) public returns (uint256 proposalId) {
require(contributorsContract().exists(_recipient)); require(contributorsContract().exists(_recipient));
proposalId = proposals.length; proposalId = proposalsCount + 1;
uint _votesNeeded = contributorsContract().coreContributorsCount() / 100 * 75; uint _votesNeeded = contributorsContract().coreContributorsCount() / 100 * 75;
var p = Proposal({ var p = proposals[proposalId];
creator: msg.sender, p.creator = msg.sender;
recipientId: _recipient, p.recipientId = _recipient;
amount: _amount, p.amount = _amount;
ipfsHash: _ipfsHash, p.ipfsHash = _ipfsHash;
hashFunction: _hashFunction, p.hashFunction = _hashFunction;
hashSize: _hashSize, p.hashSize = _hashSize;
votesCount: 0, p.votesCount = 0;
votesNeeded: _votesNeeded, p.votesNeeded = _votesNeeded;
executed: false, p.exists = true;
exists: true
}); proposalsCount++;
proposals.push(p);
ProposalCreated(proposalId, msg.sender, p.recipientId, p.amount); ProposalCreated(proposalId, msg.sender, p.recipientId, p.amount);
} }
function getProposal(uint _proposalId) public view returns (address creator, uint256 recipientId, uint256 votesCount, uint256 votesNeeded, uint256 amount, bool executed, bytes32 ipfsHash, uint8 hashFunction, uint8 hashSize, uint256[] voterIds, bool exists) {
Proposal storage p = proposals[_proposalId];
return (
p.creator,
p.recipientId,
p.votesCount,
p.votesNeeded,
p.amount,
p.executed,
p.ipfsHash,
p.hashFunction,
p.hashSize,
p.voterIds,
p.exists
);
}
function vote(uint256 _proposalId) public coreOnly returns (uint _pId, bool _executed) { function vote(uint256 _proposalId) public coreOnly returns (uint _pId, bool _executed) {
var p = proposals[_proposalId]; var p = proposals[_proposalId];
require(!p.executed); require(!p.executed);
require(p.votes[msg.sender] != true); uint256 contributorId = contributorsContract().getContributorIdByAddress(msg.sender);
p.votes[msg.sender] = true; require(p.votes[contributorId] != true);
p.voterIds.push(contributorId);
p.votes[contributorId] = true;
p.votesCount++; p.votesCount++;
_executed = false; _executed = false;
_pId = _proposalId; _pId = _proposalId;
@ -110,11 +127,6 @@ contract Operator is Upgradeable {
ProposalVoted(_pId, msg.sender, p.votesCount); ProposalVoted(_pId, msg.sender, p.votesCount);
} }
function hasVotedFor(address _sender, uint256 _proposalId) public view returns (bool) {
Proposal storage p = proposals[_proposalId];
return p.exists && p.votes[_sender];
}
function executeProposal(uint proposalId) private returns (bool) { function executeProposal(uint proposalId) private returns (bool) {
var p = proposals[proposalId]; var p = proposals[proposalId];
require(!p.executed); require(!p.executed);