How to fix AccessControl: account missing role?

I'm trying to deploy a smart contract using injected Web3 but i get a Gas estimation failed error and a execution reverted error that reads:
"execution reverted: AccessControl: account 0x2214f7d0afd62ee25602e9433a3fcfe5f8cdaa60 (my address) is missing role 0x9aa032fddc5828092899c540d004a5a5ff64ae9e644e14e3e1589b650db53b9e"
I have no idea what role it is referring to because I have set it so the contract inherits all the roles, when I click on a function in my contract it says the Execution cost is infinite, I'm not sure if that will fix when the AccessControl error is fixed but that's also something I'd appreciate someone explain to me, here is the code:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.2;


import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";



contract MetaLeopards is ERC721, ERC721Enumerable, Pausable, AccessControl {
    using Strings for uint256;

    address public projectOwner = 0x2214f7D0Afd62EE25602e9433A3FCfe5f8CDaA60;

    string baseURI;
    string public notRevealedUri;
    string public baseExtension = ".json";

    uint256 public whiteListCost = 0.05 ether;
    uint256 public publicCost = 0.06 ether;
    uint16 public maxSupply = 4444;
    uint8 public maxMintAmount = 10;

    bytes32 public constant PROJECT_OWNER = keccak256("PROJECT_OWNER");
    bytes32 public constant ROLE_ADMIN = keccak256("ROLE_ADMIN");
    bytes32 public constant CONTRACT_MANAGER = keccak256 ("CONTRACT_MANAGER");
    bytes32 public constant WHITE_LIST_MANAGER = keccak256 ("WHITE_LIST_MANAGER");
    bytes32 public constant WHITE_LISTED = keccak256("WHITE_LISTED");

    bool public revealed = false;
    bool public whiteListCanMint = false;
    bool public publicCanMint = false;


    constructor( 
        string memory _name,
        string memory _symbol,
        string memory _initBaseURI,
        string memory _initNotRevealedUri
        ) ERC721(_name, _symbol) {

        setBaseURI(_initBaseURI);
        setNotRevealedURI(_initNotRevealedUri);


        _setRoleAdmin(PROJECT_OWNER, PROJECT_OWNER);
        _setRoleAdmin(ROLE_ADMIN, PROJECT_OWNER);
        _setRoleAdmin(CONTRACT_MANAGER, ROLE_ADMIN);
        _setRoleAdmin(WHITE_LIST_MANAGER, ROLE_ADMIN);
        _setRoleAdmin(WHITE_LISTED, WHITE_LIST_MANAGER);
        _setRoleAdmin(DEFAULT_ADMIN_ROLE, PROJECT_OWNER);

        _setupRole(PROJECT_OWNER, projectOwner);
        _setupRole(PROJECT_OWNER, address(this));

        _setupRole(ROLE_ADMIN, msg.sender);
        _setupRole(ROLE_ADMIN, address(this));

        _setupRole(CONTRACT_MANAGER, msg.sender);
        _setupRole(CONTRACT_MANAGER, address(this));   

        _setupRole(WHITE_LIST_MANAGER, msg.sender);
        _setupRole(WHITE_LIST_MANAGER, address(this));

        _setupRole(WHITE_LISTED, address(this));
        
        _setupRole(DEFAULT_ADMIN_ROLE, address(this));


        _pause;
    }


    // internal
    function _baseURI() internal view virtual override returns (string memory) {
      return baseURI;
    }



    // public
    function mint(uint8 _mintAmount) public payable {
        uint256 supply = totalSupply();
        require(!paused(), "Pausable: paused");
        require(publicCanMint == true);
        require(_mintAmount > 0);
        require(_mintAmount <= maxMintAmount);
        require(supply + _mintAmount <= maxSupply);

        if (msg.sender != projectOwner) {
            require(msg.value >= publicCost * _mintAmount);
        }

        for (uint16 i = 1; i <= _mintAmount; i++) {
            _safeMint(msg.sender, supply + i);
        }
    }

    function whiteListMint(uint8 _mintAmount) public payable onlyRole(WHITE_LISTED) {
        uint256 supply = totalSupply();
        require(!paused(), "Pausable: paused");
        require(whiteListCanMint == true);
        require(_mintAmount > 0);
        require(supply + _mintAmount <= maxSupply);

        if (msg.sender != projectOwner) {
            require(msg.value >= whiteListCost * _mintAmount);
        }

        for (uint16 i = 1; i <= _mintAmount; i++) {
            _safeMint(msg.sender, supply + i);
        }
    }

    
    function walletOfOwner(address _owner) public view returns (uint256[] memory) {
        uint256 ownerTokenCount = balanceOf(_owner);
        uint256[] memory tokenIds = new uint256[](ownerTokenCount);
        for (uint256 i; i < ownerTokenCount; i++) {
            tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
        }
        return tokenIds;
    }


    
    // only white list manager
        function addWhiteListed(address _newWhiteListed) public onlyRole(WHITE_LIST_MANAGER) {
        _grantRole(WHITE_LISTED, _newWhiteListed);
    }
  
    function removeWhiteListed(address _whiteListed) public onlyRole(WHITE_LIST_MANAGER) {
        _revokeRole(WHITE_LISTED, _whiteListed);
    }



    // only contract manager
    function setBaseURI(string memory _newBaseURI) public onlyRole(CONTRACT_MANAGER) {
        baseURI = _newBaseURI;
    }

    function setNotRevealedURI(string memory _newNotRevealedUri) public onlyRole(CONTRACT_MANAGER) {
        notRevealedUri = _newNotRevealedUri;
    } 

    function setBaseExtension(string memory _newBaseExtension) public onlyRole(CONTRACT_MANAGER) {
        baseExtension = _newBaseExtension;
    }    

    function reveal() public onlyRole(CONTRACT_MANAGER) {
        revealed = true;
    }

    function pause() public onlyRole(CONTRACT_MANAGER) {
        _pause();
    }

    function unpause() public onlyRole(CONTRACT_MANAGER) {
        _unpause();
    }

    

    // only role admin
    function addContractManager(address _newContractManager) public onlyRole(ROLE_ADMIN) {
        _grantRole(CONTRACT_MANAGER, _newContractManager);
    }
  
    function removeContractManager(address _contractManager) public onlyRole(ROLE_ADMIN) {
        _revokeRole(CONTRACT_MANAGER, _contractManager);
    }


    function addWhiteListManager(address _newWhiteListManager) public onlyRole(ROLE_ADMIN) {
        _grantRole(WHITE_LIST_MANAGER, _newWhiteListManager);
    }
  
    function removeWhiteListManager(address _whiteListManager) public onlyRole(ROLE_ADMIN) {
        _revokeRole(WHITE_LIST_MANAGER, _whiteListManager);
    }



    // only project owner
    function setWhiteListCost(uint256 _newWhiteListCost) public onlyRole(PROJECT_OWNER) {
        whiteListCost = _newWhiteListCost;
    }

    function setPublicCost(uint256 _newPublicCost) public onlyRole(PROJECT_OWNER) {
        publicCost = _newPublicCost;
    }

    function setMaxMintAmount(uint8 _newMaxMintAmount) public onlyRole(PROJECT_OWNER) {
        maxMintAmount = _newMaxMintAmount;
    }


    function addRoleAdmin(address _newRoleAdmin) public onlyRole(PROJECT_OWNER) {
        _grantRole(ROLE_ADMIN, _newRoleAdmin);
    }
  
    function removeRoleAdmin(address _roleAdmin) public onlyRole(PROJECT_OWNER) {
        _revokeRole(ROLE_ADMIN, _roleAdmin);
    }


    function transferProjectOwnerRole(address _newProjectOwner) public onlyRole(PROJECT_OWNER) {
        _grantRole(PROJECT_OWNER, _newProjectOwner);
        renounceRole(PROJECT_OWNER, msg.sender);
    }
    
    function withdraw() public payable onlyRole(PROJECT_OWNER) {
        (bool success, ) = payable(msg.sender).call{value: address(this).balance}("");
        require(success);
    }



    // overrides
    function tokenURI(uint256 tokenId) 
        public 
        view 
        virtual 
        override 
        returns (string memory) 
    {
        require(
            _exists(tokenId),
            "ERC721Metadata: URI query for nonexistent token"
        );
    
        if(revealed == false) {
            return notRevealedUri;
        }

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

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

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

I'm using Remix 0.8.7

Hi @HeyItsFinn. The role id shown is for CONTRACT_MANAGER. The hash of "CONTRACT_MANAGER" is 0x9aa032fddc5828092899c540d004a5a5ff64ae9e644e14e3e1589b650db53b9e.

It's saying that your account doesn't have the contract manager role. You need an admin (in this case someone with the ROLE_ADMIN role) to grant contract manager to this account using the grantRole function.

1 Like

How to do that? how can I role to the role_admin can u please elaborate on this with detailed explanation
thankyou for advance

Hi @Divyansh_singh ,

the error is raised because the method setBaseURI who requires CONTRACT_MANAGER role, is called before assign that role to msg.sender

just moving down the calls to setBaseURI and setNotRevealedURI right after the setting of roles should fix the problem