Skip to content

Commit

Permalink
fix infinite loop with wc
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettCleary committed Jan 31, 2025
1 parent 3fa9280 commit 1e6b6a4
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/backend/proxy/providerPreload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,30 @@ const listenToRendererCalls = (
onEventsEnabled[topic] = true
}
let prevAccounts: string[] = []
let prevChainId: string = ''
ipcRenderer[fxn]('providerApi' + topic, async (ev: any, ...args: any[]) => {
if (topic === 'accountsChanged' && args.length > 0) {
if (topic === 'accountsChanged' && args.length > 0){
const newAccts = args[0]
/**
* Our current implementation causes an infinite loop with wagmi here https://github.com/wevm/wagmi/blob/651aa72827a79f38a89f5a64209592816c8e6492/packages/connectors/src/metaMask.ts#L428-L452
* since we emit and "accountsChanged" event after a "eth_requestAccounts" request in our provider implementation. This causes onConnect to fire again in the MM connector.
* @TODO fix our provider implementation so "accountsChanged" is only emitted when accounts do change
* @TODO fix our provider implementation so "accountsChanged" is only emitted when accounts do changes
*/
if (JSON.stringify(newAccts) === JSON.stringify(prevAccounts)) {
if (JSON.stringify(newAccts) === JSON.stringify(prevAccounts)){
return
}
prevAccounts = newAccts
cb(newAccts)
return
} else if (topic === 'chainChanged' && args.length > 0) {
const newChainId = args[0]
// WC connection creates another infinite loop with wagmi here so we prevent duplicate event spamming
if (prevChainId === newChainId){
return
}
prevChainId = newChainId
cb(newChainId)
return
}
cb(...args)
})
Expand Down Expand Up @@ -185,7 +195,6 @@ function initProvider() {

// @ts-expect-error TODO fix types in MetaMaskInpageProvider
windowAny.ethereum.on('accountsChanged', (accounts: string[]) => {
console.log('accounts changed', accounts)
// @ts-expect-error TODO fix types in MetaMaskInpageProvider
windowAny.ethereum.selectedAddress = accounts[0]
// @ts-expect-error TODO fix types in MetaMaskInpageProvider
Expand Down

0 comments on commit 1e6b6a4

Please sign in to comment.