Chapter 5 · Solidity foundations · lesson 14
Interfaces and external calls
11 min
Builds on Functions and visibility
Chapter 5 · Solidity foundations · lesson 14
11 min
Builds on Functions and visibility
Everything so far has happened inside one contract. This lesson is where your code starts talking to code you did not write — and where the danger begins.
interface ICounter {
function increment() external;
function count() external view returns (uint256);
}
No bodies, no state, no constructor. It tells the compiler how to encode a call to something at an address that claims to behave this way.
ICounter(someAddress) does not check anything. There is no verification that
the address holds a contract, that the contract has those functions, or that
they do what the names suggest.
You are asserting a shape. If you are wrong, the call reverts — or worse, silently hits a different function whose selector happens to match.
Each counter.increment() is a whole separate execution:
msg.sender becomes your contract's address, not the original user.That third point is the important one.
Predict
Three habits worth forming now:
.call() returns false rather than
reverting.None of this is paranoia. Every one of these has been used to take real money.
Check