Skip to content

Commit

Permalink
Merge pull request #89 from unstoppabledomains/braden/log-1558-fix-us…
Browse files Browse the repository at this point in the history
…echainid-bug

[LOG-1558] Fix chainId tracking bug
  • Loading branch information
perfect-cents authored Jan 24, 2023
2 parents 3eb3c43 + f65e756 commit 4bb15be
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/web3-react/src/UAuthConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,36 @@ class UAuthConnector extends Connector {
}
if (!this.provider) return cancelActivation()

this.provider.on('connect', ({chainId}): void => {
this.actions.update({chainId: parseChainId(chainId)})
})

this.provider.on('disconnect', (error): void => {
// 1013 indicates that MetaMask is attempting to reestablish the connection
// https://github.com/MetaMask/providers/releases/tag/v8.0.0
if (error.code === 1013) {
console.debug(
'MetaMask logged connection error 1013: "Try again later"',
)
return
}
this.actions.resetState()
this.onError?.(error)
})

this.provider.on('chainChanged', (chainId: string): void => {
this.actions.update({chainId: parseChainId(chainId)})
})

this.provider.on('accountsChanged', (accounts: string[]): void => {
if (accounts.length === 0) {
// handle this edge case by disconnecting
this.actions.resetState()
} else {
this.actions.update({accounts})
}
})

return Promise.all([
this.provider.request({method: 'eth_chainId'}) as Promise<string>,
this.provider.request({method: 'eth_requestAccounts'}) as Promise<
Expand Down

0 comments on commit 4bb15be

Please sign in to comment.