What are Interfaces in Solidity?

What are Interfaces in Solidity?

Interfaces are similar to abstract contracts and are created using interface keyword. There are some characteristics for interfaces. Following are the key ones:

  • Interface can not have any function with implementation.
  • Functions of an interface can be only of type external.
  • Interface can not have constructor.
  • Interface can not have state variable.
  • Interface can have enum, structs which can be accessed using interface name dot notation.

The real value of interfaces is in what they really allow us to do - to connect information. We don't have to copy and paste multiple contracts code over and over, we can access contract information through interfaces so we can connect our contracts in a way where we do not have to copy and paste code. Let's see how this works. We will start off with just a contract.

No alt text provided for this image

To interact with this contract without copy and pasting the code we will use interfaces. Let's create it. It will do two things - it will increment and it is going to count.

No alt text provided for this image

This interface is going to connect to the Counter contract. Think of it as a device that is going to connect. We have a Counter that can count and we have an interface just following all the rules of the interfaces, and it is going to count as well. How do we tie this all together? We will create another contract and we will see that we can interact between first contract and last one with this interface.

In the last contract we will have a function called incrementCounter, which will take an address as input and the address is going to count. It will be external. Our contract should be able to increment, so we can do that by bringing in ICounter (name of the interface) and set what we want the counter to be and we want it to be the address (_counter). Next we can grab our increments, we can count (increment()).

No alt text provided for this image

So what is happening here? We are going through our ICounter, then the address that we are going to be putting in is going to incrementing per addresses, it is going to trigger the increment() from the first contract and it is going to add one to the function increment. So we are actually grabbing the function from the first contract, we didn't inherit anything, we are interfacing it, we are connecting to it.

Now, let's create another function (getCount) so we could see the incriminations happening in the contract.

No alt text provided for this image

Let's recap. We have an interface (ICounter) which has count and an increment. First contract (Counter) has a variable (count) and a function (increment). We a connecting them in our last contract (MyContract) with .increment() and .count().

No alt text provided for this image

So through the interface (ICounter) we are going to be able to interact between both contracts, and we are not going to have to copy and paste these functions out. This is really great, especially when we are making expanded multi complex, reusable contracts.

Let's see it in action. We will deploy both contracts. For example I want retrieve the address (we will copy the address of the first contract) and want to see how many times we have it. To see the interactions between contracts we will copy an address (remember we are counting addresses) in incrementCounter and in the getCount. Click on incrementCounter and after this on getCounter. We will se number 1, this mean that we have one address. If we want to increment again, we will click on the incrementCounter buttonand we will see number 2 and so on.

No alt text provided for this image

Remember that the count that we a grabbing in our last contract (MyContract) is a data storage, it is not a function (functions just running it), it is actually coming from or first contract (Counter). It is our state variable from Counter contract, we never declared it in MyContract.

To understand it better let's have a look at our deployed Counter contract. If we will click count we will see the same value like in MyContract.

No alt text provided for this image

Our state variable has now same access for both our contracts. If we will increment in first contract we will se that the value is changing in both contracts.

No alt text provided for this image

They are interacting! This is the power of the interface.

I uploaded this code to my GitHub account.

See you in the future articles about #smartcontract , #solidity , #web3 , #cryptocurrencies , #blockchain , #blockchaintechnology , #blockchaindeveloper .

要查看或添加评论,请登录

Ion Platon的更多文章

  • Storage vs Memory in Solidity

    Storage vs Memory in Solidity

    Understanding the storage and memory is not always intuitive. Having a good understanding of storage versus memory is…

  • What is Mapping in Solidity?

    What is Mapping in Solidity?

    If you are coming from other programming languages, then is good to know that the solidity mapping is similar to a…

  • Struct and Enum in Solidity

    Struct and Enum in Solidity

    A struct is a collection of key->value pairs similar to a mapping, but the values can have different types. Structs…

  • What are Libraries in Solidity?

    What are Libraries in Solidity?

    Libraries are similar to Contracts, but are mainly intended for reuse. A Library contains functions which other…

  • What is Inheritance in Solidity?

    What is Inheritance in Solidity?

    Let's take a closer inspection at Inheritance in Solidity. Inheritance is an advanced concept in #solidity and also in…

  • The Restricted Access Pattern in Solidity

    The Restricted Access Pattern in Solidity

    The Restricted Access to a Contract is a common practice. By default, a contract state is read-only, unless it is…

  • Build First Cryptocurrency in Testnet using Solidity and Remix

    Build First Cryptocurrency in Testnet using Solidity and Remix

    Creating own currency is a great way not only to solidify our coding or solidify our knowledge of building of smart…

  • The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    Let's take a look at Fallback Functions in solidity. We are going to open our eyes and become a little bit more…

  • Bytes vs Strings in Solidity

    Bytes vs Strings in Solidity

    Strings are values that are made up of ordered sequences of characters, such as "hello world!" A string can contain any…

  • Loops in Solidity

    Loops in Solidity

    Looping is a type of functionality in coding, which is very handy. Loops allow you to iterate through data and take…

社区洞察

其他会员也浏览了