-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLottery.sol
46 lines (34 loc) · 1.06 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
46
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
//1. create a contract
contract Lottery {
//2. declare owner of the contract
address public manager;
//3. create an array for storing players addresses.
address[] public players;
//4. Set your manager or owner of the contract
constructor() {
manager = msg.sender;
}
//5. create a
function enter() public payable {
require(msg.value > .01 ether);
players.push(msg.sender);
}
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players)));
}
function pickWinner() public restricted {
uint index = random() % players.length;
address payable to = payable(players[index]);
to.transfer(address(this).balance);
players = new address[](0);
}
modifier restricted() {
require(msg.sender == manager);
_;
}
function getPlayers() public view returns (address[] memory) {
return players;
}
}