If you're looking to build a cutting-edge decentralized application that combines the power of blockchain technology, real-time communication, and user-generated content, then this tutorial on building a Lottery DApp with NextJs, Solidity, and CometChat is for you.
Whether you're an experienced developer or just starting out, this step-by-step guide will walk you through the process of creating a fair and transparent lottery system on the blockchain. So why not start building your own Lottery DApp today and disrupt the traditional gambling industry?
Now, let’s jump into this tutorial.
You will need the following tools installed to build along with me:
- Nodejs (Important)
- Redux toolkit
- Tailwind CSS
- CometChat SDK
To set up your Metamask for this project, I recommend that you can watch the video below.
Clone the starter kit and open it in VS Code using the command below:
git clone https://github.com/Daltonic/tailwind_ethers_starter_kit <PROJECT_NAME> cd <PROJECT_NAME>
**yarn install** on the terminal to have all the dependencies for this project installed.
Follow the steps below to configure the CometChat SDK; at the end, you must save these keys as an environment variable.
Head to CometChat Dashboard and create an account.
Log in to the CometChat dashboard, only after registering.
From the dashboard, add a new app called DappLottery.
Select the app you just created from the list.
From the Quick Start copy the
AUTH_KEY, to your
.env.local file. See the image and code snippet.
REACT_COMET_CHAT placeholder keys with their appropriate values.
REACT_APP_COMETCHAT_APP_ID=**************** REACT_APP_COMETCHAT_AUTH_KEY=****************************** REACT_APP_COMETCHAT_REGION=**
.env.local file should be created at the root of your project.
At the root of this project, open the
hardhat.config.js file and replace its content with the following settings.
The above script instructs hardhat on these two important rules.
Networks: This block contains the configurations for your choice of networks. On deployment, hardhat will require you to specify a network for shipping your smart contracts.
Solidity: This describes the version of the compiler to be used by hardhat for compiling your smart contract codes into
Navigate to the scripts folder and then to your
deploy.js file and paste the code below into it. If you can't find a script folder, make one, create a deploy.js file, and paste the following code into it.
When run as a Hardhat deployment command, the above script will deploy your specified smart contract to the network of your choice.
If you are struggling with a low spec computer, or you want to do some web3 coding on the fly, check out this video to learn how to properly set up a web3 project with Gitpod.
Now that we've completed the initial configurations, let's create the smart contract for this project. Create a new folder called
**contracts** in your project's root.
Create a new file called
**Lottery.sol** within this contracts' folder; this file will contain all the logic that governs the smart contract.
Copy, paste, and save the following codes into the
**Lottery.sol** file. See the complete code below.
I have a book to help you master the web3 language (Solidity), grab your copy here.
Now, let's go over some of the details of what's going on in the smart contract above. We have the following items:
This is a Solidity smart contract named "DappLottery" that enables the creation of a lottery where users can purchase tickets and participate in a chance to win a prize. The smart contract has several functions that perform different tasks:
**Ownable**: This is an imported contract from OpenZeppelin that provides a basic access control mechanism to restrict access to certain functions to the contract owner only.
**Counters**: This is an imported contract from OpenZeppelin that provides a way to keep track of the total number of lotteries created.
**LotteryStruct**: This is a struct that defines the properties of a lottery, such as
**ParticipantStruct**: This is a struct that defines the properties of a participant, such as
**LotteryResultStruct**: This is a struct that defines the properties of a lottery result, such as
**sharePerWinner**, and an array of
**winners**, which are of type
**serviceBalance**: These are state variables that represent the percentage of service fee charged per lottery and the total balance earned from service fees, respectively.
**lotteryResult**: These are mappings used to store and retrieve data related to lotteries, their participants, lucky numbers, lottery results, and whether a lucky number has been used.
The following are the functions provided by this smart contract:
**constructor(uint256 _servicePercent)**: This is the constructor function that initializes the
**servicePercent**variable with the percentage of service fee charged per lottery.
**createLottery()**: This function allows the creation of a new lottery with a
**expiresAt**. It also checks for some conditions before creating the lottery such as ensuring that the
**image**are not empty,
**ticketPrice**are not zero, and
**expiresAt**is in the future.
**importLuckyNumbers()**: This function allows the owner of a lottery to import a list of
**luckyNumbers**that will be used to select the winners of the lottery. It checks for some conditions before importing the list, such as ensuring that the
**luckyNumbers**are not empty, and that the lottery does not have any participants yet.
**buyTicket()**: This function allows users to buy tickets for a lottery by specifying the
**id**of the lottery and the
**luckyNumberId**they want to use. It checks for some conditions before allowing the purchase, such as ensuring that the lucky number has not been used before and that the user has provided enough funds to purchase the ticket.
**randomlySelectWinners()**: This function selects the winners of a lottery randomly from the list of participants using the Fisher-Yates algorithm. It checks for some conditions before selecting the winners, such as ensuring that the lottery has not been completed, and that the number of winners selected does not exceed the number of participants.
**payLotteryWinners()**: This is an internal function that pays out the winners of a lottery by calculating the share of the prize each winner is entitled to, and then transferring the appropriate amount of funds to each winner's account.
Overall, this smart contract provides a simple and secure way to create and manage lotteries on the Ethereum blockchain. It ensures transparency and fairness in the selection of winners, and automates the payment process to reduce the risk of fraud or errors.
Next, run the commands below to deploy the smart contract into the network.
yarn hardhat node # Terminal #1 yarn hardhat run scripts/deploy.js # Terminal #2
If you need further help to configure Hardhat or deploying your Fullstack DApp, watch this video. It will teach you how to do an Omini-chain deployment.
Now that we have our smart contract on the network and all of our artifacts (bytecodes and ABI) generated, let's get the front end ready with React.
In the root directory, create a new folder called
**components** to house all the NextJs components for this project. For each one of the components below, you will have to create their respective files in the components' folder.
This component contains the logo, dummy navigational elements, and a connect wallet button, see the code below.
Within the component's folder, create two files,
SubHeader.jsx respectively, and paste the above codes into it.
This component was built to display the cards in grid view, as can be seen in the image above, see the codes below to understand how to recreate it.
This component accepts a timestamp and renders a countdown that counts from days to seconds. See the codes below.
This component displays the details of a lottery, some buttons to generate lottery numbers, create group chat with, see the lottery result page, and login to the chat interface. Lastly, it contains a table to render all generated lottery numbers. See the codes below.
This component helps us to generate and send a specific number of strings to the smart contract. These generated numbers will then be put on display for users to buy as tickets for participating in the lottery. See the code snippet below.
This component authenticates users before they can chat with our platform. The CometChat SDK is used here under the hood to perform an authentication with the connected user's wallet. See the code below.
This component utilizes the CometChat SDK to perform anonymous one-to-many chat among all authenticated users who have also joined the group. Here is the code for its implementation.
This component is activated when the perform draw button is clicked. It allows you to enter the number of winners you want. See the snippet below.
Almost as similar as the Draw time component, this component displays some statistics about the just concluded lottery, the winners and the losers, what the winners took home, and what the losers lost. The component also includes a button to perform the draw, which is only enabled once the countdown is at zero. See the coded implementation below.
This is a special component created to help us load the CometChat module to the browsers window since NextJs is a server side rendering framework. See the code below.
Here is a free full video tutorial that can watch to help you learn how to build a decentralized NFT minting platform on my YouTube channel.
In this section, let’s go through all the codes that makes for each one of the pages in this project. Please take not that these various pages must be created in the pages folder in the root directory of your project.
This page contains the Header and Jackpots components, take a look at its simple implementation below. It uses a SSR (Server Side Rendering) technique to retrieve all lotteries from the blockchain without requiring a user to connect their wallet or be on a specifc chain.
This page enables a user create to a lottery, of course, it collects information about the lottery such as the lottery title, description, image url, prize to be won, ticket cost, and the expiration data for the lottery. It is important to note that for creating a lottery, a user’s wallet must be connected and and the right chain/network. See the code below.
Listen up, the way you create this page is quite different as to how the other pages have been created since its a dynamic component.
First, create a folder called
jackpots inside the pages directory. Next, create a file called
[jackpotId].jsx exactly in this format just created and paste the codes below inside of it. See the codes below.
Again, this page as the Home page utilizes the NextJs server side rendering technique to retrieive the lottery information from the chain without requiring users to login with their wallet address.
This page like the Jackpots page uses the NextJs dynamic routing technique to see the result for each one of the lottery. Head to the pages directory and create a folder called
jackpots , inside of this new folder create a file in this format called
[resultId].jsx and past the codes below inside and save.
This is an entry file that comes pre-configured with NextJs which you will find within the pages folder of your project. Open it and replace its codes with the one below.
Now, it must be brought to your notice that this project uses the redux-toolkit package to keep the shared data used across this application in a central location. Follow the steps below to replicate. Before proceeding to the step below, create a folder at the root of this project called
store and create the following file within it.
This file will help us keep together all the states of the variables we are using in this application together. Within this store directory create another folder called
states and inside of it create a file called
global_states.js and paste the codes below inside and save.
Next, create another folder in the store directory called
actions and inside of it create a file called
global_actions.js and paste the codes below inside and save.
Let’s create a ruducer or a redux slice that will help us manage everything that has to do with out global states and actions recently created. Within the
store folder, create a file named
global_reducer.js and save. See the codes below.
Lastly, let’s bundle up and help us manage all the reducers/slices in our store. Within this store folder create another file named
index.js, paste the codes below inside of it and save.
We can include as many reducers as possible here in this
We have three services used here in this application which you will create in a folder called
services in the root of this project.
The blockchain, blockchain.ssr, and the chat services. The blockchain services deals with all functions that sends information to our smart contract, while the blockchain ssr file reads data stored in our smart contract. This is extremely important and it ssr file ensures that we can retrieve data from the blockchain without needing to first connect our wallet to Metamask.
We also have a chat service which helps us communicate with the CometChat SDK. See the codes below and be sure to create each one of these files in the services folder.
Fantastic, now let’s include the essential assets used in this project.
At the root off your application, create a folder called
assets, download the images found in this location and store them in the assets directory.
Also, don’t forget to instruct NextJs to allow your application to load images from any location. At the root of your application, create a file named
next.config.js and paste the code below in it and save.
And there you have it, congratulations you have successfully created a web3 lottery app, you just need to run the following commands on your terminal to see it live on your browers.
yarn dev #terminal 2
The two commands above will spin up your project online and can be visited on the browser on localhost:3000.
If you're confused about web3 development and want visual materials, get my Fullstack NFT Marketplace and Minting courses.
Take the first step towards becoming a highly sought-after smart contract developer by enrolling in my courses on NFTs Minting and Marketplace. Enroll now and let's embark on this exciting journey together!
To conclude, this technology presents an exciting opportunity to revolutionize the traditional gambling industry.
The use of blockchain technology ensures transparency, security, and immutability in the lottery system, while also eliminating the need for intermediaries. This tutorial on building a Lottery DApp with NextJs, Solidity, and CometChat is a valuable resource for developers who want to create cutting-edge decentralized applications.
By following the step-by-step guide, we have learned how to create a fair and transparent lottery system on the blockchain. So why not start building your own Lottery DApp today and disrupt the traditional gambling industry? Don't forget to subscribe to the YouTube channel.
See you next time!
Gospel Darlington is a full-stack blockchain developer with
7+ years of experience in the software development industry.
By combining Software Development, writing, and teaching, he demonstrates how to build decentralized applications on EVM-compatible blockchain networks.
His stacks include
Solidity, and more.