Here is my solution.
2_token_migration.js
const ERC20 = artifacts.require("MyToken2");
module.exports = function () {
deployer.deploy(ERC20, "MyToken2", "MTN2", 10000);
};
token2.sol
contract MyToken2 is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
uint256 private _cap;
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant CAP_ROLE = keccak256("CAP_ROLE");
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*
* See {ERC20-constructor}.
*/
constructor(string memory name, string memory symbol, uint256 cap_) ERC20(name, 100) {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
_setupRole(CAP_ROLE, _msgSender());
require(cap_ > 0, "ERC20Capped: cap is 0");
_cap = cap_;
}
/**
* @dev Change the token's total supply.
*/
function changeCap(uint newValue) public virtual {
require(hasRole(CAP_ROLE, _msgSender()), "MyToken2: must have cap role to change cap");
_changeCap(newValue);
}
/**
* @dev Triggers the change of the token's total supply.
*/
function _changeCap(uint _newValue) internal virtual {
_cap = _newValue;
}
/**
* @dev Returns the cap on the token's total supply.
*/
function cap() public view virtual returns (uint256) {
return _cap;
}
/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "MyToken2: must have minter role to mint");
require(ERC20.totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
_mint(to, amount);
}
/**
* @dev Pauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_pause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "MyToken2: must have pauser role to pause");
_pause();
}
/**
* @dev Unpauses all token transfers.
*
* See {ERC20Pausable} and {Pausable-_unpause}.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public virtual {
require(hasRole(PAUSER_ROLE, _msgSender()), "MyToken2: must have pauser role to unpause");
_unpause();
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20, ERC20Pausable) {
super._beforeTokenTransfer(from, to, amount);
}
}
Truffle Console
truffle(develop)> i = await MyToken2.deployed()
undefined
truffle(develop)> i.cap()
<BN: 2710>
truffle(develop)> i.changeCap(100000)
{ tx:
'0x9b1c7aa746a30bbfe680c4542e4105023edf05ac29e9264f90265101926b74e9',
receipt:
{ transactionHash:
'0x9b1c7aa746a30bbfe680c4542e4105023edf05ac29e9264f90265101926b74e9',
transactionIndex: 0,
blockHash:
'0x6f2a8747f10970d3a856ff6a60aeb9c312e830ed44f803ffd2ca124eb1d00128',
blockNumber: 57,
from: '0x32eaf6dc79f055eb5e0a7760ee34f26fd213f895',
to: '0x3f9d66e33aa6ab9c509e52e1829d75aaaf501822',
gasUsed: 27876,
cumulativeGasUsed: 27876,
contractAddress: null,
logs: [],
status: true,
logsBloom:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
rawLogs: [] },
logs: [] }
truffle(develop)> i.cap()
<BN: 186a0>