Skip to content
This repository has been archived by the owner on Aug 6, 2020. It is now read-only.

Commit

Permalink
Do not throw on useWeb3Injected if no metamask (#24)
Browse files Browse the repository at this point in the history
Given that hooks cannot be used within conditionals, it's not possible to check whether there is an injected provider to conditionally call useWeb3Injected. To work around this, this commit changes its behaviour so it returns undefined instead of throwing if window.ethereum is not present.
  • Loading branch information
spalladino authored and ylv-io committed Jan 28, 2020
1 parent 8833317 commit bc24e3d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/context/providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ describe('providers', (): void => {

delete window.ethereum;
});

it('returns undefined if no injected provider', async (): Promise<void> => {
const injected = providers.tryInjected();
expect(injected).toBeUndefined();
});

it('throws if no injected provider', async (): Promise<void> => {
expect(() => providers.injected()).toThrow('A web3 provider is not attached to a window.');
});

it('create a provider from a connection', async (): Promise<void> => {
const provider = providers.connection(localConnection);

Expand Down
18 changes: 10 additions & 8 deletions src/context/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ export function connection(conn: string): Provider {
return new Web3(conn).currentProvider;
}

export function injected(): Provider {
// Detect whether the current browser is ethereum-compatible,
// and throw an error if it is not
if (window.ethereum === undefined) {
throw new Error('A web3 provider is not attached to a window.');
}

export function tryInjected(): Provider | undefined {
// Detect whether the current browser is ethereum-compatible
if (window.ethereum === undefined) return undefined;
const provider = window.ethereum as ExtendedProvider;

// disable auto refresh if possible
// Disable auto refresh if possible
if (provider.autoRefreshOnNetworkChange === true) {
provider.autoRefreshOnNetworkChange = false;
}

return provider;
}

export function injected(): Provider {
const provider = tryInjected();
if (!provider) throw new Error('A web3 provider is not attached to a window.');
return provider;
}
5 changes: 3 additions & 2 deletions src/react/useWeb3Hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ export function useWeb3Context(provider: Provider, options?: Web3ContextOptions)
return context;
}

export function useWeb3Injected(options?: Web3ContextOptions): Web3Context {
const [provider] = useState((): Provider => providers.injected());
export function useWeb3Injected(options?: Web3ContextOptions): Web3Context | undefined {
const [provider] = useState((): Provider | undefined => providers.tryInjected());
if (!provider) return undefined;
return useWeb3Context(provider, options);
}

Expand Down

0 comments on commit bc24e3d

Please sign in to comment.