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

Prevent DIV/0 revert calculating MOMP on a pool with no loans #712

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/PoolInfoUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,16 @@ contract PoolInfoUtils {
) external view returns (uint256) {
IPool pool = IPool(ajnaPool_);

(uint256 debt, , ,) = pool.debtInfo();
( , , uint256 noOfLoans) = pool.loansInfo();
noOfLoans += pool.totalAuctionsInPool();
return _priceAt(pool.depositIndex(Maths.wdiv(debt, noOfLoans * 1e18)));
if (noOfLoans == 0) {
// if there are no borrowers, return the HPB
return _priceAt(pool.depositIndex(1));
} else {
// otherwise, calculate the MOMP
(uint256 debt, , , ) = pool.debtInfo();
return _priceAt(pool.depositIndex(Maths.wdiv(debt, noOfLoans * 1e18)));
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.14;

import { ERC20HelperContract } from './ERC20DSTestPlus.sol';
import { Token } from '../utils/Tokens.sol';

import 'src/libraries/helpers/PoolHelper.sol';
import 'src/interfaces/pool/erc20/IERC20Pool.sol';
Expand Down Expand Up @@ -204,6 +205,18 @@ contract ERC20PoolInfoUtilsTest is ERC20HelperContract {

function testMomp() external {
assertEq(_poolUtils.momp(address(_pool)), 2_981.007422784467321543 * 1e18);

// ensure calculation does not revert on pools with no loans and no deposit
IERC20 otherCollateral = new Token("MompTestCollateral", "MTC");
IERC20Pool emptyPool = ERC20Pool(_poolFactory.deployPool(address(otherCollateral), address(_quote), 0.05 * 10**18));
assertEq(_poolUtils.momp(address(emptyPool)), MIN_PRICE);

// should return HPB on pools with liquidity but no loans
changePrank(_lender);
uint256 hpbIndex = 369;
_quote.approve(address(emptyPool), type(uint256).max);
emptyPool.addQuoteToken(0.0213 * 1e18, hpbIndex, type(uint256).max);
assertEq(_poolUtils.momp(address(emptyPool)), _priceAt(hpbIndex));
}

function testBorrowFeeRate() external {
Expand Down