Skip to content

Commit 9968444

Browse files
committed
fix: use Hex or CaipChainId type for chains
1 parent d3f8456 commit 9968444

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import {
1010
isNonEvmChainId,
1111
isValidQuoteRequest,
1212
BRIDGE_QUOTE_MAX_RETURN_DIFFERENCE_PERCENTAGE,
13-
getNativeAssetForChainId,
1413
isNativeAddress,
1514
UnifiedSwapBridgeEventName,
1615
type BridgeController,
1716
isCrossChain,
17+
formatChainIdToHex,
1818
isBitcoinChainId,
19+
getNativeAssetForChainId,
1920
} from '@metamask/bridge-controller';
20-
import type { Hex } from '@metamask/utils';
21+
import { type CaipChainId, type Hex, parseCaipChainId } from '@metamask/utils';
2122
import {
2223
setFromToken,
2324
setFromTokenInputValue,
@@ -81,7 +82,6 @@ import {
8182
isQuoteExpiredOrInvalid as isQuoteExpiredOrInvalidUtil,
8283
safeAmountForCalc,
8384
} from '../utils/quote';
84-
import { isNetworkAdded } from '../../../ducks/bridge/utils';
8585
import MascotBackgroundAnimation from '../../swaps/mascot-background-animation/mascot-background-animation';
8686
import { Column } from '../layout';
8787
import useRamps from '../../../hooks/ramps/useRamps/useRamps';
@@ -115,6 +115,7 @@ import { useEnableMissingNetwork } from '../hooks/useEnableMissingNetwork';
115115
import { BridgeInputGroup } from './bridge-input-group';
116116
import { PrepareBridgePageFooter } from './prepare-bridge-page-footer';
117117
import { DestinationAccountPickerModal } from './components/destination-account-picker-modal';
118+
import { NetworkConfiguration } from '@metamask/network-controller';
118119

119120
const PrepareBridgePage = ({
120121
onOpenSettings,
@@ -466,12 +467,12 @@ const PrepareBridgePage = ({
466467
return t('swapSelectToken');
467468
};
468469

469-
const getTokenOccurrences = (chainId: Hex | undefined): number => {
470+
const getTokenOccurrences = (chainId: Hex | CaipChainId | undefined): number => {
470471
if (!chainId) {
471472
return MINIMUM_TOKEN_OCCURRENCES;
472473
}
473474
return (
474-
TOKEN_OCCURRENCES_MAP[chainId as ChainId] ?? MINIMUM_TOKEN_OCCURRENCES
475+
TOKEN_OCCURRENCES_MAP[chainId as keyof typeof TOKEN_OCCURRENCES_MAP] ?? MINIMUM_TOKEN_OCCURRENCES
475476
);
476477
};
477478

@@ -500,11 +501,10 @@ const PrepareBridgePage = ({
500501
address: token.address ?? zeroAddress(),
501502
};
502503
dispatch(setFromToken(bridgeToken));
503-
dispatch(setFromTokenInputValue(null));
504504
}}
505505
networkProps={{
506-
network: fromChain,
507-
networks: fromChains,
506+
network: fromChain as NetworkConfiguration,
507+
networks: fromChains as NetworkConfiguration[],
508508
onNetworkChange: (networkConfig) => {
509509
enableMissingNetwork(networkConfig.chainId);
510510
dispatch(
@@ -590,7 +590,14 @@ const PrepareBridgePage = ({
590590
disabled={
591591
isSwitchingTemporarilyDisabled ||
592592
!isValidQuoteRequest(quoteRequest, false) ||
593-
(toChain && !isNetworkAdded(toChain))
593+
// Check if the toChain is an enabled fromChain
594+
(toChain &&
595+
!fromChains.some(
596+
({ chainId: fromChainId }) =>
597+
fromChainId === toChain.chainId ||
598+
formatChainIdToCaip(fromChainId) ===
599+
formatChainIdToCaip(toChain.chainId),
600+
))
594601
}
595602
onClick={() => {
596603
dispatch(setSelectedQuote(null));
@@ -604,10 +611,10 @@ const PrepareBridgePage = ({
604611
{
605612
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
606613
// eslint-disable-next-line @typescript-eslint/naming-convention
607-
token_symbol_source: toToken?.symbol ?? null,
614+
token_symbol_source: toToken.symbol,
608615
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
609616
// eslint-disable-next-line @typescript-eslint/naming-convention
610-
token_symbol_destination: fromToken?.symbol ?? null,
617+
token_symbol_destination: fromToken.symbol,
611618
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
612619
// eslint-disable-next-line @typescript-eslint/naming-convention
613620
token_address_source: toToken?.assetId,
@@ -656,12 +663,10 @@ const PrepareBridgePage = ({
656663
dispatch(setToToken(bridgeToken));
657664
}}
658665
networkProps={{
659-
network: toChain,
660-
networks: toChains,
666+
network: toChain as NetworkConfiguration,
667+
networks: toChains as NetworkConfiguration[],
661668
onNetworkChange: (networkConfig) => {
662-
if (isNetworkAdded(networkConfig)) {
663-
enableMissingNetwork(networkConfig.chainId);
664-
}
669+
enableMissingNetwork(networkConfig.chainId);
665670
dispatch(setToChainId(networkConfig.chainId));
666671
},
667672
header: t('yourNetworks'),

ui/pages/bridge/utils/quote.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
import { formatCurrency } from '../../../helpers/utils/confirm-tx.util';
1414
import { DEFAULT_PRECISION } from '../../../hooks/useCurrencyDisplay';
1515
import { formatAmount } from '../../confirmations/components/simulation-details/formatAmount';
16-
import type { BridgeToken } from '../../../ducks/bridge/types';
16+
import type { BridgeNetwork, BridgeToken } from '../../../ducks/bridge/types';
1717

1818
export const formatTokenAmount = (
1919
locale: string,
@@ -151,8 +151,8 @@ export const isQuoteExpiredOrInvalid = ({
151151
}: {
152152
activeQuote: QuoteResponse | null;
153153
toToken: BridgeToken | null;
154-
toChain?: NetworkConfiguration | AddNetworkFields;
155-
fromChain?: NetworkConfiguration;
154+
toChain?: BridgeNetwork;
155+
fromChain?: BridgeNetwork;
156156
isQuoteExpired: boolean;
157157
insufficientBal?: boolean;
158158
}): boolean => {

0 commit comments

Comments
 (0)