-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoting.sol
More file actions
85 lines (74 loc) · 2.38 KB
/
Voting.sol
File metadata and controls
85 lines (74 loc) · 2.38 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Voting {
struct Proposal {
address target;
bytes data;
uint yesCount;
uint noCount;
}
event ProposalCreated(uint id);
event VoteCast(uint id, address voter);
Proposal[] public proposals;
bool voted;
uint256 voteIdx = 0;
uint256 propId;
bool executed;
constructor(address[] memory addresses) {
for (uint i=0; i<addresses.length;i++){
proposals.push(Proposal(addresses[i], "", 0, 0));
}
}
function verify(address addr) public returns (bool) {
for (uint i=0; i<proposals.length;i++){
if (proposals[i].target == addr) {
return true;
}
}
return false;
}
function newProposal(address target, bytes memory data) external {
if (verify(msg.sender)) {
proposals.push(Proposal(target, data, 0, 0));
emit ProposalCreated(propId);
propId++;
}
}
function castVote(uint proposalId, bool vote) external {
if (verify(msg.sender)) {
if (voted) {
if (vote) {
proposals[proposalId].yesCount += 1;
emit VoteCast(proposalId, msg.sender);
voteIdx++;
} else {
proposals[proposalId].noCount += 1;
emit VoteCast(proposalId, msg.sender);
voteIdx++;
}
} else {
if (vote) {
proposals[proposalId].yesCount += 1;
emit VoteCast(proposalId, msg.sender);
voteIdx++;
} else {
proposals[proposalId].noCount += 1;
emit VoteCast(proposalId, msg.sender);
voteIdx++;
}
voted = true;
}
} else {
revert();
}
if (voteIdx >= 10) {
if (executed == false) {
Proposal storage proposal = proposals[proposalId];
// Execute the proposal by calling the target with the data
(bool success, ) = proposal.target.call(proposal.data);
require(success, "Proposal execution failed");
executed = true;
}
}
}
}