From 99e42b6cb65bf10eb67d4447fa1a97c5ad62d790 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Thu, 6 Oct 2022 11:04:04 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Clean=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test-foundry/aave-v2/TestBorrow.t.sol | 23 ++-- test-foundry/aave-v2/TestLiquidate.t.sol | 24 ++-- test-foundry/aave-v2/TestRepay.t.sol | 111 ++++++------------ test-foundry/aave-v2/TestSupply.t.sol | 23 ++-- test-foundry/aave-v2/TestWithdraw.t.sol | 73 ++++-------- test-foundry/aave-v2/setup/Utils.sol | 56 --------- test-foundry/aave-v3/TestBorrow.t.sol | 25 ++-- test-foundry/aave-v3/TestLiquidate.t.sol | 28 ++--- test-foundry/aave-v3/TestRepay.t.sol | 102 +++++----------- test-foundry/aave-v3/TestSupply.t.sol | 34 +++--- test-foundry/aave-v3/TestWithdraw.t.sol | 73 ++++-------- test-foundry/aave-v3/setup/Utils.sol | 56 --------- .../fuzzing/aave-v2/TestSupplyFuzzing.t.sol | 2 +- 13 files changed, 178 insertions(+), 452 deletions(-) diff --git a/test-foundry/aave-v2/TestBorrow.t.sol b/test-foundry/aave-v2/TestBorrow.t.sol index b48a6971a..b2ef3d02c 100644 --- a/test-foundry/aave-v2/TestBorrow.t.sol +++ b/test-foundry/aave-v2/TestBorrow.t.sol @@ -5,6 +5,7 @@ import "./setup/TestSetup.sol"; contract TestBorrow is TestSetup { using ReserveConfiguration for DataTypes.ReserveConfigurationMap; + using WadRayMath for uint256; // The borrower tries to borrow more than his collateral allows, the transaction reverts. function testBorrow1() public { @@ -29,8 +30,7 @@ contract TestBorrow is TestSetup { (uint256 inP2P, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrower1)); - uint256 normalizedVariableDebt = pool.getReserveNormalizedVariableDebt(dai); - uint256 expectedOnPool = underlyingToAdUnit(amount, normalizedVariableDebt); + uint256 expectedOnPool = amount.rayDiv(pool.getReserveNormalizedVariableDebt(dai)); testEquality(onPool, expectedOnPool); testEquality(inP2P, 0); @@ -50,9 +50,9 @@ contract TestBorrow is TestSetup { (uint256 supplyInP2P, ) = morpho.supplyBalanceInOf(aDai, address(supplier1)); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 expectedInP2P = p2pUnitToUnderlying(supplyInP2P, p2pBorrowIndex); + uint256 expectedInP2PInUnderlying = supplyInP2P.rayMul(p2pBorrowIndex); - testEquality(expectedInP2P, amount); + testEquality(amount, expectedInP2PInUnderlying); (uint256 inP2P, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrower1)); @@ -78,8 +78,7 @@ contract TestBorrow is TestSetup { testEquality(inP2P, supplyInP2P, "in P2P"); - uint256 normalizedVariableDebt = pool.getReserveNormalizedVariableDebt(dai); - uint256 expectedOnPool = underlyingToAdUnit(amount, normalizedVariableDebt); + uint256 expectedOnPool = amount.rayDiv(pool.getReserveNormalizedVariableDebt(dai)); testEquality(onPool, expectedOnPool, "on pool"); } @@ -121,7 +120,7 @@ contract TestBorrow is TestSetup { for (uint256 i = 0; i < NMAX; i++) { (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(suppliers[i])); - expectedInP2P = p2pUnitToUnderlying(inP2P, p2pSupplyIndex); + expectedInP2P = inP2P.rayMul(p2pSupplyIndex); testEquality(expectedInP2P, amountPerSupplier); testEquality(onPool, 0); @@ -131,7 +130,7 @@ contract TestBorrow is TestSetup { testEquality( inP2P, - underlyingToP2PUnit(amount, morpho.p2pBorrowIndex(aDai)), + amount.rayDiv(morpho.p2pBorrowIndex(aDai)), "Borrower1 in peer-to-peer" ); testEquality(onPool, 0, "Borrower1 on pool"); @@ -175,7 +174,7 @@ contract TestBorrow is TestSetup { for (uint256 i = 0; i < NMAX; i++) { (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(suppliers[i])); - expectedInP2P = p2pUnitToUnderlying(inP2P, p2pSupplyIndex); + expectedInP2P = inP2P.rayMul(p2pSupplyIndex); testEquality(expectedInP2P, amountPerSupplier, "on pool"); testEquality(onPool, 0); @@ -183,8 +182,8 @@ contract TestBorrow is TestSetup { (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrower1)); - expectedInP2P = underlyingToP2PUnit(amount / 2, morpho.p2pBorrowIndex(aDai)); - uint256 expectedOnPool = underlyingToAdUnit(amount / 2, normalizedVariableDebt); + expectedInP2P = (amount / 2).rayDiv(morpho.p2pBorrowIndex(aDai)); + uint256 expectedOnPool = (amount / 2).rayDiv(normalizedVariableDebt); testEquality(inP2P, expectedInP2P, "Borrower1 in peer-to-peer"); testEquality(onPool, expectedOnPool, "Borrower1 on pool"); @@ -202,7 +201,7 @@ contract TestBorrow is TestSetup { (, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrower1)); uint256 normalizedVariableDebt = pool.getReserveNormalizedVariableDebt(dai); - uint256 expectedOnPool = underlyingToAdUnit(2 * amount, normalizedVariableDebt); + uint256 expectedOnPool = (2 * amount).rayDiv(normalizedVariableDebt); testEquality(onPool, expectedOnPool); } diff --git a/test-foundry/aave-v2/TestLiquidate.t.sol b/test-foundry/aave-v2/TestLiquidate.t.sol index c92564e02..8e99efecf 100644 --- a/test-foundry/aave-v2/TestLiquidate.t.sol +++ b/test-foundry/aave-v2/TestLiquidate.t.sol @@ -99,8 +99,7 @@ contract TestLiquidate is TestSetup { aDai, address(borrower1) ); - uint256 expectedBorrowBalanceOnPool = aDUnitToUnderlying( - onPoolBorrower, + uint256 expectedBorrowBalanceOnPool = onPoolBorrower.rayMul( pool.getReserveNormalizedVariableDebt(dai) ); testEquality(expectedBorrowBalanceOnPool, toRepay); @@ -128,8 +127,7 @@ contract TestLiquidate is TestSetup { vars.liquidationBonus) / (vars.borrowedTokenUnit * collateralPrice * 10_000); uint256 normalizedIncome = pool.getReserveNormalizedIncome(usdc); - uint256 expectedOnPool = collateralOnPool - - underlyingToScaledBalance(amountToSeize, normalizedIncome); + uint256 expectedOnPool = collateralOnPool - amountToSeize.rayDiv(normalizedIncome); testEquality(onPoolBorrower, expectedOnPool); assertEq(inP2PBorrower, 0); @@ -174,16 +172,15 @@ contract TestLiquidate is TestSetup { address(borrower1) ); - uint256 expectedBorrowBalanceInP2P = aDUnitToUnderlying( - onPoolUsdc, + uint256 expectedBorrowBalanceInP2P = onPoolUsdc.rayMul( pool.getReserveNormalizedVariableDebt(usdc) ) + - p2pUnitToUnderlying(inP2PUsdc, morpho.p2pBorrowIndex(aUsdc)) - + inP2PUsdc.rayMul(morpho.p2pBorrowIndex(aUsdc)) - toRepay; assertApproxEqAbs(onPoolBorrower, 0, 1, "borrower borrow on pool"); assertApproxEqAbs( - p2pUnitToUnderlying(inP2PBorrower, morpho.p2pBorrowIndex(aUsdc)), + inP2PBorrower.rayMul(morpho.p2pBorrowIndex(aUsdc)), expectedBorrowBalanceInP2P, 1, "borrower borrow in peer-to-peer" @@ -210,8 +207,7 @@ contract TestLiquidate is TestSetup { assertApproxEqAbs( onPoolBorrower, - onPoolDai - - underlyingToScaledBalance(amountToSeize, pool.getReserveNormalizedIncome(dai)), + onPoolDai - amountToSeize.rayDiv(pool.getReserveNormalizedIncome(dai)), 1, "borrower supply on pool" ); @@ -256,13 +252,12 @@ contract TestLiquidate is TestSetup { address(borrower1) ); - uint256 expectedBorrowBalanceOnPool = aDUnitToUnderlying( - onPoolUsdc, + uint256 expectedBorrowBalanceOnPool = onPoolUsdc.rayMul( pool.getReserveNormalizedVariableDebt(usdc) ) - toRepay; assertApproxEqAbs( - aDUnitToUnderlying(onPoolBorrower, pool.getReserveNormalizedVariableDebt(usdc)), + onPoolBorrower.rayMul(pool.getReserveNormalizedVariableDebt(usdc)), expectedBorrowBalanceOnPool, 1, "borrower borrow on pool" @@ -291,8 +286,7 @@ contract TestLiquidate is TestSetup { testEquality( onPoolBorrower, - onPoolDai - - underlyingToScaledBalance(amountToSeize, pool.getReserveNormalizedIncome(dai)), + onPoolDai - amountToSeize.rayDiv(pool.getReserveNormalizedIncome(dai)), "borrower supply on pool" ); assertEq(inP2PBorrower, inP2PDai, "borrower supply in peer-to-peer"); diff --git a/test-foundry/aave-v2/TestRepay.t.sol b/test-foundry/aave-v2/TestRepay.t.sol index 08d6111b5..88a44d599 100644 --- a/test-foundry/aave-v2/TestRepay.t.sol +++ b/test-foundry/aave-v2/TestRepay.t.sol @@ -69,10 +69,7 @@ contract TestRepay is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToAdUnit( - suppliedAmount, - pool.getReserveNormalizedVariableDebt(dai) - ); + uint256 expectedOnPool = suppliedAmount.rayDiv(pool.getReserveNormalizedVariableDebt(dai)); testEquality(onPoolSupplier, 0, "supplier on pool"); testEquality(onPoolBorrower1, expectedOnPool, "borrower on pool"); @@ -94,10 +91,7 @@ contract TestRepay is TestSetup { (uint256 inP2PAvailableBorrower, uint256 onPoolAvailableBorrower) = morpho .borrowBalanceInOf(aDai, address(borrower2)); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - (25 * borrowedAmount) / 100, - p2pBorrowIndex - ); + uint256 expectedBorrowBalanceInP2P = ((25 * borrowedAmount) / 100).rayDiv(p2pBorrowIndex); testEquality(inP2PBorrower1, inP2PAvailableBorrower); testEquality(inP2PBorrower1, expectedBorrowBalanceInP2P); @@ -142,37 +136,32 @@ contract TestRepay is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToAdUnit( - suppliedAmount, - pool.getReserveNormalizedVariableDebt(dai) - ); - testEquality(onPoolSupplier, 0); - testEquality(onPoolBorrower1, expectedOnPool); + testEquality( + onPoolBorrower1, + suppliedAmount.rayDiv(pool.getReserveNormalizedVariableDebt(dai)) + ); testEquality(inP2PSupplier, inP2PBorrower1); // NMAX borrowers have debt waiting on pool uint256 NMAX = 20; createSigners(NMAX); - uint256 inP2P; - uint256 onPool; uint256 normalizedVariableDebt = pool.getReserveNormalizedVariableDebt(dai); - + // Minus because borrower1 must not be counted twice ! uint256 amountPerBorrower = (borrowedAmount - suppliedAmount) / (NMAX - 1); - // minus because borrower1 must not be counted twice ! - for (uint256 i = 0; i < NMAX; i++) { + + for (uint256 i; i < NMAX; i++) { if (borrowers[i] == borrower1) continue; borrowers[i].approve(usdc, to6Decimals(collateral)); borrowers[i].supply(aUsdc, to6Decimals(collateral)); borrowers[i].borrow(aDai, amountPerBorrower); - (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - expectedOnPool = underlyingToAdUnit(amountPerBorrower, normalizedVariableDebt); + (uint256 inP2P, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); testEquality(inP2P, 0); - testEquality(onPool, expectedOnPool); + testEquality(onPool, amountPerBorrower.rayDiv(normalizedVariableDebt)); } // Borrower1 repays all of his debt @@ -189,19 +178,17 @@ contract TestRepay is TestSetup { (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier1)); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit(suppliedAmount, p2pBorrowIndex); - testEquality(inP2PSupplier, expectedSupplyBalanceInP2P); + testEquality(inP2PSupplier, suppliedAmount.rayDiv(p2pBorrowIndex)); testEquality(onPoolSupplier, 0); // Now test for each individual borrower that replaced the original - for (uint256 i = 0; i < borrowers.length; i++) { + for (uint256 i; i < borrowers.length; i++) { if (borrowers[i] == borrower1) continue; - (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - uint256 expectedInP2P = p2pUnitToUnderlying(inP2P, p2pBorrowIndex); + (uint256 inP2P, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - testEquality(expectedInP2P, amountPerBorrower); + testEquality(amountPerBorrower, inP2P.rayMul(p2pBorrowIndex)); testEquality(onPool, 0); } } @@ -231,10 +218,7 @@ contract TestRepay is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToAdUnit( - suppliedAmount, - pool.getReserveNormalizedVariableDebt(dai) - ); + uint256 expectedOnPool = suppliedAmount.rayDiv(pool.getReserveNormalizedVariableDebt(dai)); testEquality(onPoolSupplier, 0); testEquality(onPoolBorrower1, expectedOnPool); @@ -250,10 +234,7 @@ contract TestRepay is TestSetup { uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - (25 * borrowedAmount) / 100, - p2pBorrowIndex - ); + uint256 expectedBorrowBalanceInP2P = ((25 * borrowedAmount) / 100).rayDiv(p2pBorrowIndex); testEquality(inP2PBorrower1, expectedBorrowBalanceInP2P); testEquality(onPoolBorrower1, 0); @@ -261,14 +242,8 @@ contract TestRepay is TestSetup { // Check balances for supplier (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier1)); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit( - suppliedAmount / 2, - p2pBorrowIndex - ); - uint256 expectedSupplyBalanceOnPool = underlyingToAdUnit( - suppliedAmount / 2, - normalizedIncome - ); + uint256 expectedSupplyBalanceInP2P = (suppliedAmount / 2).rayDiv(p2pBorrowIndex); + uint256 expectedSupplyBalanceOnPool = (suppliedAmount / 2).rayDiv(normalizedIncome); testEquality(inP2PSupplier, expectedSupplyBalanceInP2P); testEquality(onPoolSupplier, expectedSupplyBalanceOnPool); @@ -306,13 +281,11 @@ contract TestRepay is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToAdUnit( - suppliedAmount, - pool.getReserveNormalizedVariableDebt(dai) - ); - testEquality(onPoolSupplier, 0); - testEquality(onPoolBorrower1, expectedOnPool); + testEquality( + onPoolBorrower1, + suppliedAmount.rayDiv(pool.getReserveNormalizedVariableDebt(dai)) + ); testEquality(inP2PSupplier, inP2PBorrower1); // NMAX borrowers have borrowerAmount/2 (cumulated) of debt waiting on pool @@ -343,28 +316,21 @@ contract TestRepay is TestSetup { (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier1)); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); - uint256 expectedSupplyBalanceOnPool = underlyingToP2PUnit( - suppliedAmount / 2, - normalizedIncome + testEquality(inP2PSupplier, (suppliedAmount / 2).rayDiv(p2pBorrowIndex)); + testEquality( + onPoolSupplier, + (suppliedAmount / 2).rayDiv(pool.getReserveNormalizedIncome(dai)) ); - uint256 expectedSupplyBalanceInP2P = underlyingToAdUnit(suppliedAmount / 2, p2pBorrowIndex); - - testEquality(inP2PSupplier, expectedSupplyBalanceInP2P); - testEquality(onPoolSupplier, expectedSupplyBalanceOnPool); - - uint256 inP2P; - uint256 onPool; // Now test for each individual borrower that replaced the original - for (uint256 i = 0; i < borrowers.length; i++) { + for (uint256 i; i < borrowers.length; i++) { if (borrowers[i] == borrower1) continue; - (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - uint256 expectedInP2P = p2pUnitToUnderlying(inP2P, p2pBorrowIndex); + (uint256 inP2P, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); + uint256 expectedInP2P = inP2P.rayMul(p2pBorrowIndex); - testEquality(expectedInP2P, amountPerBorrower); + testEquality(amountPerBorrower, expectedInP2P); testEquality(onPool, 0); } } @@ -403,7 +369,7 @@ contract TestRepay is TestSetup { { uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - expectedBorrowBalanceInP2P = underlyingToP2PUnit(borrowedAmount, p2pBorrowIndex); + expectedBorrowBalanceInP2P = borrowedAmount.rayDiv(p2pBorrowIndex); // Check balances after match of supplier1 (uint256 inP2PBorrower, uint256 onPoolBorrower) = morpho.borrowBalanceInOf( @@ -414,10 +380,7 @@ contract TestRepay is TestSetup { assertApproxEqAbs(inP2PBorrower, expectedBorrowBalanceInP2P, 1e3); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit( - suppliedAmount, - p2pSupplyIndex - ); + uint256 expectedSupplyBalanceInP2P = suppliedAmount.rayDiv(p2pSupplyIndex); for (uint256 i = 0; i < 20; i++) { (uint256 inP2PSupplier, uint256 onPoolSupplier) = morpho.supplyBalanceInOf( @@ -443,8 +406,7 @@ contract TestRepay is TestSetup { // There should be a delta uint256 expectedSupplyP2PDeltaInUnderlying = 10 * suppliedAmount; - uint256 expectedSupplyP2PDelta = underlyingToScaledBalance( - expectedSupplyP2PDeltaInUnderlying, + uint256 expectedSupplyP2PDelta = expectedSupplyP2PDeltaInUnderlying.rayDiv( pool.getReserveNormalizedIncome(dai) ); (uint256 supplyP2PDelta, , , ) = morpho.deltas(aDai); @@ -456,8 +418,7 @@ contract TestRepay is TestSetup { borrower2.borrow(aDai, expectedSupplyP2PDeltaInUnderlying / 2); (inP2PBorrower, onPoolBorrower) = morpho.borrowBalanceInOf(aDai, address(borrower2)); - expectedBorrowBalanceInP2P = underlyingToP2PUnit( - expectedSupplyP2PDeltaInUnderlying / 2, + expectedBorrowBalanceInP2P = (expectedSupplyP2PDeltaInUnderlying / 2).rayDiv( p2pBorrowIndex ); @@ -519,7 +480,7 @@ contract TestRepay is TestSetup { address(suppliers[i]) ); assertApproxEqAbs( - p2pUnitToUnderlying(inP2PSupplier, newVars.SP2PER), + inP2PSupplier.rayMul(newVars.SP2PER), expectedSupplyBalanceInUnderlying, (expectedSupplyBalanceInUnderlying * 2) / 100, "not expected balance peer-to-peer" diff --git a/test-foundry/aave-v2/TestSupply.t.sol b/test-foundry/aave-v2/TestSupply.t.sol index 5a12e298c..f5f1b1ff0 100644 --- a/test-foundry/aave-v2/TestSupply.t.sol +++ b/test-foundry/aave-v2/TestSupply.t.sol @@ -6,6 +6,7 @@ import "./helpers/FlashLoan.sol"; contract TestSupply is TestSetup { using stdStorage for StdStorage; + using WadRayMath for uint256; // There are no available borrowers: all of the supplied amount is supplied to the pool and set `onPool`. function testSupply1() public { @@ -15,7 +16,7 @@ contract TestSupply is TestSetup { supplier1.supply(aDai, amount); uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); - uint256 expectedOnPool = underlyingToScaledBalance(amount, normalizedIncome); + uint256 expectedOnPool = amount.rayDiv(normalizedIncome); testEquality(IERC20(aDai).balanceOf(address(morpho)), amount); @@ -43,7 +44,7 @@ contract TestSupply is TestSetup { testEquality(daiBalanceAfter, expectedDaiBalanceAfter); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit(amount, p2pSupplyIndex); + uint256 expectedSupplyBalanceInP2P = amount.rayDiv(p2pSupplyIndex); (uint256 inP2PSupplier, uint256 onPoolSupplier) = morpho.supplyBalanceInOf( aDai, @@ -74,10 +75,10 @@ contract TestSupply is TestSetup { supplier1.supply(aDai, 2 * amount); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit(amount, p2pSupplyIndex); + uint256 expectedSupplyBalanceInP2P = amount.rayDiv(p2pSupplyIndex); uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); - uint256 expectedSupplyBalanceOnPool = underlyingToScaledBalance(amount, normalizedIncome); + uint256 expectedSupplyBalanceOnPool = amount.rayDiv(normalizedIncome); (uint256 inP2PSupplier, uint256 onPoolSupplier) = morpho.supplyBalanceInOf( aDai, @@ -129,14 +130,14 @@ contract TestSupply is TestSetup { for (uint256 i = 0; i < NMAX; i++) { (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - expectedInP2PInUnderlying = p2pUnitToUnderlying(inP2P, p2pSupplyIndex); + expectedInP2PInUnderlying = inP2P.rayMul(p2pSupplyIndex); testEquality(expectedInP2PInUnderlying, amountPerBorrower, "amount per borrower"); testEquality(onPool, 0, "on pool per borrower"); } (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(supplier1)); - uint256 expectedInP2P = underlyingToP2PUnit(amount, morpho.p2pBorrowIndex(aDai)); + uint256 expectedInP2P = amount.rayDiv(morpho.p2pBorrowIndex(aDai)); testEquality(inP2P, expectedInP2P); testEquality(onPool, 0); @@ -178,7 +179,7 @@ contract TestSupply is TestSetup { for (uint256 i = 0; i < NMAX; i++) { (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - expectedInP2PInUnderlying = p2pUnitToUnderlying(inP2P, p2pBorrowIndex); + expectedInP2PInUnderlying = inP2P.rayMul(p2pBorrowIndex); testEquality(expectedInP2PInUnderlying, amountPerBorrower, "borrower in peer-to-peer"); testEquality(onPool, 0); @@ -186,8 +187,8 @@ contract TestSupply is TestSetup { (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(supplier1)); - uint256 expectedInP2P = underlyingToP2PUnit(amount / 2, morpho.p2pSupplyIndex(aDai)); - uint256 expectedOnPool = underlyingToAdUnit(amount / 2, normalizedIncome); + uint256 expectedInP2P = (amount / 2).rayDiv(morpho.p2pSupplyIndex(aDai)); + uint256 expectedOnPool = (amount / 2).rayDiv(normalizedIncome); testEquality(inP2P, expectedInP2P, "in peer-to-peer"); testEquality(onPool, expectedOnPool, "in pool"); @@ -202,7 +203,7 @@ contract TestSupply is TestSetup { supplier1.supply(aDai, amount); uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); - uint256 expectedOnPool = underlyingToScaledBalance(2 * amount, normalizedIncome); + uint256 expectedOnPool = (2 * amount).rayDiv(normalizedIncome); (, uint256 onPool) = morpho.supplyBalanceInOf(aDai, address(supplier1)); testEquality(onPool, expectedOnPool); @@ -238,7 +239,7 @@ contract TestSupply is TestSetup { morpho.supply(aDai, address(supplier2), amount); uint256 poolSupplyIndex = pool.getReserveNormalizedIncome(dai); - uint256 expectedOnPool = underlyingToScaledBalance(amount, poolSupplyIndex); + uint256 expectedOnPool = amount.rayDiv(poolSupplyIndex); assertEq(ERC20(aDai).balanceOf(address(morpho)), amount, "balance of aToken"); diff --git a/test-foundry/aave-v2/TestWithdraw.t.sol b/test-foundry/aave-v2/TestWithdraw.t.sol index 9d290b577..ed779a529 100644 --- a/test-foundry/aave-v2/TestWithdraw.t.sol +++ b/test-foundry/aave-v2/TestWithdraw.t.sol @@ -31,7 +31,7 @@ contract TestWithdraw is TestSetup { (uint256 inP2P, uint256 onPool) = morpho.supplyBalanceInOf(aUsdc, address(supplier1)); uint256 expectedOnPool = to6Decimals( - underlyingToScaledBalance(2 * amount, pool.getReserveNormalizedIncome(usdc)) + (2 * amount).rayDiv(pool.getReserveNormalizedIncome(usdc)) ); testEquality(inP2P, 0); @@ -55,9 +55,7 @@ contract TestWithdraw is TestSetup { uint256 balanceBefore = supplier1.balanceOf(usdc); (uint256 inP2P, uint256 onPool) = morpho.supplyBalanceInOf(aUsdc, address(supplier1)); - uint256 expectedOnPool = to6Decimals( - underlyingToScaledBalance(amount, pool.getReserveNormalizedIncome(usdc)) - ); + uint256 expectedOnPool = to6Decimals(amount.rayDiv(pool.getReserveNormalizedIncome(usdc))); testEquality(inP2P, 0); testEquality(onPool, expectedOnPool); @@ -96,10 +94,7 @@ contract TestWithdraw is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToScaledBalance( - suppliedAmount / 2, - pool.getReserveNormalizedIncome(dai) - ); + uint256 expectedOnPool = (suppliedAmount / 2).rayDiv(pool.getReserveNormalizedIncome(dai)); testEquality(onPoolSupplier, expectedOnPool, "supplier on pool 0"); testEquality(onPoolBorrower1, 0, "borrower on pool 0"); @@ -120,7 +115,7 @@ contract TestWithdraw is TestSetup { // Check balances for supplier2 (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier2)); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedInP2P = underlyingToP2PUnit(suppliedAmount / 2, p2pSupplyIndex); + uint256 expectedInP2P = (suppliedAmount / 2).rayDiv(p2pSupplyIndex); testEquality(onPoolSupplier, expectedOnPool, "supplier on pool 2"); testEquality(inP2PSupplier, expectedInP2P, "supplier in P2P 2"); @@ -165,10 +160,7 @@ contract TestWithdraw is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToScaledBalance( - suppliedAmount / 2, - pool.getReserveNormalizedIncome(dai) - ); + uint256 expectedOnPool = (suppliedAmount / 2).rayDiv(pool.getReserveNormalizedIncome(dai)); testEquality(onPoolSupplier, expectedOnPool, "supplier on pool"); testEquality(onPoolBorrower, 0, "borrower on pool"); @@ -199,10 +191,7 @@ contract TestWithdraw is TestSetup { (inP2PBorrower, onPoolBorrower) = morpho.borrowBalanceInOf(aDai, address(borrower1)); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - borrowedAmount, - morpho.p2pBorrowIndex(aDai) - ); + uint256 expectedBorrowBalanceInP2P = borrowedAmount.rayDiv(morpho.p2pBorrowIndex(aDai)); testEquality(inP2PBorrower, expectedBorrowBalanceInP2P, "borrower in P2P"); testEquality(onPoolBorrower, 0, "borrower on pool"); @@ -215,7 +204,7 @@ contract TestWithdraw is TestSetup { if (suppliers[i] == supplier1) continue; (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(suppliers[i])); - uint256 expectedInP2P = p2pUnitToUnderlying(inP2P, p2pSupplyIndex); + uint256 expectedInP2P = inP2P.rayMul(p2pSupplyIndex); testEquality(expectedInP2P, amountPerSupplier, "new supplier in P2P"); testEquality(onPool, 0, "new supplier on pool"); @@ -247,10 +236,7 @@ contract TestWithdraw is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToScaledBalance( - suppliedAmount / 2, - pool.getReserveNormalizedIncome(dai) - ); + uint256 expectedOnPool = (suppliedAmount / 2).rayDiv(pool.getReserveNormalizedIncome(dai)); testEquality(onPoolSupplier, expectedOnPool, "supplier on pool"); testEquality(onPoolBorrower, 0, "borrower on pool"); @@ -263,12 +249,8 @@ contract TestWithdraw is TestSetup { (inP2PBorrower, onPoolBorrower) = morpho.borrowBalanceInOf(aDai, address(borrower1)); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - borrowedAmount / 2, - p2pSupplyIndex - ); - uint256 expectedBorrowBalanceOnPool = underlyingToAdUnit( - borrowedAmount / 2, + uint256 expectedBorrowBalanceInP2P = (borrowedAmount / 2).rayDiv(p2pSupplyIndex); + uint256 expectedBorrowBalanceOnPool = (borrowedAmount / 2).rayDiv( pool.getReserveNormalizedVariableDebt(dai) ); @@ -278,10 +260,7 @@ contract TestWithdraw is TestSetup { // Check balances for supplier (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier1)); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit( - (25 * suppliedAmount) / 100, - p2pSupplyIndex - ); + uint256 expectedSupplyBalanceInP2P = ((25 * suppliedAmount) / 100).rayDiv(p2pSupplyIndex); testEquality(inP2PSupplier, expectedSupplyBalanceInP2P); testEquality(onPoolSupplier, 0); @@ -322,10 +301,7 @@ contract TestWithdraw is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToScaledBalance( - suppliedAmount / 2, - pool.getReserveNormalizedIncome(dai) - ); + uint256 expectedOnPool = (suppliedAmount / 2).rayDiv(pool.getReserveNormalizedIncome(dai)); testEquality(onPoolSupplier, expectedOnPool); testEquality(onPoolBorrower, 0); @@ -356,12 +332,8 @@ contract TestWithdraw is TestSetup { (inP2PBorrower, onPoolBorrower) = morpho.borrowBalanceInOf(aDai, address(borrower1)); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - borrowedAmount / 2, - p2pSupplyIndex - ); - uint256 expectedBorrowBalanceOnPool = underlyingToAdUnit( - borrowedAmount / 2, + uint256 expectedBorrowBalanceInP2P = (borrowedAmount / 2).rayDiv(p2pSupplyIndex); + uint256 expectedBorrowBalanceOnPool = (borrowedAmount / 2).rayDiv( pool.getReserveNormalizedVariableDebt(dai) ); @@ -376,7 +348,7 @@ contract TestWithdraw is TestSetup { if (suppliers[i] == supplier1) continue; (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(suppliers[i])); - uint256 expectedInP2P = p2pUnitToUnderlying(inP2P, p2pSupplyIndex); + uint256 expectedInP2P = inP2P.rayMul(p2pSupplyIndex); testEquality(expectedInP2P, amountPerSupplier); testEquality(onPool, 0); @@ -420,7 +392,7 @@ contract TestWithdraw is TestSetup { { uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - expectedSupplyBalanceInP2P = underlyingToP2PUnit(suppliedAmount, p2pSupplyIndex); + expectedSupplyBalanceInP2P = suppliedAmount.rayDiv(p2pSupplyIndex); // Check balances after match of supplier1 and borrowers (uint256 inP2PSupplier, uint256 onPoolSupplier) = morpho.supplyBalanceInOf( @@ -431,10 +403,7 @@ contract TestWithdraw is TestSetup { testEquality(inP2PSupplier, expectedSupplyBalanceInP2P); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - borrowedAmount, - p2pBorrowIndex - ); + uint256 expectedBorrowBalanceInP2P = borrowedAmount.rayDiv(p2pBorrowIndex); for (uint256 i = 10; i < 20; i++) { (uint256 inP2PBorrower, uint256 onPoolBorrower) = morpho.borrowBalanceInOf( @@ -456,8 +425,7 @@ contract TestWithdraw is TestSetup { // There should be a delta uint256 expectedBorrowP2PDeltaInUnderlying = 10 * borrowedAmount; - uint256 expectedBorrowP2PDelta = underlyingToAdUnit( - expectedBorrowP2PDeltaInUnderlying, + uint256 expectedBorrowP2PDelta = expectedBorrowP2PDeltaInUnderlying.rayDiv( pool.getReserveNormalizedVariableDebt(dai) ); @@ -469,8 +437,7 @@ contract TestWithdraw is TestSetup { supplier2.supply(aDai, expectedBorrowP2PDeltaInUnderlying / 2); (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier2)); - expectedSupplyBalanceInP2P = underlyingToP2PUnit( - expectedBorrowP2PDeltaInUnderlying / 2, + expectedSupplyBalanceInP2P = (expectedBorrowP2PDeltaInUnderlying / 2).rayDiv( p2pSupplyIndex ); @@ -530,7 +497,7 @@ contract TestWithdraw is TestSetup { address(borrowers[i]) ); assertApproxEqAbs( - p2pUnitToUnderlying(inP2PBorrower, newVars.BP2PER), + inP2PBorrower.rayMul(newVars.BP2PER), expectedBorrowBalanceInUnderlying, (expectedBorrowBalanceInUnderlying * 2) / 100, "not expected underlying balance" diff --git a/test-foundry/aave-v2/setup/Utils.sol b/test-foundry/aave-v2/setup/Utils.sol index f5a9a08d2..55170daad 100644 --- a/test-foundry/aave-v2/setup/Utils.sol +++ b/test-foundry/aave-v2/setup/Utils.sol @@ -24,62 +24,6 @@ contract Utils is Test { return value / 1e10; } - function underlyingToScaledBalance(uint256 _scaledBalance, uint256 _normalizedIncome) - internal - pure - returns (uint256) - { - return (_scaledBalance * RAY) / _normalizedIncome; - } - - function scaledBalanceToUnderlying(uint256 _scaledBalance, uint256 _normalizedIncome) - internal - pure - returns (uint256) - { - return (_scaledBalance * _normalizedIncome) / RAY; - } - - function underlyingToAdUnit(uint256 _underlyingAmount, uint256 _normalizedVariableDebt) - internal - pure - returns (uint256) - { - return (_underlyingAmount * RAY) / _normalizedVariableDebt; - } - - function aDUnitToUnderlying(uint256 _aDUnitAmount, uint256 _normalizedVariableDebt) - internal - pure - returns (uint256) - { - return (_aDUnitAmount * _normalizedVariableDebt) / RAY; - } - - function underlyingToP2PUnit(uint256 _underlyingAmount, uint256 _p2pExchangeRate) - internal - pure - returns (uint256) - { - return (_underlyingAmount * RAY) / _p2pExchangeRate; - } - - function p2pUnitToUnderlying(uint256 _p2pUnitAmount, uint256 _p2pExchangeRate) - internal - pure - returns (uint256) - { - return (_p2pUnitAmount * _p2pExchangeRate) / RAY; - } - - function getAbsDiff(uint256 a, uint256 b) internal pure returns (uint256) { - if (a > b) { - return a - b; - } - - return b - a; - } - function testEquality(uint256 _firstValue, uint256 _secondValue) internal { assertApproxEqAbs(_firstValue, _secondValue, 20); } diff --git a/test-foundry/aave-v3/TestBorrow.t.sol b/test-foundry/aave-v3/TestBorrow.t.sol index 1e02c23f3..714487100 100644 --- a/test-foundry/aave-v3/TestBorrow.t.sol +++ b/test-foundry/aave-v3/TestBorrow.t.sol @@ -5,6 +5,7 @@ import "./setup/TestSetup.sol"; contract TestBorrow is TestSetup { using ReserveConfiguration for DataTypes.ReserveConfigurationMap; + using WadRayMath for uint256; // The borrower tries to borrow more than his collateral allows, the transaction reverts. function testBorrow1() public { @@ -30,7 +31,7 @@ contract TestBorrow is TestSetup { (uint256 inP2P, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrower1)); uint256 normalizedVariableDebt = pool.getReserveNormalizedVariableDebt(dai); - uint256 expectedOnPool = underlyingToAdUnit(amount, normalizedVariableDebt); + uint256 expectedOnPool = amount.rayDiv(normalizedVariableDebt); testEquality(onPool, expectedOnPool); testEquality(inP2P, 0); @@ -49,10 +50,7 @@ contract TestBorrow is TestSetup { (uint256 supplyInP2P, ) = morpho.supplyBalanceInOf(aDai, address(supplier1)); - uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 expectedInP2P = p2pUnitToUnderlying(supplyInP2P, p2pBorrowIndex); - - testEquality(expectedInP2P, amount); + testEquality(supplyInP2P, amount.rayDiv(morpho.p2pSupplyIndex(aDai))); (uint256 inP2P, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrower1)); @@ -79,7 +77,7 @@ contract TestBorrow is TestSetup { testEquality(inP2P, supplyInP2P, "in P2P"); uint256 normalizedVariableDebt = pool.getReserveNormalizedVariableDebt(dai); - uint256 expectedOnPool = underlyingToAdUnit(amount, normalizedVariableDebt); + uint256 expectedOnPool = amount.rayDiv(normalizedVariableDebt); assertApproxEqAbs(onPool, expectedOnPool, 1e15, "on pool"); } @@ -113,14 +111,11 @@ contract TestBorrow is TestSetup { uint256 inP2P; uint256 onPool; uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedInP2P; for (uint256 i = 0; i < NMAX; i++) { (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(suppliers[i])); - expectedInP2P = p2pUnitToUnderlying(inP2P, p2pSupplyIndex); - - testEqualityLarge(expectedInP2P, amountPerSupplier); + testEqualityLarge(amountPerSupplier, inP2P.rayMul(p2pSupplyIndex)); testEqualityLarge(onPool, 0); } @@ -128,7 +123,7 @@ contract TestBorrow is TestSetup { testEquality( inP2P, - underlyingToP2PUnit(amount, morpho.p2pBorrowIndex(aDai)), + amount.rayDiv(morpho.p2pBorrowIndex(aDai)), "Borrower1 in peer-to-peer" ); testEqualityLarge(onPool, 0, "Borrower1 on pool"); @@ -172,7 +167,7 @@ contract TestBorrow is TestSetup { for (uint256 i = 0; i < NMAX; i++) { (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(suppliers[i])); - expectedInP2P = p2pUnitToUnderlying(inP2P, p2pSupplyIndex); + expectedInP2P = inP2P.rayMul(p2pSupplyIndex); testEqualityLarge(expectedInP2P, amountPerSupplier, "on pool"); testEqualityLarge(onPool, 0, "in P2P"); @@ -180,8 +175,8 @@ contract TestBorrow is TestSetup { (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrower1)); - expectedInP2P = underlyingToP2PUnit(amount / 2, morpho.p2pBorrowIndex(aDai)); - uint256 expectedOnPool = underlyingToAdUnit(amount / 2, normalizedVariableDebt); + expectedInP2P = (amount / 2).rayDiv(morpho.p2pBorrowIndex(aDai)); + uint256 expectedOnPool = (amount / 2).rayDiv(normalizedVariableDebt); testEqualityLarge(inP2P, expectedInP2P, "Borrower1 in peer-to-peer"); testEqualityLarge(onPool, expectedOnPool, "Borrower1 on pool"); @@ -199,7 +194,7 @@ contract TestBorrow is TestSetup { (, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrower1)); uint256 normalizedVariableDebt = pool.getReserveNormalizedVariableDebt(dai); - uint256 expectedOnPool = underlyingToAdUnit(2 * amount, normalizedVariableDebt); + uint256 expectedOnPool = (2 * amount).rayDiv(normalizedVariableDebt); testEquality(onPool, expectedOnPool); } diff --git a/test-foundry/aave-v3/TestLiquidate.t.sol b/test-foundry/aave-v3/TestLiquidate.t.sol index 99e64a518..d02791f95 100644 --- a/test-foundry/aave-v3/TestLiquidate.t.sol +++ b/test-foundry/aave-v3/TestLiquidate.t.sol @@ -99,11 +99,10 @@ contract TestLiquidate is TestSetup { aDai, address(borrower1) ); - uint256 expectedBorrowBalanceOnPool = aDUnitToUnderlying( - onPoolBorrower, + uint256 borrowOnPoolInUnderlying = onPoolBorrower.rayMul( pool.getReserveNormalizedVariableDebt(dai) ); - testEqualityLarge(expectedBorrowBalanceOnPool, toRepay); + testEqualityLarge(borrowOnPoolInUnderlying, toRepay); assertEq(inP2PBorrower, 0); // Check borrower1 supply balance @@ -127,8 +126,7 @@ contract TestLiquidate is TestSetup { vars.liquidationBonus) / (vars.borrowedTokenUnit * collateralPrice * 10_000); uint256 normalizedIncome = pool.getReserveNormalizedIncome(usdc); - uint256 expectedOnPool = collateralOnPool - - underlyingToScaledBalance(amountToSeize, normalizedIncome); + uint256 expectedOnPool = collateralOnPool - amountToSeize.rayDiv(normalizedIncome); testEqualityLarge(onPoolBorrower, expectedOnPool); assertEq(inP2PBorrower, 0); @@ -173,17 +171,16 @@ contract TestLiquidate is TestSetup { address(borrower1) ); - uint256 expectedBorrowBalanceInP2P = aDUnitToUnderlying( - onPoolUsdc, + uint256 expectedBorrowBalanceInP2PInUnderlying = onPoolUsdc.rayMul( pool.getReserveNormalizedVariableDebt(usdc) ) + - p2pUnitToUnderlying(inP2PUsdc, morpho.p2pBorrowIndex(aUsdc)) - + inP2PUsdc.rayMul(morpho.p2pBorrowIndex(aUsdc)) - toRepay; assertApproxEqAbs(onPoolBorrower, 0, 1, "borrower borrow on pool"); assertApproxEqAbs( - p2pUnitToUnderlying(inP2PBorrower, morpho.p2pBorrowIndex(aUsdc)), - expectedBorrowBalanceInP2P, + inP2PBorrower.rayMul(morpho.p2pBorrowIndex(aUsdc)), + expectedBorrowBalanceInP2PInUnderlying, 1, "borrower borrow in peer-to-peer" ); @@ -208,8 +205,7 @@ contract TestLiquidate is TestSetup { testEqualityLarge( onPoolBorrower, - onPoolDai - - underlyingToScaledBalance(amountToSeize, pool.getReserveNormalizedIncome(dai)), + onPoolDai - amountToSeize.rayDiv(pool.getReserveNormalizedIncome(dai)), "borrower supply on pool" ); assertEq(inP2PBorrower, inP2PDai, "borrower supply in peer-to-peer"); @@ -253,13 +249,12 @@ contract TestLiquidate is TestSetup { address(borrower1) ); - uint256 expectedBorrowBalanceOnPool = aDUnitToUnderlying( - onPoolUsdc, + uint256 expectedBorrowBalanceOnPool = onPoolUsdc.rayMul( pool.getReserveNormalizedVariableDebt(usdc) ) - toRepay; assertApproxEqAbs( - aDUnitToUnderlying(onPoolBorrower, pool.getReserveNormalizedVariableDebt(usdc)), + onPoolBorrower.rayMul(pool.getReserveNormalizedVariableDebt(usdc)), expectedBorrowBalanceOnPool, 1, "borrower borrow on pool" @@ -287,8 +282,7 @@ contract TestLiquidate is TestSetup { testEquality( onPoolBorrower, - onPoolDai - - underlyingToScaledBalance(amountToSeize, pool.getReserveNormalizedIncome(dai)), + onPoolDai - amountToSeize.rayDiv(pool.getReserveNormalizedIncome(dai)), "borrower supply on pool" ); assertEq(inP2PBorrower, inP2PDai, "borrower supply in peer-to-peer"); diff --git a/test-foundry/aave-v3/TestRepay.t.sol b/test-foundry/aave-v3/TestRepay.t.sol index 60e4671ef..0dde469ff 100644 --- a/test-foundry/aave-v3/TestRepay.t.sol +++ b/test-foundry/aave-v3/TestRepay.t.sol @@ -69,10 +69,7 @@ contract TestRepay is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToAdUnit( - suppliedAmount, - pool.getReserveNormalizedVariableDebt(dai) - ); + uint256 expectedOnPool = suppliedAmount.rayDiv(pool.getReserveNormalizedVariableDebt(dai)); testEquality(onPoolSupplier, 0, "supplier on pool"); testEqualityLarge(onPoolBorrower1, expectedOnPool, "borrower on pool"); @@ -94,10 +91,7 @@ contract TestRepay is TestSetup { (uint256 inP2PAvailableBorrower, uint256 onPoolAvailableBorrower) = morpho .borrowBalanceInOf(aDai, address(borrower2)); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - (25 * borrowedAmount) / 100, - p2pBorrowIndex - ); + uint256 expectedBorrowBalanceInP2P = ((25 * borrowedAmount) / 100).rayDiv(p2pBorrowIndex); testEqualityLarge(inP2PBorrower1, inP2PAvailableBorrower, "borrower in P2P 1"); testEqualityLarge(inP2PBorrower1, expectedBorrowBalanceInP2P, "borrower in P2P 2"); @@ -142,10 +136,7 @@ contract TestRepay is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToAdUnit( - suppliedAmount, - pool.getReserveNormalizedVariableDebt(dai) - ); + uint256 expectedOnPool = suppliedAmount.rayDiv(pool.getReserveNormalizedVariableDebt(dai)); testEquality(onPoolSupplier, 0); testEqualityLarge(onPoolBorrower1, expectedOnPool); @@ -155,21 +146,19 @@ contract TestRepay is TestSetup { uint256 NMAX = 20; createSigners(NMAX); - uint256 inP2P; - uint256 onPool; uint256 normalizedVariableDebt = pool.getReserveNormalizedVariableDebt(dai); uint256 amountPerBorrower = (borrowedAmount - suppliedAmount) / (NMAX - 1); // minus because borrower1 must not be counted twice ! - for (uint256 i = 0; i < NMAX; i++) { + for (uint256 i; i < NMAX; i++) { if (borrowers[i] == borrower1) continue; borrowers[i].approve(usdc, to6Decimals(collateral)); borrowers[i].supply(aUsdc, to6Decimals(collateral)); borrowers[i].borrow(aDai, amountPerBorrower); - (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - expectedOnPool = underlyingToAdUnit(amountPerBorrower, normalizedVariableDebt); + (uint256 inP2P, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); + expectedOnPool = amountPerBorrower.rayDiv(normalizedVariableDebt); testEqualityLarge(inP2P, 0); testEqualityLarge(onPool, expectedOnPool); @@ -189,19 +178,18 @@ contract TestRepay is TestSetup { (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier1)); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit(suppliedAmount, p2pBorrowIndex); - testEqualityLarge(inP2PSupplier, expectedSupplyBalanceInP2P); + testEqualityLarge(inP2PSupplier, suppliedAmount.rayDiv(p2pBorrowIndex)); testEqualityLarge(onPoolSupplier, 0); // Now test for each individual borrower that replaced the original - for (uint256 i = 0; i < borrowers.length; i++) { + for (uint256 i; i < borrowers.length; i++) { if (borrowers[i] == borrower1) continue; - (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - uint256 expectedInP2P = p2pUnitToUnderlying(inP2P, p2pBorrowIndex); + (uint256 inP2P, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); + uint256 inPP2InUnderlying = inP2P.rayMul(p2pBorrowIndex); - testEqualityLarge(expectedInP2P, amountPerBorrower); + testEqualityLarge(inPP2InUnderlying, amountPerBorrower); testEqualityLarge(onPool, 0); } } @@ -231,10 +219,7 @@ contract TestRepay is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToAdUnit( - suppliedAmount, - pool.getReserveNormalizedVariableDebt(dai) - ); + uint256 expectedOnPool = suppliedAmount.rayDiv(pool.getReserveNormalizedVariableDebt(dai)); testEquality(onPoolSupplier, 0); testEqualityLarge(onPoolBorrower1, expectedOnPool); @@ -250,10 +235,7 @@ contract TestRepay is TestSetup { uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - (25 * borrowedAmount) / 100, - p2pBorrowIndex - ); + uint256 expectedBorrowBalanceInP2P = ((25 * borrowedAmount) / 100).rayDiv(p2pBorrowIndex); testEqualityLarge(inP2PBorrower1, expectedBorrowBalanceInP2P); testEqualityLarge(onPoolBorrower1, 0); @@ -261,14 +243,8 @@ contract TestRepay is TestSetup { // Check balances for supplier (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier1)); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit( - suppliedAmount / 2, - p2pBorrowIndex - ); - uint256 expectedSupplyBalanceOnPool = underlyingToAdUnit( - suppliedAmount / 2, - normalizedIncome - ); + uint256 expectedSupplyBalanceInP2P = (suppliedAmount / 2).rayDiv(p2pBorrowIndex); + uint256 expectedSupplyBalanceOnPool = (suppliedAmount / 2).rayDiv(normalizedIncome); testEqualityLarge(inP2PSupplier, expectedSupplyBalanceInP2P); testEqualityLarge(onPoolSupplier, expectedSupplyBalanceOnPool); @@ -306,13 +282,11 @@ contract TestRepay is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToAdUnit( - suppliedAmount, - pool.getReserveNormalizedVariableDebt(dai) - ); - testEquality(onPoolSupplier, 0); - testEqualityLarge(onPoolBorrower1, expectedOnPool); + testEqualityLarge( + onPoolBorrower1, + suppliedAmount.rayDiv(pool.getReserveNormalizedVariableDebt(dai)) + ); testEqualityLarge(inP2PSupplier, inP2PBorrower1); // NMAX borrowers have borrowerAmount/2 (cumulated) of debt waiting on pool @@ -343,28 +317,21 @@ contract TestRepay is TestSetup { (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier1)); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); - uint256 expectedSupplyBalanceOnPool = underlyingToP2PUnit( - suppliedAmount / 2, - normalizedIncome + testEqualityLarge(inP2PSupplier, (suppliedAmount / 2).rayDiv(p2pBorrowIndex)); + testEqualityLarge( + onPoolSupplier, + (suppliedAmount / 2).rayDiv(pool.getReserveNormalizedIncome(dai)) ); - uint256 expectedSupplyBalanceInP2P = underlyingToAdUnit(suppliedAmount / 2, p2pBorrowIndex); - - testEqualityLarge(inP2PSupplier, expectedSupplyBalanceInP2P); - testEqualityLarge(onPoolSupplier, expectedSupplyBalanceOnPool); - - uint256 inP2P; - uint256 onPool; // Now test for each individual borrower that replaced the original - for (uint256 i = 0; i < borrowers.length; i++) { + for (uint256 i; i < borrowers.length; i++) { if (borrowers[i] == borrower1) continue; - (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - uint256 expectedInP2P = p2pUnitToUnderlying(inP2P, p2pBorrowIndex); + (uint256 inP2P, uint256 onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); + uint256 inP2PInUnderlying = inP2P.rayMul(p2pBorrowIndex); - testEqualityLarge(expectedInP2P, amountPerBorrower); + testEqualityLarge(inP2PInUnderlying, amountPerBorrower); testEquality(onPool, 0); } } @@ -405,7 +372,7 @@ contract TestRepay is TestSetup { { uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - expectedBorrowBalanceInP2P = underlyingToP2PUnit(borrowedAmount, p2pBorrowIndex); + expectedBorrowBalanceInP2P = borrowedAmount.rayDiv(p2pBorrowIndex); // Check balances after match of supplier1 (uint256 inP2PBorrower, uint256 onPoolBorrower) = morpho.borrowBalanceInOf( @@ -416,10 +383,7 @@ contract TestRepay is TestSetup { testEqualityLarge(inP2PBorrower, expectedBorrowBalanceInP2P); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit( - suppliedAmount, - p2pSupplyIndex - ); + uint256 expectedSupplyBalanceInP2P = suppliedAmount.rayDiv(p2pSupplyIndex); for (uint256 i = 0; i < 20; i++) { (uint256 inP2PSupplier, uint256 onPoolSupplier) = morpho.supplyBalanceInOf( @@ -445,8 +409,7 @@ contract TestRepay is TestSetup { // There should be a delta uint256 expectedSupplyP2PDeltaInUnderlying = 10 * suppliedAmount; - uint256 expectedSupplyP2PDelta = underlyingToScaledBalance( - expectedSupplyP2PDeltaInUnderlying, + uint256 expectedSupplyP2PDelta = expectedSupplyP2PDeltaInUnderlying.rayDiv( pool.getReserveNormalizedIncome(dai) ); (uint256 supplyP2PDelta, , , ) = morpho.deltas(aDai); @@ -458,8 +421,7 @@ contract TestRepay is TestSetup { borrower2.borrow(aDai, expectedSupplyP2PDeltaInUnderlying / 2); (inP2PBorrower, onPoolBorrower) = morpho.borrowBalanceInOf(aDai, address(borrower2)); - expectedBorrowBalanceInP2P = underlyingToP2PUnit( - expectedSupplyP2PDeltaInUnderlying / 2, + expectedBorrowBalanceInP2P = (expectedSupplyP2PDeltaInUnderlying / 2).rayDiv( p2pBorrowIndex ); @@ -524,7 +486,7 @@ contract TestRepay is TestSetup { address(suppliers[i]) ); assertApproxEqAbs( - p2pUnitToUnderlying(inP2PSupplier, newVars.SP2PER), + inP2PSupplier.rayMul(newVars.SP2PER), expectedSupplyBalanceInUnderlying, (expectedSupplyBalanceInUnderlying * 2) / 100, "not expected balance peer-to-peer" diff --git a/test-foundry/aave-v3/TestSupply.t.sol b/test-foundry/aave-v3/TestSupply.t.sol index 43a686db7..6052c96a4 100644 --- a/test-foundry/aave-v3/TestSupply.t.sol +++ b/test-foundry/aave-v3/TestSupply.t.sol @@ -5,6 +5,8 @@ import "./setup/TestSetup.sol"; import "./helpers/FlashLoan.sol"; contract TestSupply is TestSetup { + using WadRayMath for uint256; + // There are no available borrowers: all of the supplied amount is supplied to the pool and set `onPool`. function testSupply1() public { uint256 amount = 10_000 ether; @@ -13,7 +15,7 @@ contract TestSupply is TestSetup { supplier1.supply(aDai, amount); uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); - uint256 expectedOnPool = underlyingToScaledBalance(amount, normalizedIncome); + uint256 expectedOnPool = amount.rayDiv(normalizedIncome); testEquality(IERC20(aDai).balanceOf(address(morpho)), amount); @@ -41,7 +43,7 @@ contract TestSupply is TestSetup { testEquality(daiBalanceAfter, expectedDaiBalanceAfter); uint256 p2pSupplyIndex = lens.getUpdatedP2PSupplyIndex(aDai); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit(amount, p2pSupplyIndex); + uint256 expectedSupplyBalanceInP2P = amount.rayDiv(p2pSupplyIndex); (uint256 inP2PSupplier, uint256 onPoolSupplier) = morpho.supplyBalanceInOf( aDai, @@ -72,10 +74,10 @@ contract TestSupply is TestSetup { supplier1.supply(aDai, 2 * amount); uint256 p2pSupplyIndex = lens.getUpdatedP2PSupplyIndex(aDai); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit(amount, p2pSupplyIndex); + uint256 expectedSupplyBalanceInP2P = amount.rayDiv(p2pSupplyIndex); uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); - uint256 expectedSupplyBalanceOnPool = underlyingToScaledBalance(amount, normalizedIncome); + uint256 expectedSupplyBalanceOnPool = amount.rayDiv(normalizedIncome); (uint256 inP2PSupplier, uint256 onPoolSupplier) = morpho.supplyBalanceInOf( aDai, @@ -127,14 +129,14 @@ contract TestSupply is TestSetup { for (uint256 i = 0; i < NMAX; i++) { (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - expectedInP2PInUnderlying = p2pUnitToUnderlying(inP2P, p2pSupplyIndex); + expectedInP2PInUnderlying = inP2P.rayMul(p2pSupplyIndex); testEqualityLarge(expectedInP2PInUnderlying, amountPerBorrower, "amount per borrower"); testEqualityLarge(onPool, 0, "on pool per borrower"); } (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(supplier1)); - uint256 expectedInP2P = underlyingToP2PUnit(amount, morpho.p2pBorrowIndex(aDai)); + uint256 expectedInP2P = amount.rayDiv(morpho.p2pBorrowIndex(aDai)); testEquality(inP2P, expectedInP2P); testEquality(onPool, 0); @@ -169,27 +171,23 @@ contract TestSupply is TestSetup { uint256 inP2P; uint256 onPool; - uint256 expectedInP2PInUnderlying; + uint256 inP2PInUnderlying; uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); - for (uint256 i = 0; i < NMAX; i++) { + for (uint256 i; i < NMAX; i++) { (inP2P, onPool) = morpho.borrowBalanceInOf(aDai, address(borrowers[i])); - expectedInP2PInUnderlying = p2pUnitToUnderlying(inP2P, p2pBorrowIndex); + inP2PInUnderlying = inP2P.rayMul(p2pBorrowIndex); - testEqualityLarge( - expectedInP2PInUnderlying, - amountPerBorrower, - "borrower in peer-to-peer" - ); + testEqualityLarge(inP2PInUnderlying, amountPerBorrower, "borrower in peer-to-peer"); testEqualityLarge(onPool, 0); } (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(supplier1)); - uint256 expectedInP2P = underlyingToP2PUnit(amount / 2, morpho.p2pSupplyIndex(aDai)); - uint256 expectedOnPool = underlyingToAdUnit(amount / 2, normalizedIncome); + uint256 expectedInP2P = (amount / 2).rayDiv(morpho.p2pSupplyIndex(aDai)); + uint256 expectedOnPool = (amount / 2).rayDiv(normalizedIncome); testEqualityLarge(inP2P, expectedInP2P, "in peer-to-peer"); testEqualityLarge(onPool, expectedOnPool, "in pool"); @@ -204,7 +202,7 @@ contract TestSupply is TestSetup { supplier1.supply(aDai, amount); uint256 normalizedIncome = pool.getReserveNormalizedIncome(dai); - uint256 expectedOnPool = underlyingToScaledBalance(2 * amount, normalizedIncome); + uint256 expectedOnPool = (2 * amount).rayDiv(normalizedIncome); (, uint256 onPool) = morpho.supplyBalanceInOf(aDai, address(supplier1)); testEqualityLarge(onPool, expectedOnPool); @@ -241,7 +239,7 @@ contract TestSupply is TestSetup { morpho.supply(aDai, address(supplier2), amount); uint256 poolSupplyIndex = pool.getReserveNormalizedIncome(dai); - uint256 expectedOnPool = underlyingToScaledBalance(amount, poolSupplyIndex); + uint256 expectedOnPool = amount.rayDiv(poolSupplyIndex); assertEq(ERC20(aDai).balanceOf(address(morpho)), amount, "balance of aToken"); diff --git a/test-foundry/aave-v3/TestWithdraw.t.sol b/test-foundry/aave-v3/TestWithdraw.t.sol index 150a3a31c..748933059 100644 --- a/test-foundry/aave-v3/TestWithdraw.t.sol +++ b/test-foundry/aave-v3/TestWithdraw.t.sol @@ -31,7 +31,7 @@ contract TestWithdraw is TestSetup { (uint256 inP2P, uint256 onPool) = morpho.supplyBalanceInOf(aUsdc, address(supplier1)); uint256 expectedOnPool = to6Decimals( - underlyingToScaledBalance(2 * amount, pool.getReserveNormalizedIncome(usdc)) + (2 * amount).rayDiv(pool.getReserveNormalizedIncome(usdc)) ); testEquality(inP2P, 0); @@ -55,9 +55,7 @@ contract TestWithdraw is TestSetup { uint256 balanceBefore = supplier1.balanceOf(usdc); (uint256 inP2P, uint256 onPool) = morpho.supplyBalanceInOf(aUsdc, address(supplier1)); - uint256 expectedOnPool = to6Decimals( - underlyingToScaledBalance(amount, pool.getReserveNormalizedIncome(usdc)) - ); + uint256 expectedOnPool = to6Decimals(amount.rayDiv(pool.getReserveNormalizedIncome(usdc))); testEquality(inP2P, 0); testEquality(onPool, expectedOnPool); @@ -96,10 +94,7 @@ contract TestWithdraw is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToScaledBalance( - suppliedAmount / 2, - pool.getReserveNormalizedIncome(dai) - ); + uint256 expectedOnPool = (suppliedAmount / 2).rayDiv(pool.getReserveNormalizedIncome(dai)); testEqualityLarge(onPoolSupplier, expectedOnPool, "supplier on pool 0"); testEquality(onPoolBorrower1, 0, "borrower on pool 0"); @@ -120,7 +115,7 @@ contract TestWithdraw is TestSetup { // Check balances for supplier2 (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier2)); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedInP2P = underlyingToP2PUnit(suppliedAmount / 2, p2pSupplyIndex); + uint256 expectedInP2P = (suppliedAmount / 2).rayDiv(p2pSupplyIndex); testEqualityLarge(onPoolSupplier, expectedOnPool, "supplier on pool 2"); testEqualityLarge(inP2PSupplier, expectedInP2P, "supplier in P2P 2"); @@ -162,10 +157,7 @@ contract TestWithdraw is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToScaledBalance( - suppliedAmount / 2, - pool.getReserveNormalizedIncome(dai) - ); + uint256 expectedOnPool = (suppliedAmount / 2).rayDiv(pool.getReserveNormalizedIncome(dai)); testEqualityLarge(onPoolSupplier, expectedOnPool, "supplier on pool"); testEqualityLarge(onPoolBorrower, 0, "borrower on pool"); @@ -196,10 +188,7 @@ contract TestWithdraw is TestSetup { (inP2PBorrower, onPoolBorrower) = morpho.borrowBalanceInOf(aDai, address(borrower1)); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - borrowedAmount, - morpho.p2pBorrowIndex(aDai) - ); + uint256 expectedBorrowBalanceInP2P = borrowedAmount.rayDiv(morpho.p2pBorrowIndex(aDai)); testEqualityLarge(inP2PBorrower, expectedBorrowBalanceInP2P, "borrower in P2P"); testEqualityLarge(onPoolBorrower, 0, "borrower on pool"); @@ -212,7 +201,7 @@ contract TestWithdraw is TestSetup { if (suppliers[i] == supplier1) continue; (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(suppliers[i])); - uint256 expectedInP2P = p2pUnitToUnderlying(inP2P, p2pSupplyIndex); + uint256 expectedInP2P = inP2P.rayMul(p2pSupplyIndex); testEqualityLarge(expectedInP2P, amountPerSupplier, "new supplier in P2P"); testEqualityLarge(onPool, 0, "new supplier on pool"); @@ -244,10 +233,7 @@ contract TestWithdraw is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToScaledBalance( - suppliedAmount / 2, - pool.getReserveNormalizedIncome(dai) - ); + uint256 expectedOnPool = (suppliedAmount / 2).rayDiv(pool.getReserveNormalizedIncome(dai)); testEqualityLarge(onPoolSupplier, expectedOnPool, "supplier on pool"); testEquality(onPoolBorrower, 0, "borrower on pool"); @@ -260,12 +246,8 @@ contract TestWithdraw is TestSetup { (inP2PBorrower, onPoolBorrower) = morpho.borrowBalanceInOf(aDai, address(borrower1)); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - borrowedAmount / 2, - p2pSupplyIndex - ); - uint256 expectedBorrowBalanceOnPool = underlyingToAdUnit( - borrowedAmount / 2, + uint256 expectedBorrowBalanceInP2P = (borrowedAmount / 2).rayDiv(p2pSupplyIndex); + uint256 expectedBorrowBalanceOnPool = (borrowedAmount / 2).rayDiv( pool.getReserveNormalizedVariableDebt(dai) ); @@ -275,10 +257,7 @@ contract TestWithdraw is TestSetup { // Check balances for supplier (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier1)); - uint256 expectedSupplyBalanceInP2P = underlyingToP2PUnit( - (25 * suppliedAmount) / 100, - p2pSupplyIndex - ); + uint256 expectedSupplyBalanceInP2P = ((25 * suppliedAmount) / 100).rayDiv(p2pSupplyIndex); testEquality(inP2PSupplier, expectedSupplyBalanceInP2P); testEquality(onPoolSupplier, 0); @@ -319,10 +298,7 @@ contract TestWithdraw is TestSetup { address(supplier1) ); - uint256 expectedOnPool = underlyingToScaledBalance( - suppliedAmount / 2, - pool.getReserveNormalizedIncome(dai) - ); + uint256 expectedOnPool = (suppliedAmount / 2).rayDiv(pool.getReserveNormalizedIncome(dai)); testEqualityLarge(onPoolSupplier, expectedOnPool, "supplier on pool"); testEquality(onPoolBorrower, 0, "borrower on pool"); @@ -353,12 +329,8 @@ contract TestWithdraw is TestSetup { (inP2PBorrower, onPoolBorrower) = morpho.borrowBalanceInOf(aDai, address(borrower1)); uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - borrowedAmount / 2, - p2pSupplyIndex - ); - uint256 expectedBorrowBalanceOnPool = underlyingToAdUnit( - borrowedAmount / 2, + uint256 expectedBorrowBalanceInP2P = (borrowedAmount / 2).rayDiv(p2pSupplyIndex); + uint256 expectedBorrowBalanceOnPool = (borrowedAmount / 2).rayDiv( pool.getReserveNormalizedVariableDebt(dai) ); @@ -373,7 +345,7 @@ contract TestWithdraw is TestSetup { if (suppliers[i] == supplier1) continue; (inP2P, onPool) = morpho.supplyBalanceInOf(aDai, address(suppliers[i])); - uint256 expectedInP2P = p2pUnitToUnderlying(inP2P, p2pSupplyIndex); + uint256 expectedInP2P = inP2P.rayMul(p2pSupplyIndex); testEqualityLarge(expectedInP2P, amountPerSupplier); testEqualityLarge(onPool, 0); @@ -419,7 +391,7 @@ contract TestWithdraw is TestSetup { { uint256 p2pSupplyIndex = morpho.p2pSupplyIndex(aDai); - expectedSupplyBalanceInP2P = underlyingToP2PUnit(suppliedAmount, p2pSupplyIndex); + expectedSupplyBalanceInP2P = suppliedAmount.rayDiv(p2pSupplyIndex); // Check balances after match of supplier1 and borrowers (uint256 inP2PSupplier, uint256 onPoolSupplier) = morpho.supplyBalanceInOf( @@ -430,10 +402,7 @@ contract TestWithdraw is TestSetup { testEqualityLarge(inP2PSupplier, expectedSupplyBalanceInP2P); uint256 p2pBorrowIndex = morpho.p2pBorrowIndex(aDai); - uint256 expectedBorrowBalanceInP2P = underlyingToP2PUnit( - borrowedAmount, - p2pBorrowIndex - ); + uint256 expectedBorrowBalanceInP2P = borrowedAmount.rayDiv(p2pBorrowIndex); for (uint256 i = 10; i < 20; i++) { (uint256 inP2PBorrower, uint256 onPoolBorrower) = morpho.borrowBalanceInOf( @@ -455,8 +424,7 @@ contract TestWithdraw is TestSetup { // There should be a delta uint256 expectedBorrowP2PDeltaInUnderlying = 10 * borrowedAmount; - uint256 expectedBorrowP2PDelta = underlyingToAdUnit( - expectedBorrowP2PDeltaInUnderlying, + uint256 expectedBorrowP2PDelta = expectedBorrowP2PDeltaInUnderlying.rayDiv( pool.getReserveNormalizedVariableDebt(dai) ); @@ -472,8 +440,7 @@ contract TestWithdraw is TestSetup { supplier2.supply(aDai, expectedBorrowP2PDeltaInUnderlying / 2); (inP2PSupplier, onPoolSupplier) = morpho.supplyBalanceInOf(aDai, address(supplier2)); - expectedSupplyBalanceInP2P = underlyingToP2PUnit( - expectedBorrowP2PDeltaInUnderlying / 2, + expectedSupplyBalanceInP2P = (expectedBorrowP2PDeltaInUnderlying / 2).rayDiv( p2pSupplyIndex ); @@ -537,7 +504,7 @@ contract TestWithdraw is TestSetup { address(borrowers[i]) ); assertApproxEqAbs( - p2pUnitToUnderlying(inP2PBorrower, newVars.BP2PER), + inP2PBorrower.rayMul(newVars.BP2PER), expectedBorrowBalanceInUnderlying, (expectedBorrowBalanceInUnderlying * 2) / 100, "not expected underlying balance" diff --git a/test-foundry/aave-v3/setup/Utils.sol b/test-foundry/aave-v3/setup/Utils.sol index 3f074e4ae..a9cd77710 100644 --- a/test-foundry/aave-v3/setup/Utils.sol +++ b/test-foundry/aave-v3/setup/Utils.sol @@ -24,62 +24,6 @@ contract Utils is Test { return value / 1e10; } - function underlyingToScaledBalance(uint256 _scaledBalance, uint256 _normalizedIncome) - internal - pure - returns (uint256) - { - return _scaledBalance.rayDiv(_normalizedIncome); - } - - function scaledBalanceToUnderlying(uint256 _scaledBalance, uint256 _normalizedIncome) - internal - pure - returns (uint256) - { - return _scaledBalance.rayMul(_normalizedIncome); - } - - function underlyingToAdUnit(uint256 _underlyingAmount, uint256 _normalizedVariableDebt) - internal - pure - returns (uint256) - { - return _underlyingAmount.rayDiv(_normalizedVariableDebt); - } - - function aDUnitToUnderlying(uint256 _aDUnitAmount, uint256 _normalizedVariableDebt) - internal - pure - returns (uint256) - { - return _aDUnitAmount.rayMul(_normalizedVariableDebt); - } - - function underlyingToP2PUnit(uint256 _underlyingAmount, uint256 _p2pExchangeRate) - internal - pure - returns (uint256) - { - return _underlyingAmount.rayDiv(_p2pExchangeRate); - } - - function p2pUnitToUnderlying(uint256 _p2pUnitAmount, uint256 _p2pExchangeRate) - internal - pure - returns (uint256) - { - return _p2pUnitAmount.rayMul(_p2pExchangeRate); - } - - function getAbsDiff(uint256 a, uint256 b) internal pure returns (uint256) { - if (a > b) { - return a - b; - } - - return b - a; - } - function testEquality(uint256 _firstValue, uint256 _secondValue) internal { assertApproxEqAbs(_firstValue, _secondValue, 20); } diff --git a/test-foundry/fuzzing/aave-v2/TestSupplyFuzzing.t.sol b/test-foundry/fuzzing/aave-v2/TestSupplyFuzzing.t.sol index 9828d6bd6..c22a54f32 100644 --- a/test-foundry/fuzzing/aave-v2/TestSupplyFuzzing.t.sol +++ b/test-foundry/fuzzing/aave-v2/TestSupplyFuzzing.t.sol @@ -22,7 +22,7 @@ contract TestSupplyFuzzing is TestSetupFuzzing { supplier1.supply(asset, amount); uint256 normalizedIncome = lendingPool.getReserveNormalizedIncome(underlying); - uint256 expectedOnPool = underlyingToScaledBalance(amount, normalizedIncome); + uint256 expectedOnPool = amount.rayDiv(normalizedIncome); testEquality(ERC20(asset).balanceOf(address(morpho)), amount, "balance of aToken");