-
Notifications
You must be signed in to change notification settings - Fork 0
/
lottery.sol
69 lines (62 loc) · 2.05 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.7;
interface IRandom{
function retrieveRandom() external returns(uint);
}
contract Lottery {
struct Participant{
address payable participantAddr;
uint noOfLotts;
uint etherPaid;
}
uint randNum;
address contractAddress;
address public manager;
address[] public participants;
address payable winnerAddr;
mapping(address=>Participant) public participantList;
modifier isManager(){
require(msg.sender == manager);
_;
}
constructor(address _contractAddress){
manager = msg.sender;
contractAddress = _contractAddress;
}
function participate() external{
// require(msg.value == 1000 gwei,"The amount must be 1 Ether");
require(msg.sender != manager,"The manager is not allowed...");
if(participantList[msg.sender].participantAddr == address(0)){//kinda like mapping iteration
participantList[msg.sender] = Participant(payable(msg.sender),1,2);
participants.push(msg.sender);
}
else{
participantList[msg.sender].noOfLotts++;
participantList[msg.sender].etherPaid+=2;
}
}
// function random() private view returns(uint){
// bytes32 randomChar = keccak256(abi.encodePacked(block.difficulty, block.gaslimit, block.timestamp));
// uint randNum = uint(randomChar);
// return randNum%participants.length;
//}
function random() private returns(uint){
uint randomChar = IRandom(contractAddress).retrieveRandom();
randNum = randomChar%participants.length;
return randNum;
}
function selectWinner() public isManager{
require(participants.length>=5);
random();
// address contractAddr = address(this);
uint randomWin = randNum;
winnerAddr = payable(participants[randomWin]);
// winnerAddr.transfer(contractAddr.balance);
}
function retrieveWinner() public view returns(address){
return winnerAddr;
}
function reset() public isManager{
participants = new address [](0);
}
}