Skip to content

Commit

Permalink
feat: add shim for metamask chain change disconnect (#190)
Browse files Browse the repository at this point in the history
* feat: add shim for metamask chain change disconnect

* chore: changeset
  • Loading branch information
tmm authored Feb 20, 2022
1 parent 26c6631 commit 7034bb8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/thin-wolves-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'wagmi-private': patch
'wagmi': patch
'wagmi-testing': patch
---

add shim for metamask chain changed to prevent disconnect
11 changes: 10 additions & 1 deletion packages/private/src/connectors/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ type InjectedConnectorOptions = {
* @see https://github.com/MetaMask/metamask-extension/issues/10353
*/
shimDisconnect?: boolean
/**
* MetaMask 10.9.3 emits disconnect event when chain is changed.
* This flag disables the disconnect event and relies on the accountsChanged event for updating wallet connection.
* This workaround is experimental and might result in stale connections.
* @see https://github.com/MetaMask/metamask-extension/issues/13375#issuecomment-1027663334
*/
shimChainChangedDisconnect?: boolean
}

const shimKey = 'wagmi.shimDisconnect'
Expand Down Expand Up @@ -57,7 +64,8 @@ export class InjectedConnector extends Connector<
if (provider.on) {
provider.on('accountsChanged', this.onAccountsChanged)
provider.on('chainChanged', this.onChainChanged)
provider.on('disconnect', this.onDisconnect)
if (!this.options?.shimChainChangedDisconnect)
provider.on('disconnect', this.onDisconnect)
}

const account = await this.getAccount()
Expand Down Expand Up @@ -169,6 +177,7 @@ export class InjectedConnector extends Connector<
},
],
})
return chain
} catch (addError) {
throw new AddChainError()
}
Expand Down
4 changes: 2 additions & 2 deletions packages/private/src/constants/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const chain: Record<ChainName, Chain> = {
name: 'Rinkeby Arbitrum',
nativeCurrency: {
name: 'Rinkeby ArbEther',
symbol: 'rinkArbETH',
symbol: 'ARETH',
decimals: 18,
},
rpcUrls: ['https://rinkeby.arbitrum.io/rpc'],
Expand Down Expand Up @@ -214,7 +214,7 @@ export const chain: Record<ChainName, Chain> = {
},
hardhat: {
id: 31_337,
name: 'hardhat',
name: 'Hardhat',
rpcUrls: ['http://127.0.0.1:8545'],
},
}
Expand Down

1 comment on commit 7034bb8

@vercel
Copy link

@vercel vercel bot commented on 7034bb8 Feb 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.