Users should be able to buy a ticket which is an actual NFT. The funds from each ticket purchase are gathered in a prize pool. After a certain period of time a random winner should be chosen. We also want to be able to update our NFT tickets in the future.
The smart contracts are using Beacon proxy which allows updating the implementation of all the proxies by a single transaction.
The Beacon contract registers the latest implementation of Ticket contract. This is used by TicketProxy contract which goes to beacon for latest implementation and uses it for the transaction further.
The setup involves a TicketFactory contract which can be used for deploying multiple Ticket contracts.
User flow:
- When the ticket is deployed, it is initialised with startBlock and endBlock which indicates the block limits where users can come and buy the tickets.
- In this period, the Ticket owner can declare a surprise winner which will be randomly chosen by the function and in the same transaction, the 50% funds will be transferred.
- When the purchase period ends, owner will declare the next winner which will be rewarded the remaining funds.
Assumptions:
- There are 2 type of winners, surprise winner (in purchase period) and lottery winner (after the purchase period ends)
- One lotteryId can be eligible for both the prices.
- The random function is not considering the manipulations from miner's and owner's ends hence cannot be used in production.
Contracts deployed on Ropsten Testnet:
- Beacon: 0xcdca8b01A3Aa9fb635A78e57816bB5DC5E446C3a
- Ticket Implementation: 0x81e67E8290e37976D3a0cCe0Ce535f9eA76fABfb
- TicketFactory: 0x79Bd86F33bcBB3EAF87E5c5b77EAa02E554621C3
- TicketProxy: 0x85d89e4a10632d6ee0e0e43c1e1b9da6fe59f59e
Transactions:
- Clone the repository using this command
git clone git@github.com:ameesha1205/nft-lottery.git
- Install dependencies:
yarn
Now you can try deploying the contracts on ropsten or hardhat local network:
For hardhat:
npx hardhat run scripts/lottery.js
For Ropsten: First create an env file and add the needed keys and details. (Please find .env.example in the repo)
npx hardhat run scripts/lottery.js --network ropsten
To run the smart contract tests:
yarn test
To run the smart contract test coverage:
yarn test:coverage