header-langage
简体中文
English
Scan to Download the APP
Partnered With:

Simple Introduction to Solidity | ERC721 Topic Lecture 1: Contract Overview and Related Libraries

Read this article in 3 Minutes
An overview of the ERC721 contract, and introduce the three library contracts Address, Context and String called by the ERC721 main contract.
Original title: "Solidity minimalist entry ERC721 topic: 1. ERC721 related library"
Original source: 0xAA


All codes Open source is on github (64 stars have opened WeChat communication groups  already opened, 128 stars have recorded teaching videos, and 256 have opened advanced content): github.com/AmazingAng/WTFSolidity


Unconsciously, I have completed the first 13 lectures (basics) of the Solidity minimalist tutorial, including: Helloworld.sol, variable type, storage location, function, control flow, constructor, decorator, event, Inheritance, abstract contracts, interfaces, libraries, exceptions. Before the advanced content, I decided to make a special topic on ERC721, and use the previous content comprehensively to help everyone review the basic knowledge better and understand the ERC721 contract more deeply. Hopefully, after learning this topic, everyone will be able to issue their own NFT.


ERC721 contract overview


ERC721 main contract references a total of 7 contracts:


They are:


3 library contracts : Address.sol, Context.sol and Strings.sol3 interface contracts: IERC721.sol, IERC721Receiver.sol, IERC721Metadata.sol1 EIP165 contract: ERC165.sol


So before talking about the main contract of ERC721, we will spend two lectures on the referenced library contract and interface contract.


ERC721 related library 


Address library  

< p>

The Address library is a collection of functions related to the Address variable, including judging whether an address is a contract, a safer function call. isContract() used in ERC721:



This function uses The length of non-contract address account.code is 0, so as to distinguish whether an address is a contract address.


ERC721 main contract calls isContract() in _checkOnERC721Received() function.



The purpose of this function is to judge when receiving ERC721 Token Whether the address is a contract address; if it is a contract address, continue to check whether the IERC721Receiver interface (the receiving interface of ERC721) is implemented to prevent someone from mistaking Token Go to the black hole.


Context library


Context library is very simple and encapsulates Two global variables of Solidity: msg.sender and msg.data


These two functions simply return msg.sender and msg.data. Therefore, the purpose of the Context library is to wrap the keywords of msg.sender and msg.data with functions, so as to deal with the situation that the keywords are replaced in a future upgrade of Solidity, and has no other effect.


String library


Contains two libraries Functions: toString() and toHexString(). toString() converts uint256 directly to string, for example, 777 becomes "777"; while toHexString() converts uint256 to hexadecimal first, and then converts to string, for example, 170 becomes "0xaa". ERC721 calls the toString() function:



This function first determines how many digits the incoming uint256 parameter is, and stores it in the digits variable. Then use a loop to convert the ASCII code of each digit into bytes1, store it in the buffer, and finally convert the buffer into a string and return it.


ERC721 main contract calls toString() in the tokenURI() function:


< /p>

This function splices the baseURI and the specified tokenId together, and returns the URL of the ERC721 metadata. The jpeg you bought for dozens of ETH is stored on this URL.   


Summary


This The first lecture is the first lecture of the ERC721 topic. We gave an overview of the ERC721 contract, and introduced the three library contracts Address, Context and String called by the ERC721 main contract.


Original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit