@wagmi/core 0.3.3 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.cjs.dev.js +28 -9
  2. package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.cjs.prod.js +28 -9
  3. package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.esm.js +28 -9
  4. package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.cjs.dev.js +10 -8
  5. package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.cjs.prod.js +10 -8
  6. package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.esm.js +10 -8
  7. package/connectors/mock/dist/wagmi-core-connectors-mock.cjs.dev.js +19 -6
  8. package/connectors/mock/dist/wagmi-core-connectors-mock.cjs.prod.js +19 -6
  9. package/connectors/mock/dist/wagmi-core-connectors-mock.esm.js +19 -6
  10. package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.cjs.dev.js +31 -6
  11. package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.cjs.prod.js +31 -6
  12. package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.esm.js +31 -6
  13. package/dist/{base-321ac87a.cjs.dev.js → base-d45bb8f9.cjs.dev.js} +5 -3
  14. package/dist/{base-41fb6637.esm.js → base-d69e580f.esm.js} +6 -4
  15. package/dist/{base-6841ebe0.cjs.prod.js → base-ffe4d8ed.cjs.prod.js} +5 -3
  16. package/dist/{client-4172f963.cjs.dev.js → client-20bfac7f.cjs.dev.js} +66 -12
  17. package/dist/{client-87c1ff8b.esm.js → client-61f14c97.esm.js} +66 -12
  18. package/dist/{client-a06c45f3.cjs.prod.js → client-8b124969.cjs.prod.js} +66 -12
  19. package/dist/declarations/src/actions/accounts/connect.d.ts +3 -1
  20. package/dist/declarations/src/actions/network-status/fetchFeeData.d.ts +3 -3
  21. package/dist/declarations/src/client.d.ts +1 -0
  22. package/dist/declarations/src/connectors/base.d.ts +7 -2
  23. package/dist/declarations/src/connectors/coinbaseWallet.d.ts +4 -2
  24. package/dist/declarations/src/connectors/injected.d.ts +9 -1
  25. package/dist/declarations/src/connectors/metaMask.d.ts +2 -2
  26. package/dist/declarations/src/connectors/mock/connector.d.ts +6 -2
  27. package/dist/declarations/src/connectors/mock/provider.d.ts +1 -1
  28. package/dist/declarations/src/connectors/walletConnect.d.ts +7 -2
  29. package/dist/wagmi-core.cjs.dev.js +9 -6
  30. package/dist/wagmi-core.cjs.prod.js +9 -6
  31. package/dist/wagmi-core.esm.js +11 -8
  32. package/package.json +2 -3
@@ -1,8 +1,11 @@
1
- import { b as Connector, _ as _classPrivateMethodInitSpec, f as _defineProperty, g as _classPrivateFieldInitSpec, n as normalizeChainId, h as _classPrivateMethodGet, U as UserRejectedRequestError, j as _classPrivateFieldGet, i as _classPrivateFieldSet, e as SwitchChainError } from '../../../dist/base-41fb6637.esm.js';
1
+ import { b as Connector, _ as _classPrivateMethodInitSpec, f as _defineProperty, g as _classPrivateFieldInitSpec, n as normalizeChainId, j as _classPrivateMethodGet, U as UserRejectedRequestError, h as _classPrivateFieldGet, i as _classPrivateFieldSet, e as SwitchChainError } from '../../../dist/base-d69e580f.esm.js';
2
2
  import { providers } from 'ethers';
3
3
  import { getAddress, hexValue } from 'ethers/lib/utils';
4
+ import { g as getClient } from '../../../dist/client-61f14c97.esm.js';
4
5
  import { a as allChains } from '../../../dist/chains-a0a80d94.esm.js';
5
6
  import 'eventemitter3';
7
+ import 'zustand/vanilla';
8
+ import 'zustand/middleware';
6
9
  import '../../../dist/rpcs-7cfbd91c.esm.js';
7
10
 
8
11
  /**
@@ -58,10 +61,24 @@ class WalletConnectConnector extends Connector {
58
61
  }
59
62
 
60
63
  async connect() {
64
+ let {
65
+ chainId
66
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
67
+
61
68
  try {
62
69
  var _provider$connector$p, _provider$connector, _provider$connector$p2;
63
70
 
64
- const provider = await this.getProvider(true);
71
+ let targetChainId = chainId;
72
+
73
+ if (!targetChainId) {
74
+ const lastUsedChainId = getClient().lastUsedChainId;
75
+ if (lastUsedChainId && !this.isChainUnsupported(lastUsedChainId)) targetChainId = lastUsedChainId;
76
+ }
77
+
78
+ const provider = await this.getProvider({
79
+ chainId: targetChainId,
80
+ create: true
81
+ });
65
82
  provider.on('accountsChanged', this.onAccountsChanged);
66
83
  provider.on('chainChanged', this.onChainChanged);
67
84
  provider.on('disconnect', this.onDisconnect); // Defer message to the next tick to ensure wallet connect data (provided by `.enable()`) is available
@@ -113,18 +130,26 @@ class WalletConnectConnector extends Connector {
113
130
  return chainId;
114
131
  }
115
132
 
116
- async getProvider(create) {
133
+ async getProvider() {
134
+ var _this$options;
135
+
136
+ let {
137
+ chainId = ((_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.chainId) || this.chains[0].id,
138
+ create = false
139
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
140
+
117
141
  if (!_classPrivateFieldGet(this, _provider) || create) {
118
- var _this$options, _this$options2;
142
+ var _this$options2, _this$options3;
119
143
 
120
- const rpc = !((_this$options = this.options) !== null && _this$options !== void 0 && _this$options.infuraId) ? this.chains.reduce((rpc, chain) => ({ ...rpc,
144
+ const rpc = !((_this$options2 = this.options) !== null && _this$options2 !== void 0 && _this$options2.infuraId) ? this.chains.reduce((rpc, chain) => ({ ...rpc,
121
145
  [chain.id]: chain.rpcUrls.default
122
146
  }), {}) : {};
123
147
  const WalletConnectProvider = (await import('@walletconnect/ethereum-provider')).default;
124
148
 
125
149
  _classPrivateFieldSet(this, _provider, new WalletConnectProvider({ ...this.options,
150
+ chainId,
126
151
  rpc: { ...rpc,
127
- ...((_this$options2 = this.options) === null || _this$options2 === void 0 ? void 0 : _this$options2.rpc)
152
+ ...((_this$options3 = this.options) === null || _this$options3 === void 0 ? void 0 : _this$options3.rpc)
128
153
  }
129
154
  }));
130
155
  }
@@ -256,9 +256,11 @@ class Connector extends EventEmitter__default["default"] {
256
256
  getBlockExplorerUrls(chain) {
257
257
  var _chain$blockExplorers;
258
258
 
259
- const blockExplorer = (_chain$blockExplorers = chain.blockExplorers) === null || _chain$blockExplorers === void 0 ? void 0 : _chain$blockExplorers.default;
260
- if (Array.isArray(blockExplorer)) return blockExplorer.map(x => x.url);
261
- if (blockExplorer !== null && blockExplorer !== void 0 && blockExplorer.url) return [blockExplorer.url];
259
+ const {
260
+ default: blockExplorer,
261
+ ...blockExplorers
262
+ } = (_chain$blockExplorers = chain.blockExplorers) !== null && _chain$blockExplorers !== void 0 ? _chain$blockExplorers : {};
263
+ if (blockExplorer) return [blockExplorer.url, ...Object.values(blockExplorers).map(x => x.url)];
262
264
  return [];
263
265
  }
264
266
 
@@ -250,9 +250,11 @@ class Connector extends EventEmitter {
250
250
  getBlockExplorerUrls(chain) {
251
251
  var _chain$blockExplorers;
252
252
 
253
- const blockExplorer = (_chain$blockExplorers = chain.blockExplorers) === null || _chain$blockExplorers === void 0 ? void 0 : _chain$blockExplorers.default;
254
- if (Array.isArray(blockExplorer)) return blockExplorer.map(x => x.url);
255
- if (blockExplorer !== null && blockExplorer !== void 0 && blockExplorer.url) return [blockExplorer.url];
253
+ const {
254
+ default: blockExplorer,
255
+ ...blockExplorers
256
+ } = (_chain$blockExplorers = chain.blockExplorers) !== null && _chain$blockExplorers !== void 0 ? _chain$blockExplorers : {};
257
+ if (blockExplorer) return [blockExplorer.url, ...Object.values(blockExplorers).map(x => x.url)];
256
258
  return [];
257
259
  }
258
260
 
@@ -262,4 +264,4 @@ class Connector extends EventEmitter {
262
264
 
263
265
  }
264
266
 
265
- export { AddChainError as A, ConnectorAlreadyConnectedError as C, ProviderRpcError as P, ResourceUnavailableError as R, SwitchChainNotSupportedError as S, UserRejectedRequestError as U, _classPrivateMethodInitSpec as _, ConnectorNotFoundError as a, Connector as b, ChainNotConfiguredError as c, RpcError as d, SwitchChainError as e, _defineProperty as f, _classPrivateFieldInitSpec as g, _classPrivateMethodGet as h, _classPrivateFieldSet as i, _classPrivateFieldGet as j, normalizeChainId as n };
267
+ export { AddChainError as A, ConnectorAlreadyConnectedError as C, ProviderRpcError as P, ResourceUnavailableError as R, SwitchChainNotSupportedError as S, UserRejectedRequestError as U, _classPrivateMethodInitSpec as _, ConnectorNotFoundError as a, Connector as b, ChainNotConfiguredError as c, RpcError as d, SwitchChainError as e, _defineProperty as f, _classPrivateFieldInitSpec as g, _classPrivateFieldGet as h, _classPrivateFieldSet as i, _classPrivateMethodGet as j, normalizeChainId as n };
@@ -256,9 +256,11 @@ class Connector extends EventEmitter__default["default"] {
256
256
  getBlockExplorerUrls(chain) {
257
257
  var _chain$blockExplorers;
258
258
 
259
- const blockExplorer = (_chain$blockExplorers = chain.blockExplorers) === null || _chain$blockExplorers === void 0 ? void 0 : _chain$blockExplorers.default;
260
- if (Array.isArray(blockExplorer)) return blockExplorer.map(x => x.url);
261
- if (blockExplorer !== null && blockExplorer !== void 0 && blockExplorer.url) return [blockExplorer.url];
259
+ const {
260
+ default: blockExplorer,
261
+ ...blockExplorers
262
+ } = (_chain$blockExplorers = chain.blockExplorers) !== null && _chain$blockExplorers !== void 0 ? _chain$blockExplorers : {};
263
+ if (blockExplorer) return [blockExplorer.url, ...Object.values(blockExplorers).map(x => x.url)];
262
264
  return [];
263
265
  }
264
266
 
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var base = require('./base-321ac87a.cjs.dev.js');
3
+ var base = require('./base-d45bb8f9.cjs.dev.js');
4
4
  var ethers = require('ethers');
5
5
  var create = require('zustand/vanilla');
6
6
  var middleware = require('zustand/middleware');
@@ -64,6 +64,8 @@ const shimKey = 'injected.shimDisconnect';
64
64
 
65
65
  var _provider = /*#__PURE__*/new WeakMap();
66
66
 
67
+ var _switchingChains = /*#__PURE__*/new WeakMap();
68
+
67
69
  var _isUserRejectedRequestError = /*#__PURE__*/new WeakSet();
68
70
 
69
71
  class InjectedConnector extends base.Connector {
@@ -92,6 +94,11 @@ class InjectedConnector extends base.Connector {
92
94
  value: void 0
93
95
  });
94
96
 
97
+ base._classPrivateFieldInitSpec(this, _switchingChains, {
98
+ writable: true,
99
+ value: void 0
100
+ });
101
+
95
102
  base._defineProperty(this, "onAccountsChanged", accounts => {
96
103
  if (accounts.length === 0) this.emit('disconnect');else this.emit('change', {
97
104
  account: utils.getAddress(accounts[0])
@@ -110,10 +117,19 @@ class InjectedConnector extends base.Connector {
110
117
  });
111
118
 
112
119
  base._defineProperty(this, "onDisconnect", () => {
113
- var _this$options, _getClient$storage;
120
+ var _this$options, _this$options2, _getClient$storage;
121
+
122
+ // We need this as MetaMask can emit the "disconnect" event
123
+ // upon switching chains. This workaround ensures that the
124
+ // user currently isn't in the process of switching chains.
125
+ if ((_this$options = this.options) !== null && _this$options !== void 0 && _this$options.shimChainChangedDisconnect && base._classPrivateFieldGet(this, _switchingChains)) {
126
+ base._classPrivateFieldSet(this, _switchingChains, false);
127
+
128
+ return;
129
+ }
114
130
 
115
131
  this.emit('disconnect');
116
- if ((_this$options = this.options) !== null && _this$options !== void 0 && _this$options.shimDisconnect) (_getClient$storage = getClient().storage) === null || _getClient$storage === void 0 ? void 0 : _getClient$storage.removeItem(shimKey);
132
+ if ((_this$options2 = this.options) !== null && _this$options2 !== void 0 && _this$options2.shimDisconnect) (_getClient$storage = getClient().storage) === null || _getClient$storage === void 0 ? void 0 : _getClient$storage.removeItem(shimKey);
117
133
  });
118
134
 
119
135
  let name = 'Injected';
@@ -129,8 +145,12 @@ class InjectedConnector extends base.Connector {
129
145
  }
130
146
 
131
147
  async connect() {
148
+ let {
149
+ chainId
150
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
151
+
132
152
  try {
133
- var _this$options2, _getClient$storage2;
153
+ var _this$options3, _getClient$storage2;
134
154
 
135
155
  const provider = await this.getProvider();
136
156
  if (!provider) throw new base.ConnectorNotFoundError();
@@ -144,10 +164,18 @@ class InjectedConnector extends base.Connector {
144
164
  this.emit('message', {
145
165
  type: 'connecting'
146
166
  });
147
- const account = await this.getAccount();
148
- const id = await this.getChainId();
149
- const unsupported = this.isChainUnsupported(id);
150
- if ((_this$options2 = this.options) !== null && _this$options2 !== void 0 && _this$options2.shimDisconnect) (_getClient$storage2 = getClient().storage) === null || _getClient$storage2 === void 0 ? void 0 : _getClient$storage2.setItem(shimKey, true);
167
+ const account = await this.getAccount(); // Switch to chain if provided
168
+
169
+ let id = await this.getChainId();
170
+ let unsupported = this.isChainUnsupported(id);
171
+
172
+ if (chainId && id !== chainId) {
173
+ const chain = await this.switchChain(chainId);
174
+ id = chain.id;
175
+ unsupported = this.isChainUnsupported(id);
176
+ }
177
+
178
+ if ((_this$options3 = this.options) !== null && _this$options3 !== void 0 && _this$options3.shimDisconnect) (_getClient$storage2 = getClient().storage) === null || _getClient$storage2 === void 0 ? void 0 : _getClient$storage2.setItem(shimKey, true);
151
179
  return {
152
180
  account,
153
181
  chain: {
@@ -164,14 +192,14 @@ class InjectedConnector extends base.Connector {
164
192
  }
165
193
 
166
194
  async disconnect() {
167
- var _this$options3, _getClient$storage3;
195
+ var _this$options4, _getClient$storage3;
168
196
 
169
197
  const provider = await this.getProvider();
170
198
  if (!(provider !== null && provider !== void 0 && provider.removeListener)) return;
171
199
  provider.removeListener('accountsChanged', this.onAccountsChanged);
172
200
  provider.removeListener('chainChanged', this.onChainChanged);
173
201
  provider.removeListener('disconnect', this.onDisconnect);
174
- if ((_this$options3 = this.options) !== null && _this$options3 !== void 0 && _this$options3.shimDisconnect) (_getClient$storage3 = getClient().storage) === null || _getClient$storage3 === void 0 ? void 0 : _getClient$storage3.removeItem(shimKey);
202
+ if ((_this$options4 = this.options) !== null && _this$options4 !== void 0 && _this$options4.shimDisconnect) (_getClient$storage3 = getClient().storage) === null || _getClient$storage3 === void 0 ? void 0 : _getClient$storage3.removeItem(shimKey);
175
203
  }
176
204
 
177
205
  async getAccount() {
@@ -204,9 +232,9 @@ class InjectedConnector extends base.Connector {
204
232
 
205
233
  async isAuthorized() {
206
234
  try {
207
- var _this$options4, _getClient$storage4;
235
+ var _this$options5, _getClient$storage4;
208
236
 
209
- if ((_this$options4 = this.options) !== null && _this$options4 !== void 0 && _this$options4.shimDisconnect && !((_getClient$storage4 = getClient().storage) !== null && _getClient$storage4 !== void 0 && _getClient$storage4.getItem(shimKey))) return false;
237
+ if ((_this$options5 = this.options) !== null && _this$options5 !== void 0 && _this$options5.shimDisconnect && !((_getClient$storage4 = getClient().storage) !== null && _getClient$storage4 !== void 0 && _getClient$storage4.getItem(shimKey))) return false;
210
238
  const provider = await this.getProvider();
211
239
  if (!provider) throw new base.ConnectorNotFoundError();
212
240
  const accounts = await provider.request({
@@ -220,6 +248,9 @@ class InjectedConnector extends base.Connector {
220
248
  }
221
249
 
222
250
  async switchChain(chainId) {
251
+ var _this$options6;
252
+
253
+ if ((_this$options6 = this.options) !== null && _this$options6 !== void 0 && _this$options6.shimChainChangedDisconnect) base._classPrivateFieldSet(this, _switchingChains, true);
223
254
  const provider = await this.getProvider();
224
255
  if (!provider) throw new base.ConnectorNotFoundError();
225
256
  const id = utils.hexValue(chainId);
@@ -338,6 +369,8 @@ function createStorage(_ref) {
338
369
 
339
370
  const storeKey = 'store';
340
371
 
372
+ var _isAutoConnecting = /*#__PURE__*/new WeakMap();
373
+
341
374
  var _lastUsedConnector = /*#__PURE__*/new WeakMap();
342
375
 
343
376
  var _addEffects = /*#__PURE__*/new WeakSet();
@@ -368,6 +401,11 @@ class Client {
368
401
 
369
402
  base._defineProperty(this, "store", void 0);
370
403
 
404
+ base._classPrivateFieldInitSpec(this, _isAutoConnecting, {
405
+ writable: true,
406
+ value: void 0
407
+ });
408
+
371
409
  base._classPrivateFieldInitSpec(this, _lastUsedConnector, {
372
410
  writable: true,
373
411
  value: void 0
@@ -457,6 +495,12 @@ class Client {
457
495
  return this.store.getState().error;
458
496
  }
459
497
 
498
+ get lastUsedChainId() {
499
+ var _this$data, _this$data$chain;
500
+
501
+ return (_this$data = this.data) === null || _this$data === void 0 ? void 0 : (_this$data$chain = _this$data.chain) === null || _this$data$chain === void 0 ? void 0 : _this$data$chain.id;
502
+ }
503
+
460
504
  get provider() {
461
505
  return this.store.getState().provider;
462
506
  }
@@ -492,11 +536,18 @@ class Client {
492
536
  var _this$connector$disco, _this$connector;
493
537
 
494
538
  if (this.connector) await ((_this$connector$disco = (_this$connector = this.connector).disconnect) === null || _this$connector$disco === void 0 ? void 0 : _this$connector$disco.call(_this$connector));
539
+
540
+ base._classPrivateFieldSet(this, _isAutoConnecting, false);
541
+
495
542
  this.clearState();
496
543
  this.store.destroy();
497
544
  }
498
545
 
499
546
  async autoConnect() {
547
+ if (base._classPrivateFieldGet(this, _isAutoConnecting)) return;
548
+
549
+ base._classPrivateFieldSet(this, _isAutoConnecting, true);
550
+
500
551
  if (!this.connectors.length) return; // Try last used connector first
501
552
 
502
553
  const sorted = base._classPrivateFieldGet(this, _lastUsedConnector) ? [...this.connectors].sort(x => x.id === base._classPrivateFieldGet(this, _lastUsedConnector) ? -1 : 1) : this.connectors;
@@ -522,6 +573,9 @@ class Client {
522
573
  data: undefined,
523
574
  status: 'disconnected'
524
575
  }));
576
+
577
+ base._classPrivateFieldSet(this, _isAutoConnecting, false);
578
+
525
579
  return this.data;
526
580
  }
527
581
 
@@ -1,4 +1,4 @@
1
- import { b as Connector, _ as _classPrivateMethodInitSpec, f as _defineProperty, g as _classPrivateFieldInitSpec, n as normalizeChainId, a as ConnectorNotFoundError, h as _classPrivateMethodGet, U as UserRejectedRequestError, R as ResourceUnavailableError, i as _classPrivateFieldSet, j as _classPrivateFieldGet, c as ChainNotConfiguredError, A as AddChainError, e as SwitchChainError } from './base-41fb6637.esm.js';
1
+ import { b as Connector, _ as _classPrivateMethodInitSpec, f as _defineProperty, g as _classPrivateFieldInitSpec, n as normalizeChainId, h as _classPrivateFieldGet, i as _classPrivateFieldSet, a as ConnectorNotFoundError, j as _classPrivateMethodGet, U as UserRejectedRequestError, R as ResourceUnavailableError, c as ChainNotConfiguredError, A as AddChainError, e as SwitchChainError } from './base-d69e580f.esm.js';
2
2
  import { providers, getDefaultProvider } from 'ethers';
3
3
  import create from 'zustand/vanilla';
4
4
  import { subscribeWithSelector, persist } from 'zustand/middleware';
@@ -58,6 +58,8 @@ const shimKey = 'injected.shimDisconnect';
58
58
 
59
59
  var _provider = /*#__PURE__*/new WeakMap();
60
60
 
61
+ var _switchingChains = /*#__PURE__*/new WeakMap();
62
+
61
63
  var _isUserRejectedRequestError = /*#__PURE__*/new WeakSet();
62
64
 
63
65
  class InjectedConnector extends Connector {
@@ -86,6 +88,11 @@ class InjectedConnector extends Connector {
86
88
  value: void 0
87
89
  });
88
90
 
91
+ _classPrivateFieldInitSpec(this, _switchingChains, {
92
+ writable: true,
93
+ value: void 0
94
+ });
95
+
89
96
  _defineProperty(this, "onAccountsChanged", accounts => {
90
97
  if (accounts.length === 0) this.emit('disconnect');else this.emit('change', {
91
98
  account: getAddress(accounts[0])
@@ -104,10 +111,19 @@ class InjectedConnector extends Connector {
104
111
  });
105
112
 
106
113
  _defineProperty(this, "onDisconnect", () => {
107
- var _this$options, _getClient$storage;
114
+ var _this$options, _this$options2, _getClient$storage;
115
+
116
+ // We need this as MetaMask can emit the "disconnect" event
117
+ // upon switching chains. This workaround ensures that the
118
+ // user currently isn't in the process of switching chains.
119
+ if ((_this$options = this.options) !== null && _this$options !== void 0 && _this$options.shimChainChangedDisconnect && _classPrivateFieldGet(this, _switchingChains)) {
120
+ _classPrivateFieldSet(this, _switchingChains, false);
121
+
122
+ return;
123
+ }
108
124
 
109
125
  this.emit('disconnect');
110
- if ((_this$options = this.options) !== null && _this$options !== void 0 && _this$options.shimDisconnect) (_getClient$storage = getClient().storage) === null || _getClient$storage === void 0 ? void 0 : _getClient$storage.removeItem(shimKey);
126
+ if ((_this$options2 = this.options) !== null && _this$options2 !== void 0 && _this$options2.shimDisconnect) (_getClient$storage = getClient().storage) === null || _getClient$storage === void 0 ? void 0 : _getClient$storage.removeItem(shimKey);
111
127
  });
112
128
 
113
129
  let name = 'Injected';
@@ -123,8 +139,12 @@ class InjectedConnector extends Connector {
123
139
  }
124
140
 
125
141
  async connect() {
142
+ let {
143
+ chainId
144
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
145
+
126
146
  try {
127
- var _this$options2, _getClient$storage2;
147
+ var _this$options3, _getClient$storage2;
128
148
 
129
149
  const provider = await this.getProvider();
130
150
  if (!provider) throw new ConnectorNotFoundError();
@@ -138,10 +158,18 @@ class InjectedConnector extends Connector {
138
158
  this.emit('message', {
139
159
  type: 'connecting'
140
160
  });
141
- const account = await this.getAccount();
142
- const id = await this.getChainId();
143
- const unsupported = this.isChainUnsupported(id);
144
- if ((_this$options2 = this.options) !== null && _this$options2 !== void 0 && _this$options2.shimDisconnect) (_getClient$storage2 = getClient().storage) === null || _getClient$storage2 === void 0 ? void 0 : _getClient$storage2.setItem(shimKey, true);
161
+ const account = await this.getAccount(); // Switch to chain if provided
162
+
163
+ let id = await this.getChainId();
164
+ let unsupported = this.isChainUnsupported(id);
165
+
166
+ if (chainId && id !== chainId) {
167
+ const chain = await this.switchChain(chainId);
168
+ id = chain.id;
169
+ unsupported = this.isChainUnsupported(id);
170
+ }
171
+
172
+ if ((_this$options3 = this.options) !== null && _this$options3 !== void 0 && _this$options3.shimDisconnect) (_getClient$storage2 = getClient().storage) === null || _getClient$storage2 === void 0 ? void 0 : _getClient$storage2.setItem(shimKey, true);
145
173
  return {
146
174
  account,
147
175
  chain: {
@@ -158,14 +186,14 @@ class InjectedConnector extends Connector {
158
186
  }
159
187
 
160
188
  async disconnect() {
161
- var _this$options3, _getClient$storage3;
189
+ var _this$options4, _getClient$storage3;
162
190
 
163
191
  const provider = await this.getProvider();
164
192
  if (!(provider !== null && provider !== void 0 && provider.removeListener)) return;
165
193
  provider.removeListener('accountsChanged', this.onAccountsChanged);
166
194
  provider.removeListener('chainChanged', this.onChainChanged);
167
195
  provider.removeListener('disconnect', this.onDisconnect);
168
- if ((_this$options3 = this.options) !== null && _this$options3 !== void 0 && _this$options3.shimDisconnect) (_getClient$storage3 = getClient().storage) === null || _getClient$storage3 === void 0 ? void 0 : _getClient$storage3.removeItem(shimKey);
196
+ if ((_this$options4 = this.options) !== null && _this$options4 !== void 0 && _this$options4.shimDisconnect) (_getClient$storage3 = getClient().storage) === null || _getClient$storage3 === void 0 ? void 0 : _getClient$storage3.removeItem(shimKey);
169
197
  }
170
198
 
171
199
  async getAccount() {
@@ -198,9 +226,9 @@ class InjectedConnector extends Connector {
198
226
 
199
227
  async isAuthorized() {
200
228
  try {
201
- var _this$options4, _getClient$storage4;
229
+ var _this$options5, _getClient$storage4;
202
230
 
203
- if ((_this$options4 = this.options) !== null && _this$options4 !== void 0 && _this$options4.shimDisconnect && !((_getClient$storage4 = getClient().storage) !== null && _getClient$storage4 !== void 0 && _getClient$storage4.getItem(shimKey))) return false;
231
+ if ((_this$options5 = this.options) !== null && _this$options5 !== void 0 && _this$options5.shimDisconnect && !((_getClient$storage4 = getClient().storage) !== null && _getClient$storage4 !== void 0 && _getClient$storage4.getItem(shimKey))) return false;
204
232
  const provider = await this.getProvider();
205
233
  if (!provider) throw new ConnectorNotFoundError();
206
234
  const accounts = await provider.request({
@@ -214,6 +242,9 @@ class InjectedConnector extends Connector {
214
242
  }
215
243
 
216
244
  async switchChain(chainId) {
245
+ var _this$options6;
246
+
247
+ if ((_this$options6 = this.options) !== null && _this$options6 !== void 0 && _this$options6.shimChainChangedDisconnect) _classPrivateFieldSet(this, _switchingChains, true);
217
248
  const provider = await this.getProvider();
218
249
  if (!provider) throw new ConnectorNotFoundError();
219
250
  const id = hexValue(chainId);
@@ -332,6 +363,8 @@ function createStorage(_ref) {
332
363
 
333
364
  const storeKey = 'store';
334
365
 
366
+ var _isAutoConnecting = /*#__PURE__*/new WeakMap();
367
+
335
368
  var _lastUsedConnector = /*#__PURE__*/new WeakMap();
336
369
 
337
370
  var _addEffects = /*#__PURE__*/new WeakSet();
@@ -362,6 +395,11 @@ class Client {
362
395
 
363
396
  _defineProperty(this, "store", void 0);
364
397
 
398
+ _classPrivateFieldInitSpec(this, _isAutoConnecting, {
399
+ writable: true,
400
+ value: void 0
401
+ });
402
+
365
403
  _classPrivateFieldInitSpec(this, _lastUsedConnector, {
366
404
  writable: true,
367
405
  value: void 0
@@ -451,6 +489,12 @@ class Client {
451
489
  return this.store.getState().error;
452
490
  }
453
491
 
492
+ get lastUsedChainId() {
493
+ var _this$data, _this$data$chain;
494
+
495
+ return (_this$data = this.data) === null || _this$data === void 0 ? void 0 : (_this$data$chain = _this$data.chain) === null || _this$data$chain === void 0 ? void 0 : _this$data$chain.id;
496
+ }
497
+
454
498
  get provider() {
455
499
  return this.store.getState().provider;
456
500
  }
@@ -486,11 +530,18 @@ class Client {
486
530
  var _this$connector$disco, _this$connector;
487
531
 
488
532
  if (this.connector) await ((_this$connector$disco = (_this$connector = this.connector).disconnect) === null || _this$connector$disco === void 0 ? void 0 : _this$connector$disco.call(_this$connector));
533
+
534
+ _classPrivateFieldSet(this, _isAutoConnecting, false);
535
+
489
536
  this.clearState();
490
537
  this.store.destroy();
491
538
  }
492
539
 
493
540
  async autoConnect() {
541
+ if (_classPrivateFieldGet(this, _isAutoConnecting)) return;
542
+
543
+ _classPrivateFieldSet(this, _isAutoConnecting, true);
544
+
494
545
  if (!this.connectors.length) return; // Try last used connector first
495
546
 
496
547
  const sorted = _classPrivateFieldGet(this, _lastUsedConnector) ? [...this.connectors].sort(x => x.id === _classPrivateFieldGet(this, _lastUsedConnector) ? -1 : 1) : this.connectors;
@@ -516,6 +567,9 @@ class Client {
516
567
  data: undefined,
517
568
  status: 'disconnected'
518
569
  }));
570
+
571
+ _classPrivateFieldSet(this, _isAutoConnecting, false);
572
+
519
573
  return this.data;
520
574
  }
521
575