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
interface IScaledeclares signatures, no bodies.IScale(scaleAddress)casts an address to the shape.- Calls through it are real external calls.
Keywords and builtins used here
addressboolconstructorcontractexternalfunctionpublicreturnreturnsuint256
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.
Step 2 of 4 in Composition, step 18 of 28 in Language basics.