Mystery Box

First Flight #25
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: low
Invalid

Modifier to check the owner's call can be used to reduce gas costs

## Summary
Multiple functions in the contract (`setBoxPrice()`, `addReward()`, and `withdrawFunds()`) check if the caller is the owner using the same `require` statement. This redundancy increases gas costs and reduces code maintainability. By using a single modifier, these checks can be unified, saving gas and improving the contract's efficiency.
## Vulnerability Details
The contract currently has several functions (`setBoxPrice()`, `addReward()`, and `withdrawFunds()`) that require the caller to be the owner of the contract. Each function checks ownership using a `require` statement, which is repetitive and can lead to higher gas consumption. This approach also reduces code readability and makes the contract harder to maintain.
## Impact
The repetitive use of `require(msg.sender == owner)` in multiple functions unnecessarily increases gas costs and makes the code less efficient. Over time, especially in contracts that are frequently called, this could lead to significant gas savings if refactored. Additionally, duplicating logic across functions can increase the risk of inconsistencies when updates are needed.
## Tools Used
Manual code review and Solidity testing framework (e.g., Foundry).
## Recommendations
To mitigate this issue, a single `onlyOwner` modifier should be introduced. This modifier will encapsulate the ownership check and can be reused across all owner-restricted functions, reducing code duplication and saving gas.
Here is the refactored code using the `onlyOwner` modifier:
### Modifier Definition
```solidity
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can perform this action");
_;
}
```
Updates

Lead Judging Commences

inallhonesty Lead Judge
11 months ago

Appeal created

inallhonesty Lead Judge 11 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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