/
IML_Lottery.sol
385 lines (307 loc) · 14.7 KB
/
IML_Lottery.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";
abstract contract Entropy_interface {
function get_entropy() public virtual view returns (uint256);
function new_round() virtual external;
function deposit_entropy_reward() external virtual payable;
function get_entropy_collateral() public view virtual returns (uint256);
function get_number_of_revealed() public view virtual returns(uint256);
}
contract Lottery {
event NewRound(uint256 indexed round_id);
event RoundFinished(uint256 indexed round_id, address indexed winner, uint256 reward, uint256 rng);
event Deposit(address indexed depositor, uint256 amount_deposited, uint256 amount_credited);
event Refund(address indexed receiver, uint256 indexed round_id, uint256 amount);
address public owner = msg.sender;
address payable public entropy_contract;
address payable public reward_pool_contract; // Token rewards go to the special "staking contract"
//uint256 public deposits_phase_duration = 3 days; // The length of a phase when deposits are accepted;
//uint256 public entropy_phase_duration = 1 days; // The length of a phase when entropy providers reveal their entropy inputs;
uint256 public deposits_phase_duration = 10 minutes;
uint256 public entropy_phase_duration = 5 minutes;
uint256 public entropy_fee = 30; // (will be divided by 1000 during calculations i.e. 1 means 0.1%) | this reward goes to the entropy providers reward pool
// 30 is 3%
uint256 public token_reward_fee = 100; // This reward goes to staked tokens reward pool
// 100 is 10%
uint256 public min_allowed_bet = 1000 ether; // 1K CLO for now
uint8 public max_allowed_deposits = 20; // A user can make 20 bets during a single round
uint256 public max_deposit_pool_threshold = 0; // Specifies the maximal amount the lottery will accept.
// After the amount is reached
// the lottery will no longer accept deposits.
// 0 means "unlimited".
uint256 public current_round;
uint256 public round_start_timestamp;
uint256 public round_reward;
bool public round_reward_paid = false;
mapping (uint256 => bool) public round_failed; // Allows "refunds" of not succesful rounds.
uint256 public current_interval_end; // Used for winner calculations
bool public debug_mode = true;
modifier only_debug
{
require(debug_mode);
_;
}
struct interval
{
uint256 interval_start;
uint256 interval_end;
}
struct player
{
mapping (uint256 => uint8) num_deposits;
uint256 last_round;
mapping (uint256 => mapping (uint8 => interval)) win_conditions; // This player is considered to be a winner when RNG provides a number that matches this intervals
mapping (uint256 => bool) round_refunded;
}
mapping (address => player) public players;
receive() external payable
{
if(get_phase() == 0)
{
start_new_round();
}
else if(get_phase() == 1)
{
deposit();
}
else
{
revert();
}
}
function get_round() public view returns (uint256)
{
return current_round;
}
function get_max_deposit_threshold() public view returns (uint256)
{
return max_deposit_pool_threshold;
}
function get_win_conditions(address _player, uint256 _round, uint8 _depoindex) public view returns(uint256 _start, uint256 _end)
{
_start = players[_player].win_conditions[_round][_depoindex].interval_start;
_end = players[_player].win_conditions[_round][_depoindex].interval_end;
}
function is_winner(address _user) public view returns (bool)
{
bool winner = false;
for (uint8 i = 0; i <= players[_user].num_deposits[current_round]; i++)
{
if(players[_user].win_conditions[current_round][i].interval_start < RNG() && players[_user].win_conditions[current_round][i].interval_end > RNG())
{
winner = true;
}
}
return winner;
}
function get_phase() public view returns (uint8)
{
// 0 - the lottery is not active / pending new round start
// 1 - a lottery round is in progress / acquiring deposits
// 2 - deposits are acquired / entropy revealing phase
// 3 - entropy is revealed, but winner is not paid / it is the time to pay the winner
// 4 - round is finished and the winner is paid / anyone can start a new round
uint8 _status = 0;
if(round_start_timestamp <= block.timestamp && block.timestamp <= round_start_timestamp + deposits_phase_duration)
{
_status = 1;
}
else if (round_start_timestamp < block.timestamp && block.timestamp < round_start_timestamp + deposits_phase_duration + entropy_phase_duration)
{
_status = 2;
}
else if (round_start_timestamp != 0 && round_start_timestamp < block.timestamp && block.timestamp > round_start_timestamp + deposits_phase_duration + entropy_phase_duration && !round_reward_paid)
{
_status = 3;
}
/*
else if (round_start_timestamp < block.timestamp && block.timestamp > round_start_timestamp + deposits_phase_duration + entropy_phase_duration && round_reward_paid)
{
_status = 4;
}
*/
return _status;
}
function deposit() public payable
{
require (msg.value >= min_allowed_bet, "Minimum bet condition is not met");
require (players[msg.sender].num_deposits[current_round] < max_allowed_deposits || players[msg.sender].last_round < current_round, "Too much deposits during this round");
require (get_phase() == 1, "Deposits are only allowed during the depositing phase");
if(max_deposit_pool_threshold != 0)
{
require(msg.value + round_reward <= max_deposit_pool_threshold, "Round reward threshold exceeded");
}
if(players[msg.sender].last_round < current_round)
{
players[msg.sender].last_round = current_round;
players[msg.sender].num_deposits[current_round] = 0;
}
else
{
players[msg.sender].num_deposits[current_round]++;
}
// Assign the "winning interval" for the player
players[msg.sender].win_conditions[current_round][players[msg.sender].num_deposits[current_round]].interval_start = current_interval_end;
players[msg.sender].win_conditions[current_round][players[msg.sender].num_deposits[current_round]].interval_end = current_interval_end + msg.value;
current_interval_end += msg.value;
uint256 _reward_after_fees = msg.value;
// TODO: replace it with SafeMath
// TODO: update the contract to only send rewards upon completion of the round
send_token_reward(msg.value * token_reward_fee / 1000);
_reward_after_fees -= (msg.value * token_reward_fee / 1000);
send_entropy_reward(msg.value * entropy_fee / 1000);
_reward_after_fees -= msg.value * entropy_fee / 1000;
round_reward += _reward_after_fees;
emit Deposit(msg.sender, msg.value, _reward_after_fees);
}
function refund(uint256 _round) external
{
//require(current_round > _round, "Only refunds of finished rounds are allowed");
require(round_failed[_round], "Only refunds of FAILED rounds are allowed");
// Calculating the refund amount
uint256 _reward = 0;
for (uint8 i = 0; i <= players[msg.sender].num_deposits[_round]; i++)
{
_reward += players[msg.sender].win_conditions[_round][i].interval_end - players[msg.sender].win_conditions[_round][i].interval_start;
}
uint256 _reward_before_fees = _reward;
// Subtract the entropy fee
_reward -= _reward_before_fees * entropy_fee / 1000;
_reward -= _reward_before_fees * token_reward_fee / 1000;
players[msg.sender].round_refunded[_round] = true;
payable(msg.sender).transfer(_reward);
emit Refund(msg.sender, _round, _reward);
}
function send_entropy_reward(uint256 _reward) internal
{
//entropy_contract.transfer(msg.value * entropy_fee / 1000);
//entropy_contract.transfer(_reward);
Entropy_interface(entropy_contract).deposit_entropy_reward{value: _reward}();
}
function send_token_reward(uint256 _reward) internal
{
//reward_pool_contract.transfer(msg.value * token_reward_fee / 1000);
//reward_pool_contract.transfer(_reward);
reward_pool_contract.call{value: _reward};
}
function start_new_round() public payable
{
require(current_round == 0 || round_reward_paid, "Cannot start a new round while reward for the previous one is not paid. Call finish_round function");
current_round++;
emit NewRound(current_round);
round_start_timestamp = block.timestamp;
current_interval_end = 0;
round_reward_paid = false;
Entropy_interface(entropy_contract).new_round();
//require_entropy_provider(msg.sender); // Request the starter of a new round to also provide initial entropy
// Initiate the first deposit of the round
deposit();
}
function finish_round(address payable _winner) public
{
// Important: finishing an active round does not automatically start a new one
require(block.timestamp > round_start_timestamp + deposits_phase_duration + entropy_phase_duration, "Round can be finished after the entropy reveal phase only");
//require(check_entropy_criteria(), "There is not enough entropy to ensure a fair winner calculation");
if(check_entropy_criteria())
{
// Round is succsefully completed and there was enough entropy provided
// Paying the winner
// Safe loop, cannot be more than 20 iterations
for (uint8 i = 0; i <= players[_winner].num_deposits[current_round]; i++)
{
if(players[_winner].win_conditions[current_round][i].interval_start < RNG() && players[_winner].win_conditions[current_round][i].interval_end >= RNG())
{
_winner.transfer(round_reward);
round_reward_paid = true;
}
}
emit RoundFinished(current_round, _winner, round_reward, RNG());
}
else
{
// Round is completed without sufficient entropy => allow refunds and increase the round counter
// round_successful[current_round] = false; // This values are `false` by default in solidity
round_failed[current_round] = true;
round_reward_paid = true;
emit RoundFinished(current_round, address(0), 0, 0);
}
require(round_reward_paid, "The provided address is not a winner of the current round");
}
function pay_fees() internal
{
}
function RNG() public view returns (uint256)
{
// Primitive random number generator dependant on both `entropy` and `interval` for testing reasons
uint256 _entropy = Entropy_interface(entropy_contract).get_entropy();
uint256 _timestamp = round_start_timestamp + deposits_phase_duration + entropy_phase_duration;
uint256 _result;
assembly
{
let x := sload(min_allowed_bet.slot)
_entropy := mul(_entropy, _timestamp)
_entropy := mul(_entropy, x)
_entropy := mul(_entropy, 115792089237316195423570985008687907853269984665640564039457584007913129639935)
}
// `entropy` is a random value; can be greater or less than `current_interval_end`
if(_entropy > current_interval_end)
{
_result = _entropy % current_interval_end;
}
else
{
_result = current_interval_end % _entropy;
}
return _result;
}
function check_entropy_criteria() public returns (bool)
{
// Needs to check the sufficiency of entropy for the round reward prizepool size
//return true;
return Entropy_interface(entropy_contract).get_number_of_revealed() > 0;
}
modifier only_owner
{
require(msg.sender == owner);
_;
}
modifier only_entropy_contract
{
require(msg.sender == entropy_contract);
_;
}
function set_entropy_contract(address payable _new_contract) public only_owner
{
entropy_contract = _new_contract;
}
function set_reward_contract(address payable _new_contract) public only_owner
{
reward_pool_contract = _new_contract;
}
function rescueERC20(address token, address to) external only_owner {
uint256 value = IERC20(token).balanceOf(address(this));
IERC20(token).transfer(to, value);
}
function configure(uint256 _min_bet, uint8 _max_deposits, uint256 _max_deposit_threshold, uint256 _deposit_phase_duration, uint256 _reveal_phase_duration) public only_owner
{
min_allowed_bet = _min_bet;
max_allowed_deposits = _max_deposits;
max_deposit_pool_threshold = _max_deposit_threshold;
deposits_phase_duration = _deposit_phase_duration;
entropy_phase_duration = _reveal_phase_duration;
}
function configureFees(uint256 _entropy_fee, uint256 _token_reward_fee) public only_owner
{
entropy_fee = _entropy_fee;
token_reward_fee = _token_reward_fee;
}
function forceWithdraw(uint256 _amount) only_owner only_debug public
{
payable(msg.sender).transfer(_amount);
}
function disableDebug() only_debug only_owner public
{
debug_mode = false;
}
}