Integrating FT Payments into an NFT Marketplace
In this tutorial, you'll learn the basics of how an NFT marketplace contract works and how you can modify it to allow for purchasing NFTs using Fungible Tokens. In the previous tutorials, you went through and created a fully fledged FT contract that incorporates all the standards found in the FT standard.
Introductionโ
Throughout this tutorial, you'll learn how a marketplace contract could work on NEAR. This is meant to be an example and there is no canonical implementation. Feel free to branch off and modify this contract to meet your specific needs.
Using the same repository as the previous tutorials, if you visit the market-contract
directory, you should have the necessary files to complete the tutorial.
File structureโ
This directory contains both the build script, dependencies as well as the actual contract code as outlined below.
market-contract
โโโ Cargo.lock
โโโ Cargo.toml
โโโ README.md
โโโ build.sh
โโโ src
โโโ external.rs
โโโ ft_balances.rs
โโโ internal.rs
โโโ lib.rs
โโโ nft_callbacks.rs
โโโ sale.rs
โโโ sale_views.rs
Let's start by building both the finished FT contract and the marketplace contract. Make sure you're in the root directory and run the following command in your terminal:
yarn build && cd market-contract && ./build.sh && cd ..
This will install the dependencies for the marketplace contract as well as the FT contract. In addition, it will compile them to wasm
such that your ft-tutorial/out
directory looks like this:
ft-tutorial
โโโ out
โโโ contract.wasm
โโโ nft-contract.wasm
โโโ market.wasm
Note that there's also a pre-build nft contract wasm file in the directory which you'll use to place the NFTs for sale.
Understanding the contractโ
The marketplace contract used in this tutorial is a modified version of the contract created at the end of the NFT zero to hero tutorial. If you'd like to learn about the backbone of how the NFTs are put for sale and the process by which they are minted / sold, check out the NFT zero to hero tutorial.
The core functionalities are the same in the sense that both this contract and the marketplace contract built in the NFT zero to hero have the following features:
- Users can put NFTs for sale and specify sale conditions
- Users must pay for storage deposit to put NFTs for sale and they can withdraw the storage at any time
- Users can update the price of an NFT or remove the sale entirely
- Buyers can purchase the NFTs by calling
offer
.
The only difference is that this marketplace has removed the ability to purchase NFTs for $NEAR
and instead allows users to buy them with Fungible Tokens. The fungible token is specified when the contract is initialized and only 1 type of fungible token can be used to purchase NFTs. You can't, for example, offer 100 Team Tokens for an NFT and 5000 Mike Tokens for another.
In addition, the marketplace does not support royalties. This is because FT transfers are less Gas efficient than regular $NEAR transfers. In addition, each user would have to be registered and it's much easier to say "hey seller, make sure you're registered before your NFT is sold" rather than enforcing that the seller and all accounts in the payout object are registered. When an NFT is sold, the entire price is sent directly to the seller.
Purchasing Flowโ
In order to purchase an NFT, the contract utilizes the "transfer and call" workflow that the FT contract provides. The marketplace contract implements the ft_on_transfer
method that is called whenever someone transfers FTs to the marketplace contract.
The marketplace keeps track of a balance for each user that outlines how many FTs they've sent to the contract. Each time ft_on_transfer
is called, the marketplace contract will update the balance for the user. When that user wishes to purchase an NFT, they call offer
and pass in the amount of tokens they're willing to spend. The marketplace will then decrement from their balance and transfer the NFT to the buyer / send the FTs to the seller.
It's important to note that the seller must be registered in the FT contract before a sale is made otherwise the ft_transfer
method will panic and the seller won't receive any tokens.
Looking at the Codeโ
Most of the code is the same as what's been outlined in the NFT zero to hero tutorial but we'll go through a refresher in case you're new or have forgotten some of the details.