简体   繁体   中英

Inheriting multiple ERC721 extensions

I would like to use both ERC721URIStorage for setting token URI easily but I would also like the methods in ERC721Enumberable to fetch the number of tokens made by an address.
I get this error when trying to use both:

Derived contract must override function "_beforeTokenTransfer". Two or more base classes define function with same name and parameter types.
Derived contract must override function "_burn". Two or more base classes define function with same name and parameter types.
Derived contract must override function "supportsInterface". Two or more base classes define function with same name and parameter types.
Derived contract must override function "tokenURI". Two or more base classes define function with same name and parameter types.

is this just not possible at all or is there a way to Override the duplicated functions?

Below code overrides the overlapping functions from the two extensions


pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";


contract NameOfContract is ERC721URIStorage, ERC721Enumerable {

    // Contract Code ....
    
    function _beforeTokenTransfer(address from, address to, uint256 tokenId)
        internal
        override(ERC721, ERC721Enumerable)
    {
        super._beforeTokenTransfer(from, to, tokenId);
    }

    function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
        super._burn(tokenId);
    }

    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721, ERC721URIStorage)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }

    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721, ERC721Enumerable)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }

}

Update _beforeTokenTransfer has a new parameter and argument

Now it is like

 function _beforeTokenTransfer(address from, address to, uint256 tokenId, uint256 batchSize)
    internal
    override(ERC721, ERC721Enumerable)
{
    super._beforeTokenTransfer(from, to, tokenId, batchSize);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM