Skip to content

Instantly share code, notes, and snippets.

@kennym
Created February 21, 2021 14:42
Show Gist options
  • Save kennym/751122784cab42c368e1005981d744c9 to your computer and use it in GitHub Desktop.
Save kennym/751122784cab42c368e1005981d744c9 to your computer and use it in GitHub Desktop.
Lottery contract
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 random() private view returns (uint256) {
return uint(keccak256(abi.encodePacked(block.difficulty, now, players)));
}
function pickWinner() public {
// Make sure the winner is picked by the manager not anyone else
require(msg.sender === manger);
uint index = random() % players.length;
players[index].transfer(address(this).balance);
players = new address[](0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment