Skip to content

Commit

Permalink
only activate as necessary
Browse files Browse the repository at this point in the history
closes #441
closes #398
  • Loading branch information
NoahZinsmeister committed Mar 1, 2022
1 parent 24fccb1 commit 8cd99f6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
17 changes: 11 additions & 6 deletions packages/eip1193/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export class MockEIP1193Provider extends EventEmitter {
public eth_requestAccounts = jest.fn((accounts?: string[]) => accounts)

public request(x: RequestArguments): Promise<unknown> {
// make sure to throw if we're "not connected"
if (!this.chainId) return Promise.reject(new Error())

switch (x.method) {
case 'eth_chainId':
return Promise.resolve(this.eth_chainId(this.chainId))
Expand Down Expand Up @@ -106,12 +109,6 @@ describe('EIP1193', () => {
expect(mockProvider.eth_requestAccounts.mock.calls.length).toBe(0)
})

afterEach(() => {
expect(mockProvider.eth_chainId.mock.calls.length).toBe(1)
expect(mockProvider.eth_accounts.mock.calls.length).toBe(1)
expect(mockProvider.eth_requestAccounts.mock.calls.length).toBe(0)
})

// suppress console.debugs in this block
beforeEach(() => {
jest.spyOn(console, 'debug').mockImplementation(() => {})
Expand All @@ -130,6 +127,10 @@ describe('EIP1193', () => {
activating: false,
error: undefined,
})

expect(mockProvider.eth_chainId.mock.calls.length).toBe(0)
expect(mockProvider.eth_accounts.mock.calls.length).toBe(0)
expect(mockProvider.eth_requestAccounts.mock.calls.length).toBe(0)
})

test('succeeds', async () => {
Expand All @@ -145,6 +146,10 @@ describe('EIP1193', () => {
activating: false,
error: undefined,
})

expect(mockProvider.eth_chainId.mock.calls.length).toBe(1)
expect(mockProvider.eth_accounts.mock.calls.length).toBe(1)
expect(mockProvider.eth_requestAccounts.mock.calls.length).toBe(0)
})
})

Expand Down
9 changes: 7 additions & 2 deletions packages/metamask/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type {
} from '@web3-react/types'
import { Connector } from '@web3-react/types'

type MetaMaskProvider = Provider & { isConnected?: () => boolean }

export class NoMetaMaskError extends Error {
public constructor() {
super('MetaMask not installed')
Expand All @@ -21,6 +23,9 @@ function parseChainId(chainId: string) {
}

export class MetaMask extends Connector {
/** {@inheritdoc Connector.provider} */
public provider: MetaMaskProvider | undefined

private readonly options?: Parameters<typeof detectEthereumProvider>[0]
private eagerConnection?: Promise<void>

Expand All @@ -47,7 +52,7 @@ export class MetaMask extends Connector {
.then((m) => m.default(this.options))
.then((provider) => {
if (provider) {
this.provider = provider as Provider
this.provider = provider as MetaMaskProvider

this.provider.on('connect', ({ chainId }: ProviderConnectInfo): void => {
this.actions.update({ chainId: parseChainId(chainId) })
Expand Down Expand Up @@ -107,7 +112,7 @@ export class MetaMask extends Connector {
* specified parameters first, before being prompted to switch.
*/
public async activate(desiredChainIdOrChainParameters?: number | AddEthereumChainParameter): Promise<void> {
this.actions.startActivation()
if (!this.provider?.isConnected?.()) this.actions.startActivation()

await this.isomorphicInitialize()
if (!this.provider) return this.actions.reportError(new NoMetaMaskError())
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Network extends Connector {
* @param desiredChainId - The desired chain to connect to.
*/
public async activate(desiredChainId = this.defaultChainId): Promise<void> {
this.actions.startActivation()
if (!this.provider) this.actions.startActivation()

this.provider = await this.isomorphicInitialize(desiredChainId)

Expand Down
35 changes: 25 additions & 10 deletions packages/store/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('#createWeb3ReactStoreAndActions', () => {
})

describe('#startActivation', () => {
test('#works', () => {
test('works', () => {
const [store, actions] = createWeb3ReactStoreAndActions()
actions.startActivation()
expect(store.getState()).toEqual({
Expand All @@ -33,6 +33,7 @@ describe('#createWeb3ReactStoreAndActions', () => {
error: undefined,
})
})

test('cancellation works', () => {
const [store, actions] = createWeb3ReactStoreAndActions()
const cancelActivation = actions.startActivation()
Expand Down Expand Up @@ -185,15 +186,29 @@ describe('#createWeb3ReactStoreAndActions', () => {
})
})

test('#reportError', () => {
const [store, actions] = createWeb3ReactStoreAndActions()
const error = new Error()
actions.reportError(error)
expect(store.getState()).toEqual({
chainId: undefined,
accounts: undefined,
activating: false,
error,
describe('#reportError', () => {
test('sets error', () => {
const [store, actions] = createWeb3ReactStoreAndActions()
const error = new Error()
actions.reportError(error)
expect(store.getState()).toEqual({
chainId: undefined,
accounts: undefined,
activating: false,
error,
})
})

test('resets state', () => {
const [store, actions] = createWeb3ReactStoreAndActions()
actions.reportError(new Error())
actions.reportError(undefined)
expect(store.getState()).toEqual({
chainId: undefined,
accounts: undefined,
activating: false,
error: undefined,
})
})
})
})
4 changes: 1 addition & 3 deletions packages/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ export function createWeb3ReactStoreAndActions(allowedChainIds?: number[]): [Web

// return a function that cancels the activation iff nothing else has happened
return () => {
if (nullifier === nullifierCached) {
store.setState({ ...DEFAULT_STATE, activating: false })
}
if (nullifier === nullifierCached) store.setState({ activating: false })
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/url/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Url extends Connector {

/** {@inheritdoc Connector.activate} */
public async activate(): Promise<void> {
this.actions.startActivation()
if (!this.provider) this.actions.startActivation()

await this.isomorphicInitialize()

Expand Down

0 comments on commit 8cd99f6

Please sign in to comment.