typestar

Interfaces in Solidity

The shape of a contract you only know by address.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

// An interface is the shape of a contract you only know by address.
interface IScale {
    function weigh(uint256 crateId) external view returns (uint256);
}

contract Auditor {
    IScale public scale;

    constructor(address scaleAddress) {
        scale = IScale(scaleAddress);
    }

    function checkCrate(uint256 crateId) external view returns (bool) {
        return scale.weigh(crateId) >= 18;
    }
}

How it works

  1. interface IScale declares signatures, no bodies.
  2. IScale(scaleAddress) casts an address to the shape.
  3. Calls through it are real external calls.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
430
Tokens
70
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 70 seconds.

Type this snippet

Step 2 of 4 in Composition, step 18 of 28 in Language basics.

← Previous Next →

Interfaces in other languages