/
PublicSelector.sol
171 lines (136 loc) · 5.03 KB
/
PublicSelector.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// SPDX-License-Identifier: MIT
pragma solidity 0.8.6;
import "./external/chainlink/VRFConsumerBase.sol";
import "./external/openzeppelin/EnumerableSet.sol";
import "./abstract/Ownable.sol";
import "./libraries/SafeERC20.sol";
contract PublicSelector is Ownable, VRFConsumerBase {
using SafeERC20 for IERC20;
using EnumerableSet for EnumerableSet.UintSet;
event WinnerSelected(uint256 timestamp, uint256 winner);
uint256 public ticketsCount;
string public participantsListLink;
string public participantsListSha256;
EnumerableSet.UintSet private winners;
bool public winnersSelected = false;
EnumerableSet.UintSet internal stepsBeforeSelection;
// Chainlink + Randomness
bytes32 internal keyHash;
uint256 internal fee;
uint256 public randomResult;
uint256 public previousWinnerSeed;
/**
* Constructor inherits VRFConsumerBase
*/
constructor(
address _vrfCoordinator,
address _link,
bytes32 _keyHash,
uint256 _fee
)
Ownable(msg.sender)
VRFConsumerBase(
_vrfCoordinator, // VRF Coordinator
_link // LINK Token
)
{
keyHash = _keyHash;
fee = _fee; // LINK fee
}
/// @notice Allows owner to set the VRF keyHash
/// @param _keyHash The keyHash to be used by the VRF
function setKeyHash(bytes32 _keyHash) external onlyOwner {
keyHash = _keyHash;
}
/// @notice Allows owner to set the fee per request required by the VRF
/// @param _fee The fee to be charged for a request
function setFee(uint256 _fee) external onlyOwner {
fee = _fee;
}
/**
* Requests randomness
*/
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - provide LINK to the contract");
return requestRandomness(keyHash, fee);
}
/**
* Callback function used by VRF Coordinator
*/
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
randomResult = randomness;
if (!stepsBeforeSelection.contains(1)) {
stepsBeforeSelection.add(1);
}
}
function setTicketsNumber(uint256 number) public onlyOwner {
require(!winnersSelected, "Selection performed");
ticketsCount = number;
if (!stepsBeforeSelection.contains(4)) {
stepsBeforeSelection.add(4);
}
}
function setParticipantsListLink(string memory link) public onlyOwner {
require(!winnersSelected, "Selection performed");
participantsListLink = link;
if (!stepsBeforeSelection.contains(2)) {
stepsBeforeSelection.add(2);
}
}
function setParticipantsListSha256(string memory sha256Hash) public onlyOwner {
require(!winnersSelected, "Selection performed");
participantsListSha256 = sha256Hash;
if (!stepsBeforeSelection.contains(3)) {
stepsBeforeSelection.add(3);
}
}
function selectWinners(uint256 count) public onlyOwner {
require(stepsBeforeSelection.length() == 4, "Not all steps performed");
require(randomResult != 0, "Chainlink data recheck");
require((count + winners.length()) <= ticketsCount, "Outside of ticket range");
if (previousWinnerSeed == 0) {
previousWinnerSeed = randomResult;
}
for (uint256 i = 0; i < count; i++) {
uint256 winnerSeed;
uint256 winnerIndex;
bool winnerSelected = false;
uint256 nonce = 0;
do {
winnerSeed = uint256(keccak256(abi.encodePacked(previousWinnerSeed, i, nonce)));
winnerIndex = (winnerSeed % ticketsCount) + 1;
nonce++;
winnerSelected = !winners.contains(winnerIndex);
} while (!winnerSelected);
winners.add(winnerIndex);
previousWinnerSeed = winnerSeed;
emit WinnerSelected(block.timestamp, winnerIndex);
}
winnersSelected = true;
}
function getWinners() external view returns (uint256[] memory) {
uint256[] memory win = new uint256[](winners.length());
for (uint256 i = 0; i < winners.length(); i++) {
win[i] = winners.at(i);
}
return win;
}
function getWinnersInRange(uint256 from, uint256 to) external view returns (uint256[] memory) {
require(from < to, "Incorrect range");
require(to < winners.length(), "Incorrect range");
uint256[] memory win = new uint256[](to - from + 1);
for (uint256 i = 0; i <= to - from; i++) {
win[i] = winners.at(i + from);
}
return win;
}
function recoverErc20(address token) external onlyOwner {
uint256 amount = IERC20(token).balanceOf(address(this));
if (amount > 0) {
IERC20(token).safeTransfer(owner, amount);
}
}
function recover() external onlyOwner {
payable(owner).transfer(address(this).balance);
}
}