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

Unnecessary If condition in update() of Staking.sol

Vulnerability Details

In line 65 https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Staking.sol#L65, there already exists a condition if _balance > balance so that _diff is always > 0. However, there is a condition below checks if _diff > 0. And since _diff is always > 0, we don't need to check if _ratio > 0, which is unnecessary.

Impact

Cause the function to compile slower and waste more gas.

Tools Used

Manual

Recommendations

Remove the condition of if (_diff > 0)

function update() public {
uint256 totalSupply = TKN.balanceOf(address(this));
if (totalSupply > 0) {
uint256 _balance = WETH.balanceOf(address(this));
if (_balance > balance) {
uint256 _diff = _balance - balance;
- if (_diff > 0) {
uint256 _ratio = _diff * 1e18 / totalSupply;
- if (_ratio > 0) {
index = index + _ratio;
balance = _balance;
- }
- }
}
}
}

The completed code would be:

function update() public {
uint256 totalSupply = TKN.balanceOf(address(this));
if (totalSupply > 0) {
uint256 _balance = WETH.balanceOf(address(this));
if (_balance > balance) {
uint256 _diff = _balance - balance;
uint256 _ratio = _diff * 1e18 / totalSupply;
index = index + _ratio;
balance = _balance;
}
}
}

Support

FAQs

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

Give us feedback!