What you will be building, see our for the finished work and our . git repo live demo Introduction 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: Building dynamic interfaces with Next.js Crafting Ethereum smart contracts with Solidity Incorporating static type checking using TypeScript Deploying and interacting with your smart contracts Understanding the fundamentals of blockchain-based Play-To-Earn platforms 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. Prerequisites You will need the following tools installed to build along with me: Node.js Yarn Git Bash MetaMask Next.js Solidity Redux Toolkit Tailwind CSS To set up MetaMask for this tutorial, please watch the instructional video below: https://www.youtube.com/watch?v=qV1mbFOtkxo&embedable=true 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. Setup 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 file at the root of the project and include the following keys: .env 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 and with your respective project IDs. <YOUR_ALCHEMY_PROJECT_ID> <WALLET_CONNECT_PROJECT_ID> : YOUR_ALCHEMY_PROJECT_ID Get Key Here : WALLET_CONNECT_PROJECT_ID Get Key Here Finally, run to start the project. yarn dev Our user interface is prepared to incorporate smart contracts, however, we still need to integrate Redux in order to facilitate the sharing of data. Building the Redux Store 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: Create a folder at the project root. store Inside , create two folders: and . store actions states Inside , create a file. states globalStates.ts https://gist.github.com/Daltonic/f542e5769c9f9f92009c0ab97e26ffe0?embedable=true Inside , create a file. actions globalActions.ts https://gist.github.com/Daltonic/9d5487e2e3f2517b36a1a38805e5c989?embedable=true Create a file inside the folder. globalSlices.ts store https://gist.github.com/Daltonic/0af23b96f972b0f3a934c4a26b90286d?embedable=true Create an file inside the folder. index.ts store https://gist.github.com/Daltonic/7b7ec88441e0b03b3c11ada05aa49104?embedable=true Update the file with the Redux provider. pages/_app.tsx https://gist.github.com/Daltonic/b13c27143cfd13d5e2949a8e530f160d?embedable=true We have implemented Redux toolkit in our application and plan to revisit its usage when integrating the backend with the frontend. Smart Contract Development Next, we'll develop the smart contract for our platform: Create a folder at the project root. contracts Inside , create a file and add the contract code below. contracts PlayToEarnX.sol https://gist.github.com/Daltonic/4543f76e6a0731a8795f618248d663da?embedable=true 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: This function initializes the contract. It sets the service fee percentage and assigns initial token parameters. Constructor ( constructor(uint256 _pct) ERC20('Play To Earn', 'P2E') ): This function allows a user to create a new game, specifying the title, description, number of participants, number of winners, start date, and end date. It requires a stake greater than zero and validates all input data. A new game and score structure are created and stored in their respective mappings. createGame Function: This function allows the owner of a game to delete it, provided no participants have joined. It refunds the stake to the game owner and marks the game as deleted. deleteGame Function: This function is an internal function that creates a new player for a game. It increments the total players count, adds the new player to the players mapping, and increases the game's acceptees count. playedSaved Function: This function allows a user to invite another player to a game. It checks if the game exists and if there is room for more players. An invitation is then created and added to the invitationsOf mapping. invitePlayer Function: This function allows a user to accept an invitation to a game, provided they send enough funds to cover the game's stake. It creates a new player for the game and updates the invitation status. acceptInvitation Function: This function allows a user to reject an invitation. rejectInvitation Function: This function handles the payout distribution at the end of a game. It checks if the game has ended and if it has not already been paid out. It calculates the total stakes, platform fee, creator fee, and game revenue. It pays out the platform fee to the contract owner, the creator fee to the game owner, and distributes the game revenue among the winners. It also mints new tokens for the winners. payout Function: This function sorts players based on their scores. It uses a simple bubble sort algorithm with a twist: it also takes into account whether a player has played the game or not. Players who have not played are sorted to the bottom. sortScores Function: This function allows a player to save their score for a game. It validates the game and player, then updates the player's score. saveScore Function: This function allows the contract owner to change the service fee percentage. setFeePercent Function: These functions return lists of games based on different criteria (all games, games that have been paid out, and games owned by the caller). getGames, getPaidOutGames, getMyGames Functions: This function returns the details of a specific game. getGame Function: This function returns all invitations for a specific game. getInvitations Function: This function returns all invitations received by the caller. getMyInvitations Function: This function returns all scores for a specific game. getScores Function: This internal function sends funds to a specified address. payTo Function: This internal function returns the current timestamp. currentTime Function: Contract Deployment and Seeding Now, let's deploy our smart contract and populate it with some dummy data: Create a folder at the project root. scripts Inside , create a and a file and add the following codes. scripts deploy.js seed.js Deploy Script https://gist.github.com/Daltonic/de4cb519cefa6baa8d3b2d37b24c6afd?embedable=true Seed https://gist.github.com/Daltonic/723ba48f1f3c7fdac38225afc5eb3466?embedable=true 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. https://www.youtube.com/watch?v=fJIiqeevqoU&embedable=true Frontend Integration First, create a folder at the project root, and inside it, create a file. This file will contain functions to interact with our smart contract. services blockchain.tsx https://gist.github.com/Daltonic/1a1fc8d33d508ea39a8bdc640a3072ec?embedable=true The above code is a service that interacts with our Play-To-Earn gaming smart contract on the chain. It uses the library to interact with the Ethereum blockchain for contract interaction. Ethers.js Here's a detailed breakdown of its functions: This function sets up a connection to the Ethereum blockchain and the smart contract. It uses the ethers library to create a provider (to interact with the Ethereum blockchain) and a signer (to sign transactions). Then, it creates a contract instance that can be used to interact with the smart contract. getEthereumContracts Function: This function retrieves the owner of the contract. It calls the owner function of the contract, which is defined in the OpenZeppelin's Ownable contract. getOwner Function: These functions retrieve information about the games. They call the corresponding functions in the contract and structure the returned data. getGames, getMyGames, getGame Functions: These functions retrieve information about the game invitations. They call the corresponding functions in the contract and structure the returned data. getInvitations, getMyInvitations Functions: This function retrieves scores of a game. It calls the corresponding function in the contract and structures the returned data. getScores Function: This function creates a new game by calling the createGame function of the contract. It checks if the browser provider is installed and handles any errors that might occur. createGame Function: This function deletes a game by calling the deleteGame function of the contract. It checks if the browser provider is installed and handles any errors that might occur. deleteGame Function: This function invites a player to a game by calling the invitePlayer function of the contract. It checks if the browser provider is installed and handles any errors that might occur. invitePlayer Function: This function saves a player's score by calling the saveScore function of the contract. It checks if the browser provider is installed and handles any errors that might occur. saveScore Function: This function handles payout distribution by calling the payout function of the contract. It checks if the browser provider is installed and handles any errors that might occur. payout Function: This function allows a user to respond to an invitation by calling either the acceptInvitation or rejectInvitation function of the contract, based on the user's response. It checks if the browser provider is installed and handles any errors that might occur. respondToInvite Function: These functions structure the data returned from the contract into a more manageable format. They convert the data types from the contract's format to JavaScript's format and sort the data. structuredGames, structuredInvitations, structuredScores Functions: Next, update the file inside to include the network using the following codes. provider.tsx services bitfinity https://gist.github.com/Daltonic/5cfe4e97fe6d258a256f76c46cb7beaa?embedable=true Page Interacting with Smart Contract Next, we'll link the functions in the blockchain service to their respective interfaces in the frontend: Update to get data from the function. No 1: Displaying Available Games pages/index.tsx getGames() https://gist.github.com/Daltonic/c275c2e007ad1d8ca56c2b5adc5f21c3?embedable=true Update to get data from the function. No 2: Displaying User’s Games pages/games.tsx getMyGames() https://gist.github.com/Daltonic/c0226ed85d75fb5c1ea39909ab692b05?embedable=true Notice how we combined to dispatch the games into the store before rendering on screen. useEffect() Update to get data from the function. No 3: Displaying User’s Invitations pages/invitations.tsx getMyInvitations() https://gist.github.com/Daltonic/d10d5dca4d8c8d7423345bbd39e83cbc?embedable=true We also combined to dispatch the invitations into the store before rendering on screen. useEffect() Update to use the , , and to retrieve a game invitations by specifying the game Id. No 4: Displaying Game Invitations pages/invitations/[id].tsx getServerSideProps() getGame() getInvitations() https://gist.github.com/Daltonic/232c8a64779ababf873a3e94e609b11d?embedable=true Update to use the , , and to retrieve a game players by specifying the game Id. No 5: Showcasing a Gameplay pages/gameplay/[id].tsx getServerSideProps() getGame() getScores() https://gist.github.com/Daltonic/fb73e1ed6f11043a5e63a6df903392ed?embedable=true 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. Update to use the , , and to retrieve a game score by specifying the game Id. No 6: Displaying Game Result pages/results/[id].tsx getServerSideProps() getGame() getScores() https://gist.github.com/Daltonic/e448bc7e13356314d7e7169dfd5d43ad?embedable=true It's worth mentioning that users can make a payout from this page after the game duration has ended. Components with Smart Contract Let's apply the same approach we used for the previous pages and update the following components to interact with the smart contract. Update file to use the function to call the function for form submission. No 1: Creating New Games components/CreateGame.tsx handleGameCreation() createGame() https://gist.github.com/Daltonic/6106948a6b3d49896ec1bb6847b51bad?embedable=true Update file to use the function to call the function. No 2: Handling Game Deletion components/GameActions.tsx handleDelete() deleteGame() https://gist.github.com/Daltonic/593b697b274e0fab608801bbc7e5515f?embedable=true Update file to use the function to call the function. No 3: Displaying Game Details Modal components/GameDetails.tsx closeModal() setResultModal() https://gist.github.com/Daltonic/f89025c44a207a710cf04ac79515d9dd?embedable=true Update file to use the function to call the function. No 4: Inviting Players components/GameInvitation.tsx handleResponse() respondToInvite() https://gist.github.com/Daltonic/4405f7b64b34177e6be707d29301ab25?embedable=true Update file to use the function to call the function. No 5: Launching Game Details Modal components/GameList.tsx openModal() setResultModal() https://gist.github.com/Daltonic/877971532ffa1a1e364bc1fb8ba03234?embedable=true Please note that this function actually launches the game details modal, that’s a little oversight in function name. setResultModal() Update file to the function, this will help us launch the create game form modal. No 6: Launching the Create Game Modal components/Hero.tsx dispatch setCreateModal() https://gist.github.com/Daltonic/cb1c9ee1024f9219a9813fc261d4befd?embedable=true Lastly, update file to use the function to call the function. No 7: Launching the Invite Modal components/InviteModal.tsx sendInvitation() invitePlayer() https://gist.github.com/Daltonic/6ca046aef2762c32a7c382b9c648d29f?embedable=true 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 and . YouTube channel visiting our website for additional resources https://www.youtube.com/watch?v=zi_6RWkOB1w&embedable=true Conclusion 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! About Author 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 where I share tutorials and tips on web3 development, and I regularly post articles online about the latest trends in the blockchain space. YouTube channel called Dapp Mentors Stay connected with us, join communities on Discord: Join X-Twitter: Follow LinkedIn: Connect GitHub: Explore Website: Visit