This page may contain legacy content
To get our most up-to-date content please access our documentation
January 27, 2022

ERC721 Contract – Exploring ERC721 Smart Contracts

Table of Contents

It’s nearly impossible to talk about crypto and Web3 without talking about tokens. Moreover, tokens are generally divided into two separate categories: fungible and non-fungible tokens (NFTs). Both fungible tokens and NFTs have tremendous potential and are essential elements of the current Web3 ecosystem. Therefore, as powerful features, it’s beneficial to have a regulating standard making these tokens universally compatible. The most popular regulatory standard for fungible tokens is Ethereum’s ERC20 standard, and for NFTs, it is ERC721. In this article, we will direct our attention towards NFTs, meaning that we’ll take a closer look at the ERC721 contract standard. In addition, along with exploring ERC721 smart contracts, we will briefly cover NFTs and the necessary steps to create ERC721 tokens with Moralis

Moralis is the best operating system for Web3 development, and as a user, you will be able to create both NFTs and fungible tokens with ease. In fact, with Moralis, it is possible to reduce the average development time for all our future blockchain projects by approximately 87%! 

The possibility to save such a vast amount of time partly originates from the already developed backend infrastructure that Moralis provides to all users. This, along with development tools such as Moralis Speedy Nodes, the NFT API, Price API, Web3UI kit, and many more, makes Web3 development significantly more accessible. 

So, if you are looking to become a better blockchain developer, the next step in your journey is to join Moralis. Creating an account is entirely free, and it will provide immediate access to all the tools of the platform.

What is an ERC721 Contract?

An ERC721 contract is essentially a smart contract that implements the ERC721 standard. As such, the real question that we need to answer to understand ERC721 contracts is “what is the ERC721 standard?”. 

ERC721, which stands for “Ethereum Request for Comments 721”, introduces a standard for NFTs into the Ethereum ecosystem. The standard ensures that tokens originating from an ERC721 contract implement a minimum interface. As such, it makes sure that all tokens are transferable between accounts, that it’s possible to fetch token balances, check the total supply of a token, etc. Furthermore, to be called an ERC721 contract, it must implement the following events and methods:

Events:

    event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
    event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);
    event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);

Methods

    function balanceOf(address _owner) external view returns (uint256);
    function ownerOf(uint256 _tokenId) external view returns (address);
    function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;
    function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;
    function transferFrom(address _from, address _to, uint256 _tokenId) external payable;
    function approve(address _approved, uint256 _tokenId) external payable;
    function setApprovalForAll(address _operator, bool _approved) external;
    function getApproved(uint256 _tokenId) external view returns (address);
    function isApprovedForAll(address _owner, address _operator) external view returns (bool);

The main purpose of ERC721, and other standards alike, is to create a foundation for tokens by providing this minimum standard interface. However, the standard doesn’t restrict adding additional layers of functionalities on top of the original interface. As such, this regulatory ERC721 contract standard brings some stability and uniformity to NFTs.

Even though the standard originates from Ethereum, it isn’t universally limited to this blockchain. In fact, chains that utilize EVM (Ethereum Virtual Machine) are also generally compatible with ERC721 contracts. So, if you are aiming to learn more about the ERC721 standard, you won’t be limiting yourself to the Ethereum network.

However, to make further sense of the ERC721 contract standard, we will in the following section explain what an NFT is and why they have exploded in popularity in the past years.

What are NFTs?

In today’s day and age, odds are you’ve heard of NFTs. However, if you’re still in the dark, don’t worry; we got you. Thus, in this section, we’ll explain what an NFT is and provide some insight into why these tokens have become one of the hottest trends within the crypto industry. 

The term ”fungible” originates from traditional economics and describes assets that aren’t unique. As such, this suggests that fungible assets have the same value and properties. An example here is currency; one dollar is worth the same as another and shares the same characteristics. 

On the other side of the spectrum, we find ”non-fungible” assets that instead have unique characteristics. An example from traditional economics would be property. Almost all houses are unique, making it impossible to exchange one for another without compensation. As such, this suggests that NFTs are entirely unique tokens. Due to their uniqueness, NFTs are ideal for representing non-fungible assets. 

However, a key takeaway from this is that NFTs solely represent ownership of an asset. To illustrate this point, we can use digital art as an example. An NFT doesn’t generally contain a particular art piece; however, it does point to something specific, like an image. This is comparable to a house deed. The deed itself doesn’t contain property but references a particular house and its owner. 

Nonetheless, if you want to learn more about NFTs, please read the following article explaining the tokens in further detail: ”What are NFTs?”. In addition, if you have further interest in NFTs, we also recommend checking out the tutorial on how to lazy mint NFTs

What are Smart Contracts?

Essentially, smart contracts are codified agreements between two parties. Therefore, smart contracts don’t differ all that much from traditional contracts; however, the crucial difference is that code on a blockchain regulates the terms and conditions rather than a third-party actor. Moreover, the most popular chain for dApp development is Ethereum. Thus, we will be using Ethereum contracts as an example to make this explanation more understandable. 

Ethereum contracts consist of two components: data and code. The data is considered the state of the contract; meanwhile, the code is a collection of functions. All these contracts live on-chain on specific addresses. Along with this, they are also a type of account. As they are accounts, each contract has the potential to make transactions and hold funds. However, smart contracts are regulated by code instead of being managed by a user.

Smart contracts are generally written in the programming language of Solidity. So, if you’d like to get into blockchain development, it is beneficial to somewhat grasp this language. Moreover, if you are interested, you can read more about the best languages for blockchain development here at Moralis. 

Also, if you’d like a comprehensive guide on creating smart contracts, feel free to take a closer look at the tutorial on how to create smart contracts

Exploring an ERC721 Contract 

This section will look closer at an ERC721 contract and how it is constructed. Furthermore, the ERC721 contract standard can be quite complex, or at least in comparison to ERC20. The complexity originates from the fact that the standard has multiple optional extensions and is split across numerous contracts. Moreover, there is also a significant degree of flexibility regarding how we can combine these. 

We will now take a closer look at a simple ERC721 token contract that we can use to track game items. Without further ado, this is what an ERC721 contract might look like: 

// contracts/GameItem.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract GameItem is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() public ERC721("GameItem", "ITM") {}

    function awardItem(address player, string memory tokenURI)
        public
        returns (uint256)
    {
        _tokenIds.increment();

        uint256 newItemId = _tokenIds.current();
        _mint(player, newItemId);
        _setTokenURI(newItemId, tokenURI);

        return newItemId;
    }
}

First of all, the ERC721 contract specifies which version of Solidity should be used to compile the contract. This is done in the first line of the code above, and it currently specifies that all versions newer than ”0.6.0” are compatible. The code continues with a few imports. The most important one is ”ERC721”, which includes all standard extensions.

After this, we have the contract itself, and in the case from above, it is called ”GameItem”. Following this, there is a simple constructor and a function for awarding the item to players. Whenever the function is called, it mints a token for the item and sends it to the player. However, this is just a brief overview of what an ERC721 contract might look like. There is a lot more to discover on your own, and you can adapt the contract to fit your needs. 

How to Create an ERC721 Token

Since you now have a better understanding of what an ERC721 contract is and what the standard entails, we will provide you with a short overview of the vital steps in creating ERC721 tokens or NFTs. This will outline the process of creating an NFT minting dApp that can be used to continuously mint tokens. As such, these are the necessary steps: 

  1. Initializing Moralis and acquiring an ERC721 smart contract.
  2. Structuring the dApps’ content with HTML.
  3. Adding a login function.
  4. Creating an upload function.
  5. Creating a mint function.

If you haven’t already, the first step is to create a Moralis account, as this is crucial in making the application work. After this, we must get a smart contract that suits our needs. Depending on your preferences, there might already be contracts available for you to utilize, suggesting that you don’t need to create a contract from scratch. 

Following this, we also need to structure the contents of the dApp using HTML code. However, how you want to structure the content is up to you as a developer because you decide how you want to design the frontend of your dApp. 

The last three steps revolve around the implementation of three essential functions. However, as this isn’t a tutorial on creating ERC721 tokens, we won’t be diving any deeper into those. But, if you are interested in a more extensive guide, feel free to check out the article on how to create your own NFT. 

If you prefer video guides, tune in to the Moralis YouTube channel and take a closer look at the following clip. This explains the complete process in further detail:

https://www.youtube.com/watch?v=WdQHnb_5m5Q

ERC721 vs ERC1155 Contracts

The ERC721 standard was the first widely-used standard for NFTs, and it’s the most recognizable one. The standard technically defines a minimum interface that a smart contract needs to implement. This allows the tokens created from the contract to be owned, traded, and managed. This is simply a minimum, and it doesn’t restrict adding additional functions to supplement the requirements. However, even though the ERC721 contract standard added an element of flexibility to Ethereum development, it was still possible to make further headway in the same direction.

An ERC721 token contract is designed for minting only one particular type of NFT. This is something that, in some instances, can put limitations on its potential. A good analogy here to highlight the limitations is concert tickets. It is perfectly possible to utilize an ERC721 contract to create NFTs that act as concert tickets. In this instance, the tokens’ metadata would contain a seat ID that is mapped to the buyers of the tickets/tokens. This works fine; however, problems arise when there are multiple concerts. In those instances, a new contract would have to be deployed for each show, which can be unnecessary.

To solve this issue, another token standard arose in the form of ERC1155, and tokens utilizing this are also known as semi-fungible tokens. The ERC1155 contract standard is even more dynamic, and it makes it possible to have multiple different types of NFTs within the same contract. Another excellent feature of ERC1155 contracts is that it’s possible to add more NFTs as you go along. 

So, the ERC1155 standard makes token development more flexible; however, it comes with drawbacks as these contracts are more complicated. As such, we recommend that you start with ERC721 or ERC20 contracts to learn the basics of token development.

ERC721 Contract – Summary

Some of the most prominent Web3 features are tokens, and they have great potential when it comes to dApp development. There are two main types of tokens: fungible and non-fungible, also known as NFTs. As powerful features, it’s essential to establish a standard so that all tokens have basic functionalities. To regulate fungible tokens, Ethereum presented the ERC-20 standard, and for NFTs, it was the ERC-721 token standard.

We took this article to dive deeper into ERC721 contracts, which regulate NFTs that are tokens with unique properties that potentially differ in value. This makes ERC721 tokens more flexible and ideal for representing individual assets such as houses, art, property, etc.

Implementing the ERC721 standard ensures that a contract has a few essential methods and events. This also makes sure that all tokens utilizing the standard can be owned, traded, and fully managed. The standard enables a universal practice ensuring that people know what to expect of the tokens, and it also makes them compatible throughout the complete market. 

If you want to learn how to create ERC721, ERC20, or ERC1155 contracts and tokens, Moralis is the platform for you. Not only are you able to easily create NFTs and fungible tokens for Ethereum, but other EVM-compatible blockchains alike. If you are interested, check out the articles on how to create a BSC token or how to create a Polygon token

However, this is far from the limits of the Moralis operating system. If you want to learn more, check out the Moralis blog for additional guides answering questions such as ”how does Web3 work?”, ”what is a Web3 wallet?”, and ”what is OpenZeppelin?”. 

So, if you want to know more about Web3 and become a blockchain developer, sign up with Moralis and begin your development journey today!

Moralis Money
Stay ahead of the markets with real-time, on-chain data insights. Inform your trades with true market alpha!
Moralis Money
Related Articles
November 20, 2023

What’s the Difference Between Polygon PoS vs Polygon zkEVM?

January 4, 2024

Starknet Faucet – How to Get Testnet Funds for Starknet

November 28, 2022

Palm Blockchain – What is the Palm Network?

November 12, 2022

Ethereum Webhooks – What They are and How to Use Webhooks for Ethereum 

December 20, 2022

Ethers.js Dapp Development – How to Use Ethers.js

September 6, 2022

How to Build a Polygon Dapp in 3 Steps

March 14, 2023

Notify Custom Webhooks – Real-Time Data with Moralis Streams

December 14, 2022

Ethers.js Tutorial – How to Get Started Using a Simple Ethers.js Example

🔥 LIMITED-TIME OFFER - Get early access to an Enhanced Token Page & boost your token project’s exposure! GET EARLY ACCESS 🔥