Skip to main content

Command Palette

Search for a command to run...

Interfaces

Updated
3 min readView as Markdown
Interfaces

Interfaces

  1. 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.

  2. An interface is declared using the interface keyword, 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);
}
  1. In this example, we define an interface named Token that specifies two function signatures: transfer and balanceOf. Any contract that implements these two functions with the same function signature as defined in the interface is said to conform to the Token interface.

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];
    }
}
  1. In this example, the MyToken contract implements the transfer and balanceOf functions with the same function signature as defined in the Token interface. This means that MyToken conforms to the Token interface, and can be used anywhere that requires a contract that conforms to the Token interface.

  2. 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:

  1. An interface cannot contain any state variable declarations.

  2. An interface can only contain function declarations, but not function implementations.

  3. All functions in an interface must be declared as external.

  4. All functions in an interface must be declared as pure or view or both.

  5. All functions in an interface must have the returns keyword, even if the function returns void.

  6. A contract that implements an interface must implement all the functions in the interface with the same function signature, including the function visibility (public or external) 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.