-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lottery_Smart_Contract.sol
44 lines (34 loc) · 1.18 KB
/
Lottery_Smart_Contract.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract LotteryContract {
address public manager;
address payable[] public participants;
address payable public winner;
uint256 public constant entry_fee = 1 ether;
constructor() payable{
manager = msg.sender;
}
modifier onlyOwner() {
require(manager == msg.sender, "You have to be the owner!");
_;
}
receive() external payable {
require(msg.value == entry_fee, "Must pay exactly 1 ether to participate!");
participants.push(payable(msg.sender));
}
function pickWinner() public {
require(msg.sender == manager);
require(participants.length == 2);
uint r = getRandom();
uint index = r % participants.length;
winner = participants[index];
winner.transfer(getBalance());
participants = new address payable [] (0);
}
function getBalance() public view onlyOwner returns (uint256) {
return address(this).balance;
}
function getRandom() public view returns (uint) {
return(uint(keccak256(abi.encodePacked(block.prevrandao, block.timestamp, participants.length))));
}
}