I deployed a ERC20Mintable token in truffle environment(the source file is ERC20Mintable.sol).

The mint function is declared with onlyMinter modifer:

contract ERC20Mintable is ERC20, MinterRole {
    function mint(address account, uint256 amount) public onlyMinter returns (bool) {
        _mint(account, amount);
        return true;

Minter role info is:

truffle(develop)> instance.isMinter(accounts[0])
truffle(develop)> instance.isMinter(accounts[1])
truffle(develop)> instance.isMinter(accounts[2])
truffle(develop)> instance.isMinter(accounts[3])

However,when I try to mint token, I found the modifier can not take effect to control the function call right:

For the first account:

truffle(develop)> instance.mint(accounts[0],1000)
{ tx:
   { transactionHash:
truffle(develop)> instance.totalSupply()
<BN: 3e8>//1000

For the second account:

truffle(develop)> instance.mint(accounts[1],1000)
{ tx:
   { transactionHash:
truffle(develop)> instance.totalSupply()
<BN: 7d0>//2000
truffle(develop)> instance.balanceOf(accounts[0])
<BN: 3e8>//1000
truffle(develop)> instance.balanceOf(accounts[1])
<BN: 3e8>//1000
truffle(develop)> instance.balanceOf(accounts[2])
<BN: 0>

From the result, every account is able to mint token, onlyMinter has no capability to control function call role.

How can I fix the problem? Thanks.

  • 1
    read the comments for the mint function github.com/OpenZeppelin/openzeppelin-solidity/blob/…. The argument passed is the recipient. That is not the caller of the function. – Sanjay S B Jul 8 at 4:36
  • 1
    Got it! how can I change the account in truffle env in order to test other account's function? – Ntydrm Jul 8 at 6:50
  • token.mint(accounts[9], "10000000000000000000", {from:accounts[0]}) – abcoathup Jul 8 at 9:16

To manually test the MinterRole you need to change the account you are sending the transaction from. e.g. accounts[0] has the MinterRole, accounts[1] does not have the MinterRole so reverts when you attempt to mint.

truffle(develop)> accounts = await web3.eth.getAccounts()
truffle(develop)> token.mint(accounts[9], "10000000000000000000", {from:accounts[0]})
{ tx: ....
truffle(develop)> token.mint(accounts[9], "10000000000000000000", {from:accounts[1]})
{ Error: Returned error: VM Exception while processing transaction: revert MinterRole: caller does not have the Minter role -- Reason given: MinterRole: caller does not have the Minter role.

OpenZeppelin documentation has a guide on tokens: https://docs.openzeppelin.org/v2.3.0/tokens

If you have questions about using OpenZeppelin you can also ask questions in the community forum: https://forum.zeppelin.solutions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.