hhstore / blog

My Tech Blog: about Mojo / Rust / Golang / Python / Kotlin / Flutter / VueJS / Blockchain etc.
https://github.com/hhstore/blog/issues
294 stars 24 forks source link

ETH: OpenZeppelin Contracts Code Analysis #337

Open hhstore opened 2 years ago

hhstore commented 2 years ago

related:

hhstore commented 2 years ago

OpenZeppelin 源码分析:

    fallback() external payable virtual {
        _fallback();
    }
hhstore commented 2 years ago

ERC721:

tokenURI()


contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {

    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");

        string memory baseURI = _baseURI();
        return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : "";
    }

}

safeTransferFrom():

    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        safeTransferFrom(from, to, tokenId, "");
    }

_safeMint()

    function _safeMint(address to, uint256 tokenId) internal virtual {
        _safeMint(to, tokenId, "");
    }
contract ERC721Mock is ERC721 {

    function safeMint(address to, uint256 tokenId) public {
        _safeMint(to, tokenId);
    }
}

扩展: ERC721PresetMinterPauserAutoId

pause()

contract ERC721PresetMinterPauserAutoId is
    Context,
    AccessControlEnumerable,
    ERC721Enumerable,
    ERC721Burnable,
    ERC721Pausable
{

    function pause() public virtual {
        require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to pause");
        _pause();
    }

}
hhstore commented 2 years ago

ERC1155:

mintBatch()


contract ERC1155Mock is ERC1155 {

    function mintBatch(
        address to,
        uint256[] memory ids,
        uint256[] memory values,
        bytes memory data
    ) public {
        _mintBatch(to, ids, values, data);
    }

}

示例:

hhstore commented 2 years ago

utils:

MerkleProof:

library MerkleProof {
    /**
     * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
     * defined by `root`. For this, a `proof` must be provided, containing
     * sibling hashes on the branch from the leaf to the root of the tree. Each
     * pair of leaves and each pair of pre-images are assumed to be sorted.
     */
    function verify(
        bytes32[] memory proof,
        bytes32 root,
        bytes32 leaf
    ) internal pure returns (bool) {
        return processProof(proof, leaf) == root;
    }

}
hhstore commented 2 years ago

1

hhstore commented 2 years ago

1

hhstore commented 2 years ago

1

hhstore commented 2 years ago

1

hhstore commented 2 years ago

1