From 0b8eb8cadecc16c8dc1e5bf2646dca6a401be523 Mon Sep 17 00:00:00 2001 From: Tim Liner Date: Fri, 23 Aug 2024 15:46:58 -0600 Subject: [PATCH] finish TokenizedBallot contract --- contracts/TokenizedBallot.sol | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/contracts/TokenizedBallot.sol b/contracts/TokenizedBallot.sol index 8d4660b..798b69d 100644 --- a/contracts/TokenizedBallot.sol +++ b/contracts/TokenizedBallot.sol @@ -14,6 +14,7 @@ contract TokenizedBallot { IMyToken public tokenContract; Proposal[] public proposals; uint256 public targetBlockNumber; + mapping (address => uint256) public spentVotePower; constructor( bytes32[] memory _proposalNames, @@ -22,21 +23,28 @@ contract TokenizedBallot { ) { tokenContract = IMyToken(_tokenContract); targetBlockNumber = _targetBlockNumber; - // TODO: Validate if targetBlockNumber is in the past + require( + targetBlockNumber <= block.number, + "TokenizedBallot: targetBlockNumber must be in the past" + ); for (uint i = 0; i < _proposalNames.length; i++) { proposals.push(Proposal({name: _proposalNames[i], voteCount: 0})); } } function vote(uint256 proposal, uint256 amount) external { - // TODO: Implement vote function + uint256 senderVotingPower = getVotePower(msg.sender); + require( + senderVotingPower >= amount, + "TokenizedBallot: insufficient voting power" + ); + spentVotePower[msg.sender] += amount; proposals[proposal].voteCount += amount; } - // function getVotePower(address voter ) public view returns (uint voterPower_) { - // votePower_ = 0; - - // } + function getVotePower(address voter ) public view returns (uint voterPower_) { + voterPower_ = tokenContract.getPastVotes(voter, targetBlockNumber) - spentVotePower[voter]; + } function winningProposal() public view returns (uint winningProposal_) { uint winningVoteCount = 0;