What is an ERC-721 token?

Ethereum request for comment (ERC) is a standard for implementing a token. The Ethereum community adopts these standards to ensure that the projects built on top of the Ethereum network remain interoperable.

ERC-721 token represents objects that are unique and not interchangeable. These types of tokens are called non-fungible tokens (NFT).

NFT

NFT is a term that is widely used to represent digital assets. Digital assets can be anything from digital art to an in-game collectible. All these assets are considered non-fungible because they are unique and can not be interchanged with other NFTs, unlike the fungible tokens represented by the ERC-20 standard, which hold the same value and is interchangeable.

ERC-721

ERC-721 is a standard the Ethereum community defines to make a non-fungible token. The ERC-721 smart contracts must contain eight compulsory functions and two optional functions to meet the standard of ERC-721.

contract ERC721 {
// Events
event Transfer(address indexed _from, address indexed _to, uint256 _tokenId);
event Approval(address indexed _owner, address indexed _approved, uint256 _tokenId);
// Compulsory functions
function name() constant returns (string name);
function symbol() constant returns (string symbol);
function totalSupply() constant returns (uint256 totalSupply);
function balanceOf(address _owner) constant returns (uint balance);
function ownerOf(uint256 _tokenId) constant returns (address owner);
function approve(address _to, uint256 _tokenId);
function takeOwnership(uint256 _tokenId);
function transfer(address _to, uint256 _tokenId);
//Optional functions
function tokenOfOwnerByIndex(address _owner, uint256 _index) constant returns (uint tokenId);
function tokenMetadata(uint256 _tokenId) constant returns (string infoUrl);
}

Functions

The details and a brief working of the functions are given below:

  • name(): This function obtains the token’s name. It returns a string containing the token’s name, used by front-end applications to display the token’s name.

  • symbol(): This function gets the shorthand name of the token. It returns a string containing the shorthand of the token which can be used by front-end applications to display the shorthand name.

  • totalSupply(): This function gets the token’s total supply. It can be implemented according to the use case, and it returns an unsigned integer used by front-end applications to display the total supply.

  • balanceOf(): This function gets the number of tokens owned by a specific address. It returns an unsigned integer which can be used by frontend applications to display the tokens owned by a specific address.

  • ownerOf(): This function gets the token’s owner’s address by providing the token’s id. It returns an address that can be used by front-end applications to display the owner’s details.

  • approve(): This function provides owner ability to give permission to another entity to perform a transaction or take ownership of the token on behalf of the actual owner.

  • takeOwnership(): The approved entity uses this function to withdraw the tokens from the owner’s account and transfer them into another account.

  • transfer(): This function allows the owner to transfer the token to another account.

  • tokenOfOwnerByIndex(): This function gets the token corresponding to a specific token ID. This is useful in cases when the owner owns multiple tokens, and it is hard to keep track, so this function allows one to get each individual token owned by a user by its index in the list of tokens owned by the user.

  • tokenMetadata(): This function returns a URL where the metadata of that token is stored. Storing the metadata on the blockchain is an option but a very expensive one, so the data is stored on external resources like IPFS, and the URL is returned to fetch the data from there.

Events

Events are like a notification generated by a smart contract to notify the client application of a specific occurrence. Events are logged into a data structure called log. Events can be used in the frontend to notify the user when a transaction is completed.

The events used in ERC-721 are explained briefly below:

  • Transfer: This event is emitted when a transaction takes place, and the token is transferred from one owner to another.

  • Approval: This event is emitted when the account owner approves a third party to take ownership of a token.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved