From 9c99de433e27f9d9464b1a873cc7bf8d9eefa7ea Mon Sep 17 00:00:00 2001 From: Jean-Grimal Date: Mon, 8 Jan 2024 15:19:09 +0100 Subject: [PATCH] fix: handle idle markets APY --- src/blue/BlueSnippets.sol | 6 +++++- src/metamorpho/MetaMorphoSnippets.sol | 5 ++++- test/forge/blue/TestBlueSnippets.sol | 9 +++++++++ test/forge/metamorpho/TestMetaMorphoSnippets.sol | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/blue/BlueSnippets.sol b/src/blue/BlueSnippets.sol index 7ca68ae..114aa1d 100644 --- a/src/blue/BlueSnippets.sol +++ b/src/blue/BlueSnippets.sol @@ -80,7 +80,11 @@ contract BlueSnippets { view returns (uint256 borrowRate) { - borrowRate = IIrm(marketParams.irm).borrowRateView(marketParams, market).wTaylorCompounded(1); + if (marketParams.irm == address(0)) { + borrowRate = 0; + } else { + borrowRate = IIrm(marketParams.irm).borrowRateView(marketParams, market).wTaylorCompounded(1); + } } /** diff --git a/src/metamorpho/MetaMorphoSnippets.sol b/src/metamorpho/MetaMorphoSnippets.sol index 73fbd5a..b16d0e1 100644 --- a/src/metamorpho/MetaMorphoSnippets.sol +++ b/src/metamorpho/MetaMorphoSnippets.sol @@ -105,7 +105,10 @@ contract MetaMorphoSnippets { (uint256 totalSupplyAssets,, uint256 totalBorrowAssets,) = morpho.expectedMarketBalances(marketParams); // Get the borrow rate - uint256 borrowRate = IIrm(marketParams.irm).borrowRateView(marketParams, market); + uint256 borrowRate; + if (marketParams.irm != address(0)) { + borrowRate = IIrm(marketParams.irm).borrowRateView(marketParams, market).wTaylorCompounded(1); + } // Get the supply rate uint256 utilization = totalBorrowAssets == 0 ? 0 : totalBorrowAssets.wDivUp(totalSupplyAssets); diff --git a/test/forge/blue/TestBlueSnippets.sol b/test/forge/blue/TestBlueSnippets.sol index 20c3169..38818fb 100644 --- a/test/forge/blue/TestBlueSnippets.sol +++ b/test/forge/blue/TestBlueSnippets.sol @@ -107,6 +107,15 @@ contract TestIntegrationSnippets is BaseTest { assertEq(borrowTrue, borrowToTest, "Diff in snippets vs integration borrowAPY test"); } + function testBorrowAPYIdleMarket(Market memory market) public { + MarketParams memory idleMarket; + idleMarket.loanToken = address(loanToken); + + uint256 borrowRate = snippets.borrowAPY(idleMarket, market); + + assertEq(borrowRate, 0, "borrow rate"); + } + function testSupplyAPYEqual0(Market memory market) public { vm.assume(market.totalBorrowAssets == 0); vm.assume(market.totalSupplyAssets > 100000); diff --git a/test/forge/metamorpho/TestMetaMorphoSnippets.sol b/test/forge/metamorpho/TestMetaMorphoSnippets.sol index cf3bd00..13e51ae 100644 --- a/test/forge/metamorpho/TestMetaMorphoSnippets.sol +++ b/test/forge/metamorpho/TestMetaMorphoSnippets.sol @@ -157,6 +157,21 @@ contract TestMetaMorphoSnippets is IntegrationTest { assertEq(snippets.supplyAPYMarket(marketParams, market), 0, "Diff in snippets vs integration supplyAPY test"); } + function testSupplyAPYIdleMarket() public { + Market memory market; + MarketParams memory idleMarket; + idleMarket.loanToken = address(loanToken); + + vm.prank(MORPHO_OWNER); + morpho.enableIrm(address(0)); + + morpho.createMarket(idleMarket); + + uint256 supplyAPY = snippets.supplyAPYMarket(idleMarket, market); + + assertEq(supplyAPY, 0, "supply APY"); + } + function testSupplyAPYMarket(Market memory market) public { vm.assume(market.totalBorrowAssets > 0); vm.assume(market.totalBorrowShares > 0);