Skip to content

Commit

Permalink
Merge pull request #20 from morpho-org/fix/apr-apy
Browse files Browse the repository at this point in the history
fix(apy): move from apr to apy
  • Loading branch information
tomrpl authored Dec 5, 2023
2 parents e2ed64d + 3d943fe commit cbf18b2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
20 changes: 10 additions & 10 deletions src/blue/BlueSnippets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ 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)
{
(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);
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 12 additions & 12 deletions test/forge/blue/TestBlueSnippets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,32 @@ 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);
vm.assume(market.fee < 1 ether);
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);
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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");
}
}

Expand Down

0 comments on commit cbf18b2

Please sign in to comment.