Source Code Interpretation: What exactly is the NFT you bought?

avatar
十四君
2 years ago
This article is approximately 1636 words,and reading the entire article takes about 3 minutes
This article revolves around the standard ERC721 protocol and describes how Mint, safeMint, transfer, etc. implement asset management.

abstract

secondary title

This article revolves around the standard ERC721 protocol, describes how Mint, safeMint, transfer, etc. implement asset management, and understands its security design and the cost composition of Ethereum data chaining by interpreting the code.

directory outline

  • directory outline

  • 1. What are the so-called NFT assets?

  • text

  • text

  • text

5. How expensive is storage on Ethereum?

  • secondary title

  • object oriented

  • Research and development - can read without barriers, understand the exquisite contract design

Non-RD - may not understand the listed codes, but can understand the design ideas of standard protocols

first level title

On opensea, you can see that each NFT has a unique number.

Source Code Interpretation: What exactly is the NFT you bought?

For example, No. 4132 in the azuki series, in the Details column of the page, you can see its contract address, ID number, public chain where it is deployed, and other information, while the Properties column is its setting with various attributes and corresponding rarity (non- Azuki itself carries, but it is calculated by Opensea integration).

secondary title

Source Code Interpretation: What exactly is the NFT you bought?

And when we look back at the source code (take the ERC721 standard library openzepplin code here), we will find that the program records two global dictionary-type variables, and records all the current corresponding IDs of each ID in _owners by mapping addresses with numbers. Or, it also records the total number of NFTs held by the current owner with _balances

And because ERC721 innovatively endows a variable _owners with an ID corresponding to the address, it manages addresses and balances with ERC20 only _balances, distinguishing the difference between FT (homogeneity) and NFT (non-homogeneity).

first level title

2.1 How Mint worksWhen Netflixs NFT forgets web2s business security

Mint means casting, which is the creation process of each NFT, such as the previous Love Death Machine NFT

When Netflixs NFT forgets web2s business security

  • Mint obtained the asset certificate of the NFT.

  • As can be seen from the source code, Mint mainly makes security judgments:

  • Judgment 1: Make sure that the transfer-in address is not 0x00 (the black hole address cannot be transferred out, and the transfer-in will result in asset loss)

  • Judgment 2: Make sure that the NFTID operated by this exchange does not exist

  • What the final code does is:

  • Operation 1: Add 1 to the total number of _balances held by the transferred address

Source Code Interpretation: What exactly is the NFT you bought?

Operation 2: Modify the owner of the corresponding NFTID to the transferred address

Operation 3: When the transaction is completed, an emit event is sent, allowing the off-chain to monitor the data of this transaction

There are _beforeTokenTransfer and _afterTokenTransfer in the middle, which are virtual functions. As a standard, they are used by the project side to add some specific logic codes without modifying the standard protocol.

Source Code Interpretation: What exactly is the NFT you bought?

2.2 Why safeMint is safer

safeMint means safe casting. From the code implementation, it can be seen that it also calls MInt itself, but it additionally adds the judgment of _checkOnERC721Received, which belongs to the ERC165 standard, which is equivalent to judging the other party after the transfer operation is completed. Whether the address is a black hole address (that is, an address that cannot initiate a transaction NFT operation) is to prevent when the transfer object is a contract address, and the contract does not have a pre-set transfer function, resulting in assets that cannot be transferred away, resulting in permanent losses.https://eips.ethereum.org/EIPS/eip-165

2.3 How does ERC165 prevent assets from being transferred into black holes?

The original intention of the design can be seen:

It is a proposal to standardize the contract interface. In the programming grammar, interface means interface. The functions defined in it can not be implemented and only put the parameters related to the function name. When the program is complicated, it is equivalent to a directory to tell others what I have Function.

Source Code Interpretation: What exactly is the NFT you bought?

However, the writing methods of the interface have their own advantages and disadvantages. The name defines the parameter type, and even whether it exists is different.

Therefore, this proposal finally formed the ERC165 standard, which standardized the identification rules of the interface.

The usage process is:

STEP 1 Determine whether there is a supportsInterface function, and it complies with the 165 standard

(PS: Let the contract have the NFT receiving and transferring function, which can be realized by introducing the IERC721Receiver.sol expansion package)

text

  • The standard protocol design has two transfer methods, transfer and transferFrom, which are used in two scenarios:

  • transfer Transfer: Called by the user to transfer the NFTID held by the wallet sending this message to the specified address

Compare it to:

  • transferFrom Transfer from: Called by a certain organization, the user needs to authorize a certain address first, so that it has the right to transfer.

  • Compare it to:

Transfer is a cash transaction, pay with money from your own pocket

transferFrom is to scan the code to deduct money, and the store will apply for the deduction, subject to whether the user has opened the small amount withholding authority

Next, lets take a look at the code, which may have unexpected details.

3.1 How the transfer works

Source Code Interpretation: What exactly is the NFT you bought?

He will detect whether the from party of the current transaction is the holder of this NFTID, and restrict the NFT from being transferred to the 0x00 address. Next, refresh the balance of the from transfer-out address and to transfer-in address, modify the _balances global variable and reset _owners to change the owner address of this NFTID to to.

Here is a protective detail. First execute _approve(address(0), tokenId); to clear the historical authorization. If there is no such step, the transfer of assets is completed, but the transfer authorization of its NFTID is still there. Think carefully:

3.2 How transferFrom works

The nature of the transaction here calls _safeTransfer, so its core logic is the require part,

A big detail here is: _msgSender() This is a method in openzepplins standard library Context.sol.

In fact, it is to get the sender address of the current transaction, but the encapsulated version is used here instead of using msg.sender directly

Therefore, some contracts in the middle, similar to libraries, need to consider this special situation.

Source Code Interpretation: What exactly is the NFT you bought?

The rest of the judgment is to determine whether there is an authorization record, which is easy to understand and will not be repeated

first level title

4. What other data can be stored on the chain?

After reading the transaction link, many new students were actually surprised. It turned out that the NFT I bought had only one ID whose attribution address pointed to me, thus achieving uniqueness. Even so, where is the rarity information? Where is my NFT image itself?

This is the metadata extension IERC721Metadata.sol involving ERC721

You can put anything you want, but the project party often only stores the most basic ID+IPFS address on the chain.

We can take a look at some project data through the previous Etherscan tutorial method?

The contract address on Azuki is: 0xed5af388653567af2f388e6224dc7c4b3241c544

Source Code Interpretation: What exactly is the NFT you bought?

For the recently emerging Metaverse projects Metaverse land sandbox and **Decentraland, as well as last year’s fiery **Axie Infinity, the basic metadata stored on the chain is only ID+URL.

Those like mirror are specially designed for low cost and high storage. A block usually starts at 30M, which is about 1000 times that of Ethereum.

first level title

5. How expensive is storage on Ethereum?

  • Here is where this article is a little more difficult. Lets analyze the cost composition and amount conversion of on-chain storage from the source code

  • There will be two aspects of cost generation, according to the execution process

When the user initiates a transaction, the data to be written on the chain is passed in as a parameter, and its size is a cost

The transaction executes the contract code, and the EVM calculates the gas cost consumed according to the modification and usage.

Source Code Interpretation: What exactly is the NFT you bought?

5.1 The cost of transaction initiation

  • We can check the Ethereum Yellow Paper, which has a clear definition of the gas consumed by the transaction data size

  • You can see the price of the parameters attached to the exchange:

  • Each transaction has 21000 GAS to pay

Pay 68 GAS for each non-zero byte data or code of the transaction

Pay 4 GAS for each zero-byte data or code of the transaction

So if you register some NFT attribute information in Mint, the data part of the transaction will convert characters such as abc into two hexadecimal representations, and each character is an eight-bit binary, which is equal to a byte. So it can be approximately equal to dividing the length of data by 2 as the number of bytes.

And 1kb of data, if it is all non-zero text information with information content, it is equivalent to increasing the gas consumption of 68*1000=6.8W. According to the gas price of 20gwei and the price of 2000 eth exchanged for US dollars, it can be estimated that every 1kb of data on the chain needs to be:

20*(21000+68000)*1e9/1e18 * 2000 = USD 3.5

func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64)(uint64, error)

5.2 The cost of contract storage

Since there is logic stored in the smart contract after the transaction is initiated, let’s analyze the specific consumption from the source code of Ethereum go (EIP1283). The code is specifically in the function, which is too long to stick to it:

  • Historically, the estimation of gas consumption has gone through several iterations. If Petersburg or Constantinople is not activated, the calculation will not be performed according to the following logic

  • Calculation of gas consumption depends on 3 types of data management (addition, deletion, modification)

  • From a zero-value address to a non-zero value (NEW VALUE), each storage slot consumes 2Wgas

From a non-zero value address to a zero value address (DELETE), each storage slot needs to consume 5K gas, but there will be a reward of 1.5W gas return

From non-zero to non-zero (CHANGE), each storage slot consumes 200 gas

Note that each of the above storage slots counts as 32 bytes, and 1kb storage is 32 storage slots. The process of Mint is to add new storage, so if an additional 1kb of data is stored on the chain, the cost will be 64Wgas, and the conversion amount is:

Can EIP-5058 prevent NFT project parties from running away with buckets?

Can EIP-5058 prevent NFT project parties from running away with buckets?

What are we looking at when we look at Etherscan?

When Netflixs NFT forgets web2s business security

Original article, author:十四君。Reprint/Content Collaboration/For Reporting, Please Contact report@odaily.email;Illegal reprinting must be punished by law.

ODAILY reminds readers to establish correct monetary and investment concepts, rationally view blockchain, and effectively improve risk awareness; We can actively report and report any illegal or criminal clues discovered to relevant departments.

Recommended Reading
Editor’s Picks