Skip to content

Commit a4ca36f

Browse files
committed
chore: show cached balance in useLatestbalance when chainId is not evm
1 parent be76ec8 commit a4ca36f

File tree

6 files changed

+35
-29
lines changed

6 files changed

+35
-29
lines changed

ui/hooks/bridge/useIsTxSubmittable.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,46 @@ import { SWAPS_CHAINID_DEFAULT_TOKEN_MAP } from '../../../shared/constants/swaps
33
import {
44
getBridgeQuotes,
55
getFromAmount,
6-
getFromChain,
76
getFromToken,
87
getToChain,
98
getValidationErrors,
109
getToToken,
1110
} from '../../ducks/bridge/selectors';
11+
import { getMultichainCurrentChainId } from '../../selectors/multichain';
12+
import { useMultichainSelector } from '../useMultichainSelector';
13+
import { useIsMultichainSwap } from '../../pages/bridge/hooks/useIsMultichainSwap';
1214
import useLatestBalance from './useLatestBalance';
1315

1416
export const useIsTxSubmittable = () => {
1517
const fromToken = useSelector(getFromToken);
1618
const toToken = useSelector(getToToken);
17-
const fromChain = useSelector(getFromChain);
19+
const fromChainId = useMultichainSelector(getMultichainCurrentChainId);
1820
const toChain = useSelector(getToChain);
1921
const fromAmount = useSelector(getFromAmount);
2022
const { activeQuote } = useSelector(getBridgeQuotes);
2123

24+
const isSwap = useIsMultichainSwap();
2225
const {
2326
isInsufficientBalance,
2427
isInsufficientGasBalance,
2528
isInsufficientGasForQuote,
2629
} = useSelector(getValidationErrors);
2730

28-
const { balanceAmount } = useLatestBalance(fromToken, fromChain?.chainId);
29-
const { balanceAmount: nativeAssetBalance } = useLatestBalance(
30-
fromChain?.chainId
31+
const balanceAmount = useLatestBalance(fromToken, fromChainId);
32+
const nativeAssetBalance = useLatestBalance(
33+
fromChainId
3134
? SWAPS_CHAINID_DEFAULT_TOKEN_MAP[
32-
fromChain.chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP
35+
fromChainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP
3336
]
3437
: null,
35-
fromChain?.chainId,
38+
fromChainId,
3639
);
3740

3841
return Boolean(
3942
fromToken &&
4043
toToken &&
41-
fromChain &&
42-
toChain &&
44+
fromChainId &&
45+
(isSwap || toChain) &&
4346
fromAmount &&
4447
activeQuote &&
4548
!isInsufficientBalance(balanceAmount) &&

ui/hooks/bridge/useLatestBalance.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { type Hex, type CaipChainId, isCaipChainId } from '@metamask/utils';
2-
import { BigNumber } from 'bignumber.js';
2+
import { useMemo } from 'react';
33
import { getSelectedInternalAccount } from '../../selectors';
44
import { calcLatestSrcBalance } from '../../../shared/modules/bridge-utils/balance';
55
import { useAsyncResult } from '../useAsyncResult';
6+
import { Numeric } from '../../../shared/modules/Numeric';
7+
import { calcTokenAmount } from '../../../shared/lib/transactions-controller-utils';
68
import { useMultichainSelector } from '../useMultichainSelector';
79
import {
810
getMultichainBalances,
@@ -36,23 +38,20 @@ const useLatestBalance = (
3638
);
3739
const nonEvmBalances = nonEvmBalancesByAccountId[id];
3840

39-
const { value: balanceAmount } = useAsyncResult<
40-
string | undefined
41-
>(async () => {
41+
const value = useAsyncResult<Numeric | undefined>(async () => {
4242
if (
4343
token?.address &&
4444
// TODO check whether chainId is EVM when MultichainNetworkController is integrated
4545
!isCaipChainId(chainId) &&
4646
chainId &&
4747
currentChainId === chainId
4848
) {
49-
const balanceValue = await calcLatestSrcBalance(
49+
return await calcLatestSrcBalance(
5050
global.ethereumProvider,
5151
selectedAddress,
5252
token.address,
5353
chainId,
5454
);
55-
return balanceValue?.shiftedBy(token.decimals).toString();
5655
}
5756

5857
// No need to fetch the balance for non-EVM tokens, use the balance provided by the
@@ -62,7 +61,10 @@ const useLatestBalance = (
6261
chainId === MultichainNetworks.SOLANA &&
6362
token?.decimals
6463
) {
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);
6668
}
6769

6870
return undefined;
@@ -81,9 +83,13 @@ const useLatestBalance = (
8183
);
8284
}
8385

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+
);
8793
};
8894

8995
export default useLatestBalance;

ui/hooks/bridge/useQuoteFetchEvents.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ export const useQuoteFetchEvents = () => {
4444
const fromToken = useSelector(getFromToken);
4545
const fromChain = useSelector(getFromChain);
4646

47-
const { balanceAmount } = useLatestBalance(fromToken, fromChain?.chainId);
48-
const { balanceAmount: nativeAssetBalance } = useLatestBalance(
47+
const balanceAmount = useLatestBalance(fromToken, fromChain?.chainId);
48+
const nativeAssetBalance = useLatestBalance(
4949
fromChain?.chainId
5050
? SWAPS_CHAINID_DEFAULT_TOKEN_MAP[
5151
fromChain.chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP

ui/pages/bridge/prepare/bridge-cta-button.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ export const BridgeCTAButton = ({
7373

7474
const wasTxDeclined = useSelector(getWasTxDeclined);
7575

76-
const { balanceAmount } = useLatestBalance(fromToken, fromChain?.chainId);
77-
const { balanceAmount: nativeAssetBalance } = useLatestBalance(
76+
const balanceAmount = useLatestBalance(fromToken, fromChain?.chainId);
77+
const nativeAssetBalance = useLatestBalance(
7878
fromChain?.chainId
7979
? SWAPS_CHAINID_DEFAULT_TOKEN_MAP[
8080
fromChain.chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP

ui/pages/bridge/prepare/bridge-input-group.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const BridgeInputGroup = ({
9090

9191
const currentChainId = useMultichainSelector(getMultichainCurrentChainId);
9292
const selectedChainId = networkProps?.network?.chainId ?? currentChainId;
93-
const { balanceAmount } = useLatestBalance(token, selectedChainId);
93+
const balanceAmount = useLatestBalance(token, selectedChainId);
9494

9595
const [, handleCopy] = useCopyToClipboard(MINUTE) as [
9696
boolean,

ui/pages/bridge/prepare/prepare-bridge-page.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,14 @@ const PrepareBridgePage = () => {
173173
const { quotesRefreshCount } = useSelector(getBridgeQuotes);
174174
const { openBuyCryptoInPdapp } = useRamps();
175175

176-
const { balanceAmount: nativeAssetBalance } = useLatestBalance(
176+
const nativeAssetBalance = useLatestBalance(
177177
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[
178178
fromChain?.chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP
179179
],
180180
fromChain?.chainId,
181181
);
182182

183-
const { balanceAmount: srcTokenBalance } = useLatestBalance(
184-
fromToken,
185-
fromChain?.chainId,
186-
);
183+
const srcTokenBalance = useLatestBalance(fromToken, fromChain?.chainId);
187184

188185
const {
189186
filteredTokenListGenerator: toTokenListGenerator,

0 commit comments

Comments
 (0)