-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoteSystem.sol
More file actions
88 lines (72 loc) · 2.45 KB
/
Copy pathVoteSystem.sol
File metadata and controls
88 lines (72 loc) · 2.45 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
86
87
88
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract VotingSystem {
address public owner;
struct Proposal {
uint256 id;
string desciption;
uint256 yesVotes;
uint256 noVotes;
uint256 createAt;
bool resultChecked;
}
Proposal[] public proposals;
mapping(address => bool) public isVoter;
mapping(uint256 => mapping(address => bool)) public hasVoted; // proposalId -> (voter => voted)
modifier onlyOwner() {
require(msg.sender == owner, "!Owner");
_;
}
modifier onlyVoter() {
require(isVoter[msg.sender], "!Voter");
_;
}
constructor(address[] memory _voters) {
owner = msg.sender;
// 지정된 주소들에게 voter 권한 부여
for (uint256 i = 0; i < _voters.length; i++) {
isVoter[_voters[i]] = true;
}
}
// 배포자만 새로운 안건 추가
function addProposal(string memory _description) public onlyOwner {
proposals.push(Proposal({
id: proposals.length,
description: _description,
yesVoter: 0,
noVoter: 0,
createdAt: block.timestamp,
resultChekced: false
}));
}
// voter만 특정 안건 확인
function viewProposal(uint256 _id) public view onlyVoter returns (Proposal memory) {
require(_id < proposals.length, "Invalid ID");
return proposals[_id];
}
// voter만 투표 가능 (중복 투표 방지)
function vote(uint256 _id, bool _support) public onlyVoter {
require(_id < proposals.length, "Invalid ID");
require(!hasVoted[_id][msg.sender], "Already voted");
Proposal storage proposal = proposals[_id];
if (_support) {
proposal.yesVotes += 1;
} else {
proposal.noVotes += 1;
}
hasVoted[_id][msg.sender] = true;
}
// 안건 생성 후 5분 이후 결과 판단
function checkResult(uint256 _id) public view returns (string memory) {
require(_id < proposals.length, "Invalid ID");
Proposal memory proposal = proposals[_id];
require(block.timestamp >= proposal.createAt + 5 minutes, "Too early");
if (proposal.yesVotes > proposal.noVotes) {
return "Proposal Passed";
} else if (proposal.noVotes > proposal.yesVotes) {
return "Proposal Rejected";
} else {
return "Tie";
}
}
}