forked from MukulCode/CodingClubIndia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLottery1.sol
More file actions
43 lines (32 loc) · 1.12 KB
/
Lottery1.sol
File metadata and controls
43 lines (32 loc) · 1.12 KB
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
// SPDX-License-Identifier: MIT
pragma solidity >0.5.0 <= 0.9.0;
contract Lottery1{
address public manager;
address payable[] public players; // we have made payable because anyone of the participant can recieve the winning amount
constructor() {
manager = msg.sender;
}
modifier onlyManager {
require(manager == msg.sender,"You are not manager");
_;
}
receive() external payable {
players.push(payable(msg.sender));
}
function getBalance() view public onlyManager returns(uint){
return address(this).balance;
}
function random()public view returns(uint){
return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp,players.length)));
}
function selectWinner() public onlyManager returns(address){
require(players.length >= 3);
address payable winner;
uint r = random();
uint index = r % players.length;
winner = payable(players[index]);
winner.transfer(getBalance());
players = new address payable[](0); // resetting the contract
return winner;
}
}