🎓 A1Learner
🔍
0 of 48 lessons completed

Solidity Interfaces in Depth

An interface is a list of function shapes with no bodies. A contract that follows the list can be used through it, without the caller knowing its code.

An interface lists functions without bodies

The interface names the functions. The contract below supplies them.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
interface ICounter {
    function count() external view returns (uint256);
    function increment() external;
}

contract Counter is ICounter {
    uint256 public count;

    function increment() external override {
        count += 1;
    }
}

A caller uses an interface variable

The caller stores the other contract as an interface. Every call goes through that variable.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
interface ICounter {
    function count() external view returns (uint256);
    function increment() external;
}

contract Counter {
    uint256 public count;

    function increment() external {
        count += 1;
    }
}

contract CounterCaller {
    ICounter public counter;

    constructor(address counterAddress) {
        counter = ICounter(counterAddress);
    }

    function bump() public {
        counter.increment();
    }

    function read() public view returns (uint256) {
        return counter.count();
    }
}

An interface cannot hold state

A state variable inside an interface fails to compile. Interfaces carry declarations only.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
interface IHasState {
    uint256 public stored;

    function read() external view returns (uint256);
}

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
interface IScore {
    function score() external view returns (uint256);
    function add(uint256 points) external;
}

contract ScoreBoard is IScore {
    uint256 public score;

    function add(uint256 points) external override {
        score += points;
    }
}

contract ScoreApp {
    IScore public board;

    constructor(address boardAddress) {
        board = IScore(boardAddress);
    }

    function award(uint256 points) public {
        board.add(points);
    }

    function read() public view returns (uint256) {
        return board.score();
    }
}

Key points

  • Interface: a list of function declarations with no bodies and no state.
  • Use an interface variable to call a contract at a known address.
  • An interface function is external, so calls through it cross a contract boundary.
  • The caller needs only the interface, not the full contract code.
  • An interface cannot be deployed on its own.

In this example, ScoreBoard follows the IScore interface and stores the score. ScoreApp stores the board as an interface variable. Its award function forwards points through that variable, and its read function returns the board score through the same variable.

Note

a concrete contract that follows an interface must supply every function in the list.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Remix IDE

Exercise

Which statement about an interface is true?