What you will be building, see our git repo for the finished work and our live demo.
Welcome to our comprehensive guide on "Building a Web3 Play-To-Earn Platform with Next.js, Typescript, and Solidity". In this tutorial, we'll build a decentralized Play-To-Earn platform that leverages the power of blockchain technology. You'll gain a clear understanding of the following:
By the end of this guide, you'll have a functioning decentralized platform where users can participate in Play-To-Earn games, with all activities managed and secured by Ethereum smart contracts.
As an added incentive for participating in this tutorial, we're giving away a copy of our prestigious book on becoming an in-demand Solidity developer. This offer is free for the first 300 participants. For instructions on how to claim your copy, please watch the short video below.
You will need the following tools installed to build along with me:
To set up MetaMask for this tutorial, please watch the instructional video below:
Once you have successfully completed the setup, you are eligible to receive a free copy of our book. To claim your book, please fill out the form to submit your proof-of-work.
With that said, let’s jump into the tutorial and set up our project.
We'll start by cloning a prepared frontend repository and setting up the environment variables. Run the following commands:
git clone https://github.com/Daltonic/play2earnX
cd play2earnX
yarn install
git checkout 01_no_redux
Next, create a .env
file at the root of the project and include the following keys:
NEXT_PUBLIC_RPC_URL=http://127.0.0.1:8545
NEXT_PUBLIC_ALCHEMY_ID=<YOUR_ALCHEMY_PROJECT_ID>
NEXT_PUBLIC_PROJECT_ID=<WALLET_CONNECT_PROJECT_ID>
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=somereallysecretsecret
Replace <YOUR_ALCHEMY_PROJECT_ID>
and <WALLET_CONNECT_PROJECT_ID>
with your respective project IDs.
YOUR_ALCHEMY_PROJECT_ID
: Get Key Here
WALLET_CONNECT_PROJECT_ID
: Get Key Here
Finally, run yarn dev
to start the project.
Our user interface is prepared to incorporate smart contracts, however, we still need to integrate Redux in order to facilitate the sharing of data.
The above image represents the structure of our Redux store, it will be simple since we are not creating some overly complex project.
We'll set up Redux to manage our application's global state. Follow these steps:
store
folder at the project root.store
, create two folders: actions
and states
.states
, create a globalStates.ts
file.actions
, create a globalActions.ts
file.globalSlices.ts
file inside the store
folder.index.ts
file inside the store
folder.pages/_app.tsx
file with the Redux provider.We have implemented Redux toolkit in our application and plan to revisit its usage when integrating the backend with the frontend.
Next, we'll develop the smart contract for our platform:
contracts
folder at the project root.contracts
, create a PlayToEarnX.sol
file and add the contract code below.The above smart contract is a Play-To-Earn (P2E) gaming platform, which is created with Solidity. The contract integrates features such as game creation, player invitations, score recording, and payout distribution.
Here's a breakdown of its logic, function by function:
constructor(uint256 _pct) ERC20('Play To Earn', 'P2E')
): This function initializes the contract. It sets the service fee percentage and assigns initial token parameters.Now, let's deploy our smart contract and populate it with some dummy data:
scripts
folder at the project root.scripts
, create a deploy.js
and a seed.js
file and add the following codes.
Deploy Script
Seed
Run the following commands to deploy the contract and seed it with data:
yarn hardhat node # Run in terminal 1 yarn hardhat run scripts/deploy.js # Run in terminal 2 yarn hardhat run scripts/seed.js # Run in terminal 2
If you did that correctly, you should see a similar output like the one below:
At this point we can start the integration of our smart contract to our frontend.
First, create a services
folder at the project root, and inside it, create a blockchain.tsx
file. This file will contain functions to interact with our smart contract.
The above code is a service that interacts with our Play-To-Earn gaming smart contract on the chain. It uses the Ethers.js
library to interact with the Ethereum blockchain for contract interaction.
Here's a detailed breakdown of its functions:
Next, update the provider.tsx
file inside services
to include the bitfinity
network using the following codes.
Next, we'll link the functions in the blockchain service to their respective interfaces in the frontend:
No 1: Displaying Available Games
Update pages/index.tsx
to get data from the getGames()
function.
No 2: Displaying User’s Games
Update pages/games.tsx
to get data from the getMyGames()
function.
Notice how we combined useEffect()
to dispatch the games into the store before rendering on screen.
No 3: Displaying User’s Invitations
Update pages/invitations.tsx
to get data from the getMyInvitations()
function.
We also combined useEffect()
to dispatch the invitations into the store before rendering on screen.
No 4: Displaying Game Invitations
Update pages/invitations/[id].tsx
to use the getServerSideProps()
, getGame()
, and getInvitations()
to retrieve a game invitations by specifying the game Id.
No 5: Showcasing a Gameplay
Update pages/gameplay/[id].tsx
to use the getServerSideProps()
, getGame()
, and getScores()
to retrieve a game players by specifying the game Id.
The script mentioned above is the core of the frontend code for this page. It contains the necessary functions and behaviors to create a memory game where users can compete against each other.
No 6: Displaying Game Result
Update pages/results/[id].tsx
to use the getServerSideProps()
, getGame()
, and getScores()
to retrieve a game score by specifying the game Id.
It's worth mentioning that users can make a payout from this page after the game duration has ended.
Let's apply the same approach we used for the previous pages and update the following components to interact with the smart contract.
No 1: Creating New Games
Update components/CreateGame.tsx
file to use the handleGameCreation()
function to call the createGame()
function for form submission.
No 2: Handling Game Deletion
Update components/GameActions.tsx
file to use the handleDelete()
function to call the deleteGame()
function.
No 3: Displaying Game Details Modal
Update components/GameDetails.tsx
file to use the closeModal()
function to call the setResultModal()
function.
No 4: Inviting Players
Update components/GameInvitation.tsx
file to use the handleResponse()
function to call the respondToInvite()
function.
No 5: Launching Game Details Modal
Update components/GameList.tsx
file to use the openModal()
function to call the setResultModal()
function.
Please note that this setResultModal()
function actually launches the game details modal, that’s a little oversight in function name.
No 6: Launching the Create Game Modal
Update components/Hero.tsx
file to dispatch
the setCreateModal()
function, this will help us launch the create game form modal.
No 7: Launching the Invite Modal
Lastly, update components/InviteModal.tsx
file to use the sendInvitation()
function to call the invitePlayer()
function.
The project is now complete as all components and pages are connected to the smart contract through the implementation of these updates.
If your Next.js server was offline, you can bring it back up by executing the command yarn dev
.
For further learning, we recommends watch the full video of this build on our YouTube channel and visiting our website for additional resources.
In this tutorial, we've successfully built a decentralized Play-To-Earn platform using Next.js, TypeScript, and Solidity. We've established the development environment, constructed the Redux store, and deployed our smart contract to our local chain.
By merging the smart contract with the frontend, we've ensured a seamless gaming experience. This guide has equipped you with the skills to create dynamic interfaces, craft Ethereum smart contracts, manage shared data with Redux, and interact with smart contracts from the frontend.
Now, you're ready to create your own Web3 Play-To-Earn platform. Happy coding!
I am a web3 developer and the founder of Dapp Mentors, a company that helps businesses and individuals build and launch decentralized applications. I have over 7 years of experience in the software industry, and I am passionate about using blockchain technology to create new and innovative applications. I run a YouTube channel called Dapp Mentors where I share tutorials and tips on web3 development, and I regularly post articles online about the latest trends in the blockchain space.
Stay connected with us, join communities on