Skip to content

Commit 78499a8

Browse files
Fix lint issues and improve network validation
Co-Authored-By: nacho@reown.com <nacho@reown.com>
1 parent d76da53 commit 78499a8

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ describe('NetworkController', () => {
7777

7878
it('should validate if active network is in requested networks', () => {
7979
NetworkController.setRequestedCaipNetworks(requestedCaipNetworks);
80-
80+
8181
NetworkController.setCaipNetwork({ id: 'eip155:1', name: 'Ethereum' });
8282
expect(NetworkController.isActiveNetworkInRequestedNetworks()).toBe(true);
83-
83+
8484
NetworkController.setCaipNetwork({ id: 'eip155:99', name: 'Unknown Network' });
8585
expect(NetworkController.isActiveNetworkInRequestedNetworks()).toBe(false);
8686
});

packages/core/src/controllers/NetworkController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export const NetworkController = {
122122
if (!state.caipNetwork || !state.requestedCaipNetworks?.length) {
123123
return true; // No active network or no requested networks, so no validation needed
124124
}
125-
125+
126126
return state.requestedCaipNetworks.some(network => network.id === state.caipNetwork?.id);
127127
}
128128
};

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ export interface NetworkButtonProps {
1717
}
1818

1919
export function NetworkButton({ disabled, style }: NetworkButtonProps) {
20-
const { isConnected } = useSnapshot(AccountController.state);
21-
const { caipNetwork, requestedCaipNetworks } = useSnapshot(NetworkController.state);
2220
const { loading } = useSnapshot(ModalController.state);
2321
const { themeMode, themeVariables } = useSnapshot(ThemeController.state);
24-
25-
const isNetworkSupported = !caipNetwork || !requestedCaipNetworks?.length ||
26-
requestedCaipNetworks.some(network => network.id === caipNetwork.id);
22+
23+
const isNetworkSupported =
24+
!NetworkController.state.caipNetwork ||
25+
!NetworkController.state.requestedCaipNetworks?.length ||
26+
NetworkController.state.requestedCaipNetworks.some(
27+
network => network.id === NetworkController.state.caipNetwork?.id
28+
);
2729

2830
const onNetworkPress = () => {
29-
if (isConnected && !isNetworkSupported) {
31+
if (AccountController.state.isConnected && !isNetworkSupported) {
3032
ModalController.open({ view: 'UnsupportedChain' });
3133
} else {
3234
ModalController.open({ view: 'Networks' });
@@ -40,15 +42,20 @@ export function NetworkButton({ disabled, style }: NetworkButtonProps) {
4042
return (
4143
<ThemeProvider themeMode={themeMode} themeVariables={themeVariables}>
4244
<NetworkButtonUI
43-
imageSrc={AssetUtil.getNetworkImage(caipNetwork)}
45+
imageSrc={AssetUtil.getNetworkImage(NetworkController.state.caipNetwork)}
4446
imageHeaders={ApiController._getApiHeaders()}
4547
disabled={disabled || loading}
4648
style={style}
4749
onPress={onNetworkPress}
4850
loading={loading}
4951
testID="network-button"
5052
>
51-
{caipNetwork?.name ?? (isConnected ? (isNetworkSupported ? 'Unknown Network' : 'Switch Network') : 'Select Network')}
53+
{NetworkController.state.caipNetwork?.name ??
54+
(AccountController.state.isConnected
55+
? isNetworkSupported
56+
? 'Unknown Network'
57+
: 'Switch Network'
58+
: 'Select Network')}
5259
</NetworkButtonUI>
5360
</ThemeProvider>
5461
);

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 current network is not supported by this application.
53-
Please switch to an available option to continue.
52+
The current network is not supported by this application. Please switch to an available
53+
option to continue.
5454
</Text>
5555
}
5656
contentContainerStyle={styles.contentContainer}

packages/wagmi/src/client.ts

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

0 commit comments

Comments
 (0)