From becff9fa0eddb222ac8ef394e379311e28dd1fd8 Mon Sep 17 00:00:00 2001 From: Snowstorm05 Date: Wed, 17 Dec 2025 21:36:16 +0900 Subject: [PATCH] Refactor OnlyAuthorized contract for better error handling --- src/OnlyAuthorized.sol | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/OnlyAuthorized.sol b/src/OnlyAuthorized.sol index 590560a..c3d541b 100644 --- a/src/OnlyAuthorized.sol +++ b/src/OnlyAuthorized.sol @@ -1,22 +1,28 @@ // SPDX-License-Identifier: UNLICENSED - pragma solidity ^0.8.0; contract OnlyAuthorized { - address public owner = msg.sender; + address public owner; + uint256 public n; + + error OnlyOwner(); + error ZeroAddress(); + + constructor() { + owner = msg.sender; + } modifier onlyOwner() { - require(owner == msg.sender, "Only owner"); + if (msg.sender != owner) revert OnlyOwner(); _; } - function changeOwner(address _newOwner) public onlyOwner { - owner = _newOwner; + function changeOwner(address newOwner) external onlyOwner { + if (newOwner == address(0)) revert ZeroAddress(); + owner = newOwner; } - uint256 public n; - - function setN(uint256 n_) public { - n = n_; + function setN(uint256 newN) external { + n = newN; } }