typestar

Modifiers in Solidity

Wrap a function; _ marks where the body runs.

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

// A modifier wraps a function; _ marks where the body runs.
contract ToolShed {
    address public steward;
    uint256 public toolsOut;

    constructor() {
        steward = msg.sender;
    }

    modifier onlySteward() {
        require(msg.sender == steward, "steward only");
        _;
    }

    function lend() external onlySteward {
        toolsOut += 1;
    }
}

How it works

  1. modifier onlySteward centralizes the permission check.
  2. The _; line is where the guarded body executes.
  3. external onlySteward applies it at the signature.

Keywords and builtins used here

The run, in numbers

Lines
21
Characters to type
366
Tokens
58
Three-star pace
60 tpm

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

Type this snippet

Step 2 of 3 in Guards, step 9 of 28 in Language basics.

← Previous Next →