Interfaces in Solidity

Dilara
Coinmonks
Published in
3 min readJan 16, 2024

--

An interface defines the set of functions that an instance of a smart contract that uses this interface could respond to. Interfaces are like skeletons or templates. We may think of an interface like a cookie cutter. When someone uses this, the cookie figure must be like we want. But the cookies’ taste, the dough’s color, or how long it cooked depends on who made those cookies. Ultimately, we have a cookie with the figure of what we want. But taste, color, and cooking time can be different; it’s all chef’s choice.

When we use interfaces, we know what we will get. Also, because of the similarity to skeletons, interfaces give the developers freedom of choice in how they do. On condition that, depending on the interface, they can do what they want.

We can interact with other contracts through interfaces. Interfaces are used for standardization. That means when a contract is compatible with an interface, then it has all the functions that the interface has. Thus, we know what we interact with. Also, we can call the functions that just the interface has. A smart contract can have extra functions, but we don’t know about these functions. And we should use interfaces when we do not need them.

If we inherit a contract, we don’t update the state of that contract when we call a function of that contract. Because you just call the function that is copied in your contract. So, you update the state of your contract. That function you called is not the other smart contracts function. But with interfaces, a function call updates the state of the other contract. This is because you call the function in the deployed smart contract.

In addition, if we want to call just a few functions, inheriting that contract does not make sense. Copying all the code is not necessary in that situation. Interfaces allow us to call functions without not copying all the code of that contract. Also, when a smart contract is deployed, we can’t inherit it. This is another benefit of the interfaces.

Here are some properties for interfaces:

  • Can’t have a constructor and state variables.
  • Can’t have function implementations.
  • All the declared functions in the interface must be external.

How can we use interfaces?

Here is a smart contract we need to call:

https://solidity-by-example.org/interface/

Here is an interface that we will use:

And here is our contract:

What we do here is call the Counter contracts functions with the interface. ICounter(_counter) creates the instance of a Counter contract. Thus, we can call the increment function.

In this example, the interface and our contract are in the same file. We should import the interface to our contract file if they are not the same file. Also, you can see usage like this:

When we want to create an instance of an interface, we should set the type of the variable as the interface name. The _address is the smart contracts address that we want to make the call.

I hope it can be helpful. Here are the sources that helped me to understand:

https://ethereum.stackexchange.com/questions/136513/why-do-we-need-interfaces-in-solidity

--

--