Skip to content

Commit

Permalink
fix: wevm#4367
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Nov 6, 2024
1 parent cb7dd2e commit d091cf0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-toys-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/connectors": patch
---

Fixed MetaMask connector not returning provider in some cases.
13 changes: 11 additions & 2 deletions packages/connectors/src/metaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type Connector,
createConnector,
extractRpcUrls,
ProviderNotFoundError,
} from '@wagmi/core'
import type {
Compute,
Expand Down Expand Up @@ -266,8 +267,16 @@ export function metaMask(parameters: MetaMaskParameters = {}) {
: { name: 'wagmi', url: 'https://wagmi.sh' }),
useDeeplink: parameters.useDeeplink ?? true,
})
await sdk.init()
return sdk.getProvider()!
const result = await sdk.init()
// On initial load, sometimes `sdk.getProvider` does not return provider.
// https://github.com/wevm/wagmi/issues/4367
// Use result of `init` call if available.
const provider = (() => {
if (result?.activeProvider) return result.activeProvider
return sdk.getProvider()
})()
if (!provider) throw new ProviderNotFoundError()
return provider
}

if (!provider) {
Expand Down

0 comments on commit d091cf0

Please sign in to comment.