1

In solidity 0.7.x, I am creating an interface of contract A in contract B to allow contract B call functions in contract A.

contract A {
  function transfer(address recipient, uint256 amount) public override returns (bool) {
        //definition goes here
    }
}

in contract B, the interface is:

interface A {
    function transfer(address recipient, uint256 amount) external returns (bool) //changed public to external and removed override
 }

Is this definition of interface correct? I used to copy and paste the function definition to the interface. However there was some error asking to remove override and use external instead. I don't quite understand what causes error.

2

Does the function listed have to be exactly the same.

No. The interface has to be the same, not the function. You have encountered a subtle difference. It might help to think of the interface as something that emerges from the function design but it is certainly not the only thing, since there is code. This does not work in reverse. One cannot infer the complete function from an interface. There are things going in function definitions that are not needed in interface descriptions. So, when starting with a function, use a subtractive process to remove everything that you don't need to describe the interface.

use external instead

Right. Interfaces are, by definition, external, so that is clearly part of the interface description. public does other internal things such as making it accessible and writable to contracts that inherit the contract. Those internal concerns have no bearing on the interface, so public doesn't belong there.

In the implementation contract, public will indeed produce an external function as a component of a basket of features. So, external is the correct description of the interface that an implementation-level public function will generate.

asking to remove override

Another internal concern. It tells the compiler "I know" that the function signature is the same as another function already seen. That's not important for the description of the interface.

custom modifiers

You will also find that custom modifiers don't belong. For example, onlyOwner picks up code that executes within the function but that code has no bearing on the ABI, so you will want to strip modifiers like that out of the interface description.

pure, view, payable

These compiler-level "modifiers" are important in the interface description, so they stay.

Confused?

As a general heuristic, nothing that wouldn't appear in an ABI description of the surface area would be important for an interface { description of the same. So, if it affects the ABI it stays, in the most minimalist way describable (external, payable, view) and if it's internal (onlyOwner) remove it.

Verify

The compiler provides a simple method that will confirm that an interface is correctly composed. You should use it.

contract A is AInterface {

The contract will not deploy if there is any function in the interface that is not implemented in the contract. You can interpret that as a mismatch.

Hope it helps.

1
  • Rob Hitchens, your answer is way more than what I have expected. Many thanks! – user938363 Dec 24 '20 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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