Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
fix(morpho): Fix regressions on APY calculations (#3053)
Browse files Browse the repository at this point in the history
  • Loading branch information
immasandwich authored Nov 15, 2023
1 parent d79cecf commit 55690dd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import { ZERO_ADDRESS } from '~app-toolkit/constants/address';
import { PositionTemplate } from '~app-toolkit/decorators/position-template.decorator';
import {
MorphoContractPositionDataProps,
MorphoContractPositionDefinition,
MorphoSupplyContractPositionFetcher,
} from '~apps/morpho/common/morpho.supply.contract-position-fetcher';
import { MorphoViemContractFactory } from '~apps/morpho/contracts';
import { isViemMulticallUnderlyingError } from '~multicall/errors';
import { GetDefinitionsParams, GetTokenBalancesParams } from '~position/template/contract-position.template.types';
import {
GetDataPropsParams,
GetDefinitionsParams,
GetTokenBalancesParams,
} from '~position/template/contract-position.template.types';

import { MorphoAaveV2 } from '../contracts/viem';

Expand Down Expand Up @@ -56,7 +61,11 @@ export class EthereumMorphoAaveV2SupplyContractPositionFetcher extends MorphoSup
);
}

async getDataProps({ contractPosition, multicall, definition }) {
async getDataProps({
contractPosition,
multicall,
definition,
}: GetDataPropsParams<MorphoAaveV2, MorphoContractPositionDataProps, MorphoContractPositionDefinition>) {
const lens = this.contractFactory.morphoAaveV2Lens({ address: this.lensAddress, network: this.network });
const lensContract = multicall.wrap(lens);
const marketAddress = definition.marketAddress;
Expand All @@ -71,17 +80,17 @@ export class EthereumMorphoAaveV2SupplyContractPositionFetcher extends MorphoSup
]);

const secondsPerYear = 3600 * 24 * 365;
const supplyRate = supplyRateRaw.avgSupplyRatePerYear;
const borrowRate = borrowRateRaw.avgBorrowRatePerYear;
const supplyRate = BigNumber.from(supplyRateRaw[0]);
const borrowRate = BigNumber.from(borrowRateRaw[0]);
const supplyApy = Math.pow(1 + +formatUnits(supplyRate.div(secondsPerYear), 27), secondsPerYear) - 1;
const borrowApy = Math.pow(1 + +formatUnits(borrowRate.div(secondsPerYear), 27), secondsPerYear) - 1;
const p2pDisabled = marketConfiguration.isP2PDisabled;
const p2pDisabled = marketConfiguration[2];

const underlyingToken = contractPosition.tokens[0];
const supplyRaw = totalMarketSupplyRaw.p2pSupplyAmount.add(totalMarketSupplyRaw.poolSupplyAmount);
const supplyRaw = BigNumber.from(totalMarketSupplyRaw[0]).add(totalMarketSupplyRaw[1]);
const supply = +formatUnits(supplyRaw, underlyingToken.decimals);
const supplyUSD = supply * underlyingToken.price;
const borrowRaw = totalMarketBorrowRaw.p2pBorrowAmount.add(totalMarketBorrowRaw.poolBorrowAmount);
const borrowRaw = BigNumber.from(totalMarketBorrowRaw[0]).add(totalMarketBorrowRaw[1]);
const matchedUSD = +formatUnits(borrowRaw, underlyingToken.decimals) * underlyingToken.price;
const borrow = +formatUnits(borrowRaw, underlyingToken.decimals);
const borrowUSD = borrow * underlyingToken.price;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class EthereumMorphoAaveV3SupplyContractPositionFetcher extends MorphoSup
? BigNumber.from(currentLiquidityRate)
: p2pSupplyRate.mul(supplyInP2P).add(BigNumber.from(currentLiquidityRate).mul(supplyOnPool)).div(totalSupply);
const borrowRate = totalBorrow.isZero()
? currentVariableBorrowRate
? BigNumber.from(currentVariableBorrowRate)
: p2pBorrowRate
.mul(borrowInP2P)
.add(BigNumber.from(currentVariableBorrowRate).mul(borrowOnPool))
Expand Down
4 changes: 2 additions & 2 deletions src/apps/morpho/utils/P2PInterestRates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default class P2PInterestRates {
proportionIdle,
delta,
}: RateParams) {
let p2pSupplyRate;
let p2pSupplyRate: BigNumber;

if (poolSupplyRatePerYear.gt(poolBorrowRatePerYear)) p2pSupplyRate = poolBorrowRatePerYear;
else {
Expand Down Expand Up @@ -173,7 +173,7 @@ export default class P2PInterestRates {
proportionIdle,
delta,
}: RateParams) {
let p2pBorrowRate;
let p2pBorrowRate: BigNumber;

if (poolSupplyRatePerYear.gt(poolBorrowRatePerYear)) p2pBorrowRate = poolBorrowRatePerYear;
else {
Expand Down

0 comments on commit 55690dd

Please sign in to comment.