Skip to content

Commit 043efec

Browse files
Fix unsupported network detection and UI handling
Co-Authored-By: nacho@reown.com <nacho@reown.com>
1 parent 0ddf09a commit 043efec

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

packages/core/src/__tests__/controllers/NetworkController.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,14 @@ describe('NetworkController', () => {
7474
expect(NetworkController.state.approvedCaipNetworkIds).toEqual(undefined);
7575
expect(NetworkController.state.requestedCaipNetworks).toEqual(requestedCaipNetworks);
7676
});
77+
78+
it('should validate if active network is in requested networks', () => {
79+
NetworkController.setRequestedCaipNetworks(requestedCaipNetworks);
80+
81+
NetworkController.setCaipNetwork({ id: 'eip155:1', name: 'Ethereum' });
82+
expect(NetworkController.isActiveNetworkInRequestedNetworks()).toBe(true);
83+
84+
NetworkController.setCaipNetwork({ id: 'eip155:99', name: 'Unknown Network' });
85+
expect(NetworkController.isActiveNetworkInRequestedNetworks()).toBe(false);
86+
});
7787
});

packages/core/src/controllers/NetworkController.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,12 @@ export const NetworkController = {
116116
state.approvedCaipNetworkIds = undefined;
117117
state.supportsAllNetworks = true;
118118
state.smartAccountEnabledNetworks = [];
119+
},
120+
121+
isActiveNetworkInRequestedNetworks() {
122+
if (!state.caipNetwork || !state.requestedCaipNetworks?.length) {
123+
return true; // No active network or no requested networks, so no validation needed
124+
}
125+
return state.requestedCaipNetworks.some(network => network.id === state.caipNetwork?.id);
119126
}
120127
};

packages/scaffold/src/modal/w3m-network-button/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ export function NetworkButton({ disabled, style }: NetworkButtonProps) {
2121
const { caipNetwork } = useSnapshot(NetworkController.state);
2222
const { loading } = useSnapshot(ModalController.state);
2323
const { themeMode, themeVariables } = useSnapshot(ThemeController.state);
24+
25+
const isNetworkSupported = NetworkController.isActiveNetworkInRequestedNetworks();
2426

2527
const onNetworkPress = () => {
26-
ModalController.open({ view: 'Networks' });
28+
if (isConnected && !isNetworkSupported) {
29+
ModalController.open({ view: 'UnsupportedChain' });
30+
} else {
31+
ModalController.open({ view: 'Networks' });
32+
}
2733
EventsController.sendEvent({
2834
type: 'track',
2935
event: 'CLICK_NETWORKS'
@@ -41,7 +47,7 @@ export function NetworkButton({ disabled, style }: NetworkButtonProps) {
4147
loading={loading}
4248
testID="network-button"
4349
>
44-
{caipNetwork?.name ?? (isConnected ? 'Unknown Network' : 'Select Network')}
50+
{caipNetwork?.name ?? (isConnected ? (isNetworkSupported ? 'Unknown Network' : 'Switch Network') : 'Select Network')}
4551
</NetworkButtonUI>
4652
</ThemeProvider>
4753
);

packages/scaffold/src/views/w3m-unsupported-chain-view/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export function UnsupportedChainView() {
4949
ListHeaderComponentStyle={styles.header}
5050
ListHeaderComponent={
5151
<Text variant="small-400" color="fg-200" center>
52-
The swap feature doesn't support your current network. Switch to an available option to
53-
continue.
52+
The current network is not supported by this application.
53+
Please switch to an available option to continue.
5454
</Text>
5555
}
5656
contentContainerStyle={styles.contentContainer}

packages/wagmi/src/client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,15 @@ export class AppKit extends AppKitScaffold {
470470
imageId: PresetsUtil.EIP155NetworkImageIds[id],
471471
imageUrl: this.options?.chainImages?.[id]
472472
});
473+
474+
const internalState = this.wagmiConfig.chains || [];
475+
const isNetworkSupported = internalState.length === 0 ||
476+
internalState.some((chain: Chain) =>
477+
`${ConstantsUtil.EIP155}:${chain.id}` === caipChainId);
478+
if (!isNetworkSupported) {
479+
console.warn(`Network ${caipChainId} is not in the requested networks list`);
480+
}
481+
473482
if (isConnected && address && chainId) {
474483
const caipAddress: CaipAddress = `${ConstantsUtil.EIP155}:${id}:${address}`;
475484
this.setCaipAddress(caipAddress);

0 commit comments

Comments
 (0)