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

✅ Clean tests #0 #1342

Merged
merged 1 commit into from
Oct 7, 2022
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
23 changes: 11 additions & 12 deletions test-foundry/aave-v2/TestBorrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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));

Expand All @@ -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");
}
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -175,16 +174,16 @@ 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);
}

(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");
Expand All @@ -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);
}

Expand Down
24 changes: 9 additions & 15 deletions test-foundry/aave-v2/TestLiquidate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"
Expand All @@ -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"
);
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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");
Expand Down
Loading