The NuBaGo Lottery Contract enables the creation and management of a decentralized lottery system using an ERC20 token (NuBaGo Token - NBG) for ticket purchases. This system allows for multiple lotteries, where users can purchase tickets, reveal random numbers, and eventually determine the winners. The contract handles the minting of tokens, ticket purchases, lottery state management, and winner determination.
Contract provides a robust and decentralized lottery system powered by the NuBaGo Token (NBG), allowing for secure ticket purchases, random number generation, winner determination, and refund handling. It ensures fairness and transparency in lottery operations.
- Custom ERC20 Token: The contract utilizes the
TicketTokenERC20 token (NuBaGo Token - NBG) for lottery ticket purchases. - Lottery Creation: Owners can create lotteries with configurable parameters such as the number of tickets, winners, minimum ticket sales percentage, and ticket price.
- Ticket Purchases: Users can purchase lottery tickets with NBG tokens, which are recorded and tracked by the contract.
- Random Number Generation: Users submit a hash of a random number, which is revealed later to determine winners.
- Lottery States: The lottery goes through different states:
PURCHASE,REVEAL,COMPLETED, andCANCELLED. The contract can automatically transition between these states based on time and conditions (e.g., minimum ticket sales percentage). - Winner Determination: Winners are determined through a random process by XOR'ing the random numbers revealed by participants.
- Refunds: If a lottery is canceled due to insufficient ticket sales, participants can request refunds for their tickets.
This is a custom ERC20 token used for the lottery. The contract owner can mint new tokens to be distributed to participants.
Functions:
mint(address to, uint256 amount): Allows the owner to mint new tokens and send them to a specified address.
The main contract managing the lottery system. It tracks multiple lotteries and their participants, ticket sales, and manages state transitions.
Key Variables:
ticketToken: Reference to the deployedTicketTokencontract.currentLotteryNo: Tracks the current lottery number.lotteries: A mapping of lottery numbers to lottery details.lotteryTickets: A mapping of lottery numbers to an array of ticket information.addressToTickets: A mapping of user addresses to the list of ticket numbers they hold for each lottery.
Key Events:
LotteryCreated: Emitted when a new lottery is created.TicketPurchased: Emitted when a user purchases a ticket.RandomNumberRevealed: Emitted when a user reveals their random number.RefundWithdrawn: Emitted when a user withdraws a refund.LotteryStateUpdated: Emitted when the lottery state is updated.
createLottery(...): Allows the owner to create a new lottery with parameters such as ticket price, number of winners, and minimum participation.updateLotteryState(uint256 lottery_no): Updates the lottery state based on conditions like the number of tickets sold, current time, and other rules.
buyTicketTx(uint256 lottery_no, uint256 quantity, bytes32 hash_rnd_number): Allows users to buy tickets for a specific lottery, providing a hash of a random number.
revealRndNumberTx(uint256 lottery_no, uint256 sticketno, uint256 quantity, uint256 rnd_number): Users reveal their random numbers which will be used to determine the lottery winners.
determineWinners(uint256 lottery_no): Calculates the lottery winners by XOR'ing all the revealed random numbers.
withdrawTicketRefund(uint256 lottery_no, uint256 sticketNo): Allows users to withdraw a refund for their tickets if the lottery is canceled.
getLotteryInfo(uint256 lottery_no): Retrieves essential information about a lottery.getLotterySales(uint256 lottery_no): Returns the number of tickets sold for a specific lottery.getIthWinningTicket(uint256 lottery_no, uint256 i): Returns the ticket number of the ith winning ticket for a lottery.checkIfMyTicketWon(uint256 lottery_no, uint256 ticketNo): Checks if a specific ticket is a winner.
setPaymentToken(address ercTokenAddr): Sets the payment token for the current lottery (ERC20 token).getPaymentToken(uint256 lottery_no): Returns the address of the payment token for a specific lottery.
withdrawTicketProceeds(uint256 lottery_no): Allows the owner to withdraw the proceeds from ticket sales once the lottery has been completed.
The lottery goes through several states:
- PURCHASE: Lottery is active and users can purchase tickets.
- REVEAL: Users can reveal their random numbers.
- COMPLETED: Lottery is complete and winners have been determined.
- CANCELLED: Lottery is canceled due to insufficient ticket sales.
-
Deploy the TicketToken Contract:
- Deploy the
TicketTokencontract (NuBaGo Token - NBG) for the lottery.
- Deploy the
-
Deploy the CompanyLotteries Contract:
- Deploy the
CompanyLotteriescontract by providing the address of the deployedTicketTokencontract.
- Deploy the
-
Create a Lottery:
- Call
createLotterywith desired parameters to start a new lottery.
- Call
-
Purchase Tickets:
- Users can buy tickets by calling
buyTicketTx, providing the lottery number and the quantity of tickets.
- Users can buy tickets by calling
-
Reveal Random Numbers:
- After purchasing, users can reveal their random numbers via
revealRndNumberTx.
- After purchasing, users can reveal their random numbers via
-
Determine Winners:
- Once the lottery is completed, the owner can call
determineWinnersto select the winners.
- Once the lottery is completed, the owner can call
-
Withdraw Proceeds:
- After the lottery ends, the owner can withdraw ticket proceeds via
withdrawTicketProceeds.
- After the lottery ends, the owner can withdraw ticket proceeds via
-
Refunds:
- If a lottery is canceled, users can withdraw refunds for their tickets via
withdrawTicketRefund.
- If a lottery is canceled, users can withdraw refunds for their tickets via
This contract is licensed under the MIT License.