Why is the ERC20 inheriting from IERC

Why is the ERC20 contract inheriting from IERC20 and IERC20Metadata? It's just overriding the functions. Wouldn't it be the same thing without inheriting, but less code?

Feels like I'm missing something.

Thanks!

2 Likes

IERC20 and IERC20Metadata are interfaces, not contracts. there is no extra code. there is interfaces and an implementation

1 Like

The IERC20, IERC20Metadata are Interface that must be part of code of a ERC20 contract code. After the Interface, there will be its implementation in the token contract code, so it is important part of the code and how to structure a ERC20 token contract code.

I understand that part, as well as @helio.rosa 's explanation. But why not just implement the necessary functions, why do you have to inherit from them?

Interfaces contain code as well, on deployment this code needs to be written to the blockchain, every piece of storage will cost you something, not sure how interfaces could be an exception to this.

Anyway, that's besides the point, it still doesn't tell me why you would inherit from these interfaces, instead of just implementing the functions they contain (without inheriting).

1 Like

Oh, it must actually not be part of your code, but you must implement all of the functions in your code. Interfaces only make it better and won't make you forget to implement all of the functions.

Interfaces contain code as well

Can you show me an example of that?? Interfaces contain definitions used by the compiler.

Anyway, that's besides the point, it still doesn't tell me why you would inherit from these interfaces, instead of just implementing the functions they contain (without inheriting).

Because it's a cleaner design. Contracts only need to import the interface (instead of the full contract code) to interact with it. Starting from an interface ensures that your contract implements it correctly.

For example: to interact with a ERC20 token you only need to include it's interface not the full contract code.

interface IA {
  function x() external view returns (int);
  function y() external view returns (int);
}

contract Xyz is IA {} // this tell the compiler that the contract Xyz needs to implement IA (contain functions x and y)

constract Abc {
  function qwe(address t) external {
    IA(t).x(); // this tells the compiler that t implements IA and you want to call method x
  }
}
2 Likes