-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlot (1).sol
35 lines (33 loc) · 875 Bytes
/
lot (1).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
// SPDX-License-Identifier: MIT
pragma solidity >= 0.5.0 < 0.9.0;
contract lottery
{
address public manager;
address payable[] public participants;
constructor()
{
manager=msg.sender;
}
receive() external payable
{
require(msg.value==0.1 ether);
participants.push(payable(msg.sender));
}
function getBalance() public view returns(uint)
{
require(msg.sender==manager);
return address(this).balance;
}
function random() public view returns(uint){
return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp,participants.length)));
}
function selectWinner() 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());
}
}