20,000 USDC
View results
Submission Details
Severity: gas

Gas Optimization for update() Function

Summary

This analyzes the update() function in the given smart contract and proposes improvements to reduce gas consumption without compromising its functionality.

Vulnerability Details

The update() function performs an update on the global index of earned rewards. However, there are some gas-inefficient operations in the function that can be optimized.

Impact

The current implementation of the update() function consumes unnecessary gas due to redundant calculations and condition checks. This can lead to increased transaction costs and reduced efficiency, especially when the contract is used frequently or when gas prices are high.

Tools Used

Remix IDE

Recommendations

To optimize the gas consumption in the update() function, the following changes are recommended:

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

Support

FAQs

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