forked from Lyudasorry/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlottery
More file actions
35 lines (28 loc) · 972 Bytes
/
lottery
File metadata and controls
35 lines (28 loc) · 972 Bytes
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
pragma solidity ^0.8.0;
contract Lottery {
address public manager;
address[] public players;
constructor() {
manager = msg.sender;
}
function enter() public payable {
require(msg.value > 0.01 ether, "Minimum entry fee is 0.01 ether.");
players.push(msg.sender);
}
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players.length)));
}
function pickWinner() public restricted {
uint index = random() % players.length;
address payable winner = payable(players[index]);
winner.transfer(address(this).balance);
players = new address[](0);
}
modifier restricted() {
require(msg.sender == manager, "Only the manager can call this function.");
_;
}
function getPlayers() public view returns (address[] memory) {
return players;
}
}