Skip to content

Instantly share code, notes, and snippets.

@Fallenstedt
Created August 17, 2018 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fallenstedt/a05d44bb1e87da0902c481828c8534ca to your computer and use it in GitHub Desktop.
Save Fallenstedt/a05d44bb1e87da0902c481828c8534ca to your computer and use it in GitHub Desktop.
Lottery.sol
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