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
modifier onlyStewardcentralizes the permission check.- The
_;line is where the guarded body executes. external onlyStewardapplies it at the signature.
Keywords and builtins used here
addressconstructorcontractexternalfunctionmodifierpublicrequireuint256
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.
Step 2 of 3 in Guards, step 9 of 28 in Language basics.