Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple lottery with debt collection at the end of grace period
pragma solidity ^0.4.17;
contract Lottery{
uint public MAX_PLAYERS = 2;
address[] public players;
address public manager;
uint public blockNumber;
constructor() public{
manager = msg.sender;
}
function enter() public payable{
require(players.length < MAX_PLAYERS);
require(msg.value == 1 ether);
blockNumber = block.number;
players.push(msg.sender);
}
function random() private view returns (uint){
require(block.number > blockNumber);
return uint(blockhash(blockNumber + 1));
}
function pickWinner() public restricted {
address winner = players[random() % players.length];
winner.transfer(address(this).balance);
players = new address[](0);
}
function collectDebt() public restricted {
require(msg.sender == manager);
require((blockNumber + 3) < block.number);
manager.transfer(address(this).balance);
players = new address[](0);
}
function getPlayers() public view returns(address[]) {
// Return list of players
return players;
}
modifier restricted() {
require(players.length == MAX_PLAYERS);
_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.