Created
January 21, 2020 17:43
-
-
Save finleyexp/facd1f11707bcc7839a91e335f451045 to your computer and use it in GitHub Desktop.
Copy of the iGuess.sol with abi.encodePatched(...)
This file contains hidden or 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
| // DON'T USE. INTENDED FOR EDUCATIONAL PURPOSES ONLY. PLUS, it errors. | |
| // adapted from: https://tij.me/blog/scamming-people-via-ethereum-smart-contracts/ | |
| pragma solidity >=0.4.24 <0.7.0; | |
| contract iGuess { | |
| uint256 private secretNumber; | |
| uint256 public lastPlayed; | |
| address public owner; | |
| struct Player { | |
| address addr; | |
| uint256 ethr; | |
| } | |
| Player[] players; | |
| constructor() public { | |
| // On construct set the owner and a random secret number | |
| owner = msg.sender; | |
| shuffle(); | |
| } | |
| function guess(uint256 number) public payable { | |
| // Guessing costs a minimum of 0.0001 ether | |
| require(msg.value >= 0.0001 ether); | |
| // Guess must be between zero and ten | |
| require(number >= 0 && number <= 10); | |
| // Update the last played date | |
| lastPlayed = now; | |
| // Add player to the players list (added memory -- should I have used storage?) | |
| Player memory player; | |
| player.addr = msg.sender; | |
| player.ethr = msg.value; | |
| players.push(player); | |
| // Payout if guess is correct | |
| if (number == secretNumber) { | |
| address(this).balance; | |
| } | |
| // Refresh the secret number | |
| shuffle(); | |
| } | |
| function shuffle() internal returns (uint) { | |
| // Randomly set secretNumber with a value between 1 and 10 | |
| secretNumber = uint(keccak256(abi.encodePacked(now, msg.sender))) % 10 + 1; | |
| return secretNumber; | |
| } | |
| function kill() public { | |
| // Enable owner to kill the contract after 24 hours of inactivity | |
| if (msg.sender == owner && now > lastPlayed + 24 hours) { | |
| selfdestruct(msg.sender); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment