-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lottery.sol
58 lines (44 loc) · 1.39 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
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
contract Lottery {
address public owner;
address payable[] public players;
address[] public winners;
uint public lotteryId;
constructor() {
owner = msg.sender;
lotteryId = 0;
}
function getWinners() public view returns (address[] memory){
return winners;
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
function getPlayers() public view returns (address payable[] memory) {
return players;
}
function enter() public payable {
require(msg.value >= .01 ether);
// address of player entering lottery
players.push(payable(msg.sender));
}
function getRandomNumber() public view returns (uint) {
return uint(keccak256(abi.encodePacked(owner, block.timestamp)));
}
function getLotteryId() public view returns(uint) {
return lotteryId;
}
function pickWinner() public onlyOwner {
uint randomIndex = getRandomNumber() % players.length;
players[randomIndex].transfer(address(this).balance);
winners.push(players[randomIndex]);
lotteryId++;
// Clear the players array. ['player1', 'player2'] 👉 []
players = new address payable[](0);
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}