How to Build a Web3 Events Marketplace with Next.js, Typescript, and Solidity
Tags: dapps, web3, nextjs, solidity
Published: 2024-02-04 14:52:41

What you will be building, see our [git repo](https://github.com/Daltonic/dappEventX) for the finished work and our [live demo](https://dapp-event-x.vercel.app/).


![Events Marketplace](https://github.com/Daltonic/dappEventX/raw/master/screenshots/0.png)

![Events Marketplace](https://github.com/Daltonic/dappEventX/raw/master/screenshots/1.png)

## 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](https://paper-attachments.dropboxusercontent.com/s_70534477D8955F3500A3FBCD4F334C4E7B1BEDE321CB5F5FC0792B12F0A26F99_1702985122073_tier.png)

## 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:


{% embed https://www.youtube.com/watch?v=qV1mbFOtkxo %}

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](https://forms.gle/uGtm3ZcEHb5pqd3YA).

Watch the following [instructional videos](https://www.youtube.com/playlist?list=PLUDcVqFK2t-C36KaTMzV3y1wu8Hf-qyAI) to receive up to 3-months of free premium courses on Dapp Mentors Academy, including:


- [Fullstack NFT Minting Course](https://dappmentors.org/courses/details/fullstack-nft-minting-course)
- [Fullstack NFT Marketplace Course](https://dappmentors.org/courses/details/fullstack-nft-marketplace-course)
- [Fullstack Blockchain Course](https://dappmentors.org/courses/details/fullstack-blockchain-101-course)
- etc.

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**](https://bitfinity.network/)

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](https://dashboard.alchemy.com/) 
`WALLET_CONNECT_PROJECT_ID`: [Get Key Here](https://cloud.walletconnect.com/sign-in)

Finally, run `yarn dev` to start the project.

![Dummy Data](https://paper-attachments.dropboxusercontent.com/s_BA29916586F6A7B67F0B4DA94E48E8141D23FDFFE55BA1A3EDFA7DCA6103F4B3_1703267988083_Screenshot+2023-12-22+at+6.59.42+PM.png)


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](https://paper-attachments.dropboxusercontent.com/s_70534477D8955F3500A3FBCD4F334C4E7B1BEDE321CB5F5FC0792B12F0A26F99_1702989191922_Screenshot+2023-12-19+at+1.33.05+PM.png)


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.


{% gist https://gist.github.com/Daltonic/b8fbcc4d0594d807c8da8c05455e9280 %}



4. Inside `actions`, create a `globalActions.ts` file.


{% gist https://gist.github.com/Daltonic/a0180bc1ca72eff8b3bd93106b2ce47f %}



5. Create a `globalSlices.ts` file inside the `store` folder.


{% gist https://gist.github.com/Daltonic/31834ef324af3ead415c0cebcf1a5c7e %}



6. Create an `index.ts` file inside the `store` folder.


{% gist https://gist.github.com/Daltonic/4e91a72da751a7122b10722e2a2282c3 %}



7. Update the `pages/_app.tsx` file with the Redux provider.


{% gist https://gist.github.com/Daltonic/55aa1f2995b360b42752a7c2624011de %}


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.


{% gist https://gist.github.com/Daltonic/f66b58a3f938d4d7b41da56c3c2d22be %}


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

{% gist https://gist.github.com/Daltonic/021ee2f4d05f4f7c1e80452e6fdd263f %}


**Seed**

{% gist https://gist.github.com/Daltonic/118946bc263220b15162d36837de2758 %}

3. 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](https://paper-attachments.dropboxusercontent.com/s_70534477D8955F3500A3FBCD4F334C4E7B1BEDE321CB5F5FC0792B12F0A26F99_1702992266380_Screenshot+2023-12-18+at+10.01.28+AM.png)


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


{% embed https://www.youtube.com/watch?v=JePa-DzZsJA %}

## 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.


{% gist https://gist.github.com/Daltonic/4a6faf80873bc01162436ca6b8ddf703 %}


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.


{% gist https://gist.github.com/Daltonic/94b744897ef0914a11aa9fa7e74c6b99 %}



## 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.


{% gist https://gist.github.com/Daltonic/c7c6be46e11cbf145cdc86dbbced70bb %}


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.


{% gist https://gist.github.com/Daltonic/363e75afca92a660ec3992c271d56c92 %}


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


{% gist https://gist.github.com/Daltonic/4cc8440375151be40f6526b23445de1a %}


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


{% gist https://gist.github.com/Daltonic/7e00b943e92bf6bdf07893891db89de5 %}


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


{% gist https://gist.github.com/Daltonic/9c3897daf46d0ad92b67d7607cc6ef3b %}



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


{% gist https://gist.github.com/Daltonic/ad1f5468ac8ae44d44279e83e82abcc5 %}



## 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.


{% gist https://gist.github.com/Daltonic/7ef4354e4bfbfde36a5e04e40c85e083 %}


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


{% gist https://gist.github.com/Daltonic/885854d5d5b71a2d7434fd738fea3eed %}


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**](https://www.youtube.com/@dappmentors?sub_confirmation=1) and [**visiting our website for additional resources**](https://dappmentors.org/). 


{% embed https://www.youtube.com/watch?v=Ubom39y5jX8 %}



## 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](https://dappmentors.org/), 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**](https://www.youtube.com/@dappmentors?sub_confirmation=1) 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](https://discord.gg/PgFDUVT6n9)
X-Twitter: [Follow](https://twitter.com/iDaltonic)
LinkedIn: [Connect](https://www.linkedin.com/company/dapp-mentors)
GitHub: [Explore](https://github.com/Daltonic)
Website: [Visit](https://dappmentors.org/)