Contracts-upgradeable and AccessControl DEFAULT_ADMIN_ROLE

I’ve just switched to the @openzeppelin/contracts-upgradeable.

My contract inherits from ERC20PresetMinterPauserUpgradeable which in turn inherits AccessControlUpgradeable.

After deploying with upgrades.deployProxy(...) the deploying address (which was the owner before switching to upgradeable) does no longer have the DEFAULT_ADMIN_ROLE and can therefore no longer grant other roles.

getRoleAdmin for all roles returns 0x00....0

How do I set this up correctly to have some account that can grant roles?

Hi, I think the admin role for all roles is DEFAULT_ADMIN_ROLE by default, and it is set as bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00; so when you use getRoleAdmin() it will return zero address, if you want to change it, I think you can have a look at the function _setRoleAdmin()

BTW, if you want to set permission for some functions, you should use hasRole() to restrict, and roles can be granted and revoked dynamically via the grantRole() and
revokeRole() functions.

You’re probably not calling this contract’s initialize function, which is what would initialize the roles.

Make sure that your contract’s own initializer function invokes the parent’s.

I am calling the initialize function:

function initializeContract(
    string memory _name, 
    string memory _symbol, 
  ) public initializer {
    ERC20PresetMinterPauserUpgradeable.initialize(_name, _symbol);

and in the deploy script

  { initializer: 'initializeContract(string,string,uint)' }

But you’re saying this should work in general and there is no extra steps I have to do during deployment to grant the role?

No idea why, but seems to work after redeploying. Maybe just some hardhat issue on local node.


1 Like