Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add error handling for setCorrectChain #28740

Merged
merged 21 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
63cd7bf
chore: Add error handling and unit test for setCorrectChain
gambinish Nov 26, 2024
bd04656
fix: Mock react router correctly in token-buttons-test
gambinish Nov 26, 2024
1995a8d
fix: Expand test suite to include swap button click
gambinish Nov 26, 2024
fd633a4
fix: Lint and update snapshots
gambinish Nov 26, 2024
76de6cc
fix: Lint file import order
gambinish Nov 26, 2024
d920599
Merge branch 'develop' into chore/portfolio-view-swap-check
gambinish Nov 26, 2024
cf02585
chore: incorporate dapp network switching into setCorrectChain
gambinish Nov 26, 2024
d9a858e
Merge branch 'chore/portfolio-view-swap-check' of github.com:MetaMask…
gambinish Nov 26, 2024
e829c99
fix: Tweak asset-page-test state mock
gambinish Nov 26, 2024
d6a3682
fix: Add if condition to ensure that navigation doesnt happen on chai…
gambinish Nov 27, 2024
bc9a520
fix: Active tab no longer needs to be mocked in unit test suite for a…
gambinish Nov 27, 2024
a6e695e
fix: Add unit test coverage for coin-buttons
gambinish Nov 27, 2024
da5872e
fix: Lint
gambinish Nov 27, 2024
53d1026
fix: Throw error in setCurrentChain, catch and stop execution on failure
gambinish Nov 27, 2024
f31edb0
fix: Cleanup
gambinish Nov 27, 2024
1b42ecb
fix: Cleanup
gambinish Nov 27, 2024
ddb158e
fix: Move the network switch action outside of codefence
gambinish Nov 27, 2024
30b5bb9
chore: Revert unecessary snapshot update
gambinish Nov 27, 2024
8d8272d
chore: Remove mocked error
gambinish Nov 27, 2024
954652b
Merge branch 'develop' into chore/portfolio-view-swap-check
gambinish Nov 27, 2024
8a8f65c
Merge branch 'develop' into chore/portfolio-view-swap-check
gambinish Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions ui/components/app/wallet-overview/coin-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ import useBridging from '../../../hooks/bridge/useBridging';
///: END:ONLY_INCLUDE_IF
import { ReceiveModal } from '../../multichain/receive-modal';
import {
setActiveNetwork,
setSwitchedNetworkDetails,
setActiveNetworkWithError,
///: BEGIN:ONLY_INCLUDE_IF(build-flask)
sendMultichainTransaction,
setDefaultHomeActiveTabName,
Expand Down Expand Up @@ -328,13 +328,20 @@ const CoinButtons = ({

const setCorrectChain = useCallback(async () => {
if (currentChainId !== chainId) {
const networkConfigurationId = networks[chainId];
await dispatch(setActiveNetwork(networkConfigurationId));
await dispatch(
setSwitchedNetworkDetails({
networkClientId: networkConfigurationId,
}),
);
try {
const networkConfigurationId = networks[chainId];
await dispatch(setActiveNetworkWithError(networkConfigurationId));
await dispatch(
setSwitchedNetworkDetails({
networkClientId: networkConfigurationId,
}),
);
} catch (err) {
console.error(`Failed to switch chains.
Target chainId: ${chainId}, Current chainId: ${currentChainId}.
${err}`);
throw err;
}
}
}, [currentChainId, chainId, networks, dispatch]);

Expand Down Expand Up @@ -384,7 +391,6 @@ const CoinButtons = ({

const handleSwapOnClick = useCallback(async () => {
await setCorrectChain();

///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
global.platform.openTab({
url: `${mmiPortfolioUrl}/swap`,
Expand Down
25 changes: 16 additions & 9 deletions ui/pages/asset/components/token-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import useBridging from '../../../hooks/bridge/useBridging';

import { INVALID_ASSET_TYPE } from '../../../helpers/constants/error-keys';
import {
setActiveNetwork,
showModal,
setSwitchedNetworkDetails,
setActiveNetworkWithError,
} from '../../../store/actions';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import {
Expand Down Expand Up @@ -123,14 +123,22 @@ const TokenButtons = ({
}, [token.isERC721, token.address, dispatch]);

const setCorrectChain = async () => {
// If we aren't presently on the chain of the asset, change to it
if (currentChainId !== token.chainId) {
const networkConfigurationId = networks[token.chainId];
await dispatch(setActiveNetwork(networkConfigurationId));
await dispatch(
setSwitchedNetworkDetails({
networkClientId: networkConfigurationId,
}),
);
try {
const networkConfigurationId = networks[token.chainId];
await dispatch(setActiveNetworkWithError(networkConfigurationId));
await dispatch(
gambinish marked this conversation as resolved.
Show resolved Hide resolved
setSwitchedNetworkDetails({
networkClientId: networkConfigurationId,
}),
);
} catch (err) {
console.error(`Failed to switch chains.
Target chainId: ${token.chainId}, Current chainId: ${currentChainId}.
${err}`);
throw err;
}
}
};

Expand Down Expand Up @@ -271,7 +279,6 @@ const TokenButtons = ({
}
onClick={async () => {
await setCorrectChain();

///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
global.platform.openTab({
url: `${mmiPortfolioUrl}/swap`,
Expand Down
17 changes: 17 additions & 0 deletions ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2492,6 +2492,23 @@ export function setActiveNetwork(
};
}

export function setActiveNetworkWithError(
networkConfigurationId: string,
): ThunkAction<void, MetaMaskReduxState, unknown, AnyAction> {
return async (dispatch) => {
log.debug(`background.setActiveNetwork: ${networkConfigurationId}`);
try {
await submitRequestToBackground('setActiveNetwork', [
networkConfigurationId,
]);
} catch (error) {
logErrorWithMessage(error);
dispatch(displayWarning('Had a problem changing networks!'));
throw new Error('Had a problem changing networks!');
}
};
}

export async function setActiveNetworkConfigurationId(
networkConfigurationId: string,
): Promise<undefined> {
Expand Down
Loading