From e74d4e368c5323538d6b335be5ccb19aae0c2f0f Mon Sep 17 00:00:00 2001 From: brightiron Date: Thu, 7 Dec 2023 10:55:43 -0600 Subject: [PATCH] add debt balance check and insufficient funds message if not enough funds --- .../Lending/Cooler/positions/CreateOrRepayLoan.tsx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/views/Lending/Cooler/positions/CreateOrRepayLoan.tsx b/src/views/Lending/Cooler/positions/CreateOrRepayLoan.tsx index 14b02f08c5..5b870534d3 100644 --- a/src/views/Lending/Cooler/positions/CreateOrRepayLoan.tsx +++ b/src/views/Lending/Cooler/positions/CreateOrRepayLoan.tsx @@ -62,14 +62,18 @@ export const CreateOrRepayLoan = ({ const [paymentAmount, setPaymentAmount] = useState(new DecimalBigNumber("0")); const [collateralAmount, setCollateralAmount] = useState(new DecimalBigNumber("0")); const { data: collateralBalance } = useBalance({ [networks.MAINNET]: collateralAddress || "" })[networks.MAINNET]; + const { data: debtBalance } = useBalance({ [networks.MAINNET]: debtAddress || "" })[networks.MAINNET]; const collateralValue = Number(loanToCollateral) * Number(collateralBalance || 0); - const maxYouCanBorrow = Math.min(Number(capacity), collateralValue); const loanPayable = new DecimalBigNumber( loan?.principal.add(loan?.interestDue || BigNumber.from("0")) || BigNumber.from("0"), 18, ); + + const maxYouCanBorrow = loan + ? Math.min(Number(loanPayable), Number(debtBalance)) + : Math.min(Number(capacity), collateralValue); const interestRepaid = loan?.collateral.isZero() || false; //if collateral minus principal is greater than interest... then calculate on collateral amount. const daiCard = ( @@ -124,7 +128,7 @@ export const CreateOrRepayLoan = ({ Max you Can {loan ? "Repay" : "Borrow"} - {formatNumber(loan ? Number(loanPayable.toString()) : maxYouCanBorrow, 2)} DAI + {formatNumber(maxYouCanBorrow, 2)} DAI @@ -237,9 +241,7 @@ export const CreateOrRepayLoan = ({ ); }} disabled={ - (loan - ? Number(paymentAmount) > Number(loanPayable) - : Number(paymentAmount.toString()) > maxYouCanBorrow) || + Number(paymentAmount.toString()) > maxYouCanBorrow || Number(paymentAmount.toString()) === 0 || createLoan.isLoading || repayLoan.isLoading @@ -250,6 +252,8 @@ export const CreateOrRepayLoan = ({ {loan ? Number(paymentAmount) > Number(loanPayable) ? `Payback Amount exceeds Loan` + : Number(paymentAmount.toString()) > Number(maxYouCanBorrow) + ? `Insufficient Funds for Repayment` : `Repay Loan` : Number(paymentAmount.toString()) > maxYouCanBorrow ? `Amount requested exceeds capacity`