1

I've noticed that all OpenZeppelin's token contracts import their interfaces into them.

  1. Why does OpenZeppelin import and inherit its own contract interface to the contract?
  2. Does importing a contract's interface increase the gas cost to deploy?
  3. If I want to call another smart contract, like an ERC20, would it be cheaper to import the ERC20 contract with its interface? Or just importing its interface?
import "./IERC20.sol";

contract A {
  IERC20 token = IERC20(address);
}

------------------------------------------------------------------------
// Or:

import "./ERC20.sol" // <- This contract inherits the IERC20 interface

contract B {
  ERC20 token = ERC20(address);
}

2 Answers 2

1

There is a difference between Inheriting vs Importing another contract.

Import

 import "./IERC20.sol";
    
    contract A {
      IERC20 token = IERC20(address);
    }

Inheritance

import "./IERC20.sol";

contract A is IERC20 {
  //Do some logic
}

Import is required if you want Contract A to know how to handle IERC20 contract so you could call/delegate requests to it. It doesn't increase the size of contract A

On the other hand, Inheritance allows Contract A to literally inherit the logic and functionality from IERC20. Meaning that A inherits all functions from IERC20 that have public or internal access modifiers. NOTE:(Not sure if Inheritance will increase the gas cost by much, as the contract you are inheriting in the first place also has to be deployed)

When writing SmartContracts you don't put entire logic into a single big contract. There are many reasons for it.

1.Smart Contracts have a limit to their size,24kb. Importing them allows you to circumvent this (You are not importing the entire contract, you are importing its location, its structure), which you need to call upon it latter.

2.The SmartContracts are generally immutable once deployed, so if you need to change anything, you will likely have to re-deploy the entire contract. So it's better to have the logic spread over multiple smaller contracts.

At least, that had been my experience.

3
  • 1
    I would correct your answer by saying that importing the contract does not bring with it its address, only its structure. The contract address you need to know and tell the other contract, as you did in your code. Feb 21, 2022 at 12:26
  • Thanks, I should have explained that better. Will edit
    – Sky
    Feb 21, 2022 at 12:40
  • I think the right language to use would be "implementing an interface" and "extending a contract". Feb 21, 2022 at 12:50
0

I think it is not an obligation, it is a choice. It looks more systematic.

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.