2019-01-07 16:49:34 +00:00
|
|
|
.. index:: ! contract;interface, ! interface contract
|
|
|
|
|
|
|
|
.. _interfaces:
|
|
|
|
|
|
|
|
**********
|
|
|
|
Interfaces
|
|
|
|
**********
|
|
|
|
|
2021-06-25 10:40:01 +00:00
|
|
|
Interfaces are similar to abstract contracts, but they cannot have any functions implemented.
|
|
|
|
There are further restrictions:
|
2019-01-07 16:49:34 +00:00
|
|
|
|
2020-01-21 13:42:58 +00:00
|
|
|
- They cannot inherit from other contracts, but they can inherit from other interfaces.
|
2022-03-09 17:35:14 +00:00
|
|
|
- All declared functions must be external in the interface, even if they are public in the contract.
|
2019-01-07 16:49:34 +00:00
|
|
|
- They cannot declare a constructor.
|
|
|
|
- They cannot declare state variables.
|
2021-06-25 10:40:01 +00:00
|
|
|
- They cannot declare modifiers.
|
2019-01-07 16:49:34 +00:00
|
|
|
|
|
|
|
Some of these restrictions might be lifted in the future.
|
|
|
|
|
|
|
|
Interfaces are basically limited to what the Contract ABI can represent, and the conversion between the ABI and
|
|
|
|
an interface should be possible without any information loss.
|
|
|
|
|
|
|
|
Interfaces are denoted by their own keyword:
|
|
|
|
|
2021-06-25 10:25:29 +00:00
|
|
|
.. code-block:: solidity
|
2019-01-07 16:49:34 +00:00
|
|
|
|
2020-05-13 15:45:58 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-09-08 08:48:04 +00:00
|
|
|
pragma solidity >=0.6.2 <0.9.0;
|
2019-01-07 16:49:34 +00:00
|
|
|
|
|
|
|
interface Token {
|
|
|
|
enum TokenType { Fungible, NonFungible }
|
|
|
|
struct Coin { string obverse; string reverse; }
|
|
|
|
function transfer(address recipient, uint amount) external;
|
|
|
|
}
|
|
|
|
|
|
|
|
Contracts can inherit interfaces as they would inherit other contracts.
|
|
|
|
|
2021-07-07 12:53:56 +00:00
|
|
|
All functions declared in interfaces are implicitly ``virtual`` and any
|
|
|
|
functions that override them do not need the ``override`` keyword.
|
|
|
|
This does not automatically mean that an overriding function can be overridden again -
|
|
|
|
this is only possible if the overriding function is marked ``virtual``.
|
2019-12-11 16:31:16 +00:00
|
|
|
|
2020-01-21 13:42:58 +00:00
|
|
|
Interfaces can inherit from other interfaces. This has the same rules as normal
|
|
|
|
inheritance.
|
|
|
|
|
2021-06-25 10:25:29 +00:00
|
|
|
.. code-block:: solidity
|
2020-01-21 13:42:58 +00:00
|
|
|
|
2020-05-13 15:45:58 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-09-08 08:48:04 +00:00
|
|
|
pragma solidity >=0.6.2 <0.9.0;
|
2020-01-21 13:42:58 +00:00
|
|
|
|
|
|
|
interface ParentA {
|
|
|
|
function test() external returns (uint256);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ParentB {
|
|
|
|
function test() external returns (uint256);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SubInterface is ParentA, ParentB {
|
|
|
|
// Must redefine test in order to assert that the parent
|
|
|
|
// meanings are compatible.
|
|
|
|
function test() external override(ParentA, ParentB) returns (uint256);
|
|
|
|
}
|
|
|
|
|
2019-01-07 16:49:34 +00:00
|
|
|
Types defined inside interfaces and other contract-like structures
|
|
|
|
can be accessed from other contracts: ``Token.TokenType`` or ``Token.Coin``.
|
2019-06-17 12:28:08 +00:00
|
|
|
|
2023-07-05 01:12:49 +00:00
|
|
|
.. warning::
|
2019-06-17 12:28:08 +00:00
|
|
|
|
|
|
|
Interfaces have supported ``enum`` types since :doc:`Solidity version 0.5.0 <050-breaking-changes>`, make
|
2020-01-21 13:42:58 +00:00
|
|
|
sure the pragma version specifies this version as a minimum.
|