Skip to content

Commit 0253593

Browse files
authored
Merge pull request #51 from lidofinance/fix/avliable-repay
fix: avaliable to repay
2 parents f92f51e + 6cd1396 commit 0253593

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

features/adjustment/repay/form/balance/balance.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { Loader } from '@lidofinance/lido-ui';
33
import { formatBalance } from 'utils';
44
import { useFormContext } from 'react-hook-form';
55
import { useRepayFormData } from 'features/adjustment/repay/repay-form-context';
6+
import { useVaultInfo } from 'features/overview/contexts';
7+
import { bigIntMin } from 'utils/bigint-math';
68

79
export const Balance = () => {
810
const { watch } = useFormContext();
11+
const { isLoadingVault, error: vaultError, activeVault } = useVaultInfo();
912
const {
1013
stEthBalance,
1114
wstEthBalance,
@@ -15,22 +18,33 @@ export const Balance = () => {
1518
isWstEthError,
1619
} = useRepayFormData();
1720
const token = watch('token');
18-
const isLoading = isStEthLoading || isWstEthLoading;
19-
const isError = isStEthError || isWstEthError;
20-
const balance = token === 'stETH' ? stEthBalance : wstEthBalance;
21+
const isSteth = token === 'stETH';
22+
23+
const isError = (isSteth ? isStEthError : isWstEthError) || !!vaultError;
24+
25+
const isLoading =
26+
(isSteth ? isStEthLoading : isWstEthLoading) || isLoadingVault;
27+
28+
const balance = isSteth ? stEthBalance : wstEthBalance;
29+
30+
const liability = isSteth
31+
? activeVault?.liabilityStETH
32+
: activeVault?.liabilityShares;
33+
34+
const availableToRepay = bigIntMin(liability ?? 0n, balance ?? 0n);
2135

2236
return (
2337
<Wrapper>
2438
<InfoRow>
2539
<span>Available to repay</span>
2640
{isLoading && <Loader size="small" />}
27-
{!isLoading && !isError && balance !== undefined && (
41+
{!isLoading && !isError && (
2842
<AmountInfo>
29-
{formatBalance(balance).trimmed} {token}
43+
{formatBalance(availableToRepay).trimmed} {token}
3044
</AmountInfo>
3145
)}
3246
{isError && !isLoading && (
33-
<AmountInfo>stEth amount is not available</AmountInfo>
47+
<AmountInfo>{token} amount is not available</AmountInfo>
3448
)}
3549
</InfoRow>
3650
</Wrapper>

0 commit comments

Comments
 (0)