@@ -74,10 +74,9 @@ contract TestLens is TestSetup {
7474 uint256 amount = 10_000 ether ;
7575 uint256 toBorrow = amount / 2 ;
7676
77- borrower1.approve (dai, amount );
77+ borrower1.approve (dai, type ( uint256 ).max );
7878 indexes.index1 = ICToken (cDai).exchangeRateCurrent ();
7979 borrower1.supply (cDai, amount);
80- uint256 p2pBorrowIndex = morpho.p2pBorrowIndex (cDai);
8180 borrower1.borrow (cDai, toBorrow);
8281
8382 indexes.index2 = ICToken (cDai).exchangeRateCurrent ();
@@ -94,6 +93,9 @@ contract TestLens is TestSetup {
9493
9594 uint256 total;
9695
96+ // To update p2p indexes on Morpho (they can change inside of a block because the poolSupplyIndex can change due to rounding errors).
97+ borrower1.supply (cDai, 1 );
98+ uint256 p2pBorrowIndex = morpho.p2pBorrowIndex (cDai);
9799 {
98100 uint256 onPool = amount.div (indexes.index1);
99101 uint256 matchedInP2P = toBorrow.div (morpho.p2pSupplyIndex (cDai));
@@ -1719,4 +1721,40 @@ contract TestLens is TestSetup {
17191721 morpho.setIsLiquidateBorrowPaused (cDai, true );
17201722 assertTrue (lens.getMarketPauseStatus (cDai).isLiquidateBorrowPaused);
17211723 }
1724+
1725+ function testPoolIndexGrowthInsideBlock () public {
1726+ supplier1.approve (dai, type (uint256 ).max);
1727+ supplier1.supply (cDai, 1 ether);
1728+
1729+ uint256 poolBorrowIndexBefore = lens.getIndexes (cDai, true ).poolSupplyIndex;
1730+
1731+ vm.prank (address (supplier1));
1732+ ERC20 (dai).transfer (cDai, 10_000 ether);
1733+
1734+ supplier1.supply (cDai, 1 );
1735+
1736+ uint256 poolSupplyIndexAfter = lens.getIndexes (cDai, true ).poolSupplyIndex;
1737+
1738+ assertGt (poolSupplyIndexAfter, poolBorrowIndexBefore);
1739+ }
1740+
1741+ function testP2PIndexGrowthInsideBlock () public {
1742+ borrower1.approve (dai, type (uint256 ).max);
1743+ borrower1.supply (cDai, 1 ether);
1744+ borrower1.borrow (cDai, 0.5 ether);
1745+ setDefaultMaxGasForMatchingHelper (0 , 0 , 0 , 0 );
1746+ // Bypass the borrow repay in the same block by overwritting the storage slot lastBorrowBlock[borrower1].
1747+ hevm.store (address (morpho), keccak256 (abi.encode (address (borrower1), 178 )), 0 );
1748+ // Create delta.
1749+ borrower1.repay (cDai, type (uint256 ).max);
1750+
1751+ uint256 p2pSupplyIndexBefore = lens.getCurrentP2PSupplyIndex (cDai);
1752+
1753+ vm.prank (address (supplier1));
1754+ ERC20 (dai).transfer (cDai, 10_000 ether);
1755+
1756+ uint256 p2pSupplyIndexAfter = lens.getCurrentP2PSupplyIndex (cDai);
1757+
1758+ assertGt (p2pSupplyIndexAfter, p2pSupplyIndexBefore);
1759+ }
17221760}
0 commit comments