-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lottery.sol
43 lines (35 loc) · 1.79 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.5;
contract Lottery { //Contract name is Lottery
address public owner; //Owner is a Contract Manager who control this Lottery
address payable[] public participants; //In payable array every participent store
constructor() {
owner = msg.sender; //Owner will be that person who deploy the contract
}
receive() external payable {
//If sending ether is 1 then other line execute else next line execute
require(msg.value == 1 ether, "Minimum amount to participat is 1 Etherium");
//when the required amount is sent, the sender's address will be stored in payable array
participants.push(payable(msg.sender));
}
modifier onlyOwner {
require(msg.sender == owner, "You are not the Owner");
_;
}
function getBalance() public view onlyOwner returns (uint) {
//it shows the balance
return address(this).balance;
}
function random() private view returns(uint) { // this function generates a random number
return uint(keccak256( abi.encodePacked (block.difficulty, block.timestamp, participants.length)));
}
function lotteryWinner() public onlyOwner {
//If the participants are equal or greater than 3 then other lines execute else the next line execute
require(participants.length >= 3, "Participents are not enough");
uint randomNumber = random();
uint winnerIndex = randomNumber % participants.length;
address payable winner = participants[winnerIndex];
winner.transfer(getBalance()); //All amount which is in getBalance will transfer to the winner
participants = new address payable[](0); //After the Lottery ends array will be 0
}
}