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

Sherlock-111: Wrong Inflator used in calculating HTP to determine accrualIndex #916

Merged
merged 8 commits into from
Jun 30, 2023
26 changes: 6 additions & 20 deletions tests/forge/invariants/base/handlers/unbounded/BaseHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -368,35 +368,21 @@ abstract contract BaseHandler is Test {
// poolLoansInfo returns 1e18 if no interest is pending or time elapsed... the contracts calculate 0 time elapsed which causes discrep
if (pendingFactor == 1e18) return;

// get TP of worst loan, pendingInflator and poolDebt
uint256 maxThresholdPrice;
uint256 pendingInflator;
uint256 poolDebt;
{
(, poolDebt ,,) = _pool.debtInfo();
// get TP of worst loan
(uint256 inflator, ) = _pool.inflatorInfo();

(uint256 inflator, uint256 inflatorUpdate) = _pool.inflatorInfo();

(, maxThresholdPrice,) = _pool.loansInfo();
maxThresholdPrice = Maths.wdiv(maxThresholdPrice, inflator);

(uint256 interestRate, ) = _pool.interestRateInfo();

pendingInflator = PoolCommons.pendingInflator(
inflator,
inflatorUpdate,
interestRate
);
}
(, uint256 maxThresholdPrice,) = _pool.loansInfo();
maxThresholdPrice = Maths.wdiv(maxThresholdPrice, inflator);
Copy link
Contributor Author

@grandizzy grandizzy Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, I don't think we need to wdiv and then wmul again to obtain HTP, _pool.loansInfo() already does this

    function loansInfo() external view override returns (address, uint256, uint256) {
        return (
            Loans.getMax(loans).borrower,
            Maths.wmul(Loans.getMax(loans).thresholdPrice, inflatorState.inflator),
            Loans.noOfLoans(loans)
        );
    }

, so I suggest getting rid of (uint256 inflator, ) = _pool.inflatorInfo(); and simplify this to

        // get TP of worst loan and poolDebt
        (, uint256 htp,)         = _pool.loansInfo();
        (, uint256 poolDebt, , ) = _pool.debtInfo();

        uint256 accrualIndex;

        if (htp > MAX_PRICE)      accrualIndex = 1;                          // if HTP is over the highest price bucket then no buckets earn interest
        else if (htp < MIN_PRICE) accrualIndex = MAX_FENWICK_INDEX;          // if HTP is under the lowest price bucket then all buckets earn interest
        else                      accrualIndex = _poolInfo.priceToIndex(htp);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 64e811a


// get HTP and deposit above HTP
uint256 htp = Maths.wmul(maxThresholdPrice, pendingInflator);
uint256 htp = Maths.wmul(maxThresholdPrice, inflator);
uint256 accrualIndex;

if (htp > MAX_PRICE) accrualIndex = 1; // if HTP is over the highest price bucket then no buckets earn interest
else if (htp < MIN_PRICE) accrualIndex = MAX_FENWICK_INDEX; // if HTP is under the lowest price bucket then all buckets earn interest
else accrualIndex = _poolInfo.priceToIndex(htp);

(, uint256 poolDebt,,) = _pool.debtInfo();
uint256 lupIndex = _pool.depositIndex(poolDebt);

// accrual price is less of lup and htp, and prices decrease as index increases
Expand Down
Loading