Skip to content

Commit e7c83e7

Browse files
committed
fix: balance and token icons are unavailable when the bridge page is reopened (#31343)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/31343?quickstart=1) ## **Related issues** Fixes: https://consensyssoftware.atlassian.net/browse/MMS-2147 ## **Manual testing steps** 1. Request a Bridge quote with src=native token 2. Close extension popup 3. Open popup again 4. Balance should be repopulated and token icons should be visible ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent ef0bc3b commit e7c83e7

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

ui/hooks/bridge/useLatestBalance.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
formatChainIdToCaip,
77
formatChainIdToHex,
88
ChainId,
9+
isNativeAddress,
910
} from '@metamask/bridge-controller';
10-
import { useSelector } from 'react-redux';
1111
import { getSelectedInternalAccount } from '../../selectors';
1212
import { useAsyncResult } from '../useAsync';
1313
import { Numeric } from '../../../shared/modules/Numeric';
@@ -17,7 +17,7 @@ import {
1717
getMultichainBalances,
1818
getMultichainCurrentChainId,
1919
} from '../../selectors/multichain';
20-
import { getProviderConfig } from '../../../shared/modules/selectors/networks';
20+
import { MULTICHAIN_NATIVE_CURRENCY_TO_CAIP19 } from '../../../shared/constants/multichain/assets';
2121

2222
/**
2323
* Custom hook to fetch and format the latest balance of a given token or native asset.
@@ -32,6 +32,7 @@ const useLatestBalance = (
3232
symbol: string;
3333
string?: string;
3434
chainId?: Hex | CaipChainId | ChainId;
35+
assetId?: string;
3536
} | null,
3637
) => {
3738
const { address: selectedAddress, id } = useMultichainSelector(
@@ -42,7 +43,6 @@ const useLatestBalance = (
4243
const nonEvmBalancesByAccountId = useMultichainSelector(
4344
getMultichainBalances,
4445
);
45-
const { rpcUrl } = useSelector(getProviderConfig);
4646

4747
const nonEvmBalances = nonEvmBalancesByAccountId?.[id];
4848

@@ -56,8 +56,11 @@ const useLatestBalance = (
5656
// No need to fetch the balance for non-EVM tokens, use the balance provided by the
5757
// multichain balances controller
5858
if (isSolanaChainId(chainId) && token.decimals) {
59+
const caipAssetType = isNativeAddress(token.address)
60+
? MULTICHAIN_NATIVE_CURRENCY_TO_CAIP19.SOL
61+
: token.assetId ?? token.address;
5962
return Numeric.from(
60-
nonEvmBalances?.[token.address]?.amount ?? token.string,
63+
nonEvmBalances?.[caipAssetType]?.amount ?? token?.string,
6164
10,
6265
)
6366
.shiftedBy(-1 * token.decimals)
@@ -66,8 +69,7 @@ const useLatestBalance = (
6669

6770
if (
6871
token.address &&
69-
formatChainIdToCaip(currentChainId) === formatChainIdToCaip(chainId) &&
70-
rpcUrl
72+
formatChainIdToCaip(currentChainId) === formatChainIdToCaip(chainId)
7173
) {
7274
return (
7375
await calcLatestSrcBalance(
@@ -80,7 +82,7 @@ const useLatestBalance = (
8082
}
8183

8284
return undefined;
83-
}, [currentChainId, token, selectedAddress, rpcUrl, nonEvmBalances]);
85+
}, [currentChainId, token, selectedAddress, nonEvmBalances]);
8486

8587
if (token && !token.decimals) {
8688
throw new Error(

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,15 @@ const PrepareBridgePage = () => {
253253
setToToken({
254254
...destAsset,
255255
chainId: destChainId,
256-
image: destAsset.icon ?? '',
256+
image: srcAsset.icon ?? destAsset.iconUrl ?? '',
257257
address: destAsset.address,
258258
}),
259259
);
260260
dispatch(
261261
setFromToken({
262262
...srcAsset,
263263
chainId: srcChainId,
264-
image: srcAsset.icon ?? '',
264+
image: srcAsset.icon || srcAsset.iconUrl || '',
265265
address: srcAsset.address,
266266
}),
267267
);
@@ -324,7 +324,9 @@ const PrepareBridgePage = () => {
324324
// This override allows quotes to be returned when the rpcUrl is a forked network
325325
// Otherwise quotes get filtered out by the bridge-api when the wallet's real
326326
// balance is less than the tenderly balance
327-
insufficientBal: Boolean(providerConfig?.rpcUrl?.includes('localhost')),
327+
insufficientBal: providerConfig?.rpcUrl?.includes('localhost')
328+
? true
329+
: undefined,
328330
slippage,
329331
walletAddress: selectedAccount?.address ?? '',
330332
destWalletAddress: selectedDestinationAccount?.address,

0 commit comments

Comments
 (0)