-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlottery.sol
36 lines (28 loc) · 1002 Bytes
/
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
//SPDX-License-Identifier: UNLICENSED
pragma solidity >0.5.0 <0.9.0;
contract Lottery{
address public manager;
address payable[] public participants;
uint public fees = 2 ether;
constructor(){
manager = msg.sender;
}
function enterLotteryByPayingFees() external payable{
require(msg.value==fees);
participants.push(payable(msg.sender));
}
function getBalance() public view returns(uint){
require(msg.sender == manager);
return address(this).balance;
}
function findRandomNumber() private view returns(uint){
return uint(keccak256(abi.encode(block.timestamp,block.difficulty,participants.length)))%participants.length;
}
function findWinner() public returns(address){
require(msg.sender == manager);
uint rand = findRandomNumber();
participants[rand].transfer(getBalance());
participants = new address payable[](0);
return address(participants[rand]);
}
}