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:
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.
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.
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()).
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.
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().
领英推荐
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.
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.
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.
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 .