Easiest way to add a function to ERC20.sol in ERC20PresetMinterPauser

Hi there,

I have a ERC20PresetMinterPauser contract in rinkeby. What would be the easiest way to add a function to the ERC20.sol contract and then upgrade the token?

Thanks,

CAA

1 Like

Hi @CIX,

If you deployed an upgradeable contract with the ERC20PresetMinterPauser as the implementation, you could extend from this contract as a V2 and include the additional function.

For example, upgrading Box to BoxV2, we can extend from Box and add the new function.

// contracts/BoxV2.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;

import "./Box.sol";

contract BoxV2 is Box {
    // Increments the stored value by 1
    function increment() public {
        store(retrieve() + 1);
    }
}
1 Like

Thanks I tried but I get “Nothing to compile, all contracts are up to date. Contract undefined not found”

{
  "contracts": {},
  "solidityLibs": {},
  "proxies": {
    "@openzeppelin/contracts-ethereum-package/ERC20PresetMinterPauserUpgradeSafe": [
      {
        "address": "0x747B04a0d3DF924eC71BB149B667BcDa7c1dEe4F",
        "version": "3.0.0",
        "implementation": "0x60c85f7C95A29a4265B2Ae6A3558a51b98FD539E",
        "admin": "0x991D5A711fF4Bf2420Eb28EA0aaF6c8Ef750F3F7",
        "kind": "Upgradeable"
      }
    ]
  },
  "manifestVersion": "2.2",
  "version": "1.0.0",
  "dependencies": {
    "@openzeppelin/contracts-ethereum-package": {
      "package": "0xa44bb80b290dE8a465d17B14269dF53CF0B9Bf4f",
      "version": "3.0.0"
    }
  },
  "proxyAdmin": {
    "address": "0x991D5A711fF4Bf2420Eb28EA0aaF6c8Ef750F3F7"
  }
}

    // contracts/ChangeName.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "@openzeppelin/contracts-ethereum-package/contracts/presets/ERC20PresetMinterPauser.sol";

contract ChangeName is ERC20PresetMinterPauserUpgradeSafe {
    string private _name;

    function changeTokenName(string memory name) public {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have Admin_Role role to change name");
        _name = name;
    }
}
1 Like

Hi @CIX,

You are currently using OpenZeppelin Contracts Ethereum Package. If you are creating a new upgradeable contract (rather than upgrading as you can’t upgrade between Contracts Ethereum Package and Contracts Upgradeable) then you should use OpenZeppelin Contracts Upgradeable. https://docs.openzeppelin.com/contracts/4.x/upgradeable.

As an aside, I would avoid changing a token name where possible, as this may or may not be supported by various clients.

Are you upgrading an existing contract?