How to Create Fractional NFTs: a Step-By-Step Guide
Welcome back to the second part of the article on Fractional NFTs. This part explores the technical details involved in creating a Fractional NFT smart contract. To kick off, make sure you’ve studied the first part of the article which will provide you with a foundational understanding of Fractional NFTs, and their importance in the digital asset space.
Whether you're an experienced developer or a curious enthusiast, this guide aims to provide a clear, step-by-step process for fractionalizing an NFT, offering insights into the future of decentralized asset ownership. This process will guide you through the requirements to make a single NFT divisible into multiple fungible parts.
Prerequisites
Before you proceed with this tutorial, the following are required:
Basic understanding of Blockchain Technology.
Knowledge of Solidity.
Understanding of smart contract development.
Have Metamask installed and test tokens.
Basic knowledge of how to use Remix IDE.
To recap, fractional NFTs are achieved by using fungible tokens (ERC-20) to create fractions of an NFT (ERC-721). In the following steps, we will learn how to fractionalize an NFT. So without further ado, let’s dive in.
Tutorial: Creating a Fractional NFT Smart Contract
1
Create an ERC-20 token smart contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol";
import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
contract FractionalNft is ERC20, Ownable, ERC20Permit, ERC721Holder {
IERC721 public collection;
uint16 public tokenId;
bool public initialized;
bool public forSale;
bool public redeemable;
uint256 public salePrice;
constructor() ERC20("Token Fraction", "TKF") ERC20Permit("Token Fraction") Ownable(msg.sender) {}
function initialize(address _collection, uint16 _tokenId, uint256 _amount) external onlyOwner {
require(!initialized, "Already initialized");
require(_amount > 0, "can't initialize zero amount");
collection = IERC721(_collection);
collection.safeTransferFrom(msg.sender, address(this), _tokenId);
tokenId = _tokenId;
initialized = true;
_mint(msg.sender, _amount);
}
function putForSale(uint256 price) external onlyOwner {
salePrice = price;
forSale = true;
}
function purchase() external payable {
require(forSale, "Not for sale");
require(msg.value >= salePrice, "Not enough ether to purchase");
collection.transferFrom(address(this), msg.sender, tokenId);
forSale = false;
redeemable = true;
}
function redeem(uint256 _amount) external {
require(redeemable, "Redemption not available");
uint256 totalEther = address(this).balance;
uint256 amountToRedeem = _amount * totalEther / totalSupply();
_burn(msg.sender, _amount);
(bool sent, ) = payable(msg.sender).call{value: amountToRedeem}("");
require(sent, "Failed to send Ether");
}
}
Explaining the Important Functions
initialize()
The initialize function is the main function that performs the action of fractionalizing an NFT. It carries out the process by transferring an NFT from the owner to the FractionalNFT smart contract, and then mints an amount of ERC-20 tokens to the NFT owner, which represents the fractions of the NFT that was transferred into the smart contract.
putForSale()
This function enables sale of the NFT. The value of the forSale variable is false by default, which means the NFT is not for sale, when the putForSale function is called, it takes the price the NFT is to be sold for and then sets the forSale variable to true.
purchase()
This function, which is payable, is used to purchase the NFT from the FractionalNFT smart contract. The user initiating this function needs to specify the price of the NFT in ETH during the function call. Importantly, this function can only be executed when the forSale feature is enabled. Upon a successful purchase, the NFT is transferred from the smart contract to the address of the user who made the purchase.
redeem()
The redeem function can only be called when the redeemable variable is set to true, indicating that users who own a fraction of the ERC-20 token that makes up the NFT can redeem the ETH value the NFT was sold for, from the smart contract. When a user calls the redeem function, the contract uses the fraction of token the user has from the total supply, and calculates the amount of ETH to send to the user, then it sends the ETH value to the user’s address.
Now that we have explained the main functions, let’s continue with the tutorial.
After pasting the code snippet inside the FractionalNFT.sol file, click on the Solidity Compiler tab, and click the blue compile button to compile your smart contract. Make sure there is a green tick on the Solidity compiler tab that indicates that your smart contract has compiled successfully.
We have now completed the smart contract for fractionalizing NFT, let’s create the NFT smart contract.
2
Create the NFT (ERC-721) smart contract
3
Deploy FractionalNFT contract
4
Deploy the HooziNFT contract
5
Interaction with the Deployed HooziNFT contract
6
Interaction with the Deployed FractionalNFT (ERC-20) contract
7
Transfer a fraction of the FractionalNFT ERC-20 token to another address
8
Put the NFT for sale
9
Use another address to purchase the NFT
10
Redeem ETH using the FractionalNFT ERC-20 account balance
- Switch account to the second account in the Remix VM.
- Check the ETH balance of the account so that you can track if the balance increased after redeeming the ETH in the FractionalNFT smart contract.
And with that, we've successfully completed the implementation of Fractional NFTs using an ERC-20 token and an ERC-721 token.
Conclusion
This guide has walked us through the details of breaking down an NFT into fractions, blending the features of ERC-20 and ERC-721 tokens to mold a unique digital asset ownership experience. Beyond just boosting NFT liquidity, this approach sets the stage for more inclusive and accessible ways of owning assets in the digital world. As the blockchain scene progresses, Fractional NFTs stand out as a symbol of innovation, opening up fresh opportunities for creators, collectors, and developers.