Alternative Method to Bypass the tx.origin Change in OpenZeppelin Contracts: Use _transferOwnership in Use Hello in the Main Constructor · pcaversaccio/xdeployer · Discussion #18 (original) (raw)

Instead of Ownable it's better to use AccessControl:

import "@openzeppelin/contracts/access/AccessControl.sol";

contract XXX is ERC20, AccessControl { bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");

Change msg.sender to tx.origin in the _grantRole and _mint calls:

constructor() ERC20("XXX", "XXX") {
    _grantRole(DEFAULT_ADMIN_ROLE, tx.origin);
    _grantRole(MINTER_ROLE, tx.origin);

    _mint(tx.origin, 1000 * 10 ** decimals());

To make sure only addresses that are in MINTER_ROLE can call public function mint use onlyRole modifier:

function mint(address to, uint256 amount) public onlyRole(MINTER_ROLE) { _mint(to, amount);