Interface in Solidity

Interface in Solidity

Interface

Interfaces are basically used to interact with the contracts that are already deployed on the Blockchain. We need a way to call the functions from those contracts and that’s where interfaces are helpful.

Let’s see this with an example. For example, I want to interact with the contract CodiesAlert that is already deployed on the Blockchain and the contract address is 0x7EF2e0048f5bAeDe046f6BF797943daF4ED8CB4

If you want to try it along then copy the below code and deploy it in remix IDE.

				
					// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract CodiesAlert {
    string public Name;
    function setName(string memory _name) public {
        Name = _name;
    }

    function getName() public view returns(string memory) {
        return Name;
    }
}
				
			

Create a new file in remix IDE and this is where we will define the interface to interact with the contract we had deployed in our previous step.

Declare the interface with the interface keyword and normally interface name starts with “I“. If you notice carefully we just gave the function definitions inside the interface ICA and nothing else. Since interfaces are used just to call the function from the deployed contract then we don’t need to implement anything inside. This is just the way to tell the EVM what functions we want to call.

To access the functions from the deployed contract we need to give the address of the deployed smart contract so that solidity can load the functions from there. 

Now you can call any function from the smart contract that is defined inside the interface like this.

ICA(0x7EF2e0048f5bAeDe046f6BF797943daF4ED8CB47).setName(“Anni”);

It is not necessary to define all the smart contract functions inside the interface, you can just define setName if you just want to set the Name and nothing else.

				
					// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

interface ICA {
    function setName(string memory _name) external;
    function getName() external view returns(string memory);
}

contract Access {
    function callSetName() public {
        ICA(0x7EF2e0048f5bAeDe046f6BF797943daF4ED8CB47).setName("Anni");
    }

    function returnName() public view returns(string memory) {
        return ICA(0x7EF2e0048f5bAeDe046f6BF797943daF4ED8CB47).getName();

    }
}
				
			

Interface restrictions

  • They cannot inherit from other contracts, but they can inherit from other interfaces
  • Functions of an interface can be only of type external
  • They cannot declare a constructor
  • They cannot declare state variables

You cannot declare state variables inside the interface however you can declare enum and struct. See the below example.

				
					interface ICA {
    enum orderState {ordered, shipped, delivered}
    struct vehicle {string car; string truck;}
    function setname(string memory _name) external;
}
				
			

Conclusion

Interafaces are helpful to interact with other contracts and call functions.

Leave a Reply

https://www.linkedin.com/company/77619036/admin/

welcome.

We connect you to a world of houseplants and urban gardening tailored to your home

Bloomer

Login to your account