Open In App

Solidity – Programming ERC-721 Token

Improve
Improve
Like Article
Like
Save
Share
Report

ERC stands for Ethereum Request for Comment while 721 is the proposal identifier number. It is a standard interface for Non-Fungible tokens(NFTs), it has a set of rules which make it easy to work with NFTs. Since each ERC721 token is assumed to be unique these are not interchangeable, and also not divisible. You can only trade tokens in whole units, and each one has a unique ID. ERC-721 token is a free and open standard that describes how to build non-fungible tokens(NFTs) or unique tokens on the Ethereum blockchain network. ERC-721 token is also known as the Solidity Smart Contract standard that is inheritable.

Different Functions of ERC-721

On the Ethereum network, a token is basically just a smart contract that follows some common rules, it implements a standard set of functions that can be shared by all other token contracts, such as:

Functions similar to ERC-20:

1. Name: The work of this ERC-721 function is to tell outside contracts and applications the name of this token. The implementation of this function can be done as shown below:

Solidity




contract ContractName {
  function name() constant returns (string name){
    return "My NFT";
  }
}


2. Symbol: This function provides outside programs with the token’s shorthand name or the symbol. This also provides compatibility with the ERC20 token standard. This function could be implemented as shown below:

Solidity




contract ContractName {
 function symbol() constant returns (string symbol){
   return "MNFT";
 }
}


3. totalSupply: It returns the total amount of tokens stored by the contract.

Solidity




contract ContractName {
 uint256 private totalSupply = 1000;
 function totalSupply() constant returns (uint256 sup){
   return totalSupply;
 }
}


4. balanceOf: This function returns the number of tokens in the owner’s account, it is usually used to find how many tokens the address owns.

Solidity




contract MyNFT {
  mapping(address => uint) private balances;
  function balanceOf(address _owner) constant returns (uint balance)
  {
    return balances[_owner];
  }
}


Ownership Functions:

  1. ownerOf: The purpose of this function is to return the address of the owner of a token. ERC721 token is referenced on the blockchain network via a unique ID. Using this ID we can determine the owner of a token.
  2. approve: This function grants another entity permission to transfer tokens on the owner’s behalf.
  3. takeOwnership: This function acts like a withdraw function because an outside party can call it in order to take tokens out of another user’s account. Therefore, this function can be used when a user has been approved to own a certain amount of tokens and wishes to withdraw said tokens from another user’s balance.
  4. transfer: This function enables the token owner to transfer it to another user, similar to how other digital tokens/coins work.
  5. tokenOfOwnerByIndex: Each NFT’s owner can own more than one token at one time. Because each token has a unique ID, however, it can get difficult to keep track of the individual tokens that a user may own. In order to do this, the smart contract keeps a record of the IDs of each token that each user owns. Since tokens owned by users are being recorded each individual token owned by a user can be retrieved by its index in the list (array) of tokens owned by the user,  “tokenOfOwnerByIndex” function lets us retrieve a token in this method.

Metadata Function:

1. tokenMetadata: This Metadata function lets us discover a token’s metadata or the link to its data.

Events: Events are fired whenever a contract calls them, Once they’ve been fired they’re broadcasted to any listening programs. The outside programs listen to blockchain events in order to execute logic once the event is fired based on the information that the event provides. The ERC721 standard defines two events which are as follows:

Transfer: This event is fired whenever a token changes hands and is broadcasted when a token’s ownership moves from one user to another. It details the following:

  • Which account sent the token.
  • Which account received the token.
  • Which token (by ID) was transferred.

Approval: This event is fired whenever ownership of a token is approved by a user to be taken by another user (i.e. whenever approval is executed). It details the following things:  

  • The account address which currently owns the token.
  • The account address that is allowed to own the token in the future.
  •  Which token(by ID) is approved to be having its ownership transferred.
     

Steps to Create and Deploy ERC-721 Smart Contracts

Since deploying smart contracts on the Ethereum main net will cost some real ethers so just here the smart contract will be deployed on the Ropsten testnet. But to get started with all of these things one needs to have an ETH wallet, for that a Metamask browser extension is required, after this, some test ETH is required that one can get for free by going to the Ropsten faucet.

Ethereum Remix IDE can be used for the making and deploying the smart contract that one makes using the below steps:

Step 1: Specifying SPDX license type, it is an addition after the 0.6.8 version of Solidity.

// SPDX-License-Identifier: MIT

Step 2: Declaring the solidity version which is 0.8.0.

pragma solidity 0.8.0;

Step 3: Importing ERC-721 contracts:

import "https://github.com/0xcert/ethereum-erc721/src/contracts/tokens/nf-token-metadata.sol";
import "https://github.com/0xcert/ethereum-erc721/src/contracts/ownership/ownable.sol";

Step 4:  Starting the Contract named myNFT and mention it’s extending NFTokenMetadata and Ownable contracts

contract myNFT is NFTokenMetadata, Ownable {

Step 5:  Initializing the constructor and setting name (nftName), and a symbol (nftSymbol) of our token.

constructor() {
   nftName = "gfg NFT";
   nftSymbol = "GFG";
 }

Step 6:  Now declare a function mint that takes three arguments:

  • variable _to of type address which will store the address of the receiver of the NFT token.
  • variable _tokenId of uint256(unsigned integer of 256  bits in size) type which will hold the token id.
  • variable _uri of type string which will store the URI of the JSON file. 

Declaring mint as external means, it can be accessed from other smart contracts and outside the self scope.

The next step is to mint the token. “Minting an NFT” is referred to as an act of publishing a unique instance of your ERC-721 token on the blockchain network. After that, set up the token URI using token id and URI of JSON file. There is a  token URI on an NFT  which is a unique identifier of what the token “looks” like. A URI could be an API call over HTTPS, an IPFS hash, or anything else which is unique.

// declaration of function mint : 
function mint(address _to, uint256 _tokenId, string calldata _uri) external onlyOwner {
  super._mint(_to, _tokenId);
  super._setTokenUri(_tokenId, _uri);
}
}

Below is the complete code to implement ERC-721 smart contracts:

Solidity




// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
  
  
contract myNFT is NFTokenMetadata, Ownable {
  
 constructor() {
   nftName = "gfg NFT";
   nftSymbol = "GFG";
 }
  
 function mint(address _to, uint256 _tokenId, string calldata _uri) external onlyOwner {
   super._mint(_to, _tokenId);
   super._setTokenUri(_tokenId, _uri);
 }
  
}


Deploy the code in Ethereum Remix IDE by clicking on the Deploy button.

Deploy

 

One can click on the “MYNFT AT address(MEMORY)” dropdown to see different options like approve, mint, and others where one need to put the address, and then one can do different operations like this :

MYNFT AT

 

After successful deployment, one can see the status, transaction hash, and all other details at the bottom right side like this. 

status

 



Last Updated : 19 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads