-
Notifications
You must be signed in to change notification settings - Fork 14
/
lottery.sol
45 lines (35 loc) · 1.13 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
43
44
45
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Lottery {
address public manager;
address public winner;
address[] private players;
constructor() {
manager = msg.sender;
}
modifier admin() {
require(msg.sender == manager, "Require Admin Privileges");
_;
}
modifier minEntry() {
require(msg.value > .01 ether, "Require min amount of ETH(0.011) to enter");
_;
}
function enter() public payable minEntry {
players.push(msg.sender);
}
function pickWinner() public admin {
uint index = random() % players.length;
address contractAddress = address(this);
address winnerAddress = players[index];
winner = players[index];
payable(winnerAddress).transfer(contractAddress.balance);
players = new address[](0);
}
function getPlayers() public view returns (address[] memory) {
return players;
}
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players)));
}
}