diff --git a/.changeset/sweet-toys-kick.md b/.changeset/sweet-toys-kick.md new file mode 100644 index 0000000000..ee0282bc4c --- /dev/null +++ b/.changeset/sweet-toys-kick.md @@ -0,0 +1,5 @@ +--- +"@wagmi/connectors": patch +--- + +Fixed MetaMask connector not returning provider in some cases. diff --git a/packages/connectors/src/metaMask.ts b/packages/connectors/src/metaMask.ts index 6aa6f07404..89352039c0 100644 --- a/packages/connectors/src/metaMask.ts +++ b/packages/connectors/src/metaMask.ts @@ -9,6 +9,7 @@ import { type Connector, createConnector, extractRpcUrls, + ProviderNotFoundError, } from '@wagmi/core' import type { Compute, @@ -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) {