From 79a981ec0e8cdd1262c57e248d9fd63cc57f93e8 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Tue, 28 Mar 2023 07:15:14 -0400 Subject: [PATCH 1/3] prevent revert calculating MOMP when pool has no loans --- src/PoolInfoUtils.sol | 4 +++- tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/PoolInfoUtils.sol b/src/PoolInfoUtils.sol index 01e1134c4..5a67ca44b 100644 --- a/src/PoolInfoUtils.sol +++ b/src/PoolInfoUtils.sol @@ -15,6 +15,7 @@ import { _priceAt, _reserveAuctionPrice, MAX_FENWICK_INDEX, + MAX_PRICE, MIN_PRICE } from './libraries/helpers/PoolHelper.sol'; @@ -318,9 +319,10 @@ contract PoolInfoUtils { ) external view returns (uint256) { IPool pool = IPool(ajnaPool_); - (uint256 debt, , ) = pool.debtInfo(); ( , , uint256 noOfLoans) = pool.loansInfo(); noOfLoans += pool.totalAuctionsInPool(); + if (noOfLoans == 0) return MAX_PRICE; + (uint256 debt, , ) = pool.debtInfo(); return _priceAt(pool.depositIndex(Maths.wdiv(debt, noOfLoans * 1e18))); } diff --git a/tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol b/tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol index 142769d87..38a81fabc 100644 --- a/tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol +++ b/tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol @@ -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'; @@ -204,6 +205,11 @@ 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 + IERC20 otherCollateral = new Token("MompTestCollateral", "MTC"); + IERC20Pool emptyPool = ERC20Pool(_poolFactory.deployPool(address(otherCollateral), address(_quote), 0.05 * 10**18)); + assertEq(_poolUtils.momp(address(emptyPool)), MAX_PRICE); } function testBorrowFeeRate() external { From e433d8cfba109191cdb67b74179836a7405d37d9 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Fri, 31 Mar 2023 10:38:06 -0400 Subject: [PATCH 2/3] define MOMP as HPB rather than MAX_PRICE when there are no borrowers --- src/PoolInfoUtils.sol | 11 ++++++++--- tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol | 11 +++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/PoolInfoUtils.sol b/src/PoolInfoUtils.sol index 5a67ca44b..f58dd1d90 100644 --- a/src/PoolInfoUtils.sol +++ b/src/PoolInfoUtils.sol @@ -321,9 +321,14 @@ contract PoolInfoUtils { ( , , uint256 noOfLoans) = pool.loansInfo(); noOfLoans += pool.totalAuctionsInPool(); - if (noOfLoans == 0) return MAX_PRICE; - (uint256 debt, , ) = pool.debtInfo(); - 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))); + } } /** diff --git a/tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol b/tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol index 38a81fabc..60729c360 100644 --- a/tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol +++ b/tests/forge/ERC20Pool/ERC20PoolInfoUtils.t.sol @@ -206,10 +206,17 @@ 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 + // 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)), MAX_PRICE); + 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 { From 111ce68448ee0054a82429f86e544b2b768f0908 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Sun, 2 Apr 2023 21:42:59 -0400 Subject: [PATCH 3/3] pr feedback --- lib/openzeppelin-contracts | 2 +- src/PoolInfoUtils.sol | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/openzeppelin-contracts b/lib/openzeppelin-contracts index ec825d899..d00acef40 160000 --- a/lib/openzeppelin-contracts +++ b/lib/openzeppelin-contracts @@ -1 +1 @@ -Subproject commit ec825d8999538f110e572605dc56ef7bf44cc574 +Subproject commit d00acef4059807535af0bd0dd0ddf619747a044b diff --git a/src/PoolInfoUtils.sol b/src/PoolInfoUtils.sol index f58dd1d90..c3c7ec66d 100644 --- a/src/PoolInfoUtils.sol +++ b/src/PoolInfoUtils.sol @@ -15,7 +15,6 @@ import { _priceAt, _reserveAuctionPrice, MAX_FENWICK_INDEX, - MAX_PRICE, MIN_PRICE } from './libraries/helpers/PoolHelper.sol';