From 5dc5f1ed31b719e474ce16cdc57d96392677e7d5 Mon Sep 17 00:00:00 2001 From: KIrill Guzenko Date: Tue, 24 Jul 2018 04:58:40 +0300 Subject: [PATCH] PR fixes --- contracts/governance/Voting.sol | 271 +++++++++++++++----------------- 1 file changed, 125 insertions(+), 146 deletions(-) diff --git a/contracts/governance/Voting.sol b/contracts/governance/Voting.sol index d307a2f..c004dcf 100644 --- a/contracts/governance/Voting.sol +++ b/contracts/governance/Voting.sol @@ -13,8 +13,7 @@ import "zeppelin-solidity/contracts/ownership/Ownable.sol"; contract Voting is IVoting, Ownable { using VotingLib for VotingLib.VotingStorage; - VotingLib.VotingStorage stor; - + VotingLib.VotingStorage store; event Voted(address _who, bool _yes); event CallAction(); event DelegatedTo(address _sender, uint _tokensAmount); @@ -27,9 +26,9 @@ contract Voting is IVoting, Ownable { * @param _minutesToVote - if is zero -> voting until quorum reached, else voting finish after minutesToVote minutes * @param _quorumPercent - percent of group members to make quorum reached. If minutesToVote==0 and quorum reached -> voting is finished * @param _consensusPercent - percent of voters (not of group members!) to make consensus reached. If consensus reached -> voting is finished with YES result - * @param _votingType - * @param _groupName - * @param _tokenAddress + * @param _votingType – one of the voting type, see enum votingType + * @param _groupName – for votings that for dao group members only + * @param _tokenAddress – for votings that uses token amount of voter */ constructor(IDaoBase _dao, IProposal _proposal, address _origin, VotingLib.VotingType _votingType, @@ -37,69 +36,68 @@ contract Voting is IVoting, Ownable { uint _quorumPercent, uint _consensusPercent, address _tokenAddress) public { - stor.generalConstructor(_dao, _proposal, _origin, _votingType, _minutesToVote, _groupName, _quorumPercent, _consensusPercent, _tokenAddress); + store.generalConstructor(_dao, _proposal, _origin, _votingType, _minutesToVote, _groupName, _quorumPercent, _consensusPercent, _tokenAddress); } function quorumPercent()view returns(uint){ - return stor.quorumPercent; + return store.quorumPercent; } function consensusPercent()view returns(uint){ - return stor.consensusPercent; + return store.consensusPercent; } function groupName()view returns(string){ - return stor.groupName; + return store.groupName; } function getVotersTotal() view returns(uint){ - return stor.getVotersTotal(); + return store.getVotersTotal(); } function getPowerOf(address _voter) view returns(uint){ - return stor.getPowerOf(_voter); + return store.getPowerOf(_voter); } function vote(bool _isYes) public{ - stor.libVote(msg.sender, _isYes); + store.libVote(msg.sender, _isYes); } function callActionIfEnded() public { - stor.callActionIfEnded(); + store.callActionIfEnded(); } function isFinished() public view returns(bool){ - return stor.isFinished(); + return store.isFinished(); } function isYes() public view returns(bool){ - return stor.isYes(); + return store.isYes(); } function getVotingStats() public constant returns(uint yesResults, uint noResults, uint votersTotal){ - return stor.getVotingStats(); + return store.getVotingStats(); } function cancelVoting() public onlyOwner { - stor.canceled = true; + store.canceled = true; } // ------------------ LIQUID ------------------ - function getDelegatedPowerOf(address _of) public view returns(uint) { - return stor.getDelegatedPowerOf(_of); + return store.getDelegatedPowerOf(_of); } function getDelegatedPowerByMe(address _to) public view returns(uint) { - return stor.getDelegatedPowerByMe(_to); + return store.getDelegatedPowerByMe(_to); } function delegateMyVoiceTo(address _to, uint _tokenAmount) public { - stor.delegateMyVoiceTo(_to, _tokenAmount); + store.delegateMyVoiceTo(_to, _tokenAmount); } function removeDelegation(address _to) public { - stor.removeDelegation(_to); + store.removeDelegation(_to); } } @@ -109,8 +107,6 @@ library VotingLib { event DelegatedTo(address _sender, uint _tokensAmount); event DelegationRemoved(address _from, address _to); - event consoleUint(string a, uint b); - enum VotingType{ NoVoting, Voting1p1v, @@ -151,68 +147,67 @@ library VotingLib { VotingType votingType; } - function generalConstructor(VotingStorage storage self, IDaoBase _dao, IProposal _proposal, + function generalConstructor(VotingStorage storage store, IDaoBase _dao, IProposal _proposal, address _origin, VotingType _votingType, uint _minutesToVote, string _groupName, uint _quorumPercent, uint _consensusPercent, address _tokenAddress) public - { - + { require((_quorumPercent<=100)&&(_quorumPercent>0)); require((_consensusPercent<=100)&&(_consensusPercent>0)); - self.dao = _dao; - self.proposal = _proposal; - self.minutesToVote = _minutesToVote; - self.quorumPercent = _quorumPercent; - self.consensusPercent = _consensusPercent; - self.groupName = _groupName; - self.votingType = _votingType; - self.genesis = now; - - if(VotingType.Voting1p1v!=self.votingType){ - self.tokenAddress = _tokenAddress; - self.votingID = StdDaoToken(_tokenAddress).startNewVoting(); + store.dao = _dao; + store.proposal = _proposal; + store.minutesToVote = _minutesToVote; + store.quorumPercent = _quorumPercent; + store.consensusPercent = _consensusPercent; + store.groupName = _groupName; + store.votingType = _votingType; + store.genesis = now; + + if(VotingType.Voting1p1v!=store.votingType){ + store.tokenAddress = _tokenAddress; + store.votingID = StdDaoToken(_tokenAddress).startNewVoting(); } - libVote(self, _origin, true); + libVote(store, _origin, true); } function getNow() public view returns(uint){ return now; } - function getVotersTotal(VotingStorage storage self)public view returns(uint){ - if(VotingType.Voting1p1v==self.votingType){ - return self.dao.getMembersCount(self.groupName); - }else if(VotingType.VotingSimpleToken==self.votingType){ - return StdDaoToken(self.tokenAddress).totalSupply(); - }else if(VotingType.VotingQuadratic==self.votingType){ - return StdDaoToken(self.tokenAddress).getVotingTotalForQuadraticVoting(); - }else if(VotingType.VotingLiquid==self.votingType){ - return StdDaoToken(self.tokenAddress).totalSupply(); + function getVotersTotal(VotingStorage storage store)public view returns(uint){ + if(VotingType.Voting1p1v==store.votingType){ + return store.dao.getMembersCount(store.groupName); + }else if(VotingType.VotingSimpleToken==store.votingType){ + return StdDaoToken(store.tokenAddress).totalSupply(); + }else if(VotingType.VotingQuadratic==store.votingType){ + return StdDaoToken(store.tokenAddress).getVotingTotalForQuadraticVoting(); + }else if(VotingType.VotingLiquid==store.votingType){ + return StdDaoToken(store.tokenAddress).totalSupply(); }else{ revert(); } } - function getPowerOf(VotingStorage storage self, address _voter)public view returns(uint){ - if(VotingType.Voting1p1v==self.votingType){ - if(self.dao.isGroupMember(self.groupName, _voter)){ + function getPowerOf(VotingStorage storage store, address _voter)public view returns(uint){ + if(VotingType.Voting1p1v==store.votingType){ + if(store.dao.isGroupMember(store.groupName, _voter)){ return 1; }else{ return 0; } - }else if(VotingType.VotingSimpleToken==self.votingType){ - return StdDaoToken(self.tokenAddress).getBalanceAtVoting(self.votingID, _voter); - }else if(VotingType.VotingQuadratic==self.votingType){ - return sqrt(StdDaoToken(self.tokenAddress).getBalanceAtVoting(self.votingID, _voter)); - }else if(VotingType.VotingLiquid==self.votingType){ - uint res = StdDaoToken(self.tokenAddress).getBalanceAtVoting(self.votingID, _voter); - for(uint i = 0; i < self.delegations[_voter].length; i++){ - if(!self.delegations[_voter][i].isDelegator){ - res += self.delegations[_voter][i].amount; + }else if(VotingType.VotingSimpleToken==store.votingType){ + return StdDaoToken(store.tokenAddress).getBalanceAtVoting(store.votingID, _voter); + }else if(VotingType.VotingQuadratic==store.votingType){ + return sqrt(StdDaoToken(store.tokenAddress).getBalanceAtVoting(store.votingID, _voter)); + }else if(VotingType.VotingLiquid==store.votingType){ + uint res = StdDaoToken(store.tokenAddress).getBalanceAtVoting(store.votingID, _voter); + for(uint i = 0; i < store.delegations[_voter].length; i++){ + if(!store.delegations[_voter][i].isDelegator){ + res += store.delegations[_voter][i].amount; }else{ - res -= self.delegations[_voter][i].amount; + res -= store.delegations[_voter][i].amount; } } return res; @@ -222,152 +217,136 @@ library VotingLib { } function sqrt(uint x) public pure returns (uint y){ - uint z = (x + 1) / 2; + uint z = (x+1)/2; y = x; - while (z < y) { + while (z0){ - return _isTimeElapsed(self); - + }else if(store.minutesToVote>0){ + return _isTimeElapsed(store); }else{ - return _isQuorumReached(self); + return _isQuorumReached(store); } } - function _isTimeElapsed(VotingStorage storage self) internal view returns(bool){ - emit consoleUint('uint256(now)', getNow()); - emit consoleUint('self.genesis', self.genesis); - emit consoleUint('(self.minutesToVote * 60 * 1000)', (self.minutesToVote * 60 * 1000)); - if(self.minutesToVote==0){ + function _isTimeElapsed(VotingStorage storage store) internal view returns(bool){ + if(store.minutesToVote==0){ return false; } - return (now - self.genesis) > (self.minutesToVote * 60 * 1000); + return (now - store.genesis) > (store.minutesToVote * 60 * 1000); } - function _isQuorumReached(VotingStorage storage self) internal view returns(bool){ - uint yesResults = 0; - uint noResults = 0; - uint votersTotal = 0; - - (yesResults, noResults, votersTotal) = getVotingStats(self); - return ((yesResults + noResults) * 100) >= (votersTotal * self.quorumPercent); + function _isQuorumReached(VotingStorage storage store) internal view returns(bool){ + var (yesResults, noResults, votersTotal) = getVotingStats(store); + return ((yesResults + noResults) * 100) >= (votersTotal * store.quorumPercent); } - function _isConsensusReached(VotingStorage storage self) internal view returns(bool){ - uint yesResults = 0; - uint noResults = 0; - uint votersTotal = 0; - - (yesResults, noResults, votersTotal) = getVotingStats(self); - return (yesResults * 100) >= ((yesResults + noResults) * self.consensusPercent); + function _isConsensusReached(VotingStorage storage store) internal view returns(bool){ + var (yesResults, noResults, votersTotal) = getVotingStats(store); + return (yesResults * 100) >= ((yesResults + noResults) * store.consensusPercent); } - function isYes(VotingStorage storage self) public view returns(bool){ - if(true==self.finishedWithYes){ + function isYes(VotingStorage storage store) public view returns(bool){ + if(true==store.finishedWithYes){ return true; } - return !self.canceled&& isFinished(self)&& - _isQuorumReached(self)&& _isConsensusReached(self); + return !store.canceled&& isFinished(store)&& + _isQuorumReached(store)&& _isConsensusReached(store); } - function getVotingStats(VotingStorage storage self) public constant returns(uint yesResults, uint noResults, uint votersTotal){ - for(uint i=0; i