forked from owanhunte/ethereum-solidity-course-updated-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lottery.sol
42 lines (34 loc) · 1.04 KB
/
Lottery.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
pragma solidity >=0.5.0 <0.7.0;
contract Lottery {
address public manager;
address payable[] public players;
constructor() public {
manager = msg.sender;
}
function enter() public payable {
require(
msg.value > .01 ether,
"A minimum payment of .01 ether must be sent to enter the lottery"
);
players.push(msg.sender);
}
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.difficulty, block.number, players)));
}
function pickWinner() public onlyOwner {
uint index = random() % players.length;
address contractAddress = address(this);
players[index].transfer(contractAddress.balance);
players = new address payable[](0);
}
function getPlayers() public view returns (address payable[] memory) {
return players;
}
modifier onlyOwner() {
require(
msg.sender == manager,
"Only owner can call this function."
);
_;
}
}