-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLottery.sol
48 lines (35 loc) · 1.2 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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
contract Lottery
{
address public manager; //Owner's variable
address payable[] public participants; //Participants Dynamic array
constructor ()
{
manager = msg.sender; // Assigning the owner of the contract
}
receive () payable external // function for recieving values
{
require(msg.value == 1 ether);
participants.push(payable(msg.sender));
}
//checking contract balance
function getBalance() public view returns(uint)
{
require(msg.sender == manager);
return address(this).balance;
}
function random() internal view returns(uint){
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, participants.length)));
}
function pickWinner() public{
require(msg.sender == manager);
require (participants.length >= 3);
uint r = random();
address payable winner;
uint index = r % participants.length;
winner = participants[index];
winner.transfer(getBalance());
participants = new address payable[](0);
}
}