Skip to content

Instantly share code, notes, and snippets.

@servatj
Last active January 19, 2022 22:27
Show Gist options
  • Save servatj/209410c540de7ee35492ee848c38bfa9 to your computer and use it in GitHub Desktop.
Save servatj/209410c540de7ee35492ee848c38bfa9 to your computer and use it in GitHub Desktop.
Simple Lotery smart contract
pragma solidity ^0.4.21;
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 random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.difficulty, now, players)));
}
function pickWinner() public restricted {
uint index = random() % players.length;
players[index].transfer(address(this).balance);
players = new address[](0);
}
function getPlayers() public view returns (address[]) {
return players;
}
modifier restricted() {
require(msg.sender == manager);
_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment