- Declare an interface and list its restrictions.
- Implement an interface in a contract.
- Call another deployed contract through its interface.
- Compare interfaces with abstract contracts.
What is an Interface?
- In programming, an interface separates what a component can do (function declarations) from how it does it (implementation).
- In Solidity an interface is a contract-like declaration that lists functions without bodies. Any contract that implements it promises to provide those functions.
- It acts as the skeleton of a smart contract: other developers can build on it and be sure which functions exist and what they accept and return.
- Interfaces are declared with the
interfacekeyword and, by convention, their names start with I (e.g.IERC20).
Why do we use Interfaces?
- To interact with existing contracts on the blockchain without having their source code – you only need the function signatures and the contract address, e.g. reading any ERC-20 token’s balance through
IERC20. - To define standards: token standards such as ERC-20 and ERC-721 are expressed as interfaces, and libraries like OpenZeppelin provide ready implementations to customise.
- To decouple code: a contract can depend on an interface, so different implementations can be used without changing the caller.
Interface Restrictions
Interfaces are a more restricted form of abstract contract. They:
- cannot declare state variables;
- cannot declare a constructor;
- cannot declare modifiers;
- cannot implement any function – functions have no body;
- must declare all functions as
external; - cannot inherit from contracts, but can inherit from other interfaces.
Interfaces may declare events, custom errors, structs and enums. All interface functions are implicitly virtual, so implementing functions do not need to repeat it.
Interface vs Abstract Contract
| Feature | Interface | Abstract contract |
|---|---|---|
| Implemented functions | Not allowed | Allowed (some may be unimplemented) |
| State variables | Not allowed | Allowed |
| Constructor / modifiers | Not allowed | Allowed |
| Function visibility | external only | Any |
| Can be deployed? | No | No |
Example : Declare, Implement and Use an Interface
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface ICounter {
event Incremented(uint newValue);
function increment() external;
function count() external view returns (uint);
}
// implementing the interface
contract Counter is ICounter {
uint private value;
function increment() external override {
value += 1;
emit Incremented(value);
}
function count() external view override returns (uint) {
return value;
}
}
// using ANY contract that follows ICounter, knowing only its address
contract CounterUser {
function bump(address counterAddress) external returns (uint) {
ICounter c = ICounter(counterAddress); // wrap the address in the interface
c.increment();
return c.count();
}
function counterInterfaceId() external pure returns (bytes4) {
return type(ICounter).interfaceId; // used by ERC-165 interface detection
}
}
Key Takeaways
- An interface lists external function declarations only – no state, constructor, modifiers or bodies.
- Wrap an address in an interface (
ICounter(addr)) to call another contract. - Token standards (ERC-20, ERC-721) are defined as interfaces.
The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.
Question / Answer (15 questions)
contract Counter is ICounter { function increment() external override { ... } }.ICounter(addr).increment();.MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- Solidity Team. (n.d.). Contracts (creating contracts, visibility and getters, function modifiers, constant and immutable, functions, events, custom errors, inheritance, abstract contracts, interfaces, libraries). Solidity Documentation. https://docs.soliditylang.org/en/latest/contracts.html
- Vogelsteller, F., & Buterin, V. (2015). ERC-20: Token Standard. Ethereum Improvement Proposals, no. 20. https://eips.ethereum.org/EIPS/eip-20
- Ethereum Improvement Proposals. (2018). ERC-165: Standard Interface Detection. https://eips.ethereum.org/EIPS/eip-165
- OpenZeppelin. (n.d.). OpenZeppelin Contracts Documentation. https://docs.openzeppelin.com/contracts/
- Modi, R. (2018). Solidity Programming Essentials: A beginner’s guide to build smart contracts for Ethereum and blockchain. Packt Publishing. ISBN 978-1-78883-138-3.