Interfaces

Interfaces
In Solidity, an interface is similar to an abstract contract, in that it defines a set of function signatures without providing an implementation for those functions. An interface is used to specify a contract's external-facing API, without providing a full implementation of the contract.
An interface is declared using the
interfacekeyword, and can only contain function declarations, but not variable declarations or function implementations. Any contract that implements all the functions in an interface is said to conform to the interface.
Here's an example:
pragma solidity ^0.8.0;
interface Token {
function transfer(address to, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
- In this example, we define an interface named
Tokenthat specifies two function signatures:transferandbalanceOf. Any contract that implements these two functions with the same function signature as defined in the interface is said to conform to theTokeninterface.
Here's an example of a contract that conforms to the Token interface:
pragma solidity ^0.8.0;
contract MyToken is Token {
mapping (address => uint256) balances;
function transfer(address to, uint256 amount) public override returns (bool) {
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount;
balances[to] += amount;
return true;
}
function balanceOf(address account) public view override returns (uint256) {
return balances[account];
}
}
In this example, the
MyTokencontract implements thetransferandbalanceOffunctions with the same function signature as defined in theTokeninterface. This means thatMyTokenconforms to theTokeninterface, and can be used anywhere that requires a contract that conforms to theTokeninterface.Interfaces are commonly used in Solidity for defining APIs for other contracts or external applications to interact with. By defining a clear API using an interface, other contracts or applications can interact with a contract without needing to know its implementation details.
Rules For Writing Interface
Here are some important rules for interfaces in Solidity:
An interface cannot contain any state variable declarations.
An interface can only contain function declarations, but not function implementations.
All functions in an interface must be declared as
external.All functions in an interface must be declared as
pureorviewor both.All functions in an interface must have the
returnskeyword, even if the function returnsvoid.A contract that implements an interface must implement all the functions in the interface with the same function signature, including the function visibility (
publicorexternal) and the return type(s).
It's important to note that an interface is not a contract, and cannot be deployed on the blockchain. An interface is simply a way to specify a contract's external-facing API, without providing a full implementation of the contract.
Interfaces are commonly used in Solidity for defining APIs for other contracts or external applications to interact with. By defining a clear API using an interface, other contracts or applications can interact with a contract without needing to know its implementation details.



