-
Notifications
You must be signed in to change notification settings - Fork 0
/
voter_contract.sol
73 lines (61 loc) · 2.26 KB
/
voter_contract.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
pragma solidity ^0.8.3;
contract Voter {
int256 public capitalInvestedInVote;
mapping(address => int256) capitalContributed;
struct VoteRecorded {
bool vote;
bool hasVoted;
}
mapping(address => VoteRecorded) public votesRecorded;
int256 public voteBalance;
bool public votingOpen = true;
bool public voteOutcome;
// _capitalInvested: capital (ETH) which the user uses to give weight to their vote.
// _vote: bool which represents the user's vote for (true) or against (false) the proposal.
// TODO: make payable?
// TODO: add events.
function receiveVote(int256 _capitalInvested, bool _vote)
public
returns (bool)
{
require(votingOpen = true, "ERROR vote: Vote has ended.");
require(
_capitalInvested > 0,
"ERROR vote: Capital invested must be greater than 0."
);
if (votesRecorded[msg.sender].hasVoted == false) {
// Register the vote.
votesRecorded[msg.sender].vote = _vote;
votesRecorded[msg.sender].hasVoted = true;
// Increment the total capital invested.
capitalInvestedInVote += _capitalInvested;
// Increment the voter's total contribution.
capitalContributed[msg.sender] += _capitalInvested;
// Update the vote balance.
updateVoteBalance(_capitalInvested, _vote);
} else {
// The voter has already voted.
// Let them add to their position but not change their vote.
bool existingVote = votesRecorded[msg.sender].vote;
updateVoteBalance(_capitalInvested, existingVote);
}
return true;
}
// TODO: make private or internal?
// TODO: add a time component, e.g. number of blocks.
function decideVoteOutcome() public returns (bool) {
// Close the vote;
votingOpen = false;
// Return the outcome.
voteOutcome = voteBalance > 0;
return voteOutcome;
}
// Update the vote balance based on the vote (for/against).
function updateVoteBalance(int256 _amount, bool _vote) public {
if (_vote == true) {
voteBalance += _amount;
} else {
voteBalance -= _amount;
}
}
}