_beforeConsecutiveTokenTransfer

Hello, friends.

Error: Derived contract must override function "_beforeConsecutiveTokenTransfer". Two or more base classes define function with same name and parameter types.

Code from wizard:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract MyToken is ERC721, ERC721Enumerable, ERC721URIStorage, AccessControl {
    using Counters for Counters.Counter;

    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    Counters.Counter private _tokenIdCounter;

    constructor() ERC721("MyToken", "MTK") {
        _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
        _grantRole(MINTER_ROLE, msg.sender);
    }

    function safeMint(address to, string memory uri) public onlyRole(MINTER_ROLE) {
        uint256 tokenId = _tokenIdCounter.current();
        _tokenIdCounter.increment();
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, uri);
    }

    // The following functions are overrides required by Solidity.

    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, AccessControl)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
}

I have a try on the Remix, it can be compiled correctly without any errors.

This is the foundry project.
I use VSCode + solidity

What could be causing the error?

At least two of the base contracts define the function"_beforeConsecutiveTokenTransfer", which means that you must define it in your derived contract "MyToken":
override(base1, base2,..)

The _beforeConsecutiveTokenTransfer internal function is inside ERC721Upgradeable.sol contract, you can override it in your token contract with

	function _beforeConsecutiveTokenTransfer(
		address from,
		address to,
		uint256,
		uint96 size
  ) internal override(ERC721Upgradeable, ERC721EnumerableUpgradeable) {}

1 Like

@abdhafizahmed thanks, but this code is automatically generated in the wizard, shouldn't they take this error into account when generating it.

This is an issue from the 4.8 release candidate. There was a day during which the release candidate was the default when installing from npm, instead of the stable 4.7 release. This was an error in the release process that we promptly fixed but you may have installed it during that window of time.

Please make sure you're using 4.7!

2 Likes