20,000 USDC
View results
Submission Details
Severity: gas
Valid

Modifier gas optimization for onlyOwner modifier

Summary

It is recommended to move the modifiers require statements into an internal virtual function. Moreover, the require statement should be replaced with if check + custom error. This reduces the size of compiled contracts that use the modifiers. Putting the check in an internal function decreases contract size when a modifier is used multiple times. The best way of implementing this is presented in OZ's Ownable.sol (Link 2):

- modifier onlyOwner() virtual {
- require(msg.sender == owner, "UNAUTHORIZED");
- _;
- }
+modifier onlyOwner() {
+ _checkOwner();
+ _;
+ }
+function _checkOwner() internal view virtual {
+ if (owner() != _msgSender()) {
+ revert OwnableUnauthorizedAccount(_msgSender());
+ }
+ }

In Errors.sol:

+error OwnableUnauthorizedAccount(address sender);

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.