Skip to content

A smart contract which allows user to buy ticket as NFT. A random winner is chosen by the owner and distributed the fee collected

Notifications You must be signed in to change notification settings

ameeshaagrawal/nft-lottery

Repository files navigation

NFT Lottery

Problem Statement

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.

Solution

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:

Transactions:

  1. createTicket()
  2. buyTicket()
  3. declareSurpriseWinner()
  4. declareLotteryWinner()

Setup

  1. Clone the repository using this command git clone git@github.com:ameesha1205/nft-lottery.git
  2. Install dependencies: yarn

Deploy

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

Test

To run the smart contract tests:

yarn test

Coverage

To run the smart contract test coverage:

yarn test:coverage

About

A smart contract which allows user to buy ticket as NFT. A random winner is chosen by the owner and distributed the fee collected

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published