Skip to content

Commit 8cd99f6

Browse files
only activate as necessary
closes Uniswap#441 closes Uniswap#398
1 parent 24fccb1 commit 8cd99f6

File tree

6 files changed

+46
-23
lines changed

6 files changed

+46
-23
lines changed

packages/eip1193/src/index.spec.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export class MockEIP1193Provider extends EventEmitter {
2626
public eth_requestAccounts = jest.fn((accounts?: string[]) => accounts)
2727

2828
public request(x: RequestArguments): Promise<unknown> {
29+
// make sure to throw if we're "not connected"
30+
if (!this.chainId) return Promise.reject(new Error())
31+
2932
switch (x.method) {
3033
case 'eth_chainId':
3134
return Promise.resolve(this.eth_chainId(this.chainId))
@@ -106,12 +109,6 @@ describe('EIP1193', () => {
106109
expect(mockProvider.eth_requestAccounts.mock.calls.length).toBe(0)
107110
})
108111

109-
afterEach(() => {
110-
expect(mockProvider.eth_chainId.mock.calls.length).toBe(1)
111-
expect(mockProvider.eth_accounts.mock.calls.length).toBe(1)
112-
expect(mockProvider.eth_requestAccounts.mock.calls.length).toBe(0)
113-
})
114-
115112
// suppress console.debugs in this block
116113
beforeEach(() => {
117114
jest.spyOn(console, 'debug').mockImplementation(() => {})
@@ -130,6 +127,10 @@ describe('EIP1193', () => {
130127
activating: false,
131128
error: undefined,
132129
})
130+
131+
expect(mockProvider.eth_chainId.mock.calls.length).toBe(0)
132+
expect(mockProvider.eth_accounts.mock.calls.length).toBe(0)
133+
expect(mockProvider.eth_requestAccounts.mock.calls.length).toBe(0)
133134
})
134135

135136
test('succeeds', async () => {
@@ -145,6 +146,10 @@ describe('EIP1193', () => {
145146
activating: false,
146147
error: undefined,
147148
})
149+
150+
expect(mockProvider.eth_chainId.mock.calls.length).toBe(1)
151+
expect(mockProvider.eth_accounts.mock.calls.length).toBe(1)
152+
expect(mockProvider.eth_requestAccounts.mock.calls.length).toBe(0)
148153
})
149154
})
150155

packages/metamask/src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type {
88
} from '@web3-react/types'
99
import { Connector } from '@web3-react/types'
1010

11+
type MetaMaskProvider = Provider & { isConnected?: () => boolean }
12+
1113
export class NoMetaMaskError extends Error {
1214
public constructor() {
1315
super('MetaMask not installed')
@@ -21,6 +23,9 @@ function parseChainId(chainId: string) {
2123
}
2224

2325
export class MetaMask extends Connector {
26+
/** {@inheritdoc Connector.provider} */
27+
public provider: MetaMaskProvider | undefined
28+
2429
private readonly options?: Parameters<typeof detectEthereumProvider>[0]
2530
private eagerConnection?: Promise<void>
2631

@@ -47,7 +52,7 @@ export class MetaMask extends Connector {
4752
.then((m) => m.default(this.options))
4853
.then((provider) => {
4954
if (provider) {
50-
this.provider = provider as Provider
55+
this.provider = provider as MetaMaskProvider
5156

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

112117
await this.isomorphicInitialize()
113118
if (!this.provider) return this.actions.reportError(new NoMetaMaskError())

packages/network/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class Network extends Connector {
6767
* @param desiredChainId - The desired chain to connect to.
6868
*/
6969
public async activate(desiredChainId = this.defaultChainId): Promise<void> {
70-
this.actions.startActivation()
70+
if (!this.provider) this.actions.startActivation()
7171

7272
this.provider = await this.isomorphicInitialize(desiredChainId)
7373

packages/store/src/index.spec.ts

+25-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('#createWeb3ReactStoreAndActions', () => {
2323
})
2424

2525
describe('#startActivation', () => {
26-
test('#works', () => {
26+
test('works', () => {
2727
const [store, actions] = createWeb3ReactStoreAndActions()
2828
actions.startActivation()
2929
expect(store.getState()).toEqual({
@@ -33,6 +33,7 @@ describe('#createWeb3ReactStoreAndActions', () => {
3333
error: undefined,
3434
})
3535
})
36+
3637
test('cancellation works', () => {
3738
const [store, actions] = createWeb3ReactStoreAndActions()
3839
const cancelActivation = actions.startActivation()
@@ -185,15 +186,29 @@ describe('#createWeb3ReactStoreAndActions', () => {
185186
})
186187
})
187188

188-
test('#reportError', () => {
189-
const [store, actions] = createWeb3ReactStoreAndActions()
190-
const error = new Error()
191-
actions.reportError(error)
192-
expect(store.getState()).toEqual({
193-
chainId: undefined,
194-
accounts: undefined,
195-
activating: false,
196-
error,
189+
describe('#reportError', () => {
190+
test('sets error', () => {
191+
const [store, actions] = createWeb3ReactStoreAndActions()
192+
const error = new Error()
193+
actions.reportError(error)
194+
expect(store.getState()).toEqual({
195+
chainId: undefined,
196+
accounts: undefined,
197+
activating: false,
198+
error,
199+
})
200+
})
201+
202+
test('resets state', () => {
203+
const [store, actions] = createWeb3ReactStoreAndActions()
204+
actions.reportError(new Error())
205+
actions.reportError(undefined)
206+
expect(store.getState()).toEqual({
207+
chainId: undefined,
208+
accounts: undefined,
209+
activating: false,
210+
error: undefined,
211+
})
197212
})
198213
})
199214
})

packages/store/src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ export function createWeb3ReactStoreAndActions(allowedChainIds?: number[]): [Web
6767

6868
// return a function that cancels the activation iff nothing else has happened
6969
return () => {
70-
if (nullifier === nullifierCached) {
71-
store.setState({ ...DEFAULT_STATE, activating: false })
72-
}
70+
if (nullifier === nullifierCached) store.setState({ activating: false })
7371
}
7472
}
7573

packages/url/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Url extends Connector {
4242

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

4747
await this.isomorphicInitialize()
4848

0 commit comments

Comments
 (0)