Skip to content

Commit 5d87c68

Browse files
committed
fix integration tests by increasing borrow cap
1 parent 7b69e5e commit 5d87c68

File tree

5 files changed

+59
-28
lines changed

5 files changed

+59
-28
lines changed

src/morpho/IMorphoChainlinkOracleV2.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {AggregatorV3Interface} from "@protocol/oracles/AggregatorV3Interface.sol
99
/// @custom:contact [email protected]
1010
/// @notice Interface of MorphoChainlinkOracleV2.
1111
interface IMorphoChainlinkOracleV2 {
12-
/// @notice Returns the address of the base ERC4626 vault.
12+
/// @notice Returns hte address of the base ERC4626 vault.
1313
function BASE_VAULT() external view returns (IERC4626);
1414

1515
/// @notice Returns the base vault conversion sample.

test/integration/IRModelWethUpgradePostProposalIntegration.t.sol

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import {Configs} from "@proposals/Configs.sol";
1111
import {AllChainAddresses as Addresses} from "@proposals/Addresses.sol";
1212
import {Comptroller} from "@protocol/Comptroller.sol";
1313
import {PostProposalCheck} from "@test/integration/PostProposalCheck.sol";
14+
import {MarketBase} from "@test/utils/MarketBase.sol";
1415
import {BASE_FORK_ID} from "@utils/ChainIds.sol";
1516

1617
contract IRModelWethUpgradePostProposalTest is PostProposalCheck, Configs {
1718
Comptroller comptroller;
19+
MarketBase marketBase;
1820
MErc20 mUSDbC;
1921
MErc20 mWeth;
2022
MErc20 mcbEth;
@@ -25,6 +27,7 @@ contract IRModelWethUpgradePostProposalTest is PostProposalCheck, Configs {
2527
vm.selectFork(BASE_FORK_ID);
2628

2729
comptroller = Comptroller(addresses.getAddress("UNITROLLER"));
30+
marketBase = new MarketBase(comptroller);
2831
mUSDbC = MErc20(addresses.getAddress("MOONWELL_USDBC"));
2932
mWeth = MErc20(addresses.getAddress("MOONWELL_WETH"));
3033
mcbEth = MErc20(addresses.getAddress("MOONWELL_cbETH"));
@@ -54,21 +57,8 @@ contract IRModelWethUpgradePostProposalTest is PostProposalCheck, Configs {
5457
testSupplyingWethAfterIRModelUpgradeSucceeds();
5558
uint256 borrowAmount = 74e18;
5659

57-
// Check current borrow cap and increase if needed
58-
uint256 currentBorrowCap = comptroller.borrowCaps(address(mWeth));
59-
uint256 totalBorrows = mWeth.totalBorrows();
60-
uint256 nextTotalBorrows = totalBorrows + borrowAmount;
61-
62-
// If borrow would hit cap, increase it
63-
if (currentBorrowCap != 0 && nextTotalBorrows >= currentBorrowCap) {
64-
vm.startPrank(addresses.getAddress("TEMPORAL_GOVERNOR"));
65-
MToken[] memory mTokens = new MToken[](1);
66-
mTokens[0] = mWeth;
67-
uint256[] memory newBorrowCaps = new uint256[](1);
68-
newBorrowCaps[0] = currentBorrowCap + borrowAmount;
69-
comptroller._setMarketBorrowCaps(mTokens, newBorrowCaps);
70-
vm.stopPrank();
71-
}
60+
// Ensure sufficient borrow cap using the utility function
61+
marketBase.ensureSufficientBorrowCap(mWeth, borrowAmount, addresses);
7262

7363
address[] memory mToken = new address[](1);
7464
mToken[0] = address(mWeth);

test/integration/ReentrancyBaseLiveSystemIntegration.t.sol

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {mipb02 as mip} from "@proposals/mips/mip-b02/mip-b02.sol";
1515
import {MErc20Delegator} from "@protocol/MErc20Delegator.sol";
1616
import {MaliciousBorrower} from "@test/mock/MaliciousBorrower.sol";
1717
import {PostProposalCheck} from "@test/integration/PostProposalCheck.sol";
18+
import {MarketBase} from "@test/utils/MarketBase.sol";
1819
import {ComptrollerErrorReporter} from "@protocol/TokenErrorReporter.sol";
1920
import {AllChainAddresses as Addresses} from "@proposals/Addresses.sol";
2021

@@ -24,6 +25,7 @@ contract ReentrancyPostProposalTest is
2425
ComptrollerErrorReporter
2526
{
2627
Comptroller comptroller;
28+
MarketBase marketBase;
2729
WETHRouter router;
2830

2931
function setUp() public override {
@@ -32,6 +34,7 @@ contract ReentrancyPostProposalTest is
3234
vm.selectFork(BASE_FORK_ID);
3335

3436
comptroller = Comptroller(addresses.getAddress("UNITROLLER"));
37+
marketBase = new MarketBase(comptroller);
3538
router = WETHRouter(payable(addresses.getAddress("WETH_ROUTER")));
3639
}
3740

@@ -95,6 +98,14 @@ contract ReentrancyPostProposalTest is
9598

9699
deal(addresses.getAddress("WETH"), address(borrower), 100e18); /// fund attack contract with weth
97100

101+
// Ensure sufficient borrow cap for the attack (60 WETH = 100 * 0.6)
102+
uint256 borrowAmount = 60e18;
103+
marketBase.ensureSufficientBorrowCap(
104+
MErc20(mToken),
105+
borrowAmount,
106+
addresses
107+
);
108+
98109
vm.expectRevert("re-entered"); /// cannot reenter and borrow
99110
borrower.exploit();
100111
}
@@ -108,6 +119,14 @@ contract ReentrancyPostProposalTest is
108119

109120
deal(addresses.getAddress("WETH"), address(borrower), 100e18); /// fund attack contract with weth
110121

122+
// Ensure sufficient borrow cap for the attack (60 WETH = 100 * 0.6)
123+
uint256 borrowAmount = 60e18;
124+
marketBase.ensureSufficientBorrowCap(
125+
MErc20(mToken),
126+
borrowAmount,
127+
addresses
128+
);
129+
111130
vm.expectEmit(true, true, true, true, address(comptroller));
112131
/// cannot reenter and borrow
113132

test/integration/oracle/ChainlinkOEVWrapperIntegration.t.sol

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,13 @@ contract ChainlinkOEVWrapperIntegrationTest is PostProposalCheck {
257257
// so we need to convert borrow amount to 6 decimals
258258
borrowAmount = ((liquidity * 80) / 100) / 1e12; // Changed from full amount
259259

260-
// before borrowing, increase borrow cap to make sure we borrow a significant amount
261-
vm.startPrank(addresses.getAddress("TEMPORAL_GOVERNOR"));
262-
MToken[] memory mTokens = new MToken[](1);
263-
mTokens[0] = mTokenBorrowed;
264-
uint256[] memory newBorrowCaps = new uint256[](1);
265-
uint256 currentBorrowCap = comptroller.borrowCaps(
266-
address(mTokens[0])
260+
// Ensure sufficient borrow cap using the utility function
261+
marketBase.ensureSufficientBorrowCap(
262+
mTokenBorrowed,
263+
borrowAmount,
264+
addresses
267265
);
268266

269-
newBorrowCaps[0] = currentBorrowCap + borrowAmount;
270-
comptroller._setMarketBorrowCaps(mTokens, newBorrowCaps);
271-
vm.stopPrank();
272-
273267
// make sure the mToken has enough underlying to borrow
274268
deal(
275269
MErc20(address(mTokenBorrowed)).underlying(),

test/utils/MarketBase.sol

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//SPDX-License-Identifier: GPL-3.0-or-later
22
pragma solidity 0.8.19;
33

4+
import {Test} from "@forge-std/Test.sol";
45
import {MToken} from "@protocol/MToken.sol";
56
import {Comptroller} from "@protocol/Comptroller.sol";
67
import {ExponentialNoError} from "@protocol/ExponentialNoError.sol";
8+
import {AllChainAddresses as Addresses} from "@proposals/Addresses.sol";
79

8-
contract MarketBase is ExponentialNoError {
10+
contract MarketBase is ExponentialNoError, Test {
911
Comptroller comptroller;
1012

1113
constructor(Comptroller _comptroller) {
@@ -106,4 +108,30 @@ contract MarketBase is ExponentialNoError {
106108
: borrowableAmount
107109
) - 1;
108110
}
111+
112+
/// @notice Ensures sufficient borrow cap for a given borrow amount
113+
/// @dev If the current borrow cap would be exceeded, this function increases it
114+
/// @param mToken The market token to check/increase borrow cap for
115+
/// @param borrowAmount The amount to be borrowed
116+
/// @param addresses The addresses contract to get TEMPORAL_GOVERNOR
117+
function ensureSufficientBorrowCap(
118+
MToken mToken,
119+
uint256 borrowAmount,
120+
Addresses addresses
121+
) public {
122+
uint256 currentBorrowCap = comptroller.borrowCaps(address(mToken));
123+
uint256 totalBorrows = mToken.totalBorrows();
124+
uint256 nextTotalBorrows = totalBorrows + borrowAmount;
125+
126+
// If borrow would hit cap, increase it
127+
if (currentBorrowCap != 0 && nextTotalBorrows >= currentBorrowCap) {
128+
vm.startPrank(addresses.getAddress("TEMPORAL_GOVERNOR"));
129+
MToken[] memory mTokens = new MToken[](1);
130+
mTokens[0] = mToken;
131+
uint256[] memory newBorrowCaps = new uint256[](1);
132+
newBorrowCaps[0] = currentBorrowCap + borrowAmount;
133+
comptroller._setMarketBorrowCaps(mTokens, newBorrowCaps);
134+
vm.stopPrank();
135+
}
136+
}
109137
}

0 commit comments

Comments
 (0)