From eda1548b0304fe5c139a39e733c54447ed97f859 Mon Sep 17 00:00:00 2001 From: brightiron Date: Fri, 13 Sep 2024 10:38:54 -0500 Subject: [PATCH] add dai balance check --- .../Cooler/positions/ConsolidateLoan.tsx | 97 ++++++++++++------- .../Lending/Cooler/positions/Positions.tsx | 1 + 2 files changed, 63 insertions(+), 35 deletions(-) diff --git a/src/views/Lending/Cooler/positions/ConsolidateLoan.tsx b/src/views/Lending/Cooler/positions/ConsolidateLoan.tsx index 5b19c03c0..146174c1c 100644 --- a/src/views/Lending/Cooler/positions/ConsolidateLoan.tsx +++ b/src/views/Lending/Cooler/positions/ConsolidateLoan.tsx @@ -2,12 +2,14 @@ import { Box, SvgIcon, Typography } from "@mui/material"; import { InfoNotification, Modal, PrimaryButton } from "@olympusdao/component-library"; import { BigNumber, ethers } from "ethers"; import { formatEther } from "ethers/lib/utils.js"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import lendAndBorrowIcon from "src/assets/icons/lendAndBorrow.svg?react"; import { TokenAllowanceGuard } from "src/components/TokenAllowanceGuard/TokenAllowanceGuard"; import { COOLER_CONSOLIDATION_ADDRESSES, DAI_ADDRESSES, GOHM_ADDRESSES } from "src/constants/addresses"; import { formatNumber } from "src/helpers"; import { DecimalBigNumber } from "src/helpers/DecimalBigNumber/DecimalBigNumber"; +import { useBalance } from "src/hooks/useBalance"; +import { useTestableNetworks } from "src/hooks/useTestableNetworks"; import { useConsolidateCooler } from "src/views/Lending/Cooler/hooks/useConsolidateCooler"; import { useGetCoolerLoans } from "src/views/Lending/Cooler/hooks/useGetCoolerLoans"; @@ -16,13 +18,16 @@ export const ConsolidateLoans = ({ clearingHouseAddress, loans, duration, + debtAddress, }: { coolerAddress: string; clearingHouseAddress: string; loans: NonNullable["data"]>; duration: string; + debtAddress: string; }) => { const coolerMutation = useConsolidateCooler(); + const networks = useTestableNetworks(); const [open, setOpen] = useState(false); const loanIds = loans.map(loan => loan.loanId); const totals = loans.reduce( @@ -36,7 +41,22 @@ export const ConsolidateLoans = ({ ); const maturityDate = new Date(); maturityDate.setDate(maturityDate.getDate() + Number(duration || 0)); + const { data: daiBalance } = useBalance({ [networks.MAINNET]: debtAddress || "" })[networks.MAINNET]; + const [insufficientCollateral, setInsufficientCollateral] = useState(); + useEffect(() => { + if (!daiBalance) { + setInsufficientCollateral(undefined); + return; + } + if (Number(daiBalance) < parseFloat(formatEther(totals.interest))) { + setInsufficientCollateral(true); + } else { + setInsufficientCollateral(false); + } + }, [daiBalance, totals.interest]); + + console.log("consolidate loans"); return ( <> setOpen(!open)}>Consolidate Loans @@ -54,7 +74,8 @@ export const ConsolidateLoans = ({ <> All existing open loans for this Cooler and Clearinghouse will be repaid and consolidated into a new loan - with a {duration} day duration + with a {duration} day duration. You must hold enough DAI in your wallet to cover the interest owed at + consolidation. - Approve DAI for Spending on the Consolidation Contract} - spendAmount={new DecimalBigNumber(ethers.constants.MaxUint256, 18)} - approvalText="Approve DAI for Spending" - > + {insufficientCollateral ? ( + + Insufficient DAI Balance + + ) : ( Approve gOHM for Spending on the Consolidation Contract} - spendAmount={new DecimalBigNumber(totals.collateral, 18)} - approvalText="Approve gOHM for Spending" + message={<>Approve DAI for Spending on the Consolidation Contract} + spendAmount={new DecimalBigNumber(ethers.constants.MaxUint256, 18)} + approvalText="Approve DAI for Spending" > - { - coolerMutation.mutate( - { - coolerAddress, - clearingHouseAddress, - loanIds, - }, - { - onSuccess: () => { - setOpen(false); - }, - }, - ); - }} - loading={coolerMutation.isLoading} - disabled={coolerMutation.isLoading} - fullWidth + Approve gOHM for Spending on the Consolidation Contract} + spendAmount={new DecimalBigNumber(totals.collateral, 18)} + approvalText="Approve gOHM for Spending" > - Consolidate Loans - + { + coolerMutation.mutate( + { + coolerAddress, + clearingHouseAddress, + loanIds, + }, + { + onSuccess: () => { + setOpen(false); + }, + }, + ); + }} + loading={coolerMutation.isLoading} + disabled={coolerMutation.isLoading} + fullWidth + > + Consolidate Loans + + - + )} diff --git a/src/views/Lending/Cooler/positions/Positions.tsx b/src/views/Lending/Cooler/positions/Positions.tsx index 71cdf0d75..50e1f1b8d 100644 --- a/src/views/Lending/Cooler/positions/Positions.tsx +++ b/src/views/Lending/Cooler/positions/Positions.tsx @@ -248,6 +248,7 @@ export const CoolerPositions = () => { clearingHouseAddress={clearingHouse.clearingHouseAddress} loans={loans} duration={clearingHouse.duration} + debtAddress={clearingHouse.debtAddress} /> )}