Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] 修复Solidity-103, 34课 ERC721 文档与示例代码不相符的问题. #244

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

ramsayleung
Copy link

问题

文档与代码不相符

Solidity-103 34课 ERC271 文档描述 :

ERC721主合约实现了IERC721,IERC165和IERC721Metadata定义的所有功能,包含4个状态变量和17个函数:

但示例代码只声明了 IERC721IERC721Metadata 接口, 并没有声明IERC165:

contract ERC721 is IERC721, IERC721Metadata{
...
    // 实现IERC165接口supportsInterface
    function supportsInterface(bytes4 interfaceId)
        external
        pure
        override
        returns (bool)
    {
        return
            interfaceId == type(IERC721).interfaceId ||
            interfaceId == type(IERC165).interfaceId ||
            interfaceId == type(IERC721Metadata).interfaceId;
    }
...
}

缺失上下文信息, 函数理解有难度

ERC721利用_checkOnERC721Received来确保目标合约实现了onERC721Received()函数(返回onERC721Received的selector)

function _checkOnERC721Received(
    address operator,
    address from,
    address to,
    uint256 tokenId,
    bytes memory data
) internal {
    if (to.code.length > 0) {
        try IERC721Receiver(to).onERC721Received(operator, from, tokenId, data) returns (bytes4 retval) {
            if (retval != IERC721Receiver.onERC721Received.selector) {
                // Token rejected
                revert IERC721Errors.ERC721InvalidReceiver(to);
            }
        } catch (bytes memory reason) {
        }
    }
}

_checkOnERC721Received 通过检查 onERC721Received的selector 来判断是否实现了OnERC721Received, 但是并没有提供上下文说明为什么可以这么判断, 不便于初学者理解

解决方案

  1. 更新示例代码,保证文档与示例代码相符
  2. 增加 OnERC721Received 函数的注释,以解释为什么可以通过"返回onERC721Received的selector"来判断是否实现了IERC721Receiver接口

- Update readme.md to implement missing ERC165 as document descripted
- Update readme.md to include the doc of `onERC721Received` function
Copy link

vercel bot commented Oct 11, 2024

@ramsayleung is attempting to deploy a commit to the WTF Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant