code-423n4 / 2022-03-lifinance-findings

6 stars 4 forks source link

Gas Optimizations #154

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Redundant Operation

Description

address(this).balance - _fromBalance is found to be operated twice while it could be operated once and save the result to a local variable for later use.

Permalinks

Mitigation

For greater gas-optimization, the code can be rewrite as following:

/* from */
require(address(this).balance - _fromBalance >= _anyswapData.amount, "ERR_INVALID_AMOUNT");
uint256 _postSwapBalance = address(this).balance - _fromBalance;
require(_postSwapBalance > 0, "ERR_INVALID_AMOUNT");

/* to */
uint256 _postSwapBalance = address(this).balance - _fromBalance;
require(_postSwapBalance >= _anyswapData.amount && _postSwapBalance > 0, "ERR_INVALID_AMOUNT");
gzeoneth commented 2 years ago

Warden submitted multiple QA report #149