paint-brush
Building a Transparent, Secure Lottery DApp with NextJs, Solidity, and CometChatby@daltonic
995 reads
995 reads

Building a Transparent, Secure Lottery DApp with NextJs, Solidity, and CometChat

by Darlington Gospel April 12th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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 is for you. You will need the following tools installed to build along with me: Nodejs, NextJs, Solidity, and CometChat.
featured image - Building a Transparent, Secure Lottery DApp with NextJs, Solidity, and CometChat
Darlington Gospel  HackerNoon profile picture


What you will be building, see the live demo and the git repo.


Purchasing a Ticket

CometChat One-to-many chat

Introduction

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?


And if you're interested in learning more about Web3 development, don't forget to subscribe to my YouTube channel and check out my premium web3 content and services.

Now, let’s jump into this tutorial.

Prerequisites

You will need the following tools installed to build along with me:

  • Nodejs (Important)
  • EthersJs
  • Hardhat
  • Redux toolkit
  • Yarn
  • Metamask
  • NextJs
  • Tailwind CSS
  • CometChat SDK


To set up your Metamask for this project, I recommend that you can watch the video below.

Installing Dependencies

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>


Now, run **yarn install** on the terminal to have all the dependencies for this project installed.

Configuring CometChat SDK

Follow the steps below to configure the CometChat SDK; at the end, you must save these keys as an environment variable.


STEP 1: Head to CometChat Dashboard and create an account.

Register a new CometChat account if you do not have one

STEP 2: Log in to the CometChat dashboard, only after registering.

Log in to the CometChat Dashboard with your created account

STEP 3: From the dashboard, add a new app called Dapp****Lottery.

Create a new CometChat app - Step 1

Create a new CometChat app - Step 2

STEP 4: Select the app you just created from the list.

Select your created app

STEP 5: From the Quick Start copy the APP_ID, REGION, and AUTH_KEY, to your .env.local file. See the image and code snippet.


Copy the APP_ID, REGION, and AUTH_KEY


Replace the REACT_COMET_CHAT placeholder keys with their appropriate values.


REACT_APP_COMETCHAT_APP_ID=****************
REACT_APP_COMETCHAT_AUTH_KEY=******************************
REACT_APP_COMETCHAT_REGION=**


The .env.local file should be created at the root of your project.

Configuring the Hardhat script

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 bytecodes and abi.

Configuring the Deployment Script

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.

The Smart Contract File

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 **Dapp**``**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 **Dapp**``**Lottery.sol** file. See the complete code below.


I have a book to help you master the web3 language (Solidity), grab your copy here.


Capturing Smart Contract Development

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:


  1. **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.


  2. **Counters**: This is an imported contract from OpenZeppelin that provides a way to keep track of the total number of lotteries created.


  3. **LotteryStruct**: This is a struct that defines the properties of a lottery, such as **id**, **title**, **description**, **image**, **prize**, **ticketPrice**, **participants**, **drawn**, **owner**, **createdAt**, and **expiresAt**.


  4. **ParticipantStruct**: This is a struct that defines the properties of a participant, such as **account**, **lotteryNumber**, and **paid**.


  5. **LotteryResultStruct**: This is a struct that defines the properties of a lottery result, such as **id**, **completed**, **paidout**, **timestamp**, **sharePerWinner**, and an array of **winners**, which are of type **ParticipantStruct**.


  6. **servicePercent** and **serviceBalance**: These are state variables that represent the percentage of service fee charged per lottery and the total balance earned from service fees, respectively.


  7. **lotteries**, **lotteryParticipants**, **lotteryLuckyNumbers**, **luckyNumberUsed**, and **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:

  1. **constructor(uint256 _servicePercent)**: This is the constructor function that initializes the **servicePercent** variable with the percentage of service fee charged per lottery.


  2. **createLottery()**: This function allows the creation of a new lottery with a **title**, **description**, **image**, **prize**, **ticketPrice**, and **expiresAt**. It also checks for some conditions before creating the lottery such as ensuring that the **title**, **description**, and **image** are not empty, **prize** and **ticketPrice** are not zero, and **expiresAt** is in the future.


  3. **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.


  4. **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.


  5. **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.


  6. **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

Activities of Deployment on the Terminal


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.

Developing the Frontend

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.

Components

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.

Header and Sub-header components

Header Component

Sub-header Component

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, Header.jsx and SubHeader.jsx respectively, and paste the above codes into it.

Jackpots Component

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.

Countdown Component

Countdown Component

This component accepts a timestamp and renders a countdown that counts from days to seconds. See the codes below.

Draw Time Component

The Draw Time Component

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.

Generator Component

The Generator Component

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.

Auth Chat Component

The Chat Authentication Component

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.

Chat Component

The Chat Interface

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.

Winners Component

The Winners Component

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.

Result Component

The Result Component

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.

CometChatNoSSR Component

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.

The Pages Components

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.

Home Page

The Home Page Component

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.

Create Lottery Page

Create Lottery Page

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.

The Jackpots Page

The Jackpots Page

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.

The Results Page

The Results Page

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.

The _app.tsx file

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.

State Management Files

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.

Redux States 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.

Redux Actions 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.

Redux Reducer 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 store/index file.

Services

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.

Static Assets

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.


Fullstack NFT Minting (Course)

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!

Conclusion

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!

About the Author

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 JavaScript, React, Vue, Angular, Node, React Native, NextJs, Solidity, and more.


For more information about him, kindly visit and follow his page on Twitter, Github, LinkedIn, or his website.