Created
August 17, 2018 22:36
-
-
Save Fallenstedt/a05d44bb1e87da0902c481828c8534ca to your computer and use it in GitHub Desktop.
Lottery.sol
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
pragma solidity ^0.4.17; | |
contract Lottery { | |
address public manager; | |
address[] public players; | |
constructor() public { | |
manager = msg.sender; | |
} | |
function enter() public payable { | |
require(msg.value > .01 ether); | |
players.push(msg.sender); | |
} | |
function pickWinner() public { | |
require(msg.sender == manager); | |
uint256 index = random() % players.length; | |
players[index].transfer(address(this).balance); | |
players = new address[](0); | |
} | |
/* | |
* Not really random at all but it's simple. | |
* We have reasonable idea of what difficulty, now and players is | |
* We can know the value ahead of time if we tried very hard | |
* Vulnerable for attack, but it works as a demo | |
*/ | |
function random() private view returns (uint256) { | |
return uint(keccak256(abi.encodePacked(block.difficulty, now, players))); | |
} | |
// function debugGetBalance() public view returns (uint256) { | |
// return address(this).balance; | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment