Skip to content

Commit

Permalink
fix: issue where setNetworkClientIdForDomain was called without che…
Browse files Browse the repository at this point in the history
…cking whether the origin was eligible for setting its own network (#26323)

This PR fixes a issue where `setNetworkClientIdForDomain` was frequently
called with origins that had no account permissions yet (which is the
thresshold we currently set for giving site's their own network)
resulting in [a large number of silent errors in the background that are
clogging up
sentry](https://metamask.sentry.io/issues/5659582204/?environment=production&project=273505&qu%5B%E2%80%A6%5Derrer=issue-stream&sort=freq&statsPeriod=90d&stream_index=1).

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26323?quickstart=1)

Fixes:
https://metamask.sentry.io/issues/5659582204/?environment=production&project=273505&qu%5B%E2%80%A6%5Derrer=issue-stream&sort=freq&statsPeriod=90d&stream_index=1

N/A

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

N/A
N/A

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.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/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

- [ ] 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.
  • Loading branch information
adonesky1 committed Aug 2, 2024
1 parent 82d7193 commit 6720690
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
14 changes: 10 additions & 4 deletions ui/components/multichain/network-list-menu/network-list-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
getShowNetworkBanner,
getOriginOfCurrentTab,
getUseRequestQueue,
getAllDomains,
} from '../../../selectors';
import ToggleButton from '../../ui/toggle-button';
import {
Expand Down Expand Up @@ -76,6 +77,7 @@ export const NetworkListMenu = ({ onClose }) => {

const selectedTabOrigin = useSelector(getOriginOfCurrentTab);
const useRequestQueue = useSelector(getUseRequestQueue);
const domains = useSelector(getAllDomains);

const dispatch = useDispatch();
const history = useHistory();
Expand Down Expand Up @@ -193,10 +195,14 @@ export const NetworkListMenu = ({ onClose }) => {
dispatch(setActiveNetwork(network.id));
}

// If presently on a dapp, communicate a change to
// the dapp via silent switchEthereumChain that the
// network has changed due to user action
if (useRequestQueue && selectedTabOrigin) {
// If presently on and connected to a dapp, communicate a change to
// the dapp via silent switchEthereumChain that the network has
// changed due to user action
if (
useRequestQueue &&
selectedTabOrigin &&
domains[selectedTabOrigin]
) {
setNetworkClientIdForDomain(selectedTabOrigin, network.id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@ import {
CHAIN_IDS,
MAINNET_DISPLAY_NAME,
SEPOLIA_DISPLAY_NAME,
NETWORK_TYPES,
} from '../../../../shared/constants/network';
import { NetworkListMenu } from '.';

const mockSetShowTestNetworks = jest.fn();
const mockSetProviderType = jest.fn();
const mockToggleNetworkMenu = jest.fn();
const mockSetNetworkClientIdForDomain = jest.fn();

jest.mock('../../../store/actions.ts', () => ({
setShowTestNetworks: () => mockSetShowTestNetworks,
setProviderType: () => mockSetProviderType,
toggleNetworkMenu: () => mockToggleNetworkMenu,
setNetworkClientIdForDomain: (network, id) =>
mockSetNetworkClientIdForDomain(network, id),
}));

const MOCK_ORIGIN = 'https://portfolio.metamask.io';

const render = ({
showTestNetworks = false,
currentChainId = '0x5',
providerConfigId = 'chain5',
isUnlocked = true,
origin = 'https://portfolio.metamask.io',
origin = MOCK_ORIGIN,
selectedTabOriginInDomainsState = true,
} = {}) => {
const state = {
metamask: {
Expand All @@ -40,6 +47,11 @@ const render = ({
showTestNetworks,
},
useRequestQueue: true,
domains: {
...(selectedTabOriginInDomainsState
? { [origin]: providerConfigId }
: {}),
},
},
activeTab: {
origin,
Expand All @@ -51,6 +63,10 @@ const render = ({
};

describe('NetworkListMenu', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('displays important controls', () => {
const { getByText, getByPlaceholderText } = render();

Expand Down Expand Up @@ -140,4 +156,32 @@ describe('NetworkListMenu', () => {
document.querySelectorAll('multichain-network-list-item__delete'),
).toHaveLength(0);
});

describe('selectedTabOrigin is connected to wallet', () => {
it('fires setNetworkClientIdForDomain when network item is clicked', () => {
const { getByText } = render();
fireEvent.click(getByText(MAINNET_DISPLAY_NAME));
expect(mockSetNetworkClientIdForDomain).toHaveBeenCalledWith(
MOCK_ORIGIN,
NETWORK_TYPES.MAINNET,
);
});

it('fires setNetworkClientIdForDomain when test network item is clicked', () => {
const { getByText } = render({ showTestNetworks: true });
fireEvent.click(getByText(SEPOLIA_DISPLAY_NAME));
expect(mockSetNetworkClientIdForDomain).toHaveBeenCalledWith(
MOCK_ORIGIN,
NETWORK_TYPES.SEPOLIA,
);
});
});

describe('selectedTabOrigin is not connected to wallet', () => {
it('does not fire setNetworkClientIdForDomain when network item is clicked', () => {
const { getByText } = render({ selectedTabOriginInDomainsState: false });
fireEvent.click(getByText(MAINNET_DISPLAY_NAME));
expect(mockSetNetworkClientIdForDomain).not.toHaveBeenCalled();
});
});
});

0 comments on commit 6720690

Please sign in to comment.