diff --git a/src/containers/OnBoarding/ModifySafe.tsx b/src/containers/OnBoarding/ModifySafe.tsx index ea0977b7..ef46984f 100644 --- a/src/containers/OnBoarding/ModifySafe.tsx +++ b/src/containers/OnBoarding/ModifySafe.tsx @@ -1,4 +1,4 @@ -import { ethers } from 'ethers' +import { BigNumber, ethers } from 'ethers' import React, { useCallback, useMemo, useState } from 'react' import styled from 'styled-components' import Button from '../../components/Button' @@ -90,20 +90,25 @@ const ModifySafe = ({ onLeftInput(availableEth as string) } } + const onMaxRightInput = () => { if (isDeposit) { onRightInput(availableRai.toString()) } else { const availableRaiBN = ethers.utils.parseEther(availableRai) - const raiBalanceBN = ethers.utils.parseEther( - balances.rai.toString() - ) + const raiBalanceBN = balances.rai + ? ethers.utils.parseEther(balances.rai.toString()) + : BigNumber.from('0') const isMore = raiBalanceBN.gt(availableRaiBN) onRightInput( - isMore ? availableRai.toString() : balances.rai.toString() + isMore + ? availableRai.toString() + : balances.rai + ? balances.rai.toString() + : '0' ) } } diff --git a/src/hooks/useSafe.ts b/src/hooks/useSafe.ts index d3b3633a..f3006cac 100644 --- a/src/hooks/useSafe.ts +++ b/src/hooks/useSafe.ts @@ -9,6 +9,7 @@ import { getLiquidationPrice, getRatePercentage, returnAvaiableDebt, + returnPercentAmount, returnTotalDebt, returnTotalValue, safeIsSafe, @@ -261,6 +262,22 @@ export function useSafeInfo(type: SafeTypes = 'create') { error = error ?? `RAI to repay cannot exceed owed amount` } + if (!rightInputBN.isZero()) { + const repayPercent = returnPercentAmount( + rightInput, + availableRai as string + ) + + if ( + rightInputBN.lt(BigNumber.from(availableRaiBN)) && + repayPercent > 95 + ) { + error = + error ?? + `You can only repay a minimum of ${availableRai} RAI to avoid leaving residual values` + } + } + if (!rightInputBN.isZero() && rightInputBN.gt(raiBalanceBN)) { error = error ?? `balance_issue` }