Skip to content

ERC-721 Token Standard defines functions for creating and managing Non-Fungible Tokens (NFTs) on Ethereum, including `balanceOf`, `ownerOf`, `safeTransferFrom`, and more. This README covers core functions and interaction examples using Hardhat.

Notifications You must be signed in to change notification settings

SarthakRana21/Standard_ERC-721_functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

ERC-721 Token Standard

ERC-721 is a standard for Non-Fungible Tokens (NFTs) on Ethereum, and it defines a set of required functions that a contract must implement. Below is a list of the standard functions you can expect from an ERC-721 contract:

Standard ERC-721 Functions

balanceOf(address owner)

  • Description: Returns the number of NFTs owned by a given address.
  • Returns: The number of tokens owned by the owner.
function balanceOf(address owner) external view returns (uint256);


### `ownerOf(uint256 tokenId)`
- **Description**: Returns the owner of a specific NFT.
- **Returns**: The address of the owner of the `tokenId`.
```solidity
function ownerOf(uint256 tokenId) external view returns (address);

safeTransferFrom(address from, address to, uint256 tokenId)

  • Description: Safely transfers an NFT from one address to another.
  • Returns: No return value, but it ensures the recipient is able to receive the token.
function safeTransferFrom(address from, address to, uint256 tokenId) external;

transferFrom(address from, address to, uint256 tokenId)

  • Description: Transfers an NFT from one address to another. This method does not check if the recipient can accept the token (unlike safeTransferFrom).
  • Returns: No return value.
function transferFrom(address from, address to, uint256 tokenId) external;

approve(address to, uint256 tokenId)

  • Description: Approves another address to transfer a specific token on behalf of the owner.
  • Returns: No return value.
function approve(address to, uint256 tokenId) external;

getApproved(uint256 tokenId)

  • Description: Returns the address approved to transfer the specific token.
  • Returns: The address approved for the tokenId.
function getApproved(uint256 tokenId) external view returns (address);

setApprovalForAll(address operator, bool approved)

  • Description: Approves or removes an operator to manage all tokens owned by the sender.
  • Returns: No return value.
function setApprovalForAll(address operator, bool approved) external;

isApprovedForAll(address owner, address operator)

  • Description: Checks if an operator is approved to manage all of the owner's tokens.
  • Returns: true if the operator is approved, false otherwise.
function isApprovedForAll(address owner, address operator) external view returns (bool);

safeTransferFrom(address from, address to, uint256 tokenId, bytes data)

  • Description: Similar to safeTransferFrom, but with an additional data argument for additional information during the transfer.
  • Returns: No return value, but it ensures the recipient is able to receive the token.
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;

Additional Optional Functions (Not part of the core standard, but often included)

mint(address to, uint256 tokenId)

  • Description: Mints a new NFT and assigns it to a specific address.
  • Returns: No return value.
function mint(address to, uint256 tokenId) external;

burn(uint256 tokenId)

  • Description: Burns an NFT, removing it from circulation.
  • Returns: No return value.
function burn(uint256 tokenId) external;

tokenURI(uint256 tokenId)

  • Description: Returns the metadata URI for a specific token. Typically points to a JSON file that describes the NFT.
  • Returns: The URI for the specific tokenId.
function tokenURI(uint256 tokenId) external view returns (string memory);

supportsInterface(bytes4 interfaceId)

  • Description: Checks if the contract supports a specific interface (like ERC-721 or other standards).
  • Returns: true if the interface is supported, false otherwise.
function supportsInterface(bytes4 interfaceId) external view returns (bool);

Interacting with ERC-721 Contract in Hardhat (Example)

To interact with an ERC-721 contract using Hardhat, you can call these methods as follows:

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("ERC721 Token", () => {
  let token;
  let owner;
  let addr1;

  beforeEach(async () => {
    [owner, addr1] = await ethers.getSigners();
    const Token = await ethers.getContractFactory("MyERC721Token");
    token = await Token.deploy();
    await token.deployed();
  });

  it("should have correct owner and tokenURI", async () => {
    const tokenId = 1;

    // Mint a new token
    await token.mint(owner.address, tokenId);

    // Check balance of the owner
    const balance = await token.balanceOf(owner.address);
    expect(balance).to.equal(1);

    // Check the owner of the token
    const ownerOfToken = await token.ownerOf(tokenId);
    expect(ownerOfToken).to.equal(owner.address);

    // Get the token URI
    const tokenURI = await token.tokenURI(tokenId);
    console.log("Token URI:", tokenURI);
  });
});

If you wonder how to get all the functions for your contract? use this code snippet while running test:

console.log("Contract functions: ", Object.keys(token.functions));

About

ERC-721 Token Standard defines functions for creating and managing Non-Fungible Tokens (NFTs) on Ethereum, including `balanceOf`, `ownerOf`, `safeTransferFrom`, and more. This README covers core functions and interaction examples using Hardhat.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published