How to test for roles in AccessControl?

Hi @abcoathup - I’m using ERC20PresetMinterPauserUpgradeable and having a hard time testing roles. This works:

    const [deployer] = await ethers.getSigners();
    const isAdmin = await godToken.hasRole(DEFAULT_ADMIN_ROLE, deployer.address);
    expect(isAdmin).to.equal(true);

but this doesn’t:

    const [deployer] = await ethers.getSigners();
    const isMinter = await godToken.hasRole(
      ethers.utils.keccak256(ethers.utils.formatBytes32String('MINTER_ROLE')),
      deployer.address
    );
    expect(isMinter).to.equal(true);

From the look of AccessControl.sol, MINTER_ROLE is assigned when ERC20PresetMinterPauserUpgradeable is initialized, so I’m at a loss. Any ideas?

3 Likes

Nevermind - got it! For anyone looking in the future:

ethers.utils.keccak256(ethers.utils.toUtf8Bytes('MINTER_ROLE')),
3 Likes

Hi @gigamesh,

Welcome to the community :wave:

Glad you were able to resolve and thanks for sharing your solution. I have marked it as the solution.

Another way to get the value is to read it from the contract myContract.MINTER_ROLE().

5 Likes