-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.sol
124 lines (97 loc) · 3.33 KB
/
vote.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 < 0.9.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "hardhat/console.sol";
contract Voting is Ownable {
struct Voter {
mapping(uint => bool) voted; //if true, the person already voted to a question
address addr; //voter acount address
}
struct Question {
uint id;
string value; // question string
uint upCount; // approve count
uint downCount; // opposite count
}
uint public voterCount; // number of voters
uint public questionCount; // number of questions
uint private currentId; // id of last question
address public administrator; // address of administrator
mapping(address => Voter) public voters; // voters map
mapping(uint => Question) public questions; // question string
event Voted(address voter, uint256 to, bool value); // event for vote logging
event AddedQuestion(uint id); // event when a question added
event UpdatedQuestion(uint id); // event when a question updated
event RemovedQuestion(uint id); // event when a question removed
// contructor
constructor() {
administrator = msg.sender;
voterCount = questionCount = currentId = 0;
}
// add question with string v
function addQuestion(string memory v) public {
require(msg.sender == administrator, "only administrator can add question.");
Question memory q = Question({ id: currentId, value: v, upCount: 0, downCount: 0 });
questions[currentId] = q;
questionCount = questionCount + 1;
currentId = currentId + 1;
emit AddedQuestion(q.id);
}
function updateQuestion(uint id, string memory value) public returns(bool){
Question memory q = questions[id];
if(q.id == id) {
q.value = value;
return true;
}
return false;
}
function deleteQuestion(uint id) public returns(bool) {
Question memory q = questions[id];
if(q.id == id) {
delete questions[id];
questionCount = questionCount - 1;
return true;
}
return false;
}
//add a voter to voter list
function addVoter(address _voter) public {
voters[_voter].addr = _voter;
voterCount ++;
}
//
function vote(uint id, bool value) public {
require(voters[msg.sender].voted[id] == false, "already voted");
if(value) {
questions[id].upCount = questions[id].upCount + 1;
} else {
questions[id].downCount = questions[id].downCount + 1;
}
voters[msg.sender].voted[id] = true;
emit Voted(msg.sender, id, value);
}
function winningQuestion() public view returns(uint winningQuestion_) {
// int a = 2 - 5;
// console.log(a);
// uint p = 0;
// require(questionCount > 0, "no question");
// if(questionCount == 1) return 0;
// Question memory win = questions[p];
// Question memory q;
// for(uint i = 1; i < questionCount; i++) {
// q = questions[i];
// int sum = int(win.upCount) - int(win.downCount);
// int _sum = int(q.upCount) - int(q.downCount);
// if(_sum > sum) p = i;
// }
// winningQuestion_ = p;
}
function getQuestions() public view returns(Question[] memory) {
require(questionCount > 0, "not question exist");
Question[] memory list = new Question[](currentId);
for(uint i = 0; i < currentId; i++) {
list[i] = questions[i];
}
return list;
}
}