Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas: Use else if in withdrawLiquidity #148

Open
code423n4 opened this issue Dec 15, 2021 · 0 comments
Open

Gas: Use else if in withdrawLiquidity #148

code423n4 opened this issue Dec 15, 2021 · 0 comments
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

cmichel

Vulnerability details

The if conditions in Pool.withdrawLiquidity are distinct conditions on the pool status.
Therefore, else if is semantically equivalent but more gas efficient.

if (_loanStatus == LoanStatus.DEFAULTED || _loanStatus == LoanStatus.TERMINATED) {
    uint256 _totalAsset;
    if (poolConstants.borrowAsset != address(0)) {
        _totalAsset = IERC20(poolConstants.borrowAsset).balanceOf(address(this));
    } else {
        _totalAsset = address(this).balance;
    }
    //assuming their will be no tokens in pool in any case except liquidation (to be checked) or we should store the amount in liquidate()
    _toTransfer = _toTransfer.mul(_totalAsset).div(totalSupply());
}
// @audit gas: use else if, status fields are distinct, only one of the branches is (if ever) executed anyway
if (_loanStatus == LoanStatus.CANCELLED) {
    _toTransfer = _toTransfer.add(_toTransfer.mul(poolVariables.penaltyLiquidityAmount).div(totalSupply()));
}

if (_loanStatus == LoanStatus.CLOSED) {
    //transfer repayment
    _withdrawRepayment(msg.sender);
}
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Dec 15, 2021
code423n4 added a commit that referenced this issue Dec 15, 2021
@ritik99 ritik99 added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Dec 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

2 participants