typestar

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

  1. fallback() answers any selector no function matches.
  2. msg.sig is the four-byte selector that missed.
  3. receive() stays reserved for plain transfers.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 2 in Surfaces, step 25 of 28 in Language basics.

← Previous Next →