-
Notifications
You must be signed in to change notification settings - Fork 3
/
LuckyYou.sol
390 lines (321 loc) · 12.5 KB
/
LuckyYou.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
pragma solidity ^0.8.4;
// SPDX-License-Identifier: MIT
//0xDA0bab807633f07f013f94DD0E6A4F96F8742B53
//createGiveaway, giveawayMap, participate, getParticipants, owner are working fine.
//getAllGiveaways(asking for payable), balance and getBalance(both giving diff. values),
import "@openzeppelin/contracts/utils/Counters.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
import {
SafeERC20,
IERC20
} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
abstract contract OpsReady {
address public immutable ops;
address payable public immutable gelato;
address public constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
modifier onlyOps() {
require(msg.sender == ops, "OpsReady: onlyOps");
_;
}
constructor(address _ops) {
ops = _ops;
gelato = IOps(_ops).gelato();
}
function _transfer(uint256 _amount) internal {
(bool success, ) = gelato.call{value: _amount}("");
require(success, "_transfer: ETH transfer failed");
}
}
interface IOps {
function createTaskNoPrepayment(
address _execAddress,
bytes4 _execSelector,
address _resolverAddress,
bytes calldata _resolverData,
address _feeToken
) external returns (bytes32 task);
function gelato() external view returns (address payable);
function getFeeDetails() external view returns (uint256, address) ;
}
contract LuckyYou is VRFConsumerBase, OpsReady
{
address public owner;
using Counters for Counters.Counter;
Counters.Counter private giveawayNumber;
bytes32 internal keyHash;
uint256 internal fee;
address vrfCoordinator = 0x3d2341ADb2D31f1c5530cDC622016af293177AE0;
address link = 0xb0897686c545045aFc77CF20eC7A532E3120E0F1;
address opsAddress = 0x527a819db1eb0e34426297b03bae11F2f8B3A19E;
uint public currGiveawayId=0;
bool public isLocked=false;
constructor() VRFConsumerBase(vrfCoordinator, link) OpsReady(opsAddress) payable {
keyHash = 0xf86195cf7690c55907b2b611ebb7343a6f649bff128701cc542f0569e2c549da;
fee = 0.1 * 10 ** 15;
owner = msg.sender;
}
event GiveawayCreated(
address creator,
uint256 uniqueId,
string message,
string socialLink,
uint256 deadline,
uint256 timestamp,
uint amount,
uint participationFee,
address[] participants,
bool isLive,
address winner,
bytes32 taskId,
bool isProcessing
);
event GiveawayParticipated(
address creator,
uint256 uniqueId,
bool isLive,
uint amount,
uint participationFee,
address[] participants
);
event GiveawayEnded(
address creator,
uint256 uniqueId,
bool isLive,
uint amount,
uint participationFee,
address winner
);
function getBalance() public view returns (uint) {
return address(this).balance;
}
function getLinkBalance() public view returns (uint) {
return LINK.balanceOf(address(this));
}
struct Giveaway{
address creator;
uint256 uniqueId;
string message;
string socialLink;
uint256 deadline;
uint256 timestamp;
uint amount;
uint participationFee;
address[] participants;
bool isLive;
address winner;
bytes32 taskId;
bool isProcessing;
}
mapping (uint256 => Giveaway) public giveawayMap;
function createGiveaway(string memory _message, string memory _socialLink, uint256 _deadline) public payable {
require(block.timestamp<_deadline, "Enter valid time");
require(msg.value>0, "No value given");
uint _participationFee = msg.value/100;
address[] memory empty;
giveawayNumber.increment();
uint256 newGiveawayNumber = giveawayNumber.current();
bytes32 tempTaskId=IOps(ops).createTaskNoPrepayment(
address(this),
this.endGiveaway.selector,
address(this),
abi.encodeWithSelector(this.checker.selector, newGiveawayNumber),
ETH
);
giveawayMap[newGiveawayNumber] = Giveaway(msg.sender, newGiveawayNumber, _message, _socialLink, _deadline, block.timestamp, msg.value, _participationFee, empty, true, address(0), tempTaskId, false);
emit GiveawayCreated(msg.sender, newGiveawayNumber, _message, _socialLink, _deadline, block.timestamp, msg.value, _participationFee, empty, true, address(0), tempTaskId, false);
}
function participate(uint256 giveawayId) public giveawayExist(giveawayId) payable {
Giveaway storage currGiveaway = giveawayMap[giveawayId];
require(currGiveaway.isLive==true, "Giveaway has been finished");
require(currGiveaway.deadline>=block.timestamp, "Giveaway time is over");
for(uint i=0 ; i<currGiveaway.participants.length ; i++)
{
require(currGiveaway.participants[i]!=msg.sender, "You cannot participate twice");
}
require(currGiveaway.participationFee<=msg.value, "Insufficient fee");
currGiveaway.participants.push(payable(msg.sender));
emit GiveawayParticipated(currGiveaway.creator, currGiveaway.uniqueId, currGiveaway.isLive, currGiveaway.amount, currGiveaway.participationFee, currGiveaway.participants);
}
function getParticipants(uint256 giveawayId) public giveawayExist(giveawayId) view returns (address[] memory) {
return giveawayMap[giveawayId].participants;
}
function endGiveaway(uint256 giveawayId) public giveawayExist(giveawayId) {
require(isLocked==false, "Please try again later");
Giveaway storage currGiveaway = giveawayMap[giveawayId];
require(currGiveaway.creator==msg.sender || msg.sender == owner || msg.sender == opsAddress, "tu koi aur hai");
require(currGiveaway.deadline<=block.timestamp, "Deadline not reached");
require(currGiveaway.isLive==true, "Giveaway already ended");
giveawayMap[giveawayId].isProcessing = true;
uint256 fees;
address feeToken;
(fees, feeToken) = IOps(ops).getFeeDetails();
_transfer(fees);
currGiveawayId = giveawayId;
isLocked=true;
getRandomNumber();
emit GiveawayEnded(currGiveaway.creator, currGiveaway.uniqueId, currGiveaway.isLive, currGiveaway.amount, currGiveaway.participationFee, currGiveaway.winner);
}
modifier giveawayExist(uint256 giveawayId) {
require(giveawayMap[giveawayId].uniqueId!=0, "Giveaway doesn't exist");
_;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function getRandomNumber() internal returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
return requestRandomness(keyHash, fee);
}
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
Giveaway storage currGiveaway = giveawayMap[currGiveawayId];
if(currGiveaway.participants.length==0)
{
payable(currGiveaway.creator).transfer(currGiveaway.amount);
}
else {
uint index = randomness%currGiveaway.participants.length;
payable(currGiveaway.participants[index]).transfer(currGiveaway.amount);
giveawayMap[currGiveawayId].winner=currGiveaway.participants[index];
}
giveawayMap[currGiveawayId].isProcessing=false;
giveawayMap[currGiveawayId].isLive=false;
isLocked=false;
currGiveawayId=0;
}
function getAllGiveaways() public view returns (Giveaway[] memory) {
uint totalGiveaways = giveawayNumber.current();
uint currIndex = 0;
Giveaway[] memory allGiveaways = new Giveaway[] (totalGiveaways);
for(uint i=0 ; i<totalGiveaways ; i++)
{
uint currId = giveawayMap[i+1].uniqueId;
Giveaway storage currGiveaway = giveawayMap[currId];
allGiveaways[currIndex] = currGiveaway;
currIndex += 1;
}
return allGiveaways;
}
function getParticipatedGiveaways() public view returns (Giveaway[] memory) {
uint totalGiveaways = giveawayNumber.current();
uint currIndex = 0;
uint participatedGiveawaysLength = 0;
for(uint i=0 ; i<totalGiveaways ; i++)
{
uint currId = giveawayMap[i+1].uniqueId;
Giveaway storage currGiveaway = giveawayMap[currId];
for(uint j=0 ; j<currGiveaway.participants.length ; j++)
{
if(currGiveaway.participants[j]==msg.sender)
{
participatedGiveawaysLength++;
}
}
}
Giveaway[] memory participatedGiveaways = new Giveaway[] (participatedGiveawaysLength);
for(uint i=0 ; i<totalGiveaways ; i++)
{
uint currId = giveawayMap[i+1].uniqueId;
Giveaway storage currGiveaway = giveawayMap[currId];
for(uint j=0 ; j<currGiveaway.participants.length ; j++)
{
if(currGiveaway.participants[j]==msg.sender)
{
participatedGiveaways[currIndex] = currGiveaway;
currIndex +=1;
}
}
}
return participatedGiveaways;
}
function getWonGiveaways() public view returns (Giveaway[] memory) {
uint totalGiveaways = giveawayNumber.current();
uint currIndex = 0;
uint wonGiveawaysLength = 0;
for(uint i=0 ; i<totalGiveaways ; i++)
{
uint currId = giveawayMap[i+1].uniqueId;
Giveaway storage currGiveaway = giveawayMap[currId];
if(currGiveaway.winner==msg.sender && currGiveaway.isLive==false)
{
wonGiveawaysLength++;
}
}
Giveaway[] memory wonGiveaways = new Giveaway[] (wonGiveawaysLength);
for(uint i=0 ; i<totalGiveaways ; i++)
{
uint currId = giveawayMap[i+1].uniqueId;
Giveaway storage currGiveaway = giveawayMap[currId];
if(currGiveaway.winner==msg.sender && currGiveaway.isLive==false)
{
wonGiveaways[currIndex] = currGiveaway;
currIndex +=1;
}
}
return wonGiveaways;
}
function getYourGiveaways() public view returns (Giveaway[] memory) {
uint totalGiveaways = giveawayNumber.current();
uint currIndex = 0;
uint yourGiveawaysLength = 0;
for(uint i=0 ; i<totalGiveaways ; i++)
{
uint currId = giveawayMap[i+1].uniqueId;
Giveaway storage currGiveaway = giveawayMap[currId];
if(currGiveaway.creator==msg.sender)
{
yourGiveawaysLength++;
}
}
Giveaway[] memory yourGiveaways = new Giveaway[] (yourGiveawaysLength);
for(uint i=0 ; i<totalGiveaways ; i++)
{
uint currId = giveawayMap[i+1].uniqueId;
Giveaway storage currGiveaway = giveawayMap[currId];
if(currGiveaway.creator==msg.sender)
{
yourGiveaways[currIndex] = currGiveaway;
currIndex +=1;
}
}
return yourGiveaways;
}
function getTimestamp() public view returns (uint256 time){
time = block.timestamp;
}
function rechargeEth() public payable{}
function checker(uint256 giveawayId)
external
view
returns (bool canExec, bytes memory execPayload)
{
canExec = block.timestamp >= giveawayMap[giveawayId].deadline && giveawayMap[giveawayId].isLive;
execPayload = abi.encodeWithSelector(
this.endGiveaway.selector,
giveawayId
);
}
function changeOwner(address newAddress) public onlyOwner returns (address) {
owner = newAddress;
return owner;
}
function unlockContract() public onlyOwner {
isLocked=false;
currGiveawayId=0;
}
function getFees(uint percentage) public onlyOwner {
uint totalGiveaways = giveawayNumber.current();
uint totalLiveAmount = 0;
for(uint i=0 ; i<totalGiveaways ; i++)
{
uint currId = giveawayMap[i+1].uniqueId;
Giveaway storage currGiveaway = giveawayMap[currId];
if(currGiveaway.isLive==true)
{
totalLiveAmount += currGiveaway.amount;
}
}
uint remainingFees = (address(this).balance - totalLiveAmount)/percentage;
payable(owner).transfer(remainingFees);
}
}