From 3d943fe2ce7764a5075430ffa87f4ee290e982f6 Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Tue, 5 Dec 2023 10:29:49 +0100 Subject: [PATCH] fix(apy): move from apr to apy --- src/blue/BlueSnippets.sol | 20 ++++++++-------- ...phoSnippets.sol => MetaMorphoSnippets.sol} | 6 ++--- test/forge/blue/TestBlueSnippets.sol | 24 +++++++++---------- ...nippets.sol => TestMetaMorphoSnippets.sol} | 17 ++++++------- 4 files changed, 34 insertions(+), 33 deletions(-) rename src/metamorpho/{MetamorphoSnippets.sol => MetaMorphoSnippets.sol} (97%) rename test/forge/metamorpho/{TestMetamorphoSnippets.sol => TestMetaMorphoSnippets.sol} (97%) diff --git a/src/blue/BlueSnippets.sol b/src/blue/BlueSnippets.sol index 59868d7..bc2a03b 100644 --- a/src/blue/BlueSnippets.sol +++ b/src/blue/BlueSnippets.sol @@ -48,12 +48,12 @@ contract BlueSnippets { // weird oracles / markets created /** - * @notice Calculates the supply APR (Annual Percentage Rate) for a given market. + * @notice Calculates the supply APY (Annual Percentage Yield) for a given market. * @param marketParams The parameters of the market. - * @param market The market for which the supply APR is being calculated. - * @return supplyRate The calculated supply APR. + * @param market The market for which the supply APY is being calculated. + * @return supplyRate The calculated supply APY. */ - function supplyAPR(MarketParams memory marketParams, Market memory market) + function supplyAPY(MarketParams memory marketParams, Market memory market) public view returns (uint256 supplyRate) @@ -61,7 +61,7 @@ contract BlueSnippets { (uint256 totalSupplyAssets,, uint256 totalBorrowAssets,) = morpho.expectedMarketBalances(marketParams); // Get the borrow rate - uint256 borrowRate = IIrm(marketParams.irm).borrowRateView(marketParams, market); + uint256 borrowRate = borrowAPY(marketParams, market); // Get the supply rate uint256 utilization = totalBorrowAssets == 0 ? 0 : totalBorrowAssets.wDivUp(totalSupplyAssets); @@ -70,17 +70,17 @@ contract BlueSnippets { } /** - * @notice Calculates the borrow APR (Annual Percentage Rate) for a given market. + * @notice Calculates the borrow APY (Annual Percentage Yield) for a given market. * @param marketParams The parameters of the market. - * @param market The market for which the borrow APR is being calculated. - * @return borrowRate The calculated borrow APR. + * @param market The market for which the borrow APY is being calculated. + * @return borrowRate The calculated borrow APY. */ - function borrowAPR(MarketParams memory marketParams, Market memory market) + function borrowAPY(MarketParams memory marketParams, Market memory market) public view returns (uint256 borrowRate) { - borrowRate = IIrm(marketParams.irm).borrowRateView(marketParams, market); + borrowRate = IIrm(marketParams.irm).borrowRateView(marketParams, market).wTaylorCompounded(1); } /** diff --git a/src/metamorpho/MetamorphoSnippets.sol b/src/metamorpho/MetaMorphoSnippets.sol similarity index 97% rename from src/metamorpho/MetamorphoSnippets.sol rename to src/metamorpho/MetaMorphoSnippets.sol index d2b542f..4f07d87 100644 --- a/src/metamorpho/MetamorphoSnippets.sol +++ b/src/metamorpho/MetaMorphoSnippets.sol @@ -94,7 +94,7 @@ contract MetaMorphoSnippets { } } - /// @notice Returns the current APY of a morpho blue market. + /// @notice Returns the current APY of the vault on a Morpho Blue market. /// @param marketParams The morpho blue market parameters. /// @param market The morpho blue market state. function supplyAPYMarket(MarketParams memory marketParams, Market memory market) @@ -128,9 +128,9 @@ contract MetaMorphoSnippets { MarketParams memory marketParams = morpho.idToMarketParams(idMarket); Market memory market = morpho.market(idMarket); - uint256 currentSupplyAPR = supplyAPYMarket(marketParams, market); + uint256 currentSupplyAPY = supplyAPYMarket(marketParams, market); uint256 vaultAsset = vaultAssetsInMarket(vault, marketParams); - ratio += currentSupplyAPR.wMulDown(vaultAsset); + ratio += currentSupplyAPY.wMulDown(vaultAsset); } avgSupplyRate = ratio.wDivUp(totalAmount); diff --git a/test/forge/blue/TestBlueSnippets.sol b/test/forge/blue/TestBlueSnippets.sol index 4533aab..20c3169 100644 --- a/test/forge/blue/TestBlueSnippets.sol +++ b/test/forge/blue/TestBlueSnippets.sol @@ -99,15 +99,15 @@ contract TestIntegrationSnippets is BaseTest { assertEq(expectedTotalBorrow, morpho.totalBorrowAssets(id)); } - function testBorrowAPR(Market memory market) public { + function testBorrowAPY(Market memory market) public { vm.assume(market.totalBorrowAssets > 0); vm.assume(market.totalSupplyAssets >= market.totalBorrowAssets); - uint256 borrowTrue = irm.borrowRate(marketParams, market); - uint256 borrowToTest = snippets.borrowAPR(marketParams, market); - assertEq(borrowTrue, borrowToTest, "Diff in snippets vs integration borrowAPR test"); + uint256 borrowTrue = irm.borrowRate(marketParams, market).wTaylorCompounded(1); + uint256 borrowToTest = snippets.borrowAPY(marketParams, market); + assertEq(borrowTrue, borrowToTest, "Diff in snippets vs integration borrowAPY test"); } - function testSupplyAPREqual0(Market memory market) public { + function testSupplyAPYEqual0(Market memory market) public { vm.assume(market.totalBorrowAssets == 0); vm.assume(market.totalSupplyAssets > 100000); vm.assume(market.lastUpdate > 0); @@ -115,16 +115,16 @@ contract TestIntegrationSnippets is BaseTest { vm.assume(market.totalSupplyAssets >= market.totalBorrowAssets); (uint256 totalSupplyAssets,, uint256 totalBorrowAssets,) = morpho.expectedMarketBalances(marketParams); - uint256 borrowTrue = irm.borrowRate(marketParams, market); + uint256 borrowTrue = irm.borrowRate(marketParams, market).wTaylorCompounded(1); uint256 utilization = totalBorrowAssets == 0 ? 0 : totalBorrowAssets.wDivUp(totalSupplyAssets); uint256 supplyTrue = borrowTrue.wMulDown(1 ether - market.fee).wMulDown(utilization); - uint256 supplyToTest = snippets.supplyAPR(marketParams, market); - assertEq(supplyTrue, 0, "Diff in snippets vs integration supplyAPR test"); - assertEq(supplyToTest, 0, "Diff in snippets vs integration supplyAPR test"); + uint256 supplyToTest = snippets.supplyAPY(marketParams, market); + assertEq(supplyTrue, 0, "Diff in snippets vs integration supplyAPY test"); + assertEq(supplyToTest, 0, "Diff in snippets vs integration supplyAPY test"); } - function testSupplyAPR(Market memory market) public { + function testSupplyAPY(Market memory market) public { vm.assume(market.totalBorrowAssets > 0); vm.assume(market.fee < 1 ether); vm.assume(market.totalSupplyAssets >= market.totalBorrowAssets); @@ -134,9 +134,9 @@ contract TestIntegrationSnippets is BaseTest { uint256 utilization = totalBorrowAssets == 0 ? 0 : totalBorrowAssets.wDivUp(totalSupplyAssets); uint256 supplyTrue = borrowTrue.wMulDown(1 ether - market.fee).wMulDown(utilization); - uint256 supplyToTest = snippets.supplyAPR(marketParams, market); + uint256 supplyToTest = snippets.supplyAPY(marketParams, market); - assertEq(supplyTrue, supplyToTest, "Diff in snippets vs integration supplyAPR test"); + assertEq(supplyTrue, supplyToTest, "Diff in snippets vs integration supplyAPY test"); } function testHealthfactor(uint256 amountSupplied, uint256 amountBorrowed, uint256 timeElapsed, uint256 fee) diff --git a/test/forge/metamorpho/TestMetamorphoSnippets.sol b/test/forge/metamorpho/TestMetaMorphoSnippets.sol similarity index 97% rename from test/forge/metamorpho/TestMetamorphoSnippets.sol rename to test/forge/metamorpho/TestMetaMorphoSnippets.sol index 079e580..cf3bd00 100644 --- a/test/forge/metamorpho/TestMetamorphoSnippets.sol +++ b/test/forge/metamorpho/TestMetaMorphoSnippets.sol @@ -6,17 +6,18 @@ import "@metamorpho-test/helpers/IntegrationTest.sol"; import {SafeCast} from "@openzeppelin/utils/math/SafeCast.sol"; -contract TestIntegrationSnippets is IntegrationTest { - MetaMorphoSnippets internal snippets; - +contract TestMetaMorphoSnippets is IntegrationTest { using MorphoBalancesLib for IMorpho; using MorphoLib for IMorpho; using MathLib for uint256; using Math for uint256; using MarketParamsLib for MarketParams; + MetaMorphoSnippets internal snippets; + function setUp() public virtual override { super.setUp(); + snippets = new MetaMorphoSnippets(address(morpho)); _setCap(allMarkets[0], CAP); @@ -147,13 +148,13 @@ contract TestIntegrationSnippets is IntegrationTest { uint256 borrowTrue = irm.borrowRateView(marketParams, market); uint256 utilization = totalBorrowAssets == 0 ? 0 : totalBorrowAssets.wDivUp(totalSupplyAssets); - assertEq(utilization, 0, "Diff in snippets vs integration supplyAPR test"); + assertEq(utilization, 0, "Diff in snippets vs integration supplyAPY test"); assertEq( borrowTrue.wMulDown(1 ether - market.fee).wMulDown(utilization), 0, - "Diff in snippets vs integration supplyAPR test" + "Diff in snippets vs integration supplyAPY test" ); - assertEq(snippets.supplyAPYMarket(marketParams, market), 0, "Diff in snippets vs integration supplyAPR test"); + assertEq(snippets.supplyAPYMarket(marketParams, market), 0, "Diff in snippets vs integration supplyAPY test"); } function testSupplyAPYMarket(Market memory market) public { @@ -176,10 +177,10 @@ contract TestIntegrationSnippets is IntegrationTest { // handling in if-else the situation where utilization = 0 otherwise too many rejects if (utilization == 0) { assertEq(supplyTrue, 0, "supply rate == 0"); - assertEq(supplyTrue, supplyToTest, "Diff in snippets vs integration supplyAPR test"); + assertEq(supplyTrue, supplyToTest, "Diff in snippets vs integration supplyAPY test"); } else { assertGt(supplyTrue, 0, "supply rate == 0"); - assertEq(supplyTrue, supplyToTest, "Diff in snippets vs integration supplyAPR test"); + assertEq(supplyTrue, supplyToTest, "Diff in snippets vs integration supplyAPY test"); } }