-
Notifications
You must be signed in to change notification settings - Fork 0
/
LotteryWin.sol
38 lines (29 loc) · 1018 Bytes
/
LotteryWin.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract LotteryWin {
address public manager;
address payable[] public players;
address payable public winner;
constructor(){
manager = msg.sender;
}
function participate() public payable {
require(msg.value==1 ether,"Please pay 1 ether only");
players.push(payable(msg.sender));
}
function getBalance() public view returns(uint) {
return address(this).balance;
}
function random() internal view returns(uint) {
return uint(keccak256(abi.encodePacked(block.prevrandao,block.timestamp,players.length)));
}
function pickWinner() public {
require(manager==msg.sender,"You are not the manager");
require(players.length >= 3,"Players are less than 3");
uint r = random();
uint index = r%players.length;
winner = players[index];
winner.transfer(getBalance());
players = new address payable[](0);
}
}