NFT Decentralized Exchange Development Practice: From smart contracts to front-end implementation

robot
Abstract generation in progress

Build an NFT DEX from scratch

How can decentralization be achieved in trading NFTs based on the ERC-721 protocol? Currently, mainstream NFT exchanges mostly use a listing method for trading, similar to displaying products on supermarket shelves, where buyers can purchase if they find the price suitable. This article will implement a basic NFT decentralization trading function by writing smart contracts and a simple front-end page.

NFT Introduction

NFT(Non-Fungible Token) refers to non-fungible tokens, where each Token is unique and follows the ERC-721 protocol. Typically, each NFT displays different images in the wallet and has a unique ID for differentiation. Due to the characteristics of NFTs, prices cannot be set through price curves like ERC-20 tokens, so the common trading method is in the form of an order book.

Web3 Beginner Series: Implementing an NFT DEX from Scratch

Order Book Trading Model

The order book model is mainly divided into two types:

  1. Pricing Order: The seller sets the selling price, and the buyer can purchase if they find it suitable.
  2. Purchase Order: The buyer issues a purchase order, and the seller can sell if they find the price acceptable.

Generally, the purchase order price will be lower than the pricing order. This article mainly introduces the pricing order model.

Web3 Beginner Series: Build an NFT DEX from Scratch

NFT exchange basic functions

A basic NFT exchange should include the following functions:

  1. List Product: Put the NFT on sale at the set price.
  2. Purchase Goods: Buy at NFT Pricing
  3. Collecting fees: Charged as a percentage of the transaction price

Product Listing Process

  1. Frontend: Users select NFT and set the price, then click to list.
  2. Contract: Users authorize contract operations for NFTs

Purchase Process

  1. Frontend: The user selects the NFT and clicks to purchase.
  2. Contract: Transfer buyer's funds to seller, NFT to buyer

Web3 Beginner Series: Implementing an NFT DEX from Scratch

Implement NFT exchange

1. Create test NFT

You can quickly deploy an ERC-721 protocol NFT contract for testing using Remix.

Web3 Beginner Series: Building an NFT DEX from Scratch

2. Write Smart Contracts

The contract mainly includes the following methods:

2.1 Seller Lists NFT

Process:

  1. The user selects NFT
  2. Set Price
  3. Authorize NFT to the contract
  4. Call the listing method

Listing method execution:

  1. Verify NFT ownership
  2. Add listing record
  3. Trigger listing event

Web3 Beginner Series: Build an NFT DEX from Scratch

2.2 Buyer purchases NFT

Contract execution:

  1. Read NFT data
  2. Calculate and deduct the handling fee
  3. Transfer NFT to the buyer
  4. Trigger purchase event

Web3 Beginner Series: Build an NFT DEX from Scratch

2.3 Cancel Listing

Set the isActive field of the listed records to false.

Web3 Beginner Series: Build an NFT DEX from Scratch

2.4 Withdrawal Fee

Withdraw the accumulated fees from the contract.

Web3 Beginner Series: Build an NFT DEX from Scratch

3. Develop Frontend Interface

Main tools used:

  • Ant Design Web3: Connect wallet and display NFT
  • Wagmi: Interact with wallet
  • Next.js + Vercel: Deploy the project

The front end consists of 3 pages:

  • Mint: Create test NFT
  • Buy:NFT exchange
  • Portfolio: Manage User NFT

Web3 Beginner Series: Build an NFT DEX from Scratch

3.1 Connect Wallet

Implemented using Ant Design Web3 connection components.

Web3 Beginner Series: Building an NFT DEX from Scratch

3.2 Mint Page

Use the useWriteContract method of wagmi to call the mint method of the NFT contract.

Web3 Beginner Series: Implementing an NFT DEX from Scratch

3.3 Portfolio Page

Display the NFTs owned by users, supporting the listing and delisting operations.

Web3 Beginner Series: Build an NFT DEX from Scratch

3.4 Buy Page

Display listed NFTs, support purchase operations.

Web3 Beginner Series: Implementing an NFT DEX from Scratch

Through the above steps, a basic NFT DEX has been established. The front end can be deployed to Vercel for access and use.

MINT-5.37%
View Original
This page may contain third-party content, which is provided for information purposes only (not representations/warranties) and should not be considered as an endorsement of its views by Gate, nor as financial or professional advice. See Disclaimer for details.
  • Reward
  • 6
  • Share
Comment
0/400
IntrovertMetaversevip
· 4h ago
Buying groceries at the supermarket is probably easier than this.
View OriginalReply0
DaisyUnicornvip
· 4h ago
Oh, the little flower of the protocol is even more magical than the supermarket display~
View OriginalReply0
GateUser-a180694bvip
· 4h ago
Looking forward to this dex changing the pain points of NFT trading.
View OriginalReply0
DecentralizedEldervip
· 4h ago
The market is open, guys come and check it out.
View OriginalReply0
ThatsNotARugPullvip
· 4h ago
The smart contracts part is really troublesome.
View OriginalReply0
CascadingDipBuyervip
· 4h ago
open orders buying and selling is so low get liquidated
View OriginalReply0
Trade Crypto Anywhere Anytime
qrCode
Scan to download Gate app
Community
English
  • 简体中文
  • English
  • Tiếng Việt
  • 繁體中文
  • Español
  • Русский
  • Français (Afrique)
  • Português (Portugal)
  • Bahasa Indonesia
  • 日本語
  • بالعربية
  • Українська
  • Português (Brasil)