Skip to content

Commit

Permalink
Merge pull request #32 from defisaver/feature/dfs-9127-apy-after-valu…
Browse files Browse the repository at this point in the history
…es-for-gho-on-aavev3-arbitrum

fix: fix net apy calculation for gho borrowing
  • Loading branch information
maleksandar authored Nov 26, 2024
2 parents e828099 + fd84b79 commit 9ba0ec2
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/aaveV2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export const getAaveV2AccountData = async (web3: Web3, network: NetworkNumber, a
}
});

const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy(usedAssets, assetsData);
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
payload.netApy = netApy;
payload.incentiveUsd = incentiveUsd;
payload.totalInterestUsd = totalInterestUsd;
Expand Down
2 changes: 1 addition & 1 deletion src/aaveV3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import {
Blockish, EthAddress, NetworkNumber, PositionBalances,
} from '../types/common';
import { calculateNetApy, getStakingApy, STAKING_ASSETS } from '../staking';
import { getStakingApy, STAKING_ASSETS } from '../staking';
import { multicall } from '../multicall';
import { IUiIncentiveDataProviderV3 } from '../types/contracts/generated/AaveUiIncentiveDataProviderV3';
import { getAssetsBalances } from '../assets';
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/aaveHelpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ export const aaveAnyGetAggregatedPositionData = ({
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy(usedAssets, assetsData, isMorphoAave({ selectedMarket }));
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({
usedAssets,
assetsData,
isMorpho: isMorphoAave({ selectedMarket }),
network,
});
payload.netApy = netApy;
payload.incentiveUsd = incentiveUsd;
payload.totalInterestUsd = totalInterestUsd;
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/compoundHelpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const getCompoundV2AggregatedData = ({
}
});

const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy(usedAssets, assetsData);
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
payload.netApy = netApy;
payload.incentiveUsd = incentiveUsd;
payload.totalInterestUsd = totalInterestUsd;
Expand Down Expand Up @@ -143,7 +143,7 @@ export const getCompoundV3AggregatedData = ({
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy(usedAssets, assetsData);
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
payload.netApy = netApy;
payload.incentiveUsd = incentiveUsd;
payload.totalInterestUsd = totalInterestUsd;
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/llamaLendHelpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Dec from 'decimal.js';
import {
LlamaLendAggregatedPositionData, LlamaLendAssetsData, LlamaLendMarketData, LlamaLendUsedAssets,
} from '../../types';
import { MMUsedAssets, NetworkNumber } from '../../types/common';
import { MMAssetsData, MMUsedAssets, NetworkNumber } from '../../types/common';
import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
import { mapRange } from '../../services/utils';
import { calculateNetApy } from '../../staking';
Expand All @@ -22,7 +22,7 @@ export const getLlamaLendAggregatedData = ({
payload.borrowedUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd);
payload.suppliedForYieldUsd = getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ suppliedForYield }: { suppliedForYield?: string }) => suppliedForYield || '0');

const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy(usedAssets, assetsData as any);
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData: assetsData as unknown as MMAssetsData });
payload.netApy = netApy;
payload.incentiveUsd = incentiveUsd;
payload.totalInterestUsd = totalInterestUsd;
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/morphoBlueHelpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assetAmountInWei } from '@defisaver/tokens';
import Web3 from 'web3';
import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
import { calculateNetApy } from '../../staking';
import { MMUsedAssets, NetworkNumber } from '../../types/common';
import { MMAssetsData, MMUsedAssets, NetworkNumber } from '../../types/common';
import {
MorphoBlueAggregatedPositionData, MorphoBlueAssetsData, MorphoBlueMarketData, MorphoBlueMarketInfo,
} from '../../types';
Expand Down Expand Up @@ -37,7 +37,7 @@ export const getMorphoBlueAggregatedPositionData = ({ usedAssets, assetsData, ma
payload.leftToBorrow = new Dec(usedAssets[collateralToken]?.supplied || 0).mul(oracle).mul(lltv).sub(usedAssets[loanToken]?.borrowed || 0)
.toString();

const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy(usedAssets, assetsData as any);
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData: assetsData as unknown as MMAssetsData });
payload.netApy = netApy;
payload.incentiveUsd = incentiveUsd;
payload.totalInterestUsd = totalInterestUsd;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/sparkHelpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const sparkGetAggregatedPositionData = ({
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy(usedAssets, assetsData);
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
payload.netApy = netApy;
payload.incentiveUsd = incentiveUsd;
payload.totalInterestUsd = totalInterestUsd;
Expand Down
2 changes: 1 addition & 1 deletion src/spark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ export const getSparkAccountData = async (web3: Web3, network: NetworkNumber, ad
}
});

const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy(usedAssets, assetsData);
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
payload.netApy = netApy;
payload.incentiveUsd = incentiveUsd;
payload.totalInterestUsd = totalInterestUsd;
Expand Down
6 changes: 4 additions & 2 deletions src/staking/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ export const calculateInterestEarned = (principal: string, interest: string, typ
return (+principal * (((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)) ** (BLOCKS_IN_A_YEAR * interval))) - +principal; // eslint-disable-line
};

export const calculateNetApy = (usedAssets: MMUsedAssets, assetsData: MMAssetsData, isMorpho = false) => {
export const calculateNetApy = ({
usedAssets, assetsData, isMorpho = false, network = 1,
}: { usedAssets: MMUsedAssets, assetsData: MMAssetsData, isMorpho?: boolean, network?: NetworkNumber }) => {
const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
const acc = { ..._acc };
const assetData = assetsData[usedAsset.symbol];
Expand All @@ -176,7 +178,7 @@ export const calculateNetApy = (usedAssets: MMUsedAssets, assetsData: MMAssetsDa
acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
const rate = isMorpho
? usedAsset.borrowRate === '0' ? assetData.borrowRateP2P : usedAsset.borrowRate
: usedAsset.symbol === 'GHO'
: (usedAsset.symbol === 'GHO' && network === NetworkNumber.Eth)
? usedAsset.discountedBorrowRate
: (usedAsset?.interestMode === '1' ? usedAsset.stableBorrowRate : assetData.borrowRate);
const borrowInterest = calculateInterestEarned(amount, rate as string, 'year', true);
Expand Down

0 comments on commit 9ba0ec2

Please sign in to comment.