-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHackathonMunon.sol
249 lines (212 loc) · 7.35 KB
/
HackathonMunon.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
pragma solidity ^0.5.0;
import "node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol";
contract HackathonMunon
{
// Events
event HackathonCreation
(
address hackaton_host,
uint256 hackathon_id,
string image_hash,
string name,
uint256 creation_time
);
event Registration
(
uint256 hackathon_id,
address participant_addr
);
event SponsorshipSubmited
(
uint256 hackathon_id,
uint256 value
);
event RatingSubmited
(
uint256 hackathon_id,
address reviewer_addr,
address reviewed_addr,
uint256 points
);
event CashOut
(
uint256 hackathon_id,
address participant_addr,
uint256 reward
);
event HackathonReviewEnabled
(
uint256 hackathon_id
);
event HackathonFinished
(
uint256 hackathon_id
);
using SafeMath for uint256;
// Structs
struct Hackathon
{
address host_addr;
HackathonState state;
string image_hash;
string name;
uint256 pot;
uint256 creation_time;
uint256 enable_review_time;
}
struct Participant
{
address addr;
uint256 points;
}
// Enums
enum HackathonState { RegistrationOpen, ReviewEnabled, Finished }
// Public variables
mapping(uint256 => Hackathon) public hackathons; // Stores hackathons data
mapping(uint256 => mapping(address => Participant)) public hackathon_participants; // Stores participant data
// Rating history, enables correcting ratings and prevents rating
mapping(uint256 => mapping(address => mapping(address => uint256))) public participant_ratings;
uint256 public hackathon_count; // Helps generating a new hackathon id
mapping(uint256 => mapping(address => bool)) public participant_has_cashed_out; // Helps preventing double cash out
mapping(uint256 => uint256) public total_hackathon_points; // Helps calculating pot splits
uint256 entry_fee = 0.03 ether; // Hackathon entry fee
// Modifiers
modifier paysEntryFee()
{
require(msg.value == entry_fee, "Amount not equal to pay fee");
_;
}
modifier hasNotJoined(uint256 hackathon_id)
{
require(hackathon_participants[hackathon_id][msg.sender].addr == address(0), "Participant has joined");
_;
}
modifier hasJoined(uint256 hackathon_id)
{
require(hackathon_participants[hackathon_id][msg.sender].addr != address(0), "Participant has not joined");
_;
}
modifier participantExists(uint256 hackathon_id, address participant_addr)
{
require(hackathon_participants[hackathon_id][participant_addr].addr != address(0), "Participant does not exists");
_;
}
modifier pointsAreValid(uint256 points)
{
require(points <= 5, "Points are greater than 5");
_;
}
modifier hasNotCashedOut(uint256 hackathon_id, address participant_addr)
{
require(!participant_has_cashed_out[hackathon_id][participant_addr], "Participant has already cashed out");
_;
}
modifier isRegistrationOpen(uint256 hackathon_id)
{
require(hackathons[hackathon_id].state == HackathonState.RegistrationOpen, "Hackathon registration is not open");
_;
}
modifier isReviewEnabled(uint256 hackathon_id)
{
require(hackathons[hackathon_id].state == HackathonState.ReviewEnabled, "Hackathon review is not enabled");
_;
}
modifier isFinished(uint256 hackathon_id)
{
require(hackathons[hackathon_id].state == HackathonState.Finished, "Hackathon is not finished");
_;
}
modifier isNotFinished(uint256 hackathon_id)
{
require(hackathons[hackathon_id].state != HackathonState.Finished, "Hackathon is finished");
_;
}
modifier twoMonthFromCreation(uint256 hackathon_id)
{
require(now >= hackathons[hackathon_id].creation_time + 60 days, "time must be greater than 2 months");
_;
}
modifier oneWeekFromReview(uint256 hackathon_id)
{
require(now >= hackathons[hackathon_id].enable_review_time + 7 days, "time must be greater than 1 week");
_;
}
modifier isHackathonHost(uint256 hackathon_id)
{
require(hackathons[hackathon_id].host_addr == msg.sender, "You are not the hackathon host");
_;
}
// Public methods
function createHackathon(string memory image_hash, string memory _name) public
{
hackathon_count += 1;
uint256 date_now = now;
hackathons[hackathon_count] = Hackathon(msg.sender, HackathonState.RegistrationOpen, image_hash, _name, 0, date_now, date_now);
emit HackathonCreation(msg.sender, hackathon_count, image_hash,_name, date_now);
}
function join(
uint256 hackathon_id
) public payable paysEntryFee hasNotJoined(hackathon_id) isRegistrationOpen(hackathon_id)
{
Participant memory participant = Participant(msg.sender, 0);
hackathon_participants[hackathon_id][msg.sender] = participant;
hackathons[hackathon_id].pot = hackathons[hackathon_id].pot.add(entry_fee);
emit Registration(hackathon_id, msg.sender);
}
function sponsor(
uint256 hackathon_id
) public payable isNotFinished(hackathon_id)
{
hackathons[hackathon_id].pot = hackathons[hackathon_id].pot.add(msg.value);
emit SponsorshipSubmited(hackathon_id, msg.value);
}
function rate(
uint256 hackathon_id,
address participant_addr,
uint256 points
) public hasJoined(hackathon_id) participantExists(hackathon_id, participant_addr) pointsAreValid(points) isReviewEnabled(hackathon_id)
{
uint256 rating_stored = participant_ratings[hackathon_id][msg.sender][participant_addr];
hackathon_participants[hackathon_id][participant_addr].points = hackathon_participants[hackathon_id][participant_addr].points.add(
points).sub(rating_stored);
total_hackathon_points[hackathon_id] = total_hackathon_points[hackathon_id].add(points).sub(rating_stored);
participant_ratings[hackathon_id][msg.sender][participant_addr] = points;
emit RatingSubmited(hackathon_id, msg.sender, participant_addr, points);
}
function cashOut(uint256 hackathon_id)
public hasJoined(hackathon_id) hasNotCashedOut(hackathon_id, msg.sender) isFinished(hackathon_id) returns(uint256)
{
uint256 total_points = total_hackathon_points[hackathon_id];
uint256 my_points = hackathon_participants[hackathon_id][msg.sender].points;
// Calculate reward
uint256 pot = hackathons[hackathon_id].pot;
uint256 my_reward = pot.mul(my_points).div(total_points);
msg.sender.transfer(my_reward);
participant_has_cashed_out[hackathon_id][msg.sender] = true;
emit CashOut(hackathon_id, msg.sender, my_reward);
}
function enableHackathonReview(uint256 hackathon_id) public isHackathonHost(hackathon_id)
{
hackathons[hackathon_id].state = HackathonState.ReviewEnabled;
hackathons[hackathon_id].enable_review_time = now;
emit HackathonReviewEnabled(hackathon_id);
}
function finishHackathon(uint256 hackathon_id) public isHackathonHost(hackathon_id)
{
hackathons[hackathon_id].state = HackathonState.Finished;
emit HackathonFinished(hackathon_id);
}
function forceFinishHackathon(uint256 hackathon_id) public isRegistrationOpen(hackathon_id) twoMonthFromCreation(hackathon_id)
{
hackathons[hackathon_id].state = HackathonState.Finished;
emit HackathonFinished(hackathon_id);
}
function forceFinishHackathonFromReview(uint256 hackathon_id) public isReviewEnabled(hackathon_id) oneWeekFromReview(hackathon_id)
{
hackathons[hackathon_id].state = HackathonState.Finished;
emit HackathonFinished(hackathon_id);
}
function () external payable {
revert();
}
}