The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/contracts/Lottery.sol

53 lines
1.5 KiB

pragma solidity >=0.4.22;
contract Lottery {
6 years ago
string internal constant INSUFFICIENT_FUND_MESSAGE = "Insufficient Fund";
string internal constant RESTRICTED_MESSAGE = "Unauthorized Access";
6 years ago
address public manager; // The adress of the owner of this contract
address payable[] public players; // The players of current session
constructor() public {
manager = msg.sender;
}
6 years ago
/**
* @dev The player enters into the current lottery session by
* paying at least 0.1 token.
*/
function enter() public payable {
6 years ago
require(msg.value > .1 ether, INSUFFICIENT_FUND_MESSAGE);
players.push(msg.sender);
}
6 years ago
/**
* @dev Random number used for picking the winner.
*/
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(now, msg.sender, this)));
}
6 years ago
/**
* @dev Randomly select one of the players in current session as winner
* and send all the token in smart contract balance to it.
*/
function pickWinner() public payable restricted {
uint index = random() % players.length;
players[index].transfer(address(this).balance);
players = new address payable[](0);
}
modifier restricted() {
require(msg.sender == manager, RESTRICTED_MESSAGE);
_;
}
6 years ago
/**
* @dev Returns a list of all players in the current session.
*/
function getPlayers() public view returns (address payable[] memory) {
return players;
}
}