Skip to content

Commit

Permalink
✨ instalments: implement consolidated APR
Browse files Browse the repository at this point in the history
  • Loading branch information
sebipap authored and cruzdanilo committed Apr 17, 2024
1 parent 3f975f6 commit 6ed5338
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
11 changes: 9 additions & 2 deletions components/operations/BorrowAtMaturity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import Installments from './Installments';
const BorrowAtMaturity: FC<PropsWithChildren> = ({ children }) => {
const { t } = useTranslation();
const translateOperation = useTranslateOperation();
const { symbol, errorData, setErrorData, qty, gasCost, tx, installments } = useOperationContext();
const { symbol, errorData, setErrorData, qty, gasCost, tx, installments, installmentsDetails } =
useOperationContext();
const {
isLoading: borrowAtMaturityLoading,
onMax,
Expand Down Expand Up @@ -75,6 +76,12 @@ const BorrowAtMaturity: FC<PropsWithChildren> = ({ children }) => {
}, []);
const loading = installments > 1 ? borrowInInstallmentsLoading : borrowAtMaturityLoading || previewIsLoading;

const apr = useMemo(() => {
if (installments === 1) return toPercentage(Number(fixedRate) / 1e18);
if (!installmentsDetails) return 'N/A';
return toPercentage(Number(installmentsDetails.effectiveRate) / 1e18);
}, [fixedRate, installments, installmentsDetails]);

if (tx) return <ModalGif tx={tx} tryAgain={borrow} />;

return (
Expand Down Expand Up @@ -109,7 +116,7 @@ const BorrowAtMaturity: FC<PropsWithChildren> = ({ children }) => {
<ModalInfoHealthFactor qty={qty} symbol={symbol} operation="borrowAtMaturity" />
</ModalBoxCell>
<ModalBoxCell divisor>
<ModalInfoAPR withIcon apr={toPercentage(Number(fixedRate) / 1e18)} symbol={symbol} />
<ModalInfoAPR withIcon apr={apr} symbol={symbol} />
</ModalBoxCell>
</ModalBoxRow>
</ModalBox>
Expand Down
6 changes: 4 additions & 2 deletions hooks/useBorrowInInstallments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useCallback, useMemo, useState } from 'react';
import { Hex, TransactionReceipt, zeroAddress } from 'viem';
import {
installmentsRouterABI,
useErc20Allowance,
useInstallmentsRouterBorrow,
useInstallmentsRouterBorrowEth,
usePrepareInstallmentsRouterBorrow,
Expand All @@ -19,6 +18,7 @@ import { track } from 'utils/mixpanel';
import waitForTransaction from 'utils/waitForTransaction';
import formatNumber from 'utils/formatNumber';
import WAD from '@exactly/lib/esm/fixed-point-math/WAD';
import { erc20ABI, useContractRead } from 'wagmi';

type Permit = {
value: bigint;
Expand Down Expand Up @@ -90,9 +90,11 @@ export default function useBorrowInInstallments() {
onSettled: handleSettled,
});

const allowance = useErc20Allowance(
const allowance = useContractRead(
walletAddress && installmentsRouter && marketContract
? {
abi: erc20ABI,
functionName: 'allowance',
address: marketContract.address,
args: [walletAddress, installmentsRouter.address],
}
Expand Down
9 changes: 8 additions & 1 deletion hooks/useInstallmentsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ export default function useInstallmentsData({
irmParameters,
timestamp,
] as const;
const { amounts: installmentsPrincipal, installments: installmentsRepayAmount } = split(amount, ...parameters);
const {
amounts: installmentsPrincipal,
installments: installmentsRepayAmount,
effectiveRate,
} = split(amount, ...parameters, {
rateTolerance: 10n ** 15n,
});
const totalPrincipal = installmentsPrincipal.reduce((acc, val) => acc + val, 0n);
const maxRepay = installmentsRepayAmount.reduce((acc, val) => acc + val, 0n);
const averageRepay = maxRepay / installments_;
Expand All @@ -57,6 +63,7 @@ export default function useInstallmentsData({
totalPrincipal,
maxRepay,
averageRepay,
effectiveRate,
};
},
[irmParameters, marketAccount],
Expand Down

0 comments on commit 6ed5338

Please sign in to comment.