ERC20 Methods – Can’t Override ERC20 Methods in Solidity

interfacesopenzeppelin-contractssolidity

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 …

Best Answer

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.

Related Topic