Original title: "Solidity Minimalist Introduction to ERC721 Topic: 3. ERC721 Master Contract"
Original source: 0xAA
p>
I am re-learning solidity recently, to consolidate the details, and also write an "Introduction to Solidity Minimalist" for novices to use (programmers can find another tutorial), weekly Update 1-3 lectures.
Unconsciously, I have completed the first 13 lectures (basics) of the Solidity minimalist tutorial, including: Helloworld.sol, variable type, storage location, function , control flow, constructors, decorators, events, 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. This article is the third lecture of the series, the first lecture is: 《Solidity Minimalist Introduction | ERC721 Topic Lecture 1: Contract Overview and Related Libraries"; Lecture 2: " Solidity Minimalist Introduction | ERC721 Topic Lecture 2: ERC721 Related Interfaces"
I introduced its related libraries and interfaces in the first two lectures of ERC721, and finally this lecture can introduce the main contract. The ERC721 main contract contains 6 state variables and 28 functions, which we will introduce one by one. Also, I added Chinese comments to the ERC721 code for your convenience.

· _name and _symbol are two strings, storing the name and symbol of Token.
· _owners is the mapping from tokenId to owner address, storing the holder of each Token.
·_balances is the mapping from the owner address to the number of coins held, and stores the holdings of each address.
· _tokenApprovals is the mapping from tokenId to authorization address, storing the authorization information of each token.
· _operatorApprovals is the mapping from the owner address to batch approval, storing the batch authorization information of each owner. Note that batch authorization will authorize all nfts of this series held in your wallet to another address, and others can control them at will.
· constructor: Constructor, set the name and symbol of ERC721Token (_name and _symbol variables).

· supportsInterface: implement IERC165 interface supportsInterface, see ERC721 Lecture 2

· balanceOf: implement balanceOf of IERC721, use _balances variable to query the balance of owner address.

· ownerOf: implement ownerOf of IERC721, use _owners variable to query the owner of tokenId.

· name: implement the name of IERC721Metadata, and query the Token name.

·symbol: realize the symbol of IERC721Metadata, and query the token code.

·tokenURI: realize the tokenURI of IERC721Metadata, query the URL stored in Tokenmetadata. Opensea also has a little fox wallet to display your NFT picture, and this function is called.

· _baseURI: base URI, will be called by tokenURI(), and tokenURI will be combined with tokenId, the default is empty, and sub-contracts need to rewrite this function.


p>
· getApproved: implement getApproved of IERC721, use _tokenApprovals variable to query the authorization address of tokenId.

· setApprovalForAll: implement setApprovalForAll of IERC721, authorize all Tokens held to the operator address. Call the _setApprovalForAll function.

· isApprovedForAll: implement isApprovedForAll of IERC721, use _operatorApprovals variable to query whether the owner address has authorized the NFT held by the operator address in batches.

· transferFrom: implement transferFrom of IERC721, non-secure transfer, not recommended. Call the _transfer function.

· safeTransferFrom: implement safeTransferFrom of IERC721, safe transfer, call the _safeTransfer function.

·_safeTransfer: safe transfer, safely transfer the tokenId Token from from to to, and check whether the contract recipient understands the ERC721 protocol to prevent the Token from being permanently locked. The _transfer function and the _checkOnERC721Received function are called. Condition:
1.from cannot be 0 address.
2. to cannot be 0 address.
3.tokenId Token must exist and be owned by from.
< /p>
4. If to is a smart contract, it must support IERC721Receiver-onERC721Received.

·_exists: Query whether the tokenId exists (equivalent to querying whether its owner is a non-zero address).

· _isApprovedOrOwner: Query whether the spender address can use tokenId (he is the owner or authorized address).

·_safeMint: safe mint function, mint tokenId and transfer to to address. condition: tokenId does not exist yet, If to is a smart contract, it must support the IERC721Receiver-onERC721Received interface.

· The implementation of _safeMint calls the _checkOnERC721Received function and the _mint function.

·_mint: internal minting function. Mint the tokenId and transfer it to to by adjusting the _balances and _owners variables, and release the Tranfer event at the same time. condition: tokenId does not exist yet. to is not 0 address.

·_burn: internal destruction function, which destroys the tokenId by adjusting the _balances and _owners variables, and releases the Tranfer event at the same time. Condition: tokenId exists.

· _transfer: transfer function. Transfer the tokenId from from to to by adjusting the _balances and _owner variables, and release the Tranfer event at the same time. condition: tokenId is owned by from to is not 0 address

·_approve: authorization function. By adjusting _tokenApprovals, the to address operation tokenId is authorized, and the Approval event is released at the same time.

< br>
· _setApprovalForAll: Batch authorization function. By adjusting the _operatorApprovals variable, batch authorize to to operate all tokens of owner, and release the ApprovalForAll event at the same time.

· _checkOnERC721Received: Function, used to call IERC721Receiver-onERC721Received when to is a contract, in case tokenId is accidentally transferred into a black hole.

·_beforeTokenTransfer: This function will be called before transfer (including mint and burn). Default is empty, subcontracts can choose to override.

·_afterTokenTransfer: This function will be called after the transfer (including mint and burn). Default is empty, subcontracts can choose to override.

This article is the third lecture of the ERC721 topic. I introduced all the variables and functions of the ERC721 main contract, and gave the Chinese notes of the contract. With the ERC721 standard, NFT project parties only need to wrap the mint function to issue NFT. In the next lecture, we will introduce the contract of Boring Ape BAYC, and find out what changes the most popular NFT has made to the standard ERC721 contract.
Original Link a>
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

