Add comments

pull/783/head
Rongjian Lan 6 years ago
parent 994aba8b07
commit c3a23cae10
  1. 24
      contracts/Lottery.sol

@ -1,26 +1,37 @@
pragma solidity >=0.4.22; pragma solidity >=0.4.22;
contract Lottery { contract Lottery {
string internal constant ENTER_MESSAGE = "The player needs to stake at least 0.1 ether"; string internal constant INSUFFICIENT_FUND_MESSAGE = "Insufficient Fund";
string internal constant RESTRICTED_MESSAGE = "Only manager can do"; string internal constant RESTRICTED_MESSAGE = "Unauthorized Access";
address public manager; address public manager; // The adress of the owner of this contract
address payable[] public players; address payable[] public players; // The players of current session
constructor() public { constructor() public {
manager = msg.sender; manager = msg.sender;
} }
/**
* @dev The player enters into the current lottery session by
* paying at least 0.1 token.
*/
function enter() public payable { function enter() public payable {
require(msg.value > .1 ether, ENTER_MESSAGE); require(msg.value > .1 ether, INSUFFICIENT_FUND_MESSAGE);
players.push(msg.sender); players.push(msg.sender);
} }
/**
* @dev Random number used for picking the winner.
*/
function random() private view returns (uint) { function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(now, msg.sender, this))); return uint(keccak256(abi.encodePacked(now, msg.sender, this)));
} }
/**
* @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 { function pickWinner() public payable restricted {
uint index = random() % players.length; uint index = random() % players.length;
players[index].transfer(address(this).balance); players[index].transfer(address(this).balance);
@ -32,6 +43,9 @@ contract Lottery {
_; _;
} }
/**
* @dev Returns a list of all players in the current session.
*/
function getPlayers() public view returns (address payable[] memory) { function getPlayers() public view returns (address payable[] memory) {
return players; return players;
} }

Loading…
Cancel
Save