@@ -3,9 +3,12 @@ import { Loader } from '@lidofinance/lido-ui';
33import { formatBalance } from 'utils' ;
44import { useFormContext } from 'react-hook-form' ;
55import { useRepayFormData } from 'features/adjustment/repay/repay-form-context' ;
6+ import { useVaultInfo } from 'features/overview/contexts' ;
7+ import { bigIntMin } from 'utils/bigint-math' ;
68
79export 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