paint-brush
How to Create a Web3 Events Marketplace with Next.js, Typescript, and Solidityby@daltonic
362 reads
362 reads

How to Create a Web3 Events Marketplace with Next.js, Typescript, and Solidity

by Darlington Gospel February 5th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This module will instruct learners on developing a Ticketing Management System dApp including the use of ERC720 tokens. It includes lessons on creating a smart contract, deployment, interacting with the contract, and integrating a UI.
featured image - How to Create a Web3 Events Marketplace with Next.js, Typescript, and Solidity
Darlington Gospel  HackerNoon profile picture

What you will be building, see our git repo for the finished work and our live demo.


Events Marketplace


Events Marketplace

Introduction

Welcome to our comprehensive guide on "Building a Web3 Events Marketplace with Next.js, TypeScript, and Solidity". In this tutorial, we'll construct a decentralized Events Marketplace that harnesses 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 Events Marketplaces


By the end of this guide, you'll have a functioning decentralized platform where users can list and participate in events, with all transactions 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.


Capturing Smart Contract Development

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:

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.


Watch the following instructional videos to receive up to 3-months of free premium courses on Dapp Mentors Academy, including:



Join the Bitfinity ecosystem and be a part of the next generation of dApps. Apply your Bitfinity knowledge to create a Blockchain-based House Rental dApp in the final module. Deploy your smart contracts to the Bitfinity network and revolutionize the rental industry

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/dappEventX
cd dappEventX
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.


Dummy Data


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

Store Structure

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:

  1. Create a store folder at the project root.
  2. Inside store, create two folders: actions and states.
  3. Inside states, create a globalStates.ts file.

  1. Inside actions, create a globalActions.ts file.

  1. Create a globalSlices.ts file inside the store folder.

  1. Create an index.ts file inside the store folder.

  1. Update the 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.

Smart Contract Development

Next, we'll develop the smart contract for our platform:

  1. Create a contracts folder at the project root.
  2. Inside contracts, create a DappEventX.sol file and add the contract code below.

The above smart contract is designed to manage an Events Marketplace on the blockchain. It allows users to create, update, and delete events, buy tickets for events, and handle payouts. Here's a detailed breakdown of its functions:

  1. Constructor (constructor(uint256 _pct) ERC721('Event X', 'EVX')): This function initializes the contract, sets the service fee percentage, and assigns initial token parameters.
  2. createEvent Function: This function allows a user to create a new event. It requires the title, description, image URL, capacity, ticket cost, and start and end times. It validates all input data and creates a new event structure, which is stored in the events mapping.
  3. updateEvent Function: This function allows a user to update an existing event. It requires the same parameters as the createEvent function. It checks that the event exists and that the caller is the owner of the event before updating the event data.
  4. deleteEvent Function: This function allows the owner of an event to delete it. It checks that the event exists, that the caller is the owner of the event or the contract owner, and that the event has not been paid out or refunded. It then refunds all tickets for the event and marks the event as deleted.
  5. getEvents Function: This function returns all existing events. It creates an array of the right size, then fills it with all events that have not been deleted.
  6. getMyEvents Function: This function returns all events owned by the caller. It creates an array of the right size, then fills it with all events owned by the caller that have not been deleted.
  7. getSingleEvent Function: This function returns a specific event. It simply returns the event structure from the events mapping.
  8. buyTickets Function: This function allows a user to buy tickets for an event. It checks that the event exists, that the sent value is at least the total cost of the tickets, and that there are enough seats available. It then creates new ticket structures for each ticket and adds them to the tickets mapping.
  9. getTickets Function: This function returns all tickets for a specific event. It simply returns the array of ticket structures from the tickets mapping.
  10. refundTickets Function: This internal function refunds all tickets for a specific event. It marks each ticket as refunded, sends the ticket cost back to the ticket owner, and decreases the contract balance.
  11. payout Function: This function handles payout distribution at the end of an event. It checks that the event exists, has not already been paid out, and is no longer ongoing. It then mints tickets for the event, calculates the revenue and fee, and sends the revenue minus the fee to the event owner and the fee to the contract owner. It marks the event as paid out and decreases the contract balance.
  12. mintTickets Function: This internal function mints tickets for a specific event. It marks each ticket and the event as minted, and mints a new ERC721 token for each ticket.
  13. payTo Function: This internal function sends funds to a specified address. It uses the low-level call function to send the funds and checks that the call was successful.
  14. currentTime Function: This internal function returns the current timestamp. It uses the block.timestamp global variable, multiplies it by 1000, and adds 1000.

Contract Deployment and Seeding

Now, let's deploy our smart contract and populate it with some dummy data:

  1. Create a scripts folder at the project root.
  2. Inside scripts, create a deploy.js and a seed.js file and add the following codes.


Deploy Script

Seed

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


Deployment


At this point we can start the integration of our smart contract to our frontend.

Frontend Integration

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 a Events Marketplace contract. The service interacts with a smart contract deployed on the Ethereum blockchain using the ethers.js library.


Here's a detailed breakdown of its functions:

  1. getEthereumContracts Function: 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.
  2. createEvent Function: This function creates a new event by calling the createEvent function of the contract.
  3. updateEvent Function: This function updates an existing event by calling the updateEvent function of the contract.
  4. deleteEvent Function: This function deletes an event by calling the deleteEvent function of the contract.
  5. payout Function: This function handles payout distribution at the end of an event.
  6. buyTicket Function: This function allows a user to buy tickets for an event.
  7. getEvents and getMyEvents Functions: These functions retrieve information about all events or the events owned by the caller. They call the corresponding functions in the contract and structure the returned data.
  8. getEvent Function: This function retrieves information about a specific event. It calls the corresponding function in the contract and structures the returned data.
  9. getTickets Function: This function retrieves information about the tickets for a specific event. It calls the corresponding function in the contract and structures the returned data.
  10. structuredEvent and structuredTicket Functions: 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.


Next, update the provider.tsx file inside services to include the bitfinity network using the following codes.

Page Interacting with Smart Contract

Next, we'll link the functions in the blockchain service to their respective interfaces in the frontend:


No 1: Displaying Available Events Update pages/index.tsx to get data from the getEvents() function.

Take a moment and observe how we implemented the load more button, I bet you’ll find it useful.


No 2: Displaying User’s Events Update pages/events/personal.tsx to get data from the getMyEvents() function.

No 3: Creating New Event Update pages/events/create.tsx to use the createEvent() function for form submission..

No 4: Displaying Single Event Update pages/events/[id].tsx to get data from the getEvent() and getTickets() functions.

No 5: Editing a Single Event Change pages/events/edit/[id].tsx to retrieve data from the getEvent() and update by calling updateEvent().

No 6: Displaying Event Tickets Update pages/events/tickets/[id].tsx to retrieve data from the getTickets() function.

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.


No 1: Purchasing Ticket Update components/BuyTicket.tsx file to use the handleSubmit() function to call the buyTicket() function.

No 2: Deleting and Paying out Events Lastly, update components/EventAction.tsx file to use the deleteEvent() and payout() functions.

It should be noted that this component is a dropdown and contains the relevant actions for managing a specific event.


All the components and pages of the project have been successfully connected to the smart contract, indicating the completion of the project after implementing 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 subscribing to our YouTube channel and visiting our website for additional resources.

Conclusion

In this tutorial, we have successfully built a Web3 Events Marketplace 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.


We've created dynamic interfaces, crafted Ethereum smart contracts, and managed shared data with Redux. By integrating the smart contract with the frontend, we've enabled users to list and participate in events, with transactions managed and secured by Ethereum smart contracts.


Now, you're equipped with the skills to build your own Web3 Events Marketplace. We've also provided you with a live demo and the finished work in our git repo for reference. 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 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 Discord: Join X-Twitter: Follow LinkedIn: Connect GitHub: Explore Website: Visit