From 6ed5338698a42f71aca01a556084c4f947796b93 Mon Sep 17 00:00:00 2001 From: sebipap Date: Wed, 17 Apr 2024 14:13:34 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20instalments:=20implement=20consolid?= =?UTF-8?q?ated=20APR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/operations/BorrowAtMaturity/index.tsx | 11 +++++++++-- hooks/useBorrowInInstallments.ts | 6 ++++-- hooks/useInstallmentsData.ts | 9 ++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/components/operations/BorrowAtMaturity/index.tsx b/components/operations/BorrowAtMaturity/index.tsx index d31941741..392d7444f 100644 --- a/components/operations/BorrowAtMaturity/index.tsx +++ b/components/operations/BorrowAtMaturity/index.tsx @@ -28,7 +28,8 @@ import Installments from './Installments'; const BorrowAtMaturity: FC = ({ 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, @@ -75,6 +76,12 @@ const BorrowAtMaturity: FC = ({ 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 ; return ( @@ -109,7 +116,7 @@ const BorrowAtMaturity: FC = ({ children }) => { - + diff --git a/hooks/useBorrowInInstallments.ts b/hooks/useBorrowInInstallments.ts index bcd5091ba..2b1e350b5 100644 --- a/hooks/useBorrowInInstallments.ts +++ b/hooks/useBorrowInInstallments.ts @@ -2,7 +2,6 @@ import { useCallback, useMemo, useState } from 'react'; import { Hex, TransactionReceipt, zeroAddress } from 'viem'; import { installmentsRouterABI, - useErc20Allowance, useInstallmentsRouterBorrow, useInstallmentsRouterBorrowEth, usePrepareInstallmentsRouterBorrow, @@ -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; @@ -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], } diff --git a/hooks/useInstallmentsData.ts b/hooks/useInstallmentsData.ts index 43c96606a..6e07316b5 100644 --- a/hooks/useInstallmentsData.ts +++ b/hooks/useInstallmentsData.ts @@ -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_; @@ -57,6 +63,7 @@ export default function useInstallmentsData({ totalPrincipal, maxRepay, averageRepay, + effectiveRate, }; }, [irmParameters, marketAccount],