-
Notifications
You must be signed in to change notification settings - Fork 0
/
luckydraw.sol
67 lines (51 loc) · 1.71 KB
/
luckydraw.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract luckydraw{
address payable[] private uaddr;
address public owner;
uint initialsum = 2 ether;
modifier initialfee(){
require(msg.value== initialsum,"Entry is restricted only to participants who pay 2 ether.(Participants paying more than 2 ether are also not accepted");
_;
}
modifier onlyOwner (){
require(msg.sender == owner, "Not host");
_;
}
constructor () {
owner =msg.sender;
}
function adduser() external payable initialfee noOwner{
if (!(CheckUserExist(msg.sender))){
uaddr.push(payable(msg.sender));
}
else
{
revert("Sorry,You already participated can't participate twice");
}
}
function totalwinnings() public view returns(uint){
return address(this).balance;
}
function CheckUserExist(address add) public view returns(bool res) {
res = false;
for (uint i = 0; i < uaddr.length; i++) {
if (add == uaddr[i]) {
res = true;
}
}
return res;
}
modifier noOwner() {
require(msg.sender != owner , "Host not allowed to participate");
_;
}
function rand() internal view returns (uint) {
return uint(keccak256(abi.encodePacked(block.timestamp,block.difficulty,msg.sender))) % uaddr.length;
}
function winner() payable public onlyOwner returns(address){
uint winningindex = rand();
uaddr[winningindex].transfer(address(this).balance);
return uaddr[winningindex];
}
}