-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lottery.sol
71 lines (57 loc) · 2.36 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
70
71
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Lottery {
address[2] public managers;
address[] public players;
constructor() {
managers[0] = msg.sender; // The contract deployer is the first manager
}
function enter() public payable {
require(msg.value > .01 ether, "Minimum ether required");
players.push(msg.sender);
}
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players)));
}
function pickWinner() public restricted {
uint index = random() % players.length;
payable(players[index]).transfer(address(this).balance);
players = new address[](0) ; // Correctly reset the array
}
modifier restricted() {
require(isManager(msg.sender), "Only managers can call this");
_;
}
function getPlayers() public view returns (address[] memory) {
return players;
}
function addManager(address newManager) public restricted {
require(newManager != address(0), "Invalid address");
require(!isManager(newManager), "Already a manager");
if (managers[0] == address(0)) {
managers[0] = newManager;
} else if (managers[1] == address(0)) {
managers[1] = newManager;
} else {
revert("Cannot add more managers");
}
}
function removeManager(address manager) public restricted {
require(isManager(manager), "Not a manager");
require(managers[0] != address(0) || managers[1] != address(0), "There should be at least one manager");
if (managers[0] == manager) {
managers[0] = address(0);
} else if (managers[1] == manager) {
managers[1] = address(0);
}
}
function isManager(address addr) private view returns (bool) {
return (managers[0] == addr || managers[1] == addr);
}
function setSecondManager(address secondManager) public restricted {
require(managers[1] == address(0), "Second manager already set");
require(secondManager != address(0), "Invalid address");
require(secondManager != managers[0], "Cannot set the same address as the first manager");
managers[1] = secondManager;
}
}