1
1
import { type Hex , type CaipChainId , isCaipChainId } from '@metamask/utils' ;
2
- import { BigNumber } from 'bignumber.js ' ;
2
+ import { useMemo } from 'react ' ;
3
3
import { getSelectedInternalAccount } from '../../selectors' ;
4
4
import { calcLatestSrcBalance } from '../../../shared/modules/bridge-utils/balance' ;
5
5
import { useAsyncResult } from '../useAsyncResult' ;
6
+ import { Numeric } from '../../../shared/modules/Numeric' ;
7
+ import { calcTokenAmount } from '../../../shared/lib/transactions-controller-utils' ;
6
8
import { useMultichainSelector } from '../useMultichainSelector' ;
7
9
import {
8
10
getMultichainBalances ,
@@ -36,23 +38,20 @@ const useLatestBalance = (
36
38
) ;
37
39
const nonEvmBalances = nonEvmBalancesByAccountId [ id ] ;
38
40
39
- const { value : balanceAmount } = useAsyncResult <
40
- string | undefined
41
- > ( async ( ) => {
41
+ const value = useAsyncResult < Numeric | undefined > ( async ( ) => {
42
42
if (
43
43
token ?. address &&
44
44
// TODO check whether chainId is EVM when MultichainNetworkController is integrated
45
45
! isCaipChainId ( chainId ) &&
46
46
chainId &&
47
47
currentChainId === chainId
48
48
) {
49
- const balanceValue = await calcLatestSrcBalance (
49
+ return await calcLatestSrcBalance (
50
50
global . ethereumProvider ,
51
51
selectedAddress ,
52
52
token . address ,
53
53
chainId ,
54
54
) ;
55
- return balanceValue ?. shiftedBy ( token . decimals ) . toString ( ) ;
56
55
}
57
56
58
57
// No need to fetch the balance for non-EVM tokens, use the balance provided by the
@@ -62,7 +61,10 @@ const useLatestBalance = (
62
61
chainId === MultichainNetworks . SOLANA &&
63
62
token ?. decimals
64
63
) {
65
- return nonEvmBalances ?. [ token . address ] ?. amount ?? token ?. string ;
64
+ return Numeric . from (
65
+ nonEvmBalances ?. [ token . address ] ?. amount ?? token ?. string ,
66
+ 10 ,
67
+ ) . shiftedBy ( - 1 * token . decimals ) ;
66
68
}
67
69
68
70
return undefined ;
@@ -81,9 +83,13 @@ const useLatestBalance = (
81
83
) ;
82
84
}
83
85
84
- return {
85
- balanceAmount : balanceAmount ? new BigNumber ( balanceAmount ) : undefined ,
86
- } ;
86
+ return useMemo (
87
+ ( ) =>
88
+ value ?. value
89
+ ? calcTokenAmount ( value . value . toString ( ) , token ?. decimals )
90
+ : undefined ,
91
+ [ value . value , token ?. decimals ] ,
92
+ ) ;
87
93
} ;
88
94
89
95
export default useLatestBalance ;
0 commit comments