@wagmi/core 0.3.6 → 0.4.0-test.0

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 (139) hide show
  1. package/chains/dist/wagmi-core-chains.cjs.d.ts +11 -1
  2. package/chains/dist/wagmi-core-chains.cjs.js +15 -6
  3. package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.cjs.d.ts +11 -1
  4. package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.cjs.js +15 -6
  5. package/connectors/injected/dist/wagmi-core-connectors-injected.cjs.d.ts +1 -0
  6. package/connectors/injected/dist/wagmi-core-connectors-injected.cjs.dev.js +200 -0
  7. package/connectors/injected/dist/wagmi-core-connectors-injected.cjs.js +7 -0
  8. package/connectors/injected/dist/wagmi-core-connectors-injected.cjs.prod.js +200 -0
  9. package/connectors/injected/dist/wagmi-core-connectors-injected.esm.js +196 -0
  10. package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.cjs.d.ts +11 -1
  11. package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.cjs.js +15 -6
  12. package/connectors/mock/dist/wagmi-core-connectors-mock.cjs.d.ts +11 -1
  13. package/connectors/mock/dist/wagmi-core-connectors-mock.cjs.js +15 -6
  14. package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.cjs.d.ts +11 -1
  15. package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.cjs.js +15 -6
  16. package/connectors/walletLink/dist/wagmi-core-connectors-walletLink.cjs.d.ts +11 -0
  17. package/connectors/walletLink/dist/wagmi-core-connectors-walletLink.cjs.js +16 -0
  18. package/dist/wagmi-core.cjs.d.ts +11 -1
  19. package/dist/wagmi-core.cjs.js +15 -6
  20. package/package.json +3 -4
  21. package/providers/alchemy/dist/wagmi-core-providers-alchemy.cjs.d.ts +11 -1
  22. package/providers/alchemy/dist/wagmi-core-providers-alchemy.cjs.js +15 -6
  23. package/providers/infura/dist/wagmi-core-providers-infura.cjs.d.ts +11 -1
  24. package/providers/infura/dist/wagmi-core-providers-infura.cjs.js +15 -6
  25. package/providers/jsonRpc/dist/wagmi-core-providers-jsonRpc.cjs.d.ts +11 -1
  26. package/providers/jsonRpc/dist/wagmi-core-providers-jsonRpc.cjs.js +15 -6
  27. package/providers/public/dist/wagmi-core-providers-public.cjs.d.ts +11 -1
  28. package/providers/public/dist/wagmi-core-providers-public.cjs.js +15 -6
  29. package/chains/dist/wagmi-core-chains.cjs.dev.js +0 -22
  30. package/chains/dist/wagmi-core-chains.cjs.prod.js +0 -22
  31. package/chains/dist/wagmi-core-chains.esm.js +0 -2
  32. package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.cjs.dev.js +0 -261
  33. package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.cjs.prod.js +0 -261
  34. package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.esm.js +0 -239
  35. package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.cjs.dev.js +0 -79
  36. package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.cjs.prod.js +0 -79
  37. package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.esm.js +0 -75
  38. package/connectors/mock/dist/wagmi-core-connectors-mock.cjs.dev.js +0 -265
  39. package/connectors/mock/dist/wagmi-core-connectors-mock.cjs.prod.js +0 -265
  40. package/connectors/mock/dist/wagmi-core-connectors-mock.esm.js +0 -256
  41. package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.cjs.dev.js +0 -227
  42. package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.cjs.prod.js +0 -227
  43. package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.esm.js +0 -205
  44. package/dist/base-d45bb8f9.cjs.dev.js +0 -290
  45. package/dist/base-d69e580f.esm.js +0 -267
  46. package/dist/base-ffe4d8ed.cjs.prod.js +0 -290
  47. package/dist/chains-0dd661ab.cjs.prod.js +0 -349
  48. package/dist/chains-98e2e613.cjs.dev.js +0 -349
  49. package/dist/chains-a0a80d94.esm.js +0 -329
  50. package/dist/client-20bfac7f.cjs.dev.js +0 -671
  51. package/dist/client-61f14c97.esm.js +0 -660
  52. package/dist/client-8b124969.cjs.prod.js +0 -671
  53. package/dist/declarations/src/actions/accounts/connect.d.ts +0 -18
  54. package/dist/declarations/src/actions/accounts/disconnect.d.ts +0 -1
  55. package/dist/declarations/src/actions/accounts/fetchBalance.d.ts +0 -20
  56. package/dist/declarations/src/actions/accounts/fetchSigner.d.ts +0 -3
  57. package/dist/declarations/src/actions/accounts/getAccount.d.ts +0 -7
  58. package/dist/declarations/src/actions/accounts/getNetwork.d.ts +0 -9
  59. package/dist/declarations/src/actions/accounts/index.d.ts +0 -12
  60. package/dist/declarations/src/actions/accounts/signMessage.d.ts +0 -9
  61. package/dist/declarations/src/actions/accounts/signTypedData.d.ts +0 -22
  62. package/dist/declarations/src/actions/accounts/switchNetwork.d.ts +0 -6
  63. package/dist/declarations/src/actions/accounts/watchAccount.d.ts +0 -4
  64. package/dist/declarations/src/actions/accounts/watchNetwork.d.ts +0 -3
  65. package/dist/declarations/src/actions/accounts/watchSigner.d.ts +0 -3
  66. package/dist/declarations/src/actions/contracts/getContract.d.ts +0 -10
  67. package/dist/declarations/src/actions/contracts/index.d.ts +0 -5
  68. package/dist/declarations/src/actions/contracts/readContract.d.ts +0 -14
  69. package/dist/declarations/src/actions/contracts/watchContractEvent.d.ts +0 -14
  70. package/dist/declarations/src/actions/contracts/watchReadContract.d.ts +0 -7
  71. package/dist/declarations/src/actions/contracts/writeContract.d.ts +0 -10
  72. package/dist/declarations/src/actions/ens/fetchEnsAddress.d.ts +0 -8
  73. package/dist/declarations/src/actions/ens/fetchEnsAvatar.d.ts +0 -8
  74. package/dist/declarations/src/actions/ens/fetchEnsName.d.ts +0 -8
  75. package/dist/declarations/src/actions/ens/fetchEnsResolver.d.ts +0 -9
  76. package/dist/declarations/src/actions/ens/index.d.ts +0 -4
  77. package/dist/declarations/src/actions/index.d.ts +0 -7
  78. package/dist/declarations/src/actions/network-status/fetchBlockNumber.d.ts +0 -5
  79. package/dist/declarations/src/actions/network-status/fetchFeeData.d.ts +0 -16
  80. package/dist/declarations/src/actions/network-status/index.d.ts +0 -3
  81. package/dist/declarations/src/actions/network-status/watchBlockNumber.d.ts +0 -6
  82. package/dist/declarations/src/actions/providers/getProvider.d.ts +0 -7
  83. package/dist/declarations/src/actions/providers/getWebSocketProvider.d.ts +0 -7
  84. package/dist/declarations/src/actions/providers/index.d.ts +0 -4
  85. package/dist/declarations/src/actions/providers/watchProvider.d.ts +0 -4
  86. package/dist/declarations/src/actions/providers/watchWebSocketProvider.d.ts +0 -4
  87. package/dist/declarations/src/actions/tokens/fetchToken.d.ts +0 -20
  88. package/dist/declarations/src/actions/tokens/index.d.ts +0 -1
  89. package/dist/declarations/src/actions/transactions/index.d.ts +0 -2
  90. package/dist/declarations/src/actions/transactions/sendTransaction.d.ts +0 -7
  91. package/dist/declarations/src/actions/transactions/waitForTransaction.d.ts +0 -17
  92. package/dist/declarations/src/chains.d.ts +0 -1
  93. package/dist/declarations/src/client.d.ts +0 -82
  94. package/dist/declarations/src/connectors/base.d.ts +0 -60
  95. package/dist/declarations/src/connectors/coinbaseWallet.d.ts +0 -54
  96. package/dist/declarations/src/connectors/index.d.ts +0 -2
  97. package/dist/declarations/src/connectors/injected.d.ts +0 -56
  98. package/dist/declarations/src/connectors/metaMask.d.ts +0 -13
  99. package/dist/declarations/src/connectors/mock/connector.d.ts +0 -41
  100. package/dist/declarations/src/connectors/mock/index.d.ts +0 -2
  101. package/dist/declarations/src/connectors/mock/provider.d.ts +0 -40
  102. package/dist/declarations/src/connectors/walletConnect.d.ts +0 -38
  103. package/dist/declarations/src/constants/abis.d.ts +0 -2
  104. package/dist/declarations/src/constants/blockExplorers.d.ts +0 -9
  105. package/dist/declarations/src/constants/chains.d.ts +0 -52
  106. package/dist/declarations/src/constants/index.d.ts +0 -7
  107. package/dist/declarations/src/constants/rpcs.d.ts +0 -9
  108. package/dist/declarations/src/constants/units.d.ts +0 -1
  109. package/dist/declarations/src/errors.d.ts +0 -72
  110. package/dist/declarations/src/index.d.ts +0 -13
  111. package/dist/declarations/src/providers/alchemy.d.ts +0 -7
  112. package/dist/declarations/src/providers/infura.d.ts +0 -7
  113. package/dist/declarations/src/providers/jsonRpc.d.ts +0 -11
  114. package/dist/declarations/src/providers/public.d.ts +0 -6
  115. package/dist/declarations/src/storage.d.ts +0 -12
  116. package/dist/declarations/src/types/index.d.ts +0 -148
  117. package/dist/declarations/src/utils/configureChains.d.ts +0 -20
  118. package/dist/declarations/src/utils/getInjectedName.d.ts +0 -1
  119. package/dist/declarations/src/utils/index.d.ts +0 -5
  120. package/dist/declarations/src/utils/normalizeChainId.d.ts +0 -1
  121. package/dist/declarations/src/utils/warn.d.ts +0 -1
  122. package/dist/rpcs-7cfbd91c.esm.js +0 -30
  123. package/dist/rpcs-9c4eb960.cjs.dev.js +0 -35
  124. package/dist/rpcs-b3c52116.cjs.prod.js +0 -35
  125. package/dist/wagmi-core.cjs.dev.js +0 -756
  126. package/dist/wagmi-core.cjs.prod.js +0 -756
  127. package/dist/wagmi-core.esm.js +0 -696
  128. package/providers/alchemy/dist/wagmi-core-providers-alchemy.cjs.dev.js +0 -38
  129. package/providers/alchemy/dist/wagmi-core-providers-alchemy.cjs.prod.js +0 -38
  130. package/providers/alchemy/dist/wagmi-core-providers-alchemy.esm.js +0 -34
  131. package/providers/infura/dist/wagmi-core-providers-infura.cjs.dev.js +0 -38
  132. package/providers/infura/dist/wagmi-core-providers-infura.cjs.prod.js +0 -38
  133. package/providers/infura/dist/wagmi-core-providers-infura.esm.js +0 -34
  134. package/providers/jsonRpc/dist/wagmi-core-providers-jsonRpc.cjs.dev.js +0 -45
  135. package/providers/jsonRpc/dist/wagmi-core-providers-jsonRpc.cjs.prod.js +0 -45
  136. package/providers/jsonRpc/dist/wagmi-core-providers-jsonRpc.esm.js +0 -41
  137. package/providers/public/dist/wagmi-core-providers-public.cjs.dev.js +0 -34
  138. package/providers/public/dist/wagmi-core-providers-public.cjs.prod.js +0 -34
  139. package/providers/public/dist/wagmi-core-providers-public.esm.js +0 -30
@@ -1 +1,11 @@
1
- export * from "../../dist/declarations/src/chains";
1
+ // are you seeing an error that a default export doesn't exist but your source file has a default export?
2
+ // you should run `yarn` or `yarn preconstruct dev` if preconstruct dev isn't in your postinstall hook
3
+
4
+ // curious why you need to?
5
+ // this file exists so that you can import from the entrypoint normally
6
+ // except that it points to your source file and you don't need to run build constantly
7
+ // which means we need to re-export all of the modules from your source file
8
+ // and since export * doesn't include default exports, we need to read your source file
9
+ // to check for a default export and re-export it if it exists
10
+ // it's not ideal, but it works pretty well ¯\_(ツ)_/¯
11
+ export * from "../../src/chains";
@@ -1,7 +1,16 @@
1
- 'use strict';
1
+ "use strict";
2
+ // this file might look strange and you might be wondering what it's for
3
+ // it's lets you import your source files by importing this entrypoint
4
+ // as you would import it if it was built with preconstruct build
5
+ // this file is slightly different to some others though
6
+ // it has a require hook which compiles your code with Babel
7
+ // this means that you don't have to set up @babel/register or anything like that
8
+ // but you can still require this module and it'll be compiled
2
9
 
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./wagmi-core-chains.cjs.prod.js");
5
- } else {
6
- module.exports = require("./wagmi-core-chains.cjs.dev.js");
7
- }
10
+ // this bit of code imports the require hook and registers it
11
+ let unregister = require("../../../../node_modules/.pnpm/@preconstruct+hook@0.4.0/node_modules/@preconstruct/hook").___internalHook(typeof __dirname === 'undefined' ? undefined : __dirname, "../../../..", "../..");
12
+
13
+ // this re-exports the source file
14
+ module.exports = require("../../src/chains.ts");
15
+
16
+ unregister();
@@ -1 +1,11 @@
1
- export * from "../../../dist/declarations/src/connectors/coinbaseWallet";
1
+ // are you seeing an error that a default export doesn't exist but your source file has a default export?
2
+ // you should run `yarn` or `yarn preconstruct dev` if preconstruct dev isn't in your postinstall hook
3
+
4
+ // curious why you need to?
5
+ // this file exists so that you can import from the entrypoint normally
6
+ // except that it points to your source file and you don't need to run build constantly
7
+ // which means we need to re-export all of the modules from your source file
8
+ // and since export * doesn't include default exports, we need to read your source file
9
+ // to check for a default export and re-export it if it exists
10
+ // it's not ideal, but it works pretty well ¯\_(ツ)_/¯
11
+ export * from "../../../src/connectors/coinbaseWallet";
@@ -1,7 +1,16 @@
1
- 'use strict';
1
+ "use strict";
2
+ // this file might look strange and you might be wondering what it's for
3
+ // it's lets you import your source files by importing this entrypoint
4
+ // as you would import it if it was built with preconstruct build
5
+ // this file is slightly different to some others though
6
+ // it has a require hook which compiles your code with Babel
7
+ // this means that you don't have to set up @babel/register or anything like that
8
+ // but you can still require this module and it'll be compiled
2
9
 
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./wagmi-core-connectors-coinbaseWallet.cjs.prod.js");
5
- } else {
6
- module.exports = require("./wagmi-core-connectors-coinbaseWallet.cjs.dev.js");
7
- }
10
+ // this bit of code imports the require hook and registers it
11
+ let unregister = require("../../../../../node_modules/.pnpm/@preconstruct+hook@0.4.0/node_modules/@preconstruct/hook").___internalHook(typeof __dirname === 'undefined' ? undefined : __dirname, "../../../../..", "../../..");
12
+
13
+ // this re-exports the source file
14
+ module.exports = require("../../../src/connectors/coinbaseWallet.ts");
15
+
16
+ unregister();
@@ -0,0 +1 @@
1
+ export * from "../../../dist/declarations/src/connectors/injected";
@@ -0,0 +1,200 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var providers = require('@ethersproject/providers');
6
+ var base = require('../../../dist/base-0167f1e7.cjs.dev.js');
7
+ var utils = require('ethers/lib/utils');
8
+ require('eventemitter3');
9
+
10
+ const getInjectedName = ethereum => {
11
+ if (!ethereum) return 'Injected';
12
+ if (ethereum.isMetaMask) return 'MetaMask';
13
+ if (ethereum.isCoinbaseWallet) return 'Coinbase Wallet';
14
+ if (ethereum.isFrame) return 'Frame';
15
+ if (ethereum.isTally) return 'Tally';
16
+ return 'Injected';
17
+ };
18
+
19
+ const shimKey = 'wagmi.shimDisconnect';
20
+ class InjectedConnector extends base.Connector {
21
+ id = 'injected';
22
+ ready = typeof window != 'undefined' && !!window.ethereum;
23
+ #provider;
24
+
25
+ constructor(config) {
26
+ // TODO(note): Should shimDisconnect be default truthy??
27
+ super({ ...config,
28
+ options: config?.options
29
+ });
30
+ let name = 'Injected';
31
+ if (typeof window !== 'undefined') name = getInjectedName(window.ethereum);
32
+ this.name = name;
33
+ }
34
+
35
+ async connect() {
36
+ try {
37
+ const provider = this.getProvider();
38
+ if (!provider) throw new base.ConnectorNotFoundError();
39
+
40
+ if (provider.on) {
41
+ provider.on('accountsChanged', this.onAccountsChanged);
42
+ provider.on('chainChanged', this.onChainChanged);
43
+ provider.on('disconnect', this.onDisconnect);
44
+ }
45
+
46
+ const account = await this.getAccount();
47
+ const id = await this.getChainId();
48
+ const unsupported = this.isChainUnsupported(id);
49
+ if (this.options?.shimDisconnect) typeof localStorage !== 'undefined' && localStorage.setItem(shimKey, 'true');
50
+ return {
51
+ account,
52
+ chain: {
53
+ id,
54
+ unsupported
55
+ },
56
+ provider
57
+ };
58
+ } catch (error) {
59
+ if (error.code === 4001) throw new base.UserRejectedRequestError();
60
+ throw error;
61
+ }
62
+ }
63
+
64
+ async disconnect() {
65
+ const provider = this.getProvider();
66
+ if (!provider?.removeListener) return;
67
+ provider.removeListener('accountsChanged', this.onAccountsChanged);
68
+ provider.removeListener('chainChanged', this.onChainChanged);
69
+ provider.removeListener('disconnect', this.onDisconnect);
70
+ if (this.options?.shimDisconnect) typeof localStorage !== 'undefined' && localStorage.removeItem(shimKey);
71
+ }
72
+
73
+ async getAccount() {
74
+ const provider = this.getProvider();
75
+ if (!provider) throw new base.ConnectorNotFoundError();
76
+ const accounts = await provider.request({
77
+ method: 'eth_requestAccounts'
78
+ }); // return checksum address
79
+
80
+ return utils.getAddress(accounts[0]);
81
+ }
82
+
83
+ async getChainId() {
84
+ const provider = this.getProvider();
85
+ if (!provider) throw new base.ConnectorNotFoundError();
86
+ return await provider.request({
87
+ method: 'eth_chainId'
88
+ }).then(base.normalizeChainId);
89
+ }
90
+
91
+ getProvider() {
92
+ if (typeof window !== 'undefined' && !!window.ethereum) this.#provider = window.ethereum;
93
+ return this.#provider;
94
+ }
95
+
96
+ async getSigner() {
97
+ const provider = this.getProvider();
98
+ const account = await this.getAccount();
99
+ return new providers.Web3Provider(provider).getSigner(account);
100
+ }
101
+
102
+ async isAuthorized() {
103
+ try {
104
+ if (this.options?.shimDisconnect && typeof localStorage !== 'undefined' && !localStorage.getItem(shimKey)) return false;
105
+ const provider = this.getProvider();
106
+ if (!provider) throw new base.ConnectorNotFoundError();
107
+ const accounts = await provider.request({
108
+ method: 'eth_accounts'
109
+ });
110
+ const account = accounts[0];
111
+ return !!account;
112
+ } catch {
113
+ return false;
114
+ }
115
+ }
116
+
117
+ async switchChain(chainId) {
118
+ const provider = this.getProvider();
119
+ if (!provider) throw new base.ConnectorNotFoundError();
120
+ const id = utils.hexValue(chainId);
121
+
122
+ try {
123
+ await provider.request({
124
+ method: 'wallet_switchEthereumChain',
125
+ params: [{
126
+ chainId: id
127
+ }]
128
+ });
129
+ const chains = [...this.chains, ...base.allChains];
130
+ return chains.find(x => x.id === chainId);
131
+ } catch (error) {
132
+ // Indicates chain is not added to MetaMask
133
+ if (error.code === 4902) {
134
+ try {
135
+ const chain = this.chains.find(x => x.id === chainId);
136
+ if (!chain) throw new base.ChainNotConfiguredError();
137
+ await provider.request({
138
+ method: 'wallet_addEthereumChain',
139
+ params: [{
140
+ chainId: id,
141
+ chainName: chain.name,
142
+ nativeCurrency: chain.nativeCurrency,
143
+ rpcUrls: chain.rpcUrls,
144
+ blockExplorerUrls: chain.blockExplorers?.map(x => x.url)
145
+ }]
146
+ });
147
+ } catch (addError) {
148
+ throw new base.AddChainError();
149
+ }
150
+ } else if (error.code === 4001) throw new base.UserRejectedRequestError();else throw new base.SwitchChainError();
151
+ }
152
+ }
153
+
154
+ async watchAsset({
155
+ address,
156
+ decimals = 18,
157
+ image,
158
+ symbol
159
+ }) {
160
+ const provider = this.getProvider();
161
+ if (!provider) throw new base.ConnectorNotFoundError();
162
+ await provider.request({
163
+ method: 'wallet_watchAsset',
164
+ params: {
165
+ type: 'ERC20',
166
+ options: {
167
+ address,
168
+ decimals,
169
+ image,
170
+ symbol
171
+ }
172
+ }
173
+ });
174
+ }
175
+
176
+ onAccountsChanged = accounts => {
177
+ if (accounts.length === 0) this.emit('disconnect');else this.emit('change', {
178
+ account: utils.getAddress(accounts[0])
179
+ });
180
+ };
181
+ onChainChanged = chainId => {
182
+ const id = base.normalizeChainId(chainId);
183
+ const unsupported = this.isChainUnsupported(id);
184
+ this.emit('change', {
185
+ chain: {
186
+ id,
187
+ unsupported
188
+ }
189
+ });
190
+ };
191
+ onDisconnect = err => {
192
+ // TODO: this is a hack, probs better way to deal
193
+ if (!err.message.includes('Attempting to connect.')) {
194
+ this.emit('disconnect');
195
+ if (this.options?.shimDisconnect) typeof localStorage !== 'undefined' && localStorage.removeItem(shimKey);
196
+ }
197
+ };
198
+ }
199
+
200
+ exports.InjectedConnector = InjectedConnector;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ if (process.env.NODE_ENV === "production") {
4
+ module.exports = require("./wagmi-core-connectors-injected.cjs.prod.js");
5
+ } else {
6
+ module.exports = require("./wagmi-core-connectors-injected.cjs.dev.js");
7
+ }
@@ -0,0 +1,200 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var providers = require('@ethersproject/providers');
6
+ var base = require('../../../dist/base-677c6ead.cjs.prod.js');
7
+ var utils = require('ethers/lib/utils');
8
+ require('eventemitter3');
9
+
10
+ const getInjectedName = ethereum => {
11
+ if (!ethereum) return 'Injected';
12
+ if (ethereum.isMetaMask) return 'MetaMask';
13
+ if (ethereum.isCoinbaseWallet) return 'Coinbase Wallet';
14
+ if (ethereum.isFrame) return 'Frame';
15
+ if (ethereum.isTally) return 'Tally';
16
+ return 'Injected';
17
+ };
18
+
19
+ const shimKey = 'wagmi.shimDisconnect';
20
+ class InjectedConnector extends base.Connector {
21
+ id = 'injected';
22
+ ready = typeof window != 'undefined' && !!window.ethereum;
23
+ #provider;
24
+
25
+ constructor(config) {
26
+ // TODO(note): Should shimDisconnect be default truthy??
27
+ super({ ...config,
28
+ options: config?.options
29
+ });
30
+ let name = 'Injected';
31
+ if (typeof window !== 'undefined') name = getInjectedName(window.ethereum);
32
+ this.name = name;
33
+ }
34
+
35
+ async connect() {
36
+ try {
37
+ const provider = this.getProvider();
38
+ if (!provider) throw new base.ConnectorNotFoundError();
39
+
40
+ if (provider.on) {
41
+ provider.on('accountsChanged', this.onAccountsChanged);
42
+ provider.on('chainChanged', this.onChainChanged);
43
+ provider.on('disconnect', this.onDisconnect);
44
+ }
45
+
46
+ const account = await this.getAccount();
47
+ const id = await this.getChainId();
48
+ const unsupported = this.isChainUnsupported(id);
49
+ if (this.options?.shimDisconnect) typeof localStorage !== 'undefined' && localStorage.setItem(shimKey, 'true');
50
+ return {
51
+ account,
52
+ chain: {
53
+ id,
54
+ unsupported
55
+ },
56
+ provider
57
+ };
58
+ } catch (error) {
59
+ if (error.code === 4001) throw new base.UserRejectedRequestError();
60
+ throw error;
61
+ }
62
+ }
63
+
64
+ async disconnect() {
65
+ const provider = this.getProvider();
66
+ if (!provider?.removeListener) return;
67
+ provider.removeListener('accountsChanged', this.onAccountsChanged);
68
+ provider.removeListener('chainChanged', this.onChainChanged);
69
+ provider.removeListener('disconnect', this.onDisconnect);
70
+ if (this.options?.shimDisconnect) typeof localStorage !== 'undefined' && localStorage.removeItem(shimKey);
71
+ }
72
+
73
+ async getAccount() {
74
+ const provider = this.getProvider();
75
+ if (!provider) throw new base.ConnectorNotFoundError();
76
+ const accounts = await provider.request({
77
+ method: 'eth_requestAccounts'
78
+ }); // return checksum address
79
+
80
+ return utils.getAddress(accounts[0]);
81
+ }
82
+
83
+ async getChainId() {
84
+ const provider = this.getProvider();
85
+ if (!provider) throw new base.ConnectorNotFoundError();
86
+ return await provider.request({
87
+ method: 'eth_chainId'
88
+ }).then(base.normalizeChainId);
89
+ }
90
+
91
+ getProvider() {
92
+ if (typeof window !== 'undefined' && !!window.ethereum) this.#provider = window.ethereum;
93
+ return this.#provider;
94
+ }
95
+
96
+ async getSigner() {
97
+ const provider = this.getProvider();
98
+ const account = await this.getAccount();
99
+ return new providers.Web3Provider(provider).getSigner(account);
100
+ }
101
+
102
+ async isAuthorized() {
103
+ try {
104
+ if (this.options?.shimDisconnect && typeof localStorage !== 'undefined' && !localStorage.getItem(shimKey)) return false;
105
+ const provider = this.getProvider();
106
+ if (!provider) throw new base.ConnectorNotFoundError();
107
+ const accounts = await provider.request({
108
+ method: 'eth_accounts'
109
+ });
110
+ const account = accounts[0];
111
+ return !!account;
112
+ } catch {
113
+ return false;
114
+ }
115
+ }
116
+
117
+ async switchChain(chainId) {
118
+ const provider = this.getProvider();
119
+ if (!provider) throw new base.ConnectorNotFoundError();
120
+ const id = utils.hexValue(chainId);
121
+
122
+ try {
123
+ await provider.request({
124
+ method: 'wallet_switchEthereumChain',
125
+ params: [{
126
+ chainId: id
127
+ }]
128
+ });
129
+ const chains = [...this.chains, ...base.allChains];
130
+ return chains.find(x => x.id === chainId);
131
+ } catch (error) {
132
+ // Indicates chain is not added to MetaMask
133
+ if (error.code === 4902) {
134
+ try {
135
+ const chain = this.chains.find(x => x.id === chainId);
136
+ if (!chain) throw new base.ChainNotConfiguredError();
137
+ await provider.request({
138
+ method: 'wallet_addEthereumChain',
139
+ params: [{
140
+ chainId: id,
141
+ chainName: chain.name,
142
+ nativeCurrency: chain.nativeCurrency,
143
+ rpcUrls: chain.rpcUrls,
144
+ blockExplorerUrls: chain.blockExplorers?.map(x => x.url)
145
+ }]
146
+ });
147
+ } catch (addError) {
148
+ throw new base.AddChainError();
149
+ }
150
+ } else if (error.code === 4001) throw new base.UserRejectedRequestError();else throw new base.SwitchChainError();
151
+ }
152
+ }
153
+
154
+ async watchAsset({
155
+ address,
156
+ decimals = 18,
157
+ image,
158
+ symbol
159
+ }) {
160
+ const provider = this.getProvider();
161
+ if (!provider) throw new base.ConnectorNotFoundError();
162
+ await provider.request({
163
+ method: 'wallet_watchAsset',
164
+ params: {
165
+ type: 'ERC20',
166
+ options: {
167
+ address,
168
+ decimals,
169
+ image,
170
+ symbol
171
+ }
172
+ }
173
+ });
174
+ }
175
+
176
+ onAccountsChanged = accounts => {
177
+ if (accounts.length === 0) this.emit('disconnect');else this.emit('change', {
178
+ account: utils.getAddress(accounts[0])
179
+ });
180
+ };
181
+ onChainChanged = chainId => {
182
+ const id = base.normalizeChainId(chainId);
183
+ const unsupported = this.isChainUnsupported(id);
184
+ this.emit('change', {
185
+ chain: {
186
+ id,
187
+ unsupported
188
+ }
189
+ });
190
+ };
191
+ onDisconnect = err => {
192
+ // TODO: this is a hack, probs better way to deal
193
+ if (!err.message.includes('Attempting to connect.')) {
194
+ this.emit('disconnect');
195
+ if (this.options?.shimDisconnect) typeof localStorage !== 'undefined' && localStorage.removeItem(shimKey);
196
+ }
197
+ };
198
+ }
199
+
200
+ exports.InjectedConnector = InjectedConnector;
@@ -0,0 +1,196 @@
1
+ import { Web3Provider } from '@ethersproject/providers';
2
+ import { C as Connector, g as ConnectorNotFoundError, U as UserRejectedRequestError, n as normalizeChainId, a as allChains, f as ChainNotConfiguredError, A as AddChainError, S as SwitchChainError } from '../../../dist/base-0de5bfca.esm.js';
3
+ import { getAddress, hexValue } from 'ethers/lib/utils';
4
+ import 'eventemitter3';
5
+
6
+ const getInjectedName = ethereum => {
7
+ if (!ethereum) return 'Injected';
8
+ if (ethereum.isMetaMask) return 'MetaMask';
9
+ if (ethereum.isCoinbaseWallet) return 'Coinbase Wallet';
10
+ if (ethereum.isFrame) return 'Frame';
11
+ if (ethereum.isTally) return 'Tally';
12
+ return 'Injected';
13
+ };
14
+
15
+ const shimKey = 'wagmi.shimDisconnect';
16
+ class InjectedConnector extends Connector {
17
+ id = 'injected';
18
+ ready = typeof window != 'undefined' && !!window.ethereum;
19
+ #provider;
20
+
21
+ constructor(config) {
22
+ // TODO(note): Should shimDisconnect be default truthy??
23
+ super({ ...config,
24
+ options: config?.options
25
+ });
26
+ let name = 'Injected';
27
+ if (typeof window !== 'undefined') name = getInjectedName(window.ethereum);
28
+ this.name = name;
29
+ }
30
+
31
+ async connect() {
32
+ try {
33
+ const provider = this.getProvider();
34
+ if (!provider) throw new ConnectorNotFoundError();
35
+
36
+ if (provider.on) {
37
+ provider.on('accountsChanged', this.onAccountsChanged);
38
+ provider.on('chainChanged', this.onChainChanged);
39
+ provider.on('disconnect', this.onDisconnect);
40
+ }
41
+
42
+ const account = await this.getAccount();
43
+ const id = await this.getChainId();
44
+ const unsupported = this.isChainUnsupported(id);
45
+ if (this.options?.shimDisconnect) typeof localStorage !== 'undefined' && localStorage.setItem(shimKey, 'true');
46
+ return {
47
+ account,
48
+ chain: {
49
+ id,
50
+ unsupported
51
+ },
52
+ provider
53
+ };
54
+ } catch (error) {
55
+ if (error.code === 4001) throw new UserRejectedRequestError();
56
+ throw error;
57
+ }
58
+ }
59
+
60
+ async disconnect() {
61
+ const provider = this.getProvider();
62
+ if (!provider?.removeListener) return;
63
+ provider.removeListener('accountsChanged', this.onAccountsChanged);
64
+ provider.removeListener('chainChanged', this.onChainChanged);
65
+ provider.removeListener('disconnect', this.onDisconnect);
66
+ if (this.options?.shimDisconnect) typeof localStorage !== 'undefined' && localStorage.removeItem(shimKey);
67
+ }
68
+
69
+ async getAccount() {
70
+ const provider = this.getProvider();
71
+ if (!provider) throw new ConnectorNotFoundError();
72
+ const accounts = await provider.request({
73
+ method: 'eth_requestAccounts'
74
+ }); // return checksum address
75
+
76
+ return getAddress(accounts[0]);
77
+ }
78
+
79
+ async getChainId() {
80
+ const provider = this.getProvider();
81
+ if (!provider) throw new ConnectorNotFoundError();
82
+ return await provider.request({
83
+ method: 'eth_chainId'
84
+ }).then(normalizeChainId);
85
+ }
86
+
87
+ getProvider() {
88
+ if (typeof window !== 'undefined' && !!window.ethereum) this.#provider = window.ethereum;
89
+ return this.#provider;
90
+ }
91
+
92
+ async getSigner() {
93
+ const provider = this.getProvider();
94
+ const account = await this.getAccount();
95
+ return new Web3Provider(provider).getSigner(account);
96
+ }
97
+
98
+ async isAuthorized() {
99
+ try {
100
+ if (this.options?.shimDisconnect && typeof localStorage !== 'undefined' && !localStorage.getItem(shimKey)) return false;
101
+ const provider = this.getProvider();
102
+ if (!provider) throw new ConnectorNotFoundError();
103
+ const accounts = await provider.request({
104
+ method: 'eth_accounts'
105
+ });
106
+ const account = accounts[0];
107
+ return !!account;
108
+ } catch {
109
+ return false;
110
+ }
111
+ }
112
+
113
+ async switchChain(chainId) {
114
+ const provider = this.getProvider();
115
+ if (!provider) throw new ConnectorNotFoundError();
116
+ const id = hexValue(chainId);
117
+
118
+ try {
119
+ await provider.request({
120
+ method: 'wallet_switchEthereumChain',
121
+ params: [{
122
+ chainId: id
123
+ }]
124
+ });
125
+ const chains = [...this.chains, ...allChains];
126
+ return chains.find(x => x.id === chainId);
127
+ } catch (error) {
128
+ // Indicates chain is not added to MetaMask
129
+ if (error.code === 4902) {
130
+ try {
131
+ const chain = this.chains.find(x => x.id === chainId);
132
+ if (!chain) throw new ChainNotConfiguredError();
133
+ await provider.request({
134
+ method: 'wallet_addEthereumChain',
135
+ params: [{
136
+ chainId: id,
137
+ chainName: chain.name,
138
+ nativeCurrency: chain.nativeCurrency,
139
+ rpcUrls: chain.rpcUrls,
140
+ blockExplorerUrls: chain.blockExplorers?.map(x => x.url)
141
+ }]
142
+ });
143
+ } catch (addError) {
144
+ throw new AddChainError();
145
+ }
146
+ } else if (error.code === 4001) throw new UserRejectedRequestError();else throw new SwitchChainError();
147
+ }
148
+ }
149
+
150
+ async watchAsset({
151
+ address,
152
+ decimals = 18,
153
+ image,
154
+ symbol
155
+ }) {
156
+ const provider = this.getProvider();
157
+ if (!provider) throw new ConnectorNotFoundError();
158
+ await provider.request({
159
+ method: 'wallet_watchAsset',
160
+ params: {
161
+ type: 'ERC20',
162
+ options: {
163
+ address,
164
+ decimals,
165
+ image,
166
+ symbol
167
+ }
168
+ }
169
+ });
170
+ }
171
+
172
+ onAccountsChanged = accounts => {
173
+ if (accounts.length === 0) this.emit('disconnect');else this.emit('change', {
174
+ account: getAddress(accounts[0])
175
+ });
176
+ };
177
+ onChainChanged = chainId => {
178
+ const id = normalizeChainId(chainId);
179
+ const unsupported = this.isChainUnsupported(id);
180
+ this.emit('change', {
181
+ chain: {
182
+ id,
183
+ unsupported
184
+ }
185
+ });
186
+ };
187
+ onDisconnect = err => {
188
+ // TODO: this is a hack, probs better way to deal
189
+ if (!err.message.includes('Attempting to connect.')) {
190
+ this.emit('disconnect');
191
+ if (this.options?.shimDisconnect) typeof localStorage !== 'undefined' && localStorage.removeItem(shimKey);
192
+ }
193
+ };
194
+ }
195
+
196
+ export { InjectedConnector };