1+ import { valueToBigNumber } from '@aave/math-utils' ;
12import { SxProps } from '@mui/material' ;
23import React , { Dispatch , useEffect } from 'react' ;
34
45import { SwapError , SwapState } from '../../types' ;
56import { BalanceLowerThanInput } from './BalanceLowerThanInput' ;
67
78export const hasInsufficientBalance = ( state : SwapState ) => {
8- return Number ( state . debouncedInputAmount ) > Number ( state . sourceToken . balance ) ;
9+ // Determine which token pays for the swap (handles inverted flows like RepayWithCollateral)
10+ const payingToken =
11+ state . sellAmountToken || ( state . isInvertedSwap ? state . destinationToken : state . sourceToken ) ;
12+
13+ // Prefer the computed sell amount if available; otherwise derive from the edited side
14+ const requiredAmount =
15+ state . sellAmountFormatted ||
16+ ( state . side === 'sell' ? state . debouncedInputAmount : state . debouncedOutputAmount ) ;
17+
18+ return valueToBigNumber ( requiredAmount || 0 ) . isGreaterThan (
19+ valueToBigNumber ( payingToken . balance || 0 )
20+ ) ;
921} ;
1022
1123export const InsufficientBalanceGuard = ( {
@@ -20,8 +32,15 @@ export const InsufficientBalanceGuard = ({
2032 isSwapFlowSelected : boolean ;
2133} ) => {
2234 useEffect ( ( ) => {
23- const hasInsufficientBalance =
24- Number ( state . debouncedInputAmount ) > Number ( state . sourceToken . balance ) ;
35+ const payingToken =
36+ state . sellAmountToken || ( state . isInvertedSwap ? state . destinationToken : state . sourceToken ) ;
37+ const requiredAmount =
38+ state . sellAmountFormatted ||
39+ ( state . side === 'sell' ? state . debouncedInputAmount : state . debouncedOutputAmount ) ;
40+
41+ const hasInsufficientBalance = valueToBigNumber ( requiredAmount || 0 ) . isGreaterThan (
42+ valueToBigNumber ( payingToken . balance || 0 )
43+ ) ;
2544
2645 if ( hasInsufficientBalance ) {
2746 const isAlreadyBalanceError =
@@ -46,9 +65,25 @@ export const InsufficientBalanceGuard = ({
4665 setState ( { actionsBlocked : false } ) ;
4766 }
4867 }
49- } , [ state . debouncedInputAmount , state . sourceToken . balance ] ) ;
68+ } , [
69+ state . debouncedInputAmount ,
70+ state . debouncedOutputAmount ,
71+ state . sourceToken . balance ,
72+ state . destinationToken . balance ,
73+ state . sellAmountFormatted ,
74+ state . isInvertedSwap ,
75+ state . side ,
76+ ] ) ;
77+
78+ const payingToken =
79+ state . sellAmountToken || ( state . isInvertedSwap ? state . destinationToken : state . sourceToken ) ;
80+ const requiredAmount =
81+ state . sellAmountFormatted ||
82+ ( state . side === 'sell' ? state . debouncedInputAmount : state . debouncedOutputAmount ) ;
5083
51- if ( Number ( state . debouncedInputAmount ) > Number ( state . sourceToken . balance ) ) {
84+ if (
85+ valueToBigNumber ( requiredAmount || 0 ) . isGreaterThan ( valueToBigNumber ( payingToken . balance || 0 ) )
86+ ) {
5287 return < BalanceLowerThanInput sx = { { mb : ! isSwapFlowSelected ? 0 : 4 , ...sx } } /> ;
5388 }
5489
0 commit comments