/
NeoGame.sol
422 lines (366 loc) · 14.9 KB
/
NeoGame.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
pragma solidity ^0.4.24;
import "openzeppelin-solidity/contracts/token/ERC20/SafeERC20.sol";
import "./Token.sol";
contract NeoGame {
uint8 constant internal BET_SIZE = 5;
uint8 constant internal TECH_BONUS = 10;
uint8 constant internal SECONDS_PER_BLOCK = 15;
uint8 constant internal FAST_WITHDRAW_PERCENTAGE_LIMIT = 20;
uint8 constant internal BONUS_BET_VARIANTS = 21;
uint8 constant internal MAIN_BET_VARIANTS = 49;
uint8 constant internal PRIZE_FUND_PERCENTAGE = 70;
uint8 constant internal ALL_BETS_ARE_MADE = 240;
uint16 constant internal BLOCK_QUANTITY_ETHEREUM_IS_SAVING = 256;
uint16 constant internal BLOCKS_PER_PERIOD = 40320; // ((86400 / 15) * 7);
uint32 constant internal PERIOD_IN_SECONDS = 604800;
using SafeERC20 for Token;
event NewBet(uint256 gameIndex, uint256 ticketIndex, address player);
event GameCreated(uint256 gameIndex);
event WinnerNumbersBeingSet(uint256 gameIndex);
event PrizeSent(uint256 gameIndex, uint256 ticketIndex, uint256 prize, address player);
modifier gameIsNotExpired(uint256 _gameIndex) {
require(!isGameExpired(_gameIndex));
_;
}
modifier gameNumbersNotSet(uint256 _gameIndex) {
require(!isGameNumbersAreDefined(_gameIndex));
_;
}
modifier blockNumberIsEnoughToDefineWinnerNumbers(uint256 _gameIndex) {
require(block.number > games[_gameIndex].block + BET_SIZE + 1);
_;
}
modifier gameIsNotReleased(uint256 _gameIndex) {
require(!isGameReleased(_gameIndex));
_;
}
modifier ticketIsNotReleased(uint256 _gameIndex, uint256 _ticketIndex) {
require(!games[_gameIndex].tickets[_ticketIndex].released);
_;
}
struct Ticket {
address player;
uint8[BET_SIZE] mainBet;
uint8 bonusBet;
uint256 amount;
bool released;
}
struct BigPrizeWinner {
address player;
uint256 win;
}
struct Game {
uint8[BET_SIZE] winningNumbers;
uint8 bonusNumber;
uint256 referenceGame;
uint256 block;
uint256 prizeFund;
bool released;
Ticket[] tickets;
BigPrizeWinner[] bigPrizeWinners;
}
Token public token;
Game[] public games;
constructor(address _token) public {
token = Token(_token);
}
/**
* 0 Fallback function
*/
function() public {revert();}
function getWinningNumbers(uint256 _blockNumber) public view returns (uint8[5], uint8) {
uint8[5] memory winningNumbers = [0, 0, 0, 0, 0];
uint8 index = 0;
uint8 indexOffset = 0;
uint8 newNumber;
uint8 bonusNumber;
while (index < 5 &&
(block.number >= _blockNumber + indexOffset) &&
(_blockNumber + BLOCK_QUANTITY_ETHEREUM_IS_SAVING > block.number)) {
newNumber = getBallNumber(_blockNumber + indexOffset, MAIN_BET_VARIANTS);
indexOffset++;
if (!inArray(winningNumbers, newNumber)) {
winningNumbers[index] = newNumber;
index++;
}
}
if (index > 4) {
bonusNumber = getBallNumber(_blockNumber + indexOffset, BONUS_BET_VARIANTS);
}
if (bonusNumber == 0) {
winningNumbers = [0, 0, 0, 0, 0];
}
return (winningNumbers, bonusNumber);
}
function getBallNumber(uint256 _blockNumber, uint8 _numberVariants) public view returns (uint8) {
return uint8(uint256(block.blockhash(_blockNumber)) % _numberVariants + 1);
}
/**
* 3.4.1.1 Get matches. How much numbers are the same in 2 arrays
* 1st array is winner numbers
* 2nd is the bet (bet is always ascending for optimisation
*/
function getMatches(uint8[BET_SIZE] _winningNumbers, uint8[BET_SIZE] _bet) public pure returns (uint8) {
uint8 matches = 0;
for (uint m = 0; m < BET_SIZE; m++) {
for (uint j = 0; j < BET_SIZE; j++) {
if (_winningNumbers[m] == _bet[j]) {
matches++;
break;
} else if (_winningNumbers[m] < _bet[j]) {
// bet is always ascending
break;
}
}
}
return matches;
}
/**
* 2.1 Calculating the next game block number
*/
function getNextPeriodBlock() public view returns (uint256) {
uint256 secondsPassedInCurrentPeriod = now % PERIOD_IN_SECONDS;
uint256 nextBlock = block.number + ((PERIOD_IN_SECONDS - secondsPassedInCurrentPeriod) / SECONDS_PER_BLOCK);
if (block.number + ALL_BETS_ARE_MADE > nextBlock) {
nextBlock = nextBlock + BLOCKS_PER_PERIOD;
}
return nextBlock;
}
/**
* 1 Make a bet
*/
function bet(uint256 _amount, uint8[BET_SIZE] _mainBet, uint8 _bonusBet) public {
require(_amount > 0);
requireBetCorrect(_mainBet, _bonusBet);
if (isNeedToCreateNewGame()) {
createGame();
}
token.safeTransferFrom(msg.sender, address(this), _amount);
games[games.length - 1].tickets.push(
Ticket({
player : msg.sender,
amount : _amount,
mainBet : _mainBet,
bonusBet : _bonusBet,
released : false
}));
emit NewBet(games.length - 1, games[games.length - 1].tickets.length - 1, msg.sender);
}
function createGame() private {
if (games.length > 0) {
if (games.length > 1) {
releaseGame(games.length - 2);
}
if (games[games.length - 1].prizeFund == 0) {
games[games.length - 1].prizeFund = calculatePrizeFund();
}
}
games.length++;
uint256 gameIndex = games.length - 1;
games[gameIndex].block = getNextPeriodBlock();
games[gameIndex].referenceGame = gameIndex;
emit GameCreated(games.length - 1);
}
function calculatePrizeFund() public view returns (uint256) {
return token.balanceOf(address(this)) * PRIZE_FUND_PERCENTAGE / 100;
}
function isGameNumbersAreDefined(uint256 _gameIndex) public view returns (bool) {
return (games[_gameIndex].bonusNumber != 0);
}
function isGameExpired(uint256 _gameIndex) public view returns (bool) {
return (block.number >= games[_gameIndex].block + BLOCK_QUANTITY_ETHEREUM_IS_SAVING);
}
function isNeedToCreateNewGame() public view returns (bool) {
if (games.length == 0) {
return true;
} else if (block.number + ALL_BETS_ARE_MADE > games[games.length - 1].block) {
return true;
}
return false;
}
function isGameReleased(uint256 _gameIndex) public view returns (bool) {
return games[games[_gameIndex].referenceGame].released;
}
function releaseGame(uint256 _gameIndex) private {
sendJackPots(_gameIndex);
games[_gameIndex].released = true;
}
function sendJackPots(uint256 _gameIndex) private {
if (isGameReleased(_gameIndex)) {
return;
}
if (!isGameNumbersAreDefined(_gameIndex)) {
return;
}
uint256 allBigWinnersPrizes = calculateBigWinnersPrizes(_gameIndex);
uint256 bigPrizeWinnersLength = games[_gameIndex].bigPrizeWinners.length;
uint256 win;
uint i = 0;
if (allBigWinnersPrizes <= games[_gameIndex].prizeFund) {
for (i = 0; i < bigPrizeWinnersLength; i++) {
if (games[_gameIndex].bigPrizeWinners[i].win == 0) {
win = games[_gameIndex].prizeFund;
} else {
win = games[_gameIndex].bigPrizeWinners[i].win;
}
token.safeTransfer(games[_gameIndex].bigPrizeWinners[i].player, win);
emit PrizeSent(_gameIndex, 0, win, games[_gameIndex].bigPrizeWinners[i].player);
}
} else {
for (i = 0; i < bigPrizeWinnersLength; i++) {
if (games[_gameIndex].bigPrizeWinners[i].win == 0) {
win = games[_gameIndex].prizeFund;
} else {
win = games[_gameIndex].bigPrizeWinners[i].win;
}
win = win * games[_gameIndex].prizeFund / allBigWinnersPrizes;
token.safeTransfer(games[_gameIndex].bigPrizeWinners[i].player, win);
emit PrizeSent(_gameIndex, 0, win, games[_gameIndex].bigPrizeWinners[i].player);
}
}
delete games[_gameIndex].bigPrizeWinners;
}
function setNewReferenceGameToExpiredGames(uint256 _referenceIndex) internal {
if (_referenceIndex > 0) {
for (uint256 i = _referenceIndex - 1; i >= 0; i--) {
if (isGameNumbersAreDefined(i)) {
break;
} else {
games[i].referenceGame = _referenceIndex;
}
}
}
}
function setWinnerNumbers(uint256 _gameIndex)
gameNumbersNotSet(_gameIndex)
blockNumberIsEnoughToDefineWinnerNumbers(_gameIndex)
gameIsNotExpired(_gameIndex)
{
(games[_gameIndex].winningNumbers, games[_gameIndex].bonusNumber) = getWinningNumbers(games[_gameIndex].block);
if (isGameNumbersAreDefined(_gameIndex)) {
if (games[_gameIndex].prizeFund == 0) {
games[_gameIndex].prizeFund = calculatePrizeFund();
}
token.safeTransfer(msg.sender, TECH_BONUS);
setNewReferenceGameToExpiredGames(_gameIndex);
emit WinnerNumbersBeingSet(_gameIndex);
}
}
function calculateBigWinnersPrizes(uint256 _gameIndex) public view returns (uint256) {
uint256 allPrizes = 0;
for (uint256 i = 0; i < games[_gameIndex].bigPrizeWinners.length; i++) {
if (games[_gameIndex].bigPrizeWinners[i].win == 0) {
allPrizes = allPrizes + games[_gameIndex].prizeFund;
} else {
allPrizes = allPrizes + games[_gameIndex].bigPrizeWinners[i].win;
}
}
return allPrizes;
}
function requestPrize(uint256 _gameIndex, uint256 _ticketIndex) public gameIsNotReleased(_gameIndex) ticketIsNotReleased(_gameIndex, _ticketIndex) {
uint256 multiplier = getMultiplier(_gameIndex, _ticketIndex);
if (multiplier > 0) {
uint256 win = multiplier * games[_gameIndex].tickets[_ticketIndex].amount;
uint256 prizeFund = games[games[_gameIndex].referenceGame].prizeFund;
if (win >= prizeFund) {
games[games[_gameIndex].referenceGame].bigPrizeWinners.push(
BigPrizeWinner({
player : games[_gameIndex].tickets[_ticketIndex].player,
win : 0
}));
} else if (win >= (prizeFund * FAST_WITHDRAW_PERCENTAGE_LIMIT / 100)) {
games[games[_gameIndex].referenceGame].bigPrizeWinners.push(
BigPrizeWinner({
player : games[_gameIndex].tickets[_ticketIndex].player,
win : win
}));
} else {
token.safeTransfer(games[_gameIndex].tickets[_ticketIndex].player, win);
games[games[_gameIndex].referenceGame].prizeFund = games[games[_gameIndex].referenceGame].prizeFund - win;
emit PrizeSent(_gameIndex, _ticketIndex, win, games[_gameIndex].tickets[_ticketIndex].player);
}
games[_gameIndex].tickets[_ticketIndex].released = true;
}
}
function getMultiplier(uint256 _gameIndex, uint256 _ticketIndex) public returns (uint256) {
uint256 multiplier;
uint256 referenceGameIndex = games[_gameIndex].referenceGame;
if (isGameNumbersAreDefined(referenceGameIndex)) {
uint8 mainBetMatches = getMatches(games[referenceGameIndex].winningNumbers, games[_gameIndex].tickets[_ticketIndex].mainBet);
bool bonusBetGuessed = (games[referenceGameIndex].bonusNumber == games[_gameIndex].tickets[_ticketIndex].bonusBet);
if (bonusBetGuessed == false) {
if (mainBetMatches == 2) {
multiplier = 1;
} else if (mainBetMatches == 3) {
multiplier = 20;
} else if (mainBetMatches == 4) {
multiplier = 1000;
} else if (mainBetMatches == 5) {
multiplier = 300000;
}
} else {
if (mainBetMatches == 2) {
multiplier = 20;
} else if (mainBetMatches == 3) {
multiplier = 200;
} else if (mainBetMatches == 4) {
multiplier = 30000;
} else if (mainBetMatches == 5) {
multiplier = games[referenceGameIndex].prizeFund / (10 ** 18);
}
}
}
return multiplier;
}
/**
* 1.1 Check if bet has the right format:
*/
function requireBetCorrect(uint8[5] _mainBet, uint8 _bonusBet) internal pure {
require(_bonusBet > 0 && _bonusBet <= BONUS_BET_VARIANTS);
uint8 prevNum = 0;
for (uint8 i = 0; i < 5; i++) {
require(_mainBet[i] > prevNum);
require(_mainBet[i] <= MAIN_BET_VARIANTS);
prevNum = _mainBet[i];
}
}
/* ===================== HELPERS ===================== */
/**
* 3.3.1.1 If number in array
*/
function inArray(uint8[BET_SIZE] _numberArr, uint8 _number) public pure returns (bool) {
bool isInArray = false;
for (uint i = 0; i < BET_SIZE; i++) {
if (_number == _numberArr[i]) {
isInArray = true;
break;
}
}
return isInArray;
}
// GETTERS:
function getWinnerNumbersByGameIndex(uint256 _gameIndex) public returns (uint8[BET_SIZE], uint8) {
return (games[_gameIndex].winningNumbers, games[_gameIndex].bonusNumber);
}
function getTicketBetByGameAndIndex(uint256 _gameIndex, uint256 _ticketIndex) public returns (uint8[BET_SIZE], uint8) {
return (games[_gameIndex].tickets[_ticketIndex].mainBet, games[_gameIndex].tickets[_ticketIndex].bonusBet);
}
function getTicketsLength(uint256 _gameIndex) public returns (uint256) {
return games[_gameIndex].tickets.length;
}
function getPrizeFund(uint256 _gameIndex) public returns (uint256) {
return games[_gameIndex].prizeFund;
}
function getGameLength() public returns (uint256) {
return games.length;
}
function getGameBlockNumber(uint256 _gameIndex) public returns (uint256) {
return games[_gameIndex].block;
}
function getReferenceGame(uint256 _gameIndex) public returns (uint256) {
return games[_gameIndex].referenceGame;
}
function getBigWinnersLength(uint256 _gameIndex) public returns (uint256) {
return games[_gameIndex].bigPrizeWinners.length;
}
}