/
YDTSwapLottery.sol
659 lines (551 loc) · 25.1 KB
/
YDTSwapLottery.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "./interfaces/IYDTSwapLottery.sol";
import "./interfaces/IRandomNumberGenerator.sol";
/** @title YDTSwapLottery Lottery.
* @notice It is a contract for a lottery system using
* randomness provided externally.
*/
contract YDTSwapLottery is ReentrancyGuard, IYDTSwapLottery, Ownable {
using SafeERC20 for IERC20;
address public injectorAddress;
address public operatorAddress;
address public treasuryAddress;
uint256 public currentLotteryId;
uint256 public currentTicketId;
uint256 public maxNumberTicketsPerBuyOrClaim = 100;
uint256 public maxPriceTicketInYDT = 50 ether;
uint256 public minPriceTicketInYDT = 0.005 ether;
uint256 public pendingInjectionNextLottery;
uint256 public constant MIN_DISCOUNT_DIVISOR = 300;
uint256 public constant MIN_LENGTH_LOTTERY = 5 minutes; // 5 minutes
uint256 public constant MAX_LENGTH_LOTTERY = 7 days; // 4 days
uint256 public constant MAX_TREASURY_FEE = 3000; // 30%
IERC20 public YDTtoken;
IRandomNumberGenerator public randomGenerator;
enum Status {
Pending,
Open,
Close,
Claimable
}
struct Lottery {
Status status;
uint256 startTime;
uint256 endTime;
uint256 priceTicketInYDT;
uint256 discountDivisor;
uint256[6] rewardsBreakdown; // 0: 1 matching number // 5: 6 matching numbers
uint256 treasuryFee; // 500: 5% // 200: 2% // 50: 0.5%
uint256[6] YDTPerBracket;
uint256[6] countWinnersPerBracket;
uint256 firstTicketId;
uint256 firstTicketIdNextLottery;
uint256 amountCollectedInYDT;
uint32 finalNumber;
}
struct Ticket {
uint32 number;
address owner;
}
// Mapping are cheaper than arrays
mapping(uint256 => Lottery) private _lotteries;
mapping(uint256 => Ticket) private _tickets;
// Bracket calculator is used for verifying claims for ticket prizes
mapping(uint32 => uint32) private _bracketCalculator;
// Keeps track of number of ticket per unique combination for each lotteryId
mapping(uint256 => mapping(uint32 => uint256)) private _numberTicketsPerLotteryId;
// Keep track of user ticket ids for a given lotteryId
mapping(address => mapping(uint256 => uint256[])) private _userTicketIdsPerLotteryId;
modifier notContract() {
require(!_isContract(msg.sender), "Contract not allowed");
require(msg.sender == tx.origin, "Proxy contract not allowed");
_;
}
modifier onlyOperator() {
require(msg.sender == operatorAddress, "Not operator");
_;
}
event LotteryClose(uint256 indexed lotteryId, uint256 firstTicketIdNextLottery);
event LotteryInjection(uint256 indexed lotteryId, uint256 injectedAmount);
event LotteryOpen(
uint256 indexed lotteryId,
uint256 startTime,
uint256 endTime,
uint256 priceTicketInYDT,
uint256 firstTicketId,
uint256 injectedAmount
);
event LotteryNumberDrawn(uint256 indexed lotteryId, uint256 finalNumber, uint256 countWinningTickets);
event NewOperatorAndTreasuryAndInjectorAddresses(address operator, address treasury, address injector);
event NewRandomGenerator(address indexed randomGenerator);
event TicketsPurchase(address indexed buyer, uint256 indexed lotteryId, uint256 numberTickets);
event TicketsClaim(address indexed claimer, uint256 amount, uint256 indexed lotteryId, uint256 numberTickets);
/**
* @notice Constructor
* @dev RandomNumberGenerator must be deployed prior to this contract
* @param _YDTtokenAddress: address of the YDT token
* @param _randomGeneratorAddress: address of the RandomGenerator contract used to work with ChainLink VRF
*/
constructor(address _YDTtokenAddress, address _randomGeneratorAddress) {
YDTtoken = IERC20(_YDTtokenAddress);
randomGenerator = IRandomNumberGenerator(_randomGeneratorAddress);
// Initializes a mapping
_bracketCalculator[0] = 1;
_bracketCalculator[1] = 11;
_bracketCalculator[2] = 111;
_bracketCalculator[3] = 1111;
_bracketCalculator[4] = 11111;
_bracketCalculator[5] = 111111;
}
/**
* @notice Buy tickets for the current lottery
* @param _lotteryId: lotteryId
* @param _ticketNumbers: array of ticket numbers between 1,000,000 and 1,999,999
* @dev Callable by users
*/
function buyTickets(uint256 _lotteryId, uint32[] calldata _ticketNumbers)
external
override
notContract
nonReentrant
{
require(_ticketNumbers.length != 0, "No ticket specified");
require(_ticketNumbers.length <= maxNumberTicketsPerBuyOrClaim, "Too many tickets");
require(_lotteries[_lotteryId].status == Status.Open, "Lottery closed");
require(block.timestamp < _lotteries[_lotteryId].endTime, "Lottery ended");
// Calculate number of YDT to this contract
uint256 amountYDTToTransfer = _calculateTotalPriceForBulkTickets(
_lotteries[_lotteryId].discountDivisor,
_lotteries[_lotteryId].priceTicketInYDT,
_ticketNumbers.length
);
// Transfer YDT tokens to this contract
YDTtoken.safeTransferFrom(address(msg.sender), address(this), amountYDTToTransfer);
// Increment the total amount collected for the lottery round
_lotteries[_lotteryId].amountCollectedInYDT += amountYDTToTransfer;
for (uint256 i = 0; i < _ticketNumbers.length; i++) {
uint32 thisTicketNumber = _ticketNumbers[i];
require((thisTicketNumber >= 1000000) && (thisTicketNumber <= 1999999), "Outside range");
_numberTicketsPerLotteryId[_lotteryId][1 + (thisTicketNumber % 10)]++;
_numberTicketsPerLotteryId[_lotteryId][11 + (thisTicketNumber % 100)]++;
_numberTicketsPerLotteryId[_lotteryId][111 + (thisTicketNumber % 1000)]++;
_numberTicketsPerLotteryId[_lotteryId][1111 + (thisTicketNumber % 10000)]++;
_numberTicketsPerLotteryId[_lotteryId][11111 + (thisTicketNumber % 100000)]++;
_numberTicketsPerLotteryId[_lotteryId][111111 + (thisTicketNumber % 1000000)]++;
_userTicketIdsPerLotteryId[msg.sender][_lotteryId].push(currentTicketId);
_tickets[currentTicketId] = Ticket({number: thisTicketNumber, owner: msg.sender});
// Increase lottery ticket number
currentTicketId++;
}
emit TicketsPurchase(msg.sender, _lotteryId, _ticketNumbers.length);
}
/**
* @notice Claim a set of winning tickets for a lottery
* @param _lotteryId: lottery id
* @param _ticketIds: array of ticket ids
* @param _brackets: array of brackets for the ticket ids
* @dev Callable by users only, not contract!
*/
function claimTickets(
uint256 _lotteryId,
uint256[] calldata _ticketIds,
uint32[] calldata _brackets
) external override notContract nonReentrant {
require(_ticketIds.length == _brackets.length, "Not same length");
require(_ticketIds.length != 0, "Length must be >0");
require(_ticketIds.length <= maxNumberTicketsPerBuyOrClaim, "Too many tickets");
require(_lotteries[_lotteryId].status == Status.Claimable, "Lottery not claimable");
// Initializes the rewardInYDTToTransfer
uint256 rewardInYDTToTransfer;
for (uint256 i = 0; i < _ticketIds.length; i++) {
require(_brackets[i] < 6, "Bracket out of range"); // Must be between 0 and 5
uint256 thisTicketId = _ticketIds[i];
require(_lotteries[_lotteryId].firstTicketIdNextLottery > thisTicketId, "TicketId too high");
require(_lotteries[_lotteryId].firstTicketId <= thisTicketId, "TicketId too low");
require(msg.sender == _tickets[thisTicketId].owner, "Not the owner");
// Update the lottery ticket owner to 0x address
_tickets[thisTicketId].owner = address(0);
uint256 rewardForTicketId = _calculateRewardsForTicketId(_lotteryId, thisTicketId, _brackets[i]);
// Check user is claiming the correct bracket
require(rewardForTicketId != 0, "No prize for this bracket");
if (_brackets[i] != 5) {
require(
_calculateRewardsForTicketId(_lotteryId, thisTicketId, _brackets[i] + 1) == 0,
"Bracket must be higher"
);
}
// Increment the reward to transfer
rewardInYDTToTransfer += rewardForTicketId;
}
// Transfer money to msg.sender
YDTtoken.safeTransfer(msg.sender, rewardInYDTToTransfer);
emit TicketsClaim(msg.sender, rewardInYDTToTransfer, _lotteryId, _ticketIds.length);
}
/**
* @notice Close lottery
* @param _lotteryId: lottery id
* @dev Callable by operator
*/
function closeLottery(uint256 _lotteryId) external override onlyOperator nonReentrant {
require(_lotteries[_lotteryId].status == Status.Open, "Lottery closed");
require(block.timestamp > _lotteries[_lotteryId].endTime, "Lottery ended");
_lotteries[_lotteryId].firstTicketIdNextLottery = currentTicketId;
// Request a random number from the generator based on a seed
randomGenerator.getRandomNumber();
_lotteries[_lotteryId].status = Status.Close;
emit LotteryClose(_lotteryId, currentTicketId);
}
/**
* @notice Draw the final number, calculate reward in YDT per group, and make lottery claimable
* @param _lotteryId: lottery id
* @param _autoInject: reinjects funds into next lottery (vs. withdrawing all)
* @dev Callable by operator
*/
function drawFinalNumberAndMakeLotteryClaimable(uint256 _lotteryId, bool _autoInject)
external
override
onlyOperator
nonReentrant
{
require(_lotteries[_lotteryId].status == Status.Close, "Lottery not close");
require(_lotteryId == randomGenerator.viewLatestLotteryId(), "Numbers not drawn");
// Calculate the finalNumber based on the randomResult generated by ChainLink's fallback
uint32 finalNumber = randomGenerator.viewRandomResult();
// Initialize a number to count addresses in the previous bracket
uint256 numberAddressesInPreviousBracket;
// Calculate the amount to share post-treasury fee
uint256 amountToShareToWinners = (
((_lotteries[_lotteryId].amountCollectedInYDT) * (10000 - _lotteries[_lotteryId].treasuryFee))
) / 10000;
// Initializes the amount to withdraw to treasury
uint256 amountToWithdrawToTreasury;
// Calculate prizes in YDT for each bracket by starting from the highest one
for (uint32 i = 0; i < 6; i++) {
uint32 j = 5 - i;
uint32 transformedWinningNumber = _bracketCalculator[j] + (finalNumber % (uint32(10)**(j + 1)));
_lotteries[_lotteryId].countWinnersPerBracket[j] =
_numberTicketsPerLotteryId[_lotteryId][transformedWinningNumber] -
numberAddressesInPreviousBracket;
// A. If number of users for this _bracket number is superior to 0
if (
(_numberTicketsPerLotteryId[_lotteryId][transformedWinningNumber] - numberAddressesInPreviousBracket) !=
0
) {
// B. If rewards at this bracket are > 0, calculate, else, report the numberAddresses from previous bracket
if (_lotteries[_lotteryId].rewardsBreakdown[j] != 0) {
_lotteries[_lotteryId].YDTPerBracket[j] =
((_lotteries[_lotteryId].rewardsBreakdown[j] * amountToShareToWinners) /
(_numberTicketsPerLotteryId[_lotteryId][transformedWinningNumber] -
numberAddressesInPreviousBracket)) /
10000;
// Update numberAddressesInPreviousBracket
numberAddressesInPreviousBracket = _numberTicketsPerLotteryId[_lotteryId][transformedWinningNumber];
}
// A. No YDT to distribute, they are added to the amount to withdraw to treasury address
} else {
_lotteries[_lotteryId].YDTPerBracket[j] = 0;
amountToWithdrawToTreasury +=
(_lotteries[_lotteryId].rewardsBreakdown[j] * amountToShareToWinners) /
10000;
}
}
// Update internal statuses for lottery
_lotteries[_lotteryId].finalNumber = finalNumber;
_lotteries[_lotteryId].status = Status.Claimable;
if (_autoInject) {
pendingInjectionNextLottery = amountToWithdrawToTreasury;
amountToWithdrawToTreasury = 0;
}
amountToWithdrawToTreasury += (_lotteries[_lotteryId].amountCollectedInYDT - amountToShareToWinners);
// Transfer YDT to treasury address
YDTtoken.safeTransfer(treasuryAddress, amountToWithdrawToTreasury);
emit LotteryNumberDrawn(currentLotteryId, finalNumber, numberAddressesInPreviousBracket);
}
/**
* @notice Change the random generator
* @dev The calls to functions are used to verify the new generator implements them properly.
* It is necessary to wait for the VRF response before starting a round.
* Callable only by the contract owner
* @param _randomGeneratorAddress: address of the random generator
*/
function changeRandomGenerator(address _randomGeneratorAddress) external onlyOwner {
require(_lotteries[currentLotteryId].status == Status.Claimable, "Lottery not in claimable");
// Request a random number from the generator based on a seed
IRandomNumberGenerator(_randomGeneratorAddress).getRandomNumber();
// Calculate the finalNumber based on the randomResult generated by ChainLink's fallback
IRandomNumberGenerator(_randomGeneratorAddress).viewRandomResult();
randomGenerator = IRandomNumberGenerator(_randomGeneratorAddress);
emit NewRandomGenerator(_randomGeneratorAddress);
}
/**
* @notice Inject funds
* @param _lotteryId: lottery id
* @param _amount: amount to inject in YDT token
* @dev Callable by owner or injector address
*/
function injectFunds(uint256 _lotteryId, uint256 _amount) external override {
require((msg.sender == owner()) || (msg.sender == injectorAddress));
require(_lotteries[_lotteryId].status == Status.Open, "Lottery closed");
YDTtoken.safeTransferFrom(address(msg.sender), address(this), _amount);
_lotteries[_lotteryId].amountCollectedInYDT += _amount;
emit LotteryInjection(_lotteryId, _amount);
}
/**
* @notice Start the lottery
* @dev Callable by operator
* @param _endTime: endTime of the lottery
* @param _priceTicketInYDT: price of a ticket in YDT
* @param _discountDivisor: the divisor to calculate the discount magnitude for bulks
* @param _rewardsBreakdown: breakdown of rewards per bracket (must sum to 10,000)
* @param _treasuryFee: treasury fee (10,000 = 100%, 100 = 1%)
*/
function startLottery(
uint256 _endTime,
uint256 _priceTicketInYDT,
uint256 _discountDivisor,
uint256[6] calldata _rewardsBreakdown,
uint256 _treasuryFee
) external override onlyOperator {
require(
(currentLotteryId == 0) || (_lotteries[currentLotteryId].status == Status.Claimable),
"Not time to start lottery"
);
require(
((_endTime - block.timestamp) > MIN_LENGTH_LOTTERY) && ((_endTime - block.timestamp) < MAX_LENGTH_LOTTERY),
"Lottery length outside of range"
);
require(
(_priceTicketInYDT >= minPriceTicketInYDT) && (_priceTicketInYDT <= maxPriceTicketInYDT),
"Outside of limits"
);
require(_discountDivisor >= MIN_DISCOUNT_DIVISOR, "Discount divisor too low");
require(_treasuryFee <= MAX_TREASURY_FEE, "Treasury fee too high");
require(
(_rewardsBreakdown[0] +
_rewardsBreakdown[1] +
_rewardsBreakdown[2] +
_rewardsBreakdown[3] +
_rewardsBreakdown[4] +
_rewardsBreakdown[5]) == 10000,
"Rewards must equal 10000"
);
currentLotteryId++;
_lotteries[currentLotteryId] = Lottery({
status: Status.Open,
startTime: block.timestamp,
endTime: _endTime,
priceTicketInYDT: _priceTicketInYDT,
discountDivisor: _discountDivisor,
rewardsBreakdown: _rewardsBreakdown,
treasuryFee: _treasuryFee,
YDTPerBracket: [uint256(0), uint256(0), uint256(0), uint256(0), uint256(0), uint256(0)],
countWinnersPerBracket: [uint256(0), uint256(0), uint256(0), uint256(0), uint256(0), uint256(0)],
firstTicketId: currentTicketId,
firstTicketIdNextLottery: currentTicketId,
amountCollectedInYDT: pendingInjectionNextLottery,
finalNumber: 0
});
emit LotteryOpen(
currentLotteryId,
block.timestamp,
_endTime,
_priceTicketInYDT,
currentTicketId,
pendingInjectionNextLottery
);
pendingInjectionNextLottery = 0;
}
/**
* @notice Set YDT price ticket upper/lower limit
* @dev Only callable by owner
* @param _minPriceTicketInYDT: minimum price of a ticket in YDT
* @param _maxPriceTicketInYDT: maximum price of a ticket in YDT
*/
function setMinAndMaxTicketPriceInYDT(uint256 _minPriceTicketInYDT, uint256 _maxPriceTicketInYDT)
external
onlyOwner
{
require(_minPriceTicketInYDT <= _maxPriceTicketInYDT, "minPrice must be < maxPrice");
minPriceTicketInYDT = _minPriceTicketInYDT;
maxPriceTicketInYDT = _maxPriceTicketInYDT;
}
/**
* @notice Set max number of tickets
* @dev Only callable by owner
*/
function setMaxNumberTicketsPerBuy(uint256 _maxNumberTicketsPerBuy) external onlyOwner {
require(_maxNumberTicketsPerBuy != 0, "Must be > 0");
maxNumberTicketsPerBuyOrClaim = _maxNumberTicketsPerBuy;
}
/**
* @notice Set operator, treasury, and injector addresses
* @dev Only callable by owner
* @param _operatorAddress: address of the operator
* @param _treasuryAddress: address of the treasury
* @param _injectorAddress: address of the injector
*/
function setOperatorAndTreasuryAndInjectorAddresses(
address _operatorAddress,
address _treasuryAddress,
address _injectorAddress
) external onlyOwner {
require(_operatorAddress != address(0), "Cannot be 0 address");
require(_treasuryAddress != address(0), "Cannot be 0 address");
require(_injectorAddress != address(0), "Cannot be 0 address");
operatorAddress = _operatorAddress;
treasuryAddress = _treasuryAddress;
injectorAddress = _injectorAddress;
emit NewOperatorAndTreasuryAndInjectorAddresses(_operatorAddress, _treasuryAddress, _injectorAddress);
}
/**
* @notice Calculate price of a set of tickets
* @param _discountDivisor: divisor for the discount
* @param _priceTicket price of a ticket (in YDT)
* @param _numberTickets number of tickets to buy
*/
function calculateTotalPriceForBulkTickets(
uint256 _discountDivisor,
uint256 _priceTicket,
uint256 _numberTickets
) external pure returns (uint256) {
require(_discountDivisor >= MIN_DISCOUNT_DIVISOR, "Must be >= MIN_DISCOUNT_DIVISOR");
require(_numberTickets != 0, "Number of tickets must be > 0");
return _calculateTotalPriceForBulkTickets(_discountDivisor, _priceTicket, _numberTickets);
}
/**
* @notice View current lottery id
*/
function viewCurrentLotteryId() external view override returns (uint256) {
return currentLotteryId;
}
/**
* @notice View lottery information
* @param _lotteryId: lottery id
*/
function viewLottery(uint256 _lotteryId) external view returns (Lottery memory) {
return _lotteries[_lotteryId];
}
/**
* @notice View rewards for a given ticket, providing a bracket, and lottery id
* @dev Computations are mostly offchain. This is used to verify a ticket!
* @param _lotteryId: lottery id
* @param _ticketId: ticket id
* @param _bracket: bracket for the ticketId to verify the claim and calculate rewards
*/
function viewRewardsForTicketId(
uint256 _lotteryId,
uint256 _ticketId,
uint32 _bracket
) external view returns (uint256) {
// Check lottery is in claimable status
if (_lotteries[_lotteryId].status != Status.Claimable) {
return 0;
}
// Check ticketId is within range
if (
(_lotteries[_lotteryId].firstTicketIdNextLottery < _ticketId) &&
(_lotteries[_lotteryId].firstTicketId >= _ticketId)
) {
return 0;
}
return _calculateRewardsForTicketId(_lotteryId, _ticketId, _bracket);
}
/**
* @notice Calculate rewards for a given ticket
* @param _lotteryId: lottery id
* @param _ticketId: ticket id
* @param _bracket: bracket for the ticketId to verify the claim and calculate rewards
*/
function _calculateRewardsForTicketId(
uint256 _lotteryId,
uint256 _ticketId,
uint32 _bracket
) internal view returns (uint256) {
// Retrieve the winning number combination
uint32 userNumber = _lotteries[_lotteryId].finalNumber;
// Retrieve the user number combination from the ticketId
uint32 winningTicketNumber = _tickets[_ticketId].number;
// Apply transformation to verify the claim provided by the user is true
uint32 transformedWinningNumber = _bracketCalculator[_bracket] +
(winningTicketNumber % (uint32(10)**(_bracket + 1)));
uint32 transformedUserNumber = _bracketCalculator[_bracket] + (userNumber % (uint32(10)**(_bracket + 1)));
// Confirm that the two transformed numbers are the same, if not throw
if (transformedWinningNumber == transformedUserNumber) {
return _lotteries[_lotteryId].YDTPerBracket[_bracket];
} else {
return 0;
}
}
/**
* @notice View user ticket ids, numbers, and statuses of user for a given lottery
* @param _user: user address
* @param _lotteryId: lottery id
* @param _cursor: cursor to start where to retrieve the tickets
* @param _size: the number of tickets to retrieve
*/
function viewUserInfoForLotteryId(
address _user,
uint256 _lotteryId,
uint256 _cursor,
uint256 _size
)
external
view
returns (
uint256[] memory,
uint32[] memory,
bool[] memory,
uint256
)
{
uint256 length = _size;
uint256 numberTicketsBoughtAtLotteryId = _userTicketIdsPerLotteryId[_user][_lotteryId].length;
if (length > (numberTicketsBoughtAtLotteryId - _cursor)) {
length = numberTicketsBoughtAtLotteryId - _cursor;
}
uint256[] memory lotteryTicketIds = new uint256[](length);
uint32[] memory ticketNumbers = new uint32[](length);
bool[] memory ticketStatuses = new bool[](length);
for (uint256 i = 0; i < length; i++) {
lotteryTicketIds[i] = _userTicketIdsPerLotteryId[_user][_lotteryId][i + _cursor];
ticketNumbers[i] = _tickets[lotteryTicketIds[i]].number;
// True = ticket claimed
if (_tickets[lotteryTicketIds[i]].owner == address(0)) {
ticketStatuses[i] = true;
} else {
// ticket not claimed (includes the ones that cannot be claimed)
ticketStatuses[i] = false;
}
}
return (lotteryTicketIds, ticketNumbers, ticketStatuses, _cursor + length);
}
/**
* @notice Calculate final price for bulk of tickets
* @param _discountDivisor: divisor for the discount (the smaller it is, the greater the discount is)
* @param _priceTicket: price of a ticket
* @param _numberTickets: number of tickets purchased
*/
function _calculateTotalPriceForBulkTickets(
uint256 _discountDivisor,
uint256 _priceTicket,
uint256 _numberTickets
) internal pure returns (uint256) {
return (_priceTicket * _numberTickets * (_discountDivisor + 1 - _numberTickets)) / _discountDivisor;
}
/**
* @notice Check if an address is a contract
*/
function _isContract(address _addr) internal view returns (bool) {
uint256 size;
assembly {
size := extcodesize(_addr)
}
return size > 0;
}
}