Web3 Beginner Series: Implementing an NFT DEX from Scratch

avatar
ODAILY
05-20
This article is machine translated
Show original

Users who have read the previous articles should know that for for ERC-20 protocol tokens, we can trade through decentralized exchanges like Uniswap. So for the ERC-721 protocol, which is Non-Fungible Token (NFT), how can we achieve decentralized trading?p p, mainstream Non-Fungible Token trading platforms adopt an order book method for trading, similar to displaying items on supermarket where purchase if they the price suitable.

<

This article will implement decentralized Non-Fungible Token trading by writing a smart contract and a simple frontend page.. Note that this article is for learning purposes only and cannot be used in a real production environment.

<>-Token (NFT)Due to the characteristics of NonT-fung, it cannot set prices through price curves like ERC-20 - because each Token is different. So, the currently common trading method is through order books.

The order book mode simply means that the price of goods is manually set, different from Uniswap's method of calculating prices through price curvesentralcurves. Order books generally have two trading modes: one is a price order, where the seller sets a selling price in their mind, and if a buyer finds the price suitable,,, purchase The purchase order, where buyers issue a purchase purchase on needs, and when the seller finds the suitable, they can sell.

>, purchase order price will be lower than the fixed price order. This article will only introduce the first fixed pricing method.

A Non-Fungible Token DEX's basic functions should include the following basic functions:

  1. Listing goods: List a Non-Fungible Token at a fixed price

  2. Purchasing goods: Purchase according to the Non-Fungible Token's fixed price

  3. DEX fees: Collect fees proportionally based

(The continues in professional and accurate manner maintaining the technical terminology and structure of the original text.)

After obtaining the user's Non-Fungible Token list, it is necessary to determine whether it has been listed or not. Unlisted Non-Fungible Tokens can be listed, while listed ones can be delisted. The determination method is to retrieve the user's listed Non-Fungible Tokens through the "getSellerListings" method in the DEX contract, and then judge whether it is currently listed based on the "isAlive" field of these Non-Fungible Tokens.

When listing, the "listNFT" contract method needs to be called, and when canceling, the "cancelListing" method needs to be called. Before listing, an additional authorization method of the Non-Fungible Token needs to be called to authorize the Non-Fungible Token to the contract, so that the Non-Fungible Token can be automatically transferred to the buyer after the subsequent transaction is completed.

3.4. Buy

In Buy, you can purchase Non-Fungible Tokens

First, we need to display the already listed Non-Fungible Tokens. Similar to the display of user-owned Non-Fungible Tokens in Portfolio, the difference here is that it is global, no longer for a specific user, and only Non-Fungible Tokens with "isAlive" need to be displayed.

When purchasing, use the "purchaseNFT" method, and when calling this method, ETH needs to be used to pay the selling price.

The "value" here is the ETH that the buyer needs to pay.

Thus, a DEX frontend page with all basic capabilities is completed, which can be deployed on Vercel.

This article was written by Yeezo from the ZAN Team (X account @zan_team) (X account @GaoYeezo 75065).

Source
Disclaimer: The content above is only the author's opinion which does not represent any position of Followin, and is not intended as, and shall not be understood or construed as, investment advice from Followin.
Like
Add to Favorites
Comments