fallback in Solidity
The catch-alls: fallback for unknown calls, receive for ether.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// fallback answers calls no function matches; receive takes
// plain ether. Together they are the contract's catch-alls.
contract NightGate {
event UnknownKnock(bytes4 selector);
uint256 public plainDeposits;
receive() external payable {
plainDeposits += 1;
}
fallback() external {
emit UnknownKnock(msg.sig);
}
}
How it works
fallback()answers any selector no function matches.msg.sigis the four-byte selector that missed.receive()stays reserved for plain transfers.
Keywords and builtins used here
contractemiteventexternalpayablepublicuint256
The run, in numbers
- Lines
- 17
- Characters to type
- 374
- Tokens
- 46
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 46 seconds.
Step 2 of 2 in Surfaces, step 25 of 28 in Language basics.