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

Unnecessary validations can be removed

Summary

There are some if conditions which are checking if a value will be bigger than zero and due to prior verifications, this case is never going to happen.

Vulnerability Details

In the function:

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) {
// @audit it will always be bigger than zero. Can be removed
uint256 _ratio = (_diff * 1e18) / totalSupply;
if (_ratio > 0) {
// @audit it will always be bigger than zero. Can be removed
index = index + _ratio;
balance = _balance;
}
}
}
}
}

We can observe that the condition if (_diff > 0) is not necessary because prior to this we have that

uint256 _diff = _balance - balance;

and in order not to underflow the above line is only executed if this condition if (_balance > balance) is met. Hence, _diff is always going to be different than 0.

And besides that, since _diff is never going to be zero and _totalSupply needs to be bigger than 0, if (_ratio > 0) is not necessary neither as _ratio will always be different than zero.

Impact

Every time the update() function is called, you will be able to save some extra gas by removing the unnecessary code.

Tools Used

None

Recommendations

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!