-
Notifications
You must be signed in to change notification settings - Fork 0
/
최은지
74 lines (53 loc) · 1.57 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
pragma solidity 0.5.1;
contract Vote {
// struct
struct candidator {
string name;
uint upVote;
}
// variablie
bool live;
address owner;
candidator[] public candidatorList;
// mapping
mapping(address => bool) Voted;
// event
event AddCandidator(string name);
event UpVote(string candidator, uint upVote);
event FinishVote(bool live);
event Voting(address owner);
// modifier
modifier onlyOwner {
require(msg.sender == owner);
_;
}
// constructor
constructor() public {
owner = msg.sender;
live = true;
emit Voting(owner);
}
// candidator
function addCandidator(string memory _name) public onlyOwner {
require(live == true);
require(candidatorList.length < 5);
candidatorList.push(candidator(_name, 0));
// emit event
emit AddCandidator(_name);
}
// voting
function upVote(uint _indexOfCandidator) public {
require(live == true);
require(_indexOfCandidator < candidatorList.length);
require(Voted[msg.sender] == false);
candidatorList[_indexOfCandidator].upVote++;
Voted[msg.sender] = true;
emit UpVote(candidatorList[_indexOfCandidator].name, candidatorList[_indexOfCandidator].upVote);
}
// finish vote
function finishVote() public onlyOwner{
require(live == true);
live = false;
emit FinishVote(live);
}
}