Skip to content

Commit

Permalink
Merge branch 'main' of github.com:morpho-labs/morpho-blue into feat/i…
Browse files Browse the repository at this point in the history
…nvariant-tests
  • Loading branch information
Jean-Grimal committed Aug 21, 2023
2 parents 17b8df5 + d814c03 commit d0fd553
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
29 changes: 16 additions & 13 deletions src/libraries/periphery/MorphoBalancesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ library MorphoBalancesLib {
function expectedMarketBalances(IMorpho morpho, MarketParams memory marketParams)
internal
view
returns (uint256 totalSupplyAssets, uint256 toralBorrow, uint256 totalSupplyShares)
returns (
uint256 totalSupplyAssets,
uint256 toralBorrowAssets,
uint256 totalSupplyShares,
uint256 totalBorrowShares
)
{
Id id = marketParams.id();

Expand All @@ -38,18 +43,17 @@ library MorphoBalancesLib {
bytes32[] memory values = morpho.extSloads(slots);
totalSupplyAssets = uint128(uint256(values[0]));
totalSupplyShares = uint256(values[0] >> 128);
toralBorrow = uint128(uint256(values[1]));
toralBorrowAssets = uint128(uint256(values[1]));
totalBorrowShares = uint256(values[1] >> 128);
uint256 lastUpdate = uint128(uint256(values[2]));
uint256 fee = uint256(values[2] >> 128);

uint256 elapsed = block.timestamp - lastUpdate;

if (elapsed == 0) return (totalSupplyAssets, toralBorrow, totalSupplyShares);

if (toralBorrow != 0) {
if (elapsed != 0 && toralBorrowAssets != 0) {
uint256 borrowRate = IIrm(marketParams.irm).borrowRateView(marketParams);
uint256 interest = toralBorrow.wMulDown(borrowRate.wTaylorCompounded(elapsed));
toralBorrow += interest;
uint256 interest = toralBorrowAssets.wMulDown(borrowRate.wTaylorCompounded(elapsed));
toralBorrowAssets += interest;
totalSupplyAssets += interest;

if (fee != 0) {
Expand All @@ -67,23 +71,23 @@ library MorphoBalancesLib {
view
returns (uint256 totalSupplyAssets)
{
(totalSupplyAssets,,) = expectedMarketBalances(morpho, marketParams);
(totalSupplyAssets,,,) = expectedMarketBalances(morpho, marketParams);
}

function expectedTotalBorrow(IMorpho morpho, MarketParams memory marketParams)
internal
view
returns (uint256 totalBorrowAssets)
{
(, totalBorrowAssets,) = expectedMarketBalances(morpho, marketParams);
(, totalBorrowAssets,,) = expectedMarketBalances(morpho, marketParams);
}

function expectedTotalSupplyShares(IMorpho morpho, MarketParams memory marketParams)
internal
view
returns (uint256 totalSupplyShares)
{
(,, totalSupplyShares) = expectedMarketBalances(morpho, marketParams);
(,, totalSupplyShares,) = expectedMarketBalances(morpho, marketParams);
}

/// @dev Warning: It does not work for `feeRecipient` because their supply shares increase is not taken into account.
Expand All @@ -94,7 +98,7 @@ library MorphoBalancesLib {
{
Id id = marketParams.id();
uint256 supplyShares = morpho.supplyShares(id, user);
(uint256 totalSupplyAssets,, uint256 totalSupplyShares) = expectedMarketBalances(morpho, marketParams);
(uint256 totalSupplyAssets,, uint256 totalSupplyShares,) = expectedMarketBalances(morpho, marketParams);

return supplyShares.toAssetsDown(totalSupplyAssets, totalSupplyShares);
}
Expand All @@ -106,8 +110,7 @@ library MorphoBalancesLib {
{
Id id = marketParams.id();
uint256 borrowShares = morpho.borrowShares(id, user);
uint256 totalBorrowShares = morpho.totalBorrowShares(id);
(, uint256 totalBorrowAssets,) = expectedMarketBalances(morpho, marketParams);
(, uint256 totalBorrowAssets,, uint256 totalBorrowShares) = expectedMarketBalances(morpho, marketParams);

return borrowShares.toAssetsUp(totalBorrowAssets, totalBorrowShares);
}
Expand Down
8 changes: 8 additions & 0 deletions test/forge/UtilsLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import "src/libraries/UtilsLib.sol";
contract UtilsLibTest is Test {
using UtilsLib for uint256;

function testExactlyOneZero(uint256 x, uint256 y) public {
assertEq(UtilsLib.exactlyOneZero(x, y), (x > 0 && y == 0) || (x == 0 && y > 0));
}

function testMin(uint256 x, uint256 y) public {
assertEq(UtilsLib.min(x, y), x < y ? x : y);
}

function testToUint128(uint256 x) public {
vm.assume(x <= type(uint128).max);
assertEq(uint256(x.toUint128()), x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {MorphoBalancesLib} from "src/libraries/periphery/MorphoBalancesLib.sol";

import "../BaseTest.sol";

contract MorphoBalanceLibTest is BaseTest {
contract MorphoBalancesLibTest is BaseTest {
using MathLib for uint256;
using MorphoLib for Morpho;
using SharesMathLib for uint256;
Expand All @@ -16,14 +16,19 @@ contract MorphoBalanceLibTest is BaseTest {
{
_generatePendingInterest(amountSupplied, amountBorrowed, timeElapsed, fee);

(uint256 virtualTotalSupply, uint256 virtualTotalBorrow, uint256 virtualTotalSupplyShares) =
morpho.expectedMarketBalances(market);
(
uint256 virtualTotalSupply,
uint256 virtualTotalBorrow,
uint256 virtualTotalSupplyShares,
uint256 virtualTotalBorrowShares
) = morpho.expectedMarketBalances(market);

morpho.accrueInterest(market);

assertEq(virtualTotalSupply, morpho.totalSupplyAssets(id), "total supply");
assertEq(virtualTotalBorrow, morpho.totalBorrowAssets(id), "total borrow");
assertEq(virtualTotalSupplyShares, morpho.totalSupplyShares(id), "total supply shares");
assertEq(virtualTotalBorrowShares, morpho.totalBorrowShares(id), "total borrow shares");
}

function testExpectedTotalSupply(uint256 amountSupplied, uint256 amountBorrowed, uint256 timeElapsed, uint256 fee)
Expand Down

0 comments on commit d0fd553

Please sign in to comment.