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:
- 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);
- 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;
- 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;
- 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;
- 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);
- 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;
- 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);
- 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;
- Description: Mints a new NFT and assigns it to a specific address.
- Returns: No return value.
function mint(address to, uint256 tokenId) external;
- Description: Burns an NFT, removing it from circulation.
- Returns: No return value.
function burn(uint256 tokenId) external;
- 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);
- 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);
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));