Skip to content

Commit

Permalink
Fixed rates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Jan 11, 2023
1 parent 8e5277f commit c0c6a37
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
82 changes: 50 additions & 32 deletions src/compound/lens/RatesLens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -379,51 +379,69 @@ abstract contract RatesLens is UsersLens {
)
{
PoolRatesVars memory ratesVars;
PoolInterestsVars memory interestsVars;

ratesVars.delta = morpho.deltas(_poolToken);
ratesVars.lastPoolIndexes = morpho.lastPoolIndexes(_poolToken);

(
ratesVars.indexes.poolSupplyIndex,
ratesVars.indexes.poolBorrowIndex,
interestsVars
) = _accruePoolInterests(ICToken(_poolToken));

interestsVars.cash =
interestsVars.cash +
_suppliedOnPool +
_repaidOnPool -
_borrowedFromPool -
_withdrawnFromPool;
interestsVars.totalBorrows = interestsVars.totalBorrows + _borrowedFromPool - _repaidOnPool;

IInterestRateModel interestRateModel = ICToken(_poolToken).interestRateModel();
poolSupplyRate = IInterestRateModel(interestRateModel).getSupplyRate(
interestsVars.cash,
interestsVars.totalBorrows,
interestsVars.totalReserves,
interestsVars.reserveFactorMantissa
);
(bool success, bytes memory result) = address(interestRateModel).staticcall(
abi.encodeWithSelector(
IInterestRateModel.getBorrowRate.selector,
bool updated = _suppliedOnPool > 0 ||
_borrowedFromPool > 0 ||
_repaidOnPool > 0 ||
_withdrawnFromPool > 0;
if (updated) {
PoolInterestsVars memory interestsVars;
(
ratesVars.indexes.poolSupplyIndex,
ratesVars.indexes.poolBorrowIndex,
interestsVars
) = _accruePoolInterests(ICToken(_poolToken));

interestsVars.cash =
interestsVars.cash +
_suppliedOnPool +
_repaidOnPool -
_borrowedFromPool -
_withdrawnFromPool;
interestsVars.totalBorrows =
interestsVars.totalBorrows +
_borrowedFromPool -
_repaidOnPool;

IInterestRateModel interestRateModel = ICToken(_poolToken).interestRateModel();

poolSupplyRate = IInterestRateModel(interestRateModel).getSupplyRate(
interestsVars.cash,
interestsVars.totalBorrows,
interestsVars.totalReserves
)
);
if (!success) revert BorrowRateFailed();
if (result.length > 32) (, poolBorrowRate) = abi.decode(result, (uint256, uint256));
else poolBorrowRate = abi.decode(result, (uint256));
interestsVars.totalReserves,
interestsVars.reserveFactorMantissa
);

(bool success, bytes memory result) = address(interestRateModel).staticcall(
abi.encodeWithSelector(
IInterestRateModel.getBorrowRate.selector,
interestsVars.cash,
interestsVars.totalBorrows,
interestsVars.totalReserves
)
);
if (!success) revert BorrowRateFailed();

if (result.length > 32) (, poolBorrowRate) = abi.decode(result, (uint256, uint256));
else poolBorrowRate = abi.decode(result, (uint256));
} else {
ratesVars.indexes.poolSupplyIndex = ICToken(_poolToken).exchangeRateStored();
ratesVars.indexes.poolBorrowIndex = ICToken(_poolToken).borrowIndex();

poolSupplyRate = ICToken(_poolToken).supplyRatePerBlock();
poolBorrowRate = ICToken(_poolToken).borrowRatePerBlock();
}

(
ratesVars.indexes.p2pSupplyIndex,
ratesVars.indexes.p2pBorrowIndex,
ratesVars.params
) = _computeP2PIndexes(
_poolToken,
true,
updated,
ratesVars.indexes.poolSupplyIndex,
ratesVars.indexes.poolBorrowIndex,
ratesVars.delta,
Expand Down
22 changes: 11 additions & 11 deletions test/compound/TestRatesLens.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
supplyRatePerBlock,
expectedSupplyRatePerBlock,
1,
1e6,
"unexpected supply rate per block"
);
assertEq(
Expand Down Expand Up @@ -337,7 +337,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
borrowRatePerBlock,
expectedBorrowRatePerBlock,
1,
1e6,
"unexpected borrow rate per block"
);
assertApproxEqAbs(balanceOnPool, amount, 1, "unexpected pool balance");
Expand Down Expand Up @@ -372,7 +372,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
supplyRatePerBlock,
p2pSupplyRatePerBlock,
1,
1e6,
"unexpected supply rate per block"
);
assertEq(balanceOnPool, 0, "unexpected pool balance");
Expand Down Expand Up @@ -406,7 +406,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
borrowRatePerBlock,
p2pBorrowRatePerBlock,
1,
1e6,
"unexpected borrow rate per block"
);
assertApproxEqAbs(balanceOnPool, 0, 1e6, "unexpected pool balance"); // compound rounding error at supply
Expand Down Expand Up @@ -442,7 +442,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
supplyRatePerBlock,
(p2pSupplyRatePerBlock + poolSupplyRatePerBlock) / 2,
1,
1e6,
"unexpected supply rate per block"
);
assertEq(balanceOnPool, expectedBalanceOnPool, "unexpected pool balance");
Expand Down Expand Up @@ -518,7 +518,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
supplyRatePerBlock,
expectedSupplyRatePerBlock,
1,
1e6,
"unexpected supply rate per block"
);
assertEq(
Expand Down Expand Up @@ -556,7 +556,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
borrowRatePerBlock,
expectedBorrowRatePerBlock,
1,
1e6,
"unexpected borrow rate per block"
);
assertApproxEqAbs(balanceOnPool, amount, 1, "unexpected pool balance");
Expand Down Expand Up @@ -590,7 +590,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
supplyRatePerBlock,
p2pSupplyRatePerBlock,
1,
1e6,
"unexpected supply rate per block"
);
assertEq(balanceOnPool, 0, "unexpected pool balance");
Expand Down Expand Up @@ -624,7 +624,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
borrowRatePerBlock,
p2pBorrowRatePerBlock,
1,
1e6,
"unexpected borrow rate per block"
);
assertApproxEqAbs(balanceOnPool, 0, 1e9, "unexpected pool balance"); // compound rounding errors
Expand Down Expand Up @@ -664,7 +664,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
supplyRatePerBlock,
p2pSupplyRatePerBlock,
1,
1e6,
"unexpected supply rate per block"
);
assertEq(balanceOnPool, 0, "unexpected pool balance");
Expand Down Expand Up @@ -705,7 +705,7 @@ contract TestRatesLens is TestSetup {
assertApproxEqAbs(
borrowRatePerBlock,
p2pBorrowRatePerBlock,
1,
1e6,
"unexpected borrow rate per block"
);
assertEq(balanceOnPool, 0, "unexpected pool balance");
Expand Down
4 changes: 2 additions & 2 deletions test/prod/aave-v2/TestUpgradeLens.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ contract TestUpgradeLens is TestSetup {
assertApproxEqAbs(
lens.getCurrentUserSupplyRatePerYear(supplyMarket.poolToken, address(user)),
expectedSupplyRate,
1e21,
1e22,
string.concat(supplyMarket.symbol, " supply rate")
);

Expand All @@ -107,7 +107,7 @@ contract TestUpgradeLens is TestSetup {
assertApproxEqAbs(
lens.getCurrentUserBorrowRatePerYear(borrowMarket.poolToken, address(user)),
expectedBorrowRate,
1e21,
1e22,
string.concat(borrowMarket.symbol, " borrow rate")
);
}
Expand Down

0 comments on commit c0c6a37

Please sign in to comment.