Skip to content

Commit

Permalink
fix: EVM wallets being allowed to stay on unsupported networks (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgeron authored Oct 10, 2024
1 parent 040134d commit 450b612
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/shy-ravens-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-connectors/walletconnect-connector": patch
"@fuels/connectors": patch
---

Fixed EVM wallets being allowed to stay on unsupported networks.
1 change: 1 addition & 0 deletions examples/react-app/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const wagmiConfig = createConfig({
[mainnet.id]: http(),
[sepolia.id]: http(),
},
syncConnectedChain: true,
connectors: [
injected({ shimDisconnect: false }),
walletConnect({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
WINDOW,
} from './constants';
import type { WalletConnectConfig } from './types';
import { subscribeAndEnforceChain } from './utils';
import { createWagmiConfig, createWeb3ModalInstance } from './web3Modal';

export class WalletConnectConnector extends PredicateConnector {
Expand All @@ -72,6 +73,11 @@ export class WalletConnectConnector extends PredicateConnector {
this.storage =
config.storage || new LocalStorage(WINDOW?.localStorage as Storage);
const wagmiConfig = config?.wagmiConfig ?? createWagmiConfig();

if (wagmiConfig._internal.syncConnectedChain !== false) {
subscribeAndEnforceChain(wagmiConfig);
}

this.customPredicate = config.predicateConfig || null;
if (HAS_WINDOW) {
this.configProviders({ ...config, wagmiConfig });
Expand Down
1 change: 1 addition & 0 deletions packages/walletconnect-connector/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './WalletConnectConnector';
export * from './types';
export * from './utils';
1 change: 1 addition & 0 deletions packages/walletconnect-connector/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './subscribeAndEnforceChain';
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
type Config,
type State,
disconnect,
getChains,
switchChain,
} from '@wagmi/core';

function getCurrentChainId(state: Pick<State, 'connections' | 'current'>) {
return state.current
? state.connections.get(state.current)?.chainId
: undefined;
}

export function subscribeAndEnforceChain(config: Config) {
if (!config) throw new Error('config is required');

config.subscribe(
(state) => ({
connections: state.connections,
status: state.status,
current: state.current,
}),
(state, _prev) => {
const chains = getChains(config);
if (state.status !== 'connected' || state.current == null) return;
const connector = state.connections.get(state.current)?.connector;
const currentChain = getCurrentChainId(state);
if (
currentChain != null &&
!chains.some((chain) => chain.id === currentChain)
) {
// Some EVM Wallets (like MetaMask) will auto-reject calls made too quickly.
setTimeout(() => {
switchChain(config, {
chainId: chains[0].id,
connector,
}).catch((error) => {
console.log(error);
disconnect(config);
});
}, 2000);
}
},
{
equalityFn: (a, b) => {
return (
getCurrentChainId(a) === getCurrentChainId(b) && a.status === b.status
);
},
},
);
// wagmiCo
}
1 change: 1 addition & 0 deletions packages/walletconnect-connector/src/web3Modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface CreateWeb3ModalProps {
export const createWagmiConfig = (): Config =>
createConfig({
chains: [sepolia, mainnet],
syncConnectedChain: true,
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
Expand Down

0 comments on commit 450b612

Please sign in to comment.