ERC 721

Written on March 20, 2022 (2 min read)

Token

  • Token (A token on Ethereum is basically just a smart contract that follows some common rules — namely it implements a standard set of functions that all other token contracts share) ERC: Ethereum Request for Comments;
  • ERC 20: An ERC20 token is a standard used for creating and issuing smart contracts on the Ethereum blockchain. Smart contracts can then be used to create smart property or tokenized assets that people can invest in, could act as currencies because the it has divisible and exchangeable properties. Fungible token (Bitcoin, ETH)
  • ERC 721: ERC721 tokens are not interchangeable since each one is assumed to be unique, and are not divisible. for example NFT (Non Fungible Token)

contract ERC721 { event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId); event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);

function balanceOf(address _owner) external view returns (uint256); function ownerOf(uint256 _tokenId) external view returns (address); function transferFrom(address _from, address _to, uint256 _tokenId) external payable; function approve(address _approved, uint256 _tokenId) external payable; }

balanceOf

This function simply takes an address, and returns how many tokens that address owns.

Note: Remember, uint256 is equivalent to uint. We’ve been using uint in our code up until now, but we’re using uint256 here because we copy/pasted from the spec.

ownerOf

This function takes a token ID , and returns the address of the person who owns it.

transferFrom

Note that the ERC721 spec has 2 different ways to transfer tokens:

  • The first way is the token’s owner calls transferFrom with his address as the _from parameter, the address he wants to transfer to as the _to parameter, and the _tokenId of the token he wants to transfer.
  • The second way is the token’s owner first calls approve with the address he wants to transfer to, and the _tokenID . The contract then stores who is approved to take a token, usually in a mapping (uint256 $latex {=>}&fg=000000$ address). Then, when the owner or the approved address calls transferFrom, the contract checks if that msg.sender is the owner or is approved by the owner to take the token, and if so it transfers the token to him.

Share this on Twitter
Leave a comment