Lottery v2

Contract info

Contract name: PancakeSwapLottery Contract address: 0x5aF6D33DE2ccEC94efb1bDF8f92Bd58085432d2c Random number generator address: 0x8c6375Aab6e5B26a30bF241EBBf29AD6e6c503c2 (Random number generator contract must be deployed first)

Audits

The PancakeSwap Lottery V2 has been audited twice so far. View the results below:

Lottery Status states

The lottery has four Status states, Pending, Open, Close, and Claimable, that determine which actions can and cannot be taken at a given time.

Read/View functions

viewCurrentLotteryId

1
function viewCurrentLotteryId() external view override returns (uint256);
Copied!
Returns the Id# of the current Lottery round as an integer. Round Id#s correlate to round number, and are incremental, e.g. the ninth round of Lottery will be 9.

viewLottery

1
function viewLottery(uint256 _lotteryId) external view returns (Lottery memory);
Copied!
Returns information on specified Lottery round as tuple (see Lottery structure below).
1
uint256 startTime;
2
uint256 endTime;
3
uint256 priceTicketInCake;
4
uint256 discountDivisor;
5
uint256[6] rewardsBreakdown; // 0: 1 matching number // 5: 6 matching numbers
6
uint256 treasuryFee; // 500: 5% // 200: 2% // 50: 0.5%
7
uint256[6] cakePerBracket;
8
uint256[6] countWinnersPerBracket;
9
uint256 firstTicketId;
10
uint256 firstTicketIdNextLottery;
11
uint256 amountCollectedInCake;
12
uint32 finalNumber;
Copied!
Name
Type
Description
startTime
uint256
Starting block for Lottery round.
endTime
uint256
Ending block for Lottery round (approximately 12 hours after a round begins).
priceTicketInCake
uint256
The price of a ticket in CAKE (approximately $5 USD).
discountDivisor
uint256
The divisor used to calculate bulk ticket discount.
rewardsBreakdown
uint256[6]
The division of rewards across brackets (total must add up to 10,000).
treasuryFee
uint256
Amount taken from funds raised per round that's moved to treasury address (maximum 3000).
cakePerBracket
uint256[6]
The amount of CAKE to distribute to winners of each bracket.
countWinnersPerBracket
uint256[6]
Moves through brackets, starting from the highest, accounting for winners when value > 0.
firstTicketId
uint256
Id of the first ticket, set with the opening of the Lottery round, that determines the range of eligible tickets for the current round.
firstTicketIdNextLottery
uint256
Id of the first ticket, set at the closing of current round, that determines the range of eligible tickets for the current round.
amountCollectedInCake
uint256
The amount of CAKE collected through ticket sales for the Lottery round.
finalNumber
uint32
The final number determined by randomResult obtained from the number generator contract (RandomNumberGenerator.sol) using Chainlink VRF.

viewNumbersAndStatusesForTicketIds

1
function viewNumbersAndStatusesForTicketIds(uint256[] calldata _ticketIds)
2
external
3
view
4
returns (uint32[] memory, bool[] memory);
Copied!
Returns the corresponding numbers and the statuses of ticketIds array of tickets defined by their ticketId.

viewRewardsForTicketId

1
function viewRewardsForTicketId(
2
uint256 _lotteryId,
3
uint256 _ticketId,
4
uint32 _bracket;
Copied!
Calculates rewards for a ticket after draw given the lotteryId, ticketId, and bracket. Filling and querying will provide a link to detailed price information on BscScan.
Name
Type
Description
lotteryId
uint256
The id of the Lottery.
ticketId
uint256
The id of the ticket.
bracket
uint32
Bracket for the ticketId to verify the claim and calculate rewards.

viewUserInfoForLotteryId

1
function viewUserInfoForLottery(
2
address _user,
3
uint256 _lotteryId,
4
uint256 _cursor,
5
uint256 _size
6
)
7
external
8
view
9
returns (
10
uint256[] memory,
11
uint32[] memory,
12
bool[] memory,
13
uint256
14
)
Copied!
Returns user lotteryTicketIds, ticketNumbers, and ticketStatuses of a user for a given Lottery (defined by lotteryID).
Name
Type
Description
user
address
The address of the user.
lotteryId
uint256
The id of the Lottery.
cursor
uint256
Cursor to start where to retrieve the tickets.
size
uint256
The number of tickets to retrieve.

calculateRewardsForTicketId

1
function _calculateRewardsForTicketId(
2
uint256 _lotteryId,
3
uint256 _ticketId,
4
uint32 _bracket
5
) internal view returns (uint256);
Copied!
Calculates rewards for a ticket after draw given the lotteryId, ticketId, and bracket.
Name
Type
Description
lotteryId
uint256
The id of the Lottery.
ticketId
uint256
The id of the ticket.
bracket
uint32
Bracket for the ticketId to verify the claim and calculate rewards.

calculateTotalPriceForBulkTickets

1
function calculateTotalPriceForBulkTickets(
2
uint256 _discountDivisor,
3
uint256 _priceTicket,
4
uint256 _numberTickets
5
) external pure returns (uint256);
Copied!
Calculates the price for a set of tickets accounting for bulk discount.
discountDivisor:
โ€‹
totalPriceForBulkTickets=priceSingleTicketโ‹…numberTicketsโ‹…(discountDivisor+1โˆ’numberTickets)discountDivisortotalPriceForBulkTickets = priceSingleTicket \cdot numberTickets \cdot \frac{(discountDivisor + 1 - numberTickets)}{discountDivisor}
โ€‹
Filling and querying will provide a link to detailed price information on BscScan.
Name
Type
Description
discountDivisor
uint256
The divisor for the discount.
priceTickets
uint256
The price of a ticket in CAKE.
numberTickets
uint256
The number of tickets to buy.

Write functions (users)

buyTickets

1
function buyTickets(uint256 _lotteryId, uint32[] calldata _ticketNumbers) external override notContract nonReentrant;
Copied!
Buy tickets for the current Open Lottery round (between 1 and 100 per purchase). Calculates the price per ticket using calculateTotalPriceForBulkTickets.
Name
Type
Description
lotteryId
uint256
The id of the lottery.
ticketNumbers
uint32
Array of ticket numbers between 1,000,000 and 1,999,999.

claimTickets

1
function claimTickets(
2
uint256 _lotteryId,
3
uint256[] calldata _ticketIds,
4
uint32[] calldata _brackets
5
) external override notContract nonReentrant;
Copied!
Claim a set of winning tickets for a Claimable Lottery round. Checks lotteryId to determine if round is claimable, ownership of ticketId, eligibility of ticket (ticketId falls between firstTicketId and firstTicketIdNextLottery), and whether ticketId falls within eligible prize bracket (between 0 and 5).
Name
Type
Description
lotteryId
uint256
The id of the Lottery.
ticketIds
uint32
Array of ticketIds.
brackets
uint32
Array of brackets for the ticket ids.

Write functions (operator/admin)

closeLottery

1
function closeLottery(uint256 _lotteryId) external override onlyOperator;
Copied!
Closes the Open Lottery to Close state. Emits LotteryClose event.
Name
Type
Description
lotteryId
uint256
The id of the Lottery.

drawFinalNumberAndMakeLotteryClaimable

1
function drawFinalNumberAndMakeLotteryClaimable(uint256 _lotteryId, bool _autoInjection) external override onlyOperator nonReentrant;
Copied!
Lottery must be in Close state. Draws the final Lottery number for results from randomResult, calculates the rewards for brackets after accounting for treasury fee, makes Lottery state Claimable, and transfers treasury fee to treasury address.
Name
Type
Description
lotteryId
uint256
The id of the Lottery.
autoInjection
bool
Automatic injection status.

changeRandomNumberGenerator

1
function changeRandomGenerator(address _randomGeneratorAddress) external onlyOwner;
Copied!
Changes the random number generator contract address. Lottery must be Claimable.
Name
Type
Description
randomGeneratorAddress
address
The random generator address.

injectFunds

1
function injectFunds(uint256 _lotteryId, uint256 _amount) external override onlyOwner;
Copied!
Inject funds into a Lottery. Lottery must be Open.
Name
Type
Description
lotteryId
uint256
The id of the Lottery.
amount
uint256
Amount, in CAKE token, to inject.

startLottery

1
function startLottery(
2
uint256 _endTime,
3
uint256 _priceTicketInCake,
4
uint256 _discountDivisor,
5
uint256[6] calldata _rewardsBreakdown,
6
uint256 _treasuryFee
7
) external override onlyOperator;
Copied!
Starts the Lottery, setting it to Open state. Status must be Claimable.
Name
Type
Description
endTime
uint256
End time of the Lottery.
priceTicketInCake
uint256
Price of a ticket in CAKE.
discountDivisor
uint256
The divisor to calculate the discount magnitude for bulks.
rewardsBreakdown
uint256[6]
Breakdown of rewards per bracket (must sum to 10,000).
trasuryFee
uint256
Treasury fee (10,000 = 100%, 100 = 1%).

recoverWrongTokens

1
function recoverWrongTokens(address _tokenAddress, uint256 _tokenAmount) external onlyOwner;
Copied!
Allows admin to recover incorrect tokens sent to address mistakenly. Cannot be CAKE tokens.
Name
Type
Description
tokenAddress
address
The address of the token to withdraw.
tokenAmount
uint256
The number of tokens to withdraw.

setMinAndMaxTicketPriceInCake

1
function recoverWrongTokens(address _tokenAddress, uint256 _tokenAmount) external onlyOwner;
Copied!
Allows admin to set upper and lower limit of ticket price in CAKE value. Minimum price must be lower than maximum price.
Name
Type
Description
minPriceTicketInCake
uint256
The minimum price in CAKE.
maxPriceTicketInCake
uint256
The maximum price in CAKE.

setMaxNumberTicketsPerBuy

1
function setMaxNumberTicketsPerBuy(uint256 _maxNumberTicketsPerBuy) external onlyOwner;
Copied!
Set max number of tickets purchasable at a time (presently 100). Max number of tickets must be higher than 0.
Name
Type
Description
maxNumberTicketsPerBuy
uint256
Max number of tickets in one purchase.

setOperatorAndTreasuryAndInjectorAddress

1
function setOperatorAndTreasuryAddresses(address _operatorAddress, address _treasuryAddress) external onlyOwner;
Copied!
Sets the address of the Lottery operator.
Name
Type
Description
operatorAddress
address
The address of the operator.
treasuryAddress
address
The address of the treasury.
injectorAddress
address
The address of the injector.

Events (User)

TicketsPurchase

1
TicketsPurchase(address indexed buyer, uint256 indexed lotteryId, uint256 numberTickets);
Copied!
Lottery tickets are purchased.
Emitter: buyTickets go to buyTicketsโ€‹

TicketsClaim

1
TicketsClaim(address indexed claimer, uint256 amount, uint256 indexed lotteryId, uint256 numberTickets);
Copied!
Lottery tickets are claimed post-draw.
Emitter: claimTickets go to claimTicketsโ€‹

Events (admin)

AdminTokenRecovery

1
AdminTokenRecovery(address token, uint256 amount);
Copied!
Admin recovers incorrect tokens from Lottery address.
Emitter: recoverWrongTokens go to recoverWrongTokensโ€‹

LotteryClose

1
LotteryClose(uint256 indexed lotteryId, uint256 firstTicketIdNextLottery);
Copied!
The Lottery is closed. lotteryId is indexed and firstTicketIdNextLottery is determined by currentTicketId.
Emitter: closeLottery go to closeLotteryโ€‹

LotteryInjection

1
LotteryInjection(uint256 indexed lotteryId, uint256 amount);
Copied!
Funds are injected into Lottery.
Emitter: injectFunds go to injectFundsโ€‹

LotteryOpen

1
LotteryOpen(
2
uint256 indexed lotteryId,
3
uint256 startTime,
4
uint256 endTime,
5
uint256 priceTicketInCake,
6
uint256 firstTicketId
7
);
Copied!
The Lottery is opened. firstTicketId is set from currentTicketId,
Emitter: startLottery go to startLotteryโ€‹

LotteryNumberDrawn

1
LotteryNumberDrawn(uint256 indexed lotteryId, uint256 finalNumber, uint256 countWinningTickets);
Copied!
Lottery numbers are drawn for Lottery round.
Emitter: drawFinalNumberAndMakeLotteryClaimable go to drawFinalNumberAndMakeLotteryClaimableโ€‹

NewOperatorAndTreasuryAndInjectorAddresses

1
NewOperatorAndTreasuryAndInjectorAddresses(address operator, address treasury);
Copied!
New operator address is set.
Emitter: setOperatorAndTreasuryAndInjectorAddresses go to setOperatorAndTreasuryAndInjectorAddressesโ€‹

NewRandomNumberGenerator

1
NewRandomGenerator(address indexed randomGenerator);
Copied!
New random number generator address is set.
Emitter: changeRandomGenerator go to changeRandomGeneratorโ€‹
Last modified 1mo ago