sherlock-audit / 2023-01-ajna-judging

1 stars 0 forks source link

0xhacksmithh - Precision loss in ```_isCollateralized()``` and ```_roundToScale()``` functions #125

Closed github-actions[bot] closed 1 year ago

github-actions[bot] commented 1 year ago

0xhacksmithh

medium

Precision loss in _isCollateralized() and _roundToScale() functions

Summary

Due division before multiplication both functions _isCollateralized() and _roundToScale() gives inaccurate results.

Vulnerability Detail

In these below 2 functions, their arithmatic calculation suffering from precision loss, that ultimately result in inaccurate result.

function _roundToScale(
        uint256 amount_,
        uint256 tokenScale_
    ) pure returns (uint256 scaledAmount_) {
        scaledAmount_ = (amount_ / tokenScale_) * tokenScale_; 
    }
function _isCollateralized(
        uint256 debt_,
        uint256 collateral_,
        uint256 price_,
        uint8 type_
    ) pure returns (bool) {
        if (type_ == uint8(PoolType.ERC20)) return Maths.wmul(collateral_, price_) >= debt_;
        else {
            //slither-disable-next-line divide-before-multiply
            collateral_ = (collateral_ / Maths.WAD) * Maths.WAD; // use collateral floor  
            return Maths.wmul(collateral_, price_) >= debt_;
        }
    }

Impact

refer summary

Code Snippet

https://github.com/sherlock-audit/2023-01-ajna/blob/main/contracts/src/libraries/helpers/PoolHelper.sol#L143-L155 https://github.com/sherlock-audit/2023-01-ajna/blob/main/contracts/src/libraries/helpers/PoolHelper.sol#L234-L239

Tool used

Manual Review

Recommendation

Multiplication should preformed before Division

Duplicate of #51