On the hardhat console, I tried calling the mint()
function in my token (built from the ERC20PresetMinterPauserUpgradeable.sol
contract) and received this error:
Here’s what my contract looks like:
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.6.2 <0.8.1;
import "@openzeppelin/contracts-upgradeable/proxy/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/GSN/ContextUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20BurnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20PausableUpgradeable.sol";
contract MyToken is
Initializable,
ContextUpgradeable,
AccessControlUpgradeable,
ERC20BurnableUpgradeable,
ERC20PausableUpgradeable
{
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
string private newName;
string private newSymbol;
bool private isPaused;
uint256 private initialSupply;
event InitializedToken(string newName, string newSymbol);
event MintedTokens(uint256 newInitialSupply);
event PausedAllTokenTransfers(bool paused);
/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
* account that deploys the contract.
*/
function initialize(string memory name, string memory symbol)
public
virtual
initializer
{
__Context_init_unchained();
__AccessControl_init_unchained();
__ERC20_init_unchained(name, symbol);
__ERC20Burnable_init_unchained();
__Pausable_init_unchained();
__ERC20Pausable_init_unchained();
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(MINTER_ROLE, _msgSender());
_setupRole(PAUSER_ROLE, _msgSender());
newName = name;
newSymbol = symbol;
emit InitializedToken(newName, newSymbol);
}
function getTokenName() public view returns (string memory) {
return newName;
}
function getTokenSymbol() public view returns (string memory) {
return newSymbol;
}
/**
* @dev Creates `amount` of tokens for `to`.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public {
require(
hasRole(MINTER_ROLE, _msgSender()),
"MyToken: must have minter role to mint"
);
_mint(to, amount);
initialSupply = amount;
emit MintedTokens(initialSupply);
}
function getTokenSupply() public view returns (uint256) {
return initialSupply;
}
/**
* @dev Pauses all token transfers.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function pause() public {
require(
hasRole(PAUSER_ROLE, _msgSender()),
"MyToken: must have pauser role to pause"
);
_pause();
isPaused = true;
emit PausedAllTokenTransfers(isPaused);
}
/**
* @dev Unpauses all token transfers.
*
* Requirements:
*
* - the caller must have the `PAUSER_ROLE`.
*/
function unpause() public {
require(
hasRole(PAUSER_ROLE, _msgSender()),
"MyToken: must have pauser role to unpause"
);
_unpause();
isPaused = false;
emit PausedAllTokenTransfers(isPaused);
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal override(ERC20Upgradeable, ERC20PausableUpgradeable) {
super._beforeTokenTransfer(from, to, amount);
// do stuff before every transfer
// e.g. check that vote (other than when minted)
// being transferred to registered candidate
// emit SomeEventBeforeTokenTransfer();
}
}
Any ideas on what I’m doing wrong? I have no idea
Edit:
The contract address I’m using as mint()
's first argument is the address which MyToken was deployed to.