The startLottery function is only callable by the Operator in order to start a new lottery round.
closeLottery - Operator
functioncloseLottery(uint256 _lotteryId) externaloverrideonlyOperatornonReentrant {require(_lotteries[_lotteryId].status ==Status.Open,"Lottery not open");require(block.timestamp > _lotteries[_lotteryId].endTime,"Lottery not over"); _lotteries[_lotteryId].firstTicketIdNextLottery = currentTicketId;// Request a random number from the generator based on a seedrandomGenerator.getRandomNumber(uint256(keccak256(abi.encodePacked(_lotteryId, currentTicketId)))); _lotteries[_lotteryId].status =Status.Close; emit LotteryClose(_lotteryId, currentTicketId); }
Callable by the Operator to close a round of the lottery.
drawFinalNumberAndMakeLotteryClaimable - Operator
functiondrawFinalNumberAndMakeLotteryClaimable(uint256 _lotteryId, bool _autoInjection)externaloverrideonlyOperatornonReentrant {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].amountCollectedInCake) * (10000- _lotteries[_lotteryId].treasuryFee)) ) /10000;// Initializes the amount to withdraw to treasury uint256 amountToWithdrawToTreasury;// Calculate prizes in CAKE for each bracket by starting from the highest onefor (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 0if ( (_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].cakePerBracket[j] = ((_lotteries[_lotteryId].rewardsBreakdown[j] * amountToShareToWinners) / (_numberTicketsPerLotteryId[_lotteryId][transformedWinningNumber] - numberAddressesInPreviousBracket)) /10000;// Update numberAddressesInPreviousBracket numberAddressesInPreviousBracket = _numberTicketsPerLotteryId[_lotteryId][transformedWinningNumber];
}// A. No CAKE to distribute, they are added to the amount to withdraw to treasury address } else { _lotteries[_lotteryId].cakePerBracket[j] =0; amountToWithdrawToTreasury += (_lotteries[_lotteryId].rewardsBreakdown[j] * amountToShareToWinners) /10000; } }// Update internal statuses for lottery _lotteries[_lotteryId].finalNumber = finalNumber; _lotteries[_lotteryId].status =Status.Claimable;if (_autoInjection) { pendingInjectionNextLottery = amountToWithdrawToTreasury; amountToWithdrawToTreasury =0; } amountToWithdrawToTreasury += (_lotteries[_lotteryId].amountCollectedInCake - amountToShareToWinners);// Transfer CAKE to treasury addresscakeToken.safeTransfer(treasuryAddress, amountToWithdrawToTreasury); emit LotteryNumberDrawn(currentLotteryId, finalNumber, numberAddressesInPreviousBracket); }
For Operator to draw the final number using ChainLink VRF function.
In the case of tokens other than CAKE mistakenly being sent to the lottery contract, this function is used to recover them and is only callable by the Owner
setMinAndMaxTicketPriceInCake - Owner
functionsetMinAndMaxTicketPriceInCake(uint256 _minPriceTicketInCake, uint256 _maxPriceTicketInCake)externalonlyOwner {require(_minPriceTicketInCake <= _maxPriceTicketInCake,"minPrice must be < maxPrice"); minPriceTicketInCake = _minPriceTicketInCake; maxPriceTicketInCake = _maxPriceTicketInCake; }
To prevent the Operator setting the tickets to arbitrary prices during the event of a flash crash/pump.
functionsetOperatorAndTreasuryAndInjectorAddresses( address _operatorAddress, address _treasuryAddress, address _injectorAddress ) externalonlyOwner {require(_operatorAddress !=address(0),"Cannot be zero address");require(_treasuryAddress !=address(0),"Cannot be zero address");require(_injectorAddress !=address(0),"Cannot be zero address"); operatorAddress = _operatorAddress; treasuryAddress = _treasuryAddress; injectorAddress = _injectorAddress; emit NewOperatorAndTreasuryAndInjectorAddresses(_operatorAddress, _treasuryAddress, _injectorAddress); }
Function used to set the Operator, Treasury, and Injector addresses.
changeRandomGenerator - Owner
functionchangeRandomGenerator(address _randomGeneratorAddress) externalonlyOwner {require( (currentLotteryId ==0) || (_lotteries[currentLotteryId].status ==Status.Claimable),"Lottery not in claimable" );// Request a random number from the generator based on a seedIRandomNumberGenerator(_randomGeneratorAddress).getRandomNumber(uint256(keccak256(abi.encodePacked(currentLotteryId, currentTicketId))) );// Calculate the finalNumber based on the randomResult generated by ChainLink's fallbackIRandomNumberGenerator(_randomGeneratorAddress).viewRandomResult(); randomGenerator =IRandomNumberGenerator(_randomGeneratorAddress); emit NewRandomGenerator(_randomGeneratorAddress); }
For the Owner to update the RandomNumberGenerator contract in case we need to update the drawing logic, or release an update.
injectorAddress (onlyInjector)
Injector is the address used to fund the lottery with periodic injections
operatorAddress (onlyOperator)
The lottery scheduler account used to run regular operations.