-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: EVM wallets being allowed to stay on unsupported networks (#368)
- Loading branch information
1 parent
040134d
commit 450b612
Showing
7 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './WalletConnectConnector'; | ||
export * from './types'; | ||
export * from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './subscribeAndEnforceChain'; |
54 changes: 54 additions & 0 deletions
54
packages/walletconnect-connector/src/utils/subscribeAndEnforceChain.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters