4

I try to override ERC20 methods ... The doc says it should be possible but I get the error:

CompileError: @openzeppelin/contracts/token/ERC20/ERC20.sol:96:5: TypeError: Trying to override non-virtual function. Did you forget to add "virtual"?
function totalSupply() public view override returns (uint256) {
^ (Relevant source part starts here and spans across multiple lines).

The method:

function totalSupply() public view override returns (uint256) {
    return _totalSupply;
}

OpenZeppelin version: 3.3.0

Any idea of what could it be? (I'm very new to Solidity but I think that should work?)

EDIT Taked a bad excample... but I get this error for every function thats in ERC20 ...

2

2 Answers 2

0

You are correct. It is possible to override only those functions that are declared as virtual.

For v3.3 if you inherit from ERC20 then you can override:

  • transfer
  • allowance
  • approve
  • transferFrom
  • increaseAllowance
  • decreaseAllowance
  • _transfer
  • _mint
  • _burn
  • _approve
  • _beforeTokenTransfer

To override any other function modify a copy of ERC20.sol and inherit from it.

0

Functions marked with the override keyword, override virtual functions. Functions in interfaces are by default virtual whereas virtual methods outside an interface must be explicitly marked with the virtual keyword.

Therefore, make sure to import IERC20.sol (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.3/contracts/token/ERC20/IERC20.sol) in your ERC20 contract. Next you should be able to override every method of the interface.

5
  • I dont want to take IERC20 because I also want to use mint/burn ... but I can't if I make the contract an IERC20 (that you meant by importing right? and importing ofcause) So I need to import IERC20 and write my own mint/burn or is there another way? - btw. mint/burn I can override Sorry for y noobish questions xD
    – MaTok
    Dec 19, 2020 at 17:32
  • Why can't you use the mint/burn functions of the OpenZeppelin contract ? I don't understand.
    – clement
    Dec 19, 2020 at 17:57
  • By the way, if you use the OZ ERC20 contract without IERC20.sol it will not compile.
    – clement
    Dec 19, 2020 at 17:59
  • I mint/burn is in ERC20... to override other functions I need ERC20 ... so is there a way to benefit from both? Like want to override totalsupply() but also using mint/burn e.g.?
    – MaTok
    Dec 19, 2020 at 18:25
  • > By the way, if you use the OZ ERC20 contract without IERC20.sol it will not compile It will ERC20.sol imports IERC20.sol (openzeppelin/[email protected])
    – MaTok
    Dec 19, 2020 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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