@wagmi/core 0.3.8 → 0.4.0-test.2
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.
- package/chains/dist/wagmi-core-chains.cjs.dev.js +1 -1
- package/chains/dist/wagmi-core-chains.cjs.prod.js +1 -1
- package/chains/dist/wagmi-core-chains.esm.js +1 -1
- package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.cjs.dev.js +2 -3
- package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.cjs.prod.js +2 -3
- package/connectors/coinbaseWallet/dist/wagmi-core-connectors-coinbaseWallet.esm.js +2 -3
- package/connectors/injected/dist/wagmi-core-connectors-injected.cjs.d.ts +1 -0
- package/connectors/injected/dist/wagmi-core-connectors-injected.cjs.dev.js +200 -0
- package/connectors/injected/dist/wagmi-core-connectors-injected.cjs.js +7 -0
- package/connectors/injected/dist/wagmi-core-connectors-injected.cjs.prod.js +200 -0
- package/connectors/injected/dist/wagmi-core-connectors-injected.esm.js +196 -0
- package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.cjs.dev.js +3 -4
- package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.cjs.prod.js +3 -4
- package/connectors/metaMask/dist/wagmi-core-connectors-metaMask.esm.js +3 -4
- package/connectors/mock/dist/wagmi-core-connectors-mock.cjs.dev.js +3 -8
- package/connectors/mock/dist/wagmi-core-connectors-mock.cjs.prod.js +3 -8
- package/connectors/mock/dist/wagmi-core-connectors-mock.esm.js +2 -3
- package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.cjs.dev.js +3 -4
- package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.cjs.prod.js +3 -4
- package/connectors/walletConnect/dist/wagmi-core-connectors-walletConnect.esm.js +3 -4
- package/connectors/walletLink/dist/wagmi-core-connectors-walletLink.cjs.d.ts +11 -0
- package/connectors/walletLink/dist/wagmi-core-connectors-walletLink.cjs.js +16 -0
- package/dist/base-7f682723.cjs.prod.js +623 -0
- package/dist/base-ae774f26.cjs.dev.js +623 -0
- package/dist/base-c546f171.esm.js +603 -0
- package/dist/{chains-b4a51a84.cjs.dev.js → chains-7e369752.cjs.dev.js} +6 -6
- package/dist/{chains-7204b2dd.cjs.prod.js → chains-d045862c.cjs.prod.js} +6 -6
- package/dist/{chains-42648bc5.esm.js → chains-fa406563.esm.js} +6 -6
- package/dist/{client-a774f14a.cjs.prod.js → client-42cf11b7.cjs.dev.js} +3 -2
- package/dist/{client-e01209d5.esm.js → client-60ee0298.esm.js} +3 -2
- package/dist/{client-d5a20a13.cjs.dev.js → client-ed3aaa6a.cjs.prod.js} +3 -2
- package/dist/declarations/src/client.d.ts +1 -1
- package/dist/declarations/src/connectors/base.d.ts +1 -1
- package/dist/declarations/src/connectors/mock/provider.d.ts +1 -2
- package/dist/declarations/src/types/index.d.ts +1 -0
- package/dist/declarations/src/utils/eventemitter.d.ts +135 -0
- package/dist/wagmi-core.cjs.dev.js +4 -5
- package/dist/wagmi-core.cjs.prod.js +4 -5
- package/dist/wagmi-core.esm.js +7 -8
- package/package.json +1 -2
- package/dist/base-4c248b82.esm.js +0 -267
- package/dist/base-9fa7951e.cjs.dev.js +0 -290
- package/dist/base-b7c3f31e.cjs.prod.js +0 -290
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { g as arbitrum, h as arbitrumRinkeby, i as foundry, j as goerli, k as hardhat, l as kovan, m as localhost, n as mainnet, o as optimism, p as optimismKovan, q as polygon, r as polygonMumbai, s as rinkeby, t as ropsten } from '../../dist/chains-
|
|
1
|
+
export { g as arbitrum, h as arbitrumRinkeby, i as foundry, j as goerli, k as hardhat, l as kovan, m as localhost, n as mainnet, o as optimism, p as optimismKovan, q as polygon, r as polygonMumbai, s as rinkeby, t as ropsten } from '../../dist/chains-fa406563.esm.js';
|
|
2
2
|
import '../../dist/rpcs-7cfbd91c.esm.js';
|
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var base = require('../../../dist/base-
|
|
5
|
+
var base = require('../../../dist/base-ae774f26.cjs.dev.js');
|
|
6
6
|
var ethers = require('ethers');
|
|
7
7
|
var utils = require('ethers/lib/utils');
|
|
8
|
-
var chains = require('../../../dist/chains-
|
|
9
|
-
require('eventemitter3');
|
|
8
|
+
var chains = require('../../../dist/chains-7e369752.cjs.dev.js');
|
|
10
9
|
require('../../../dist/rpcs-9c4eb960.cjs.dev.js');
|
|
11
10
|
|
|
12
11
|
function _interopNamespace(e) {
|
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var base = require('../../../dist/base-
|
|
5
|
+
var base = require('../../../dist/base-7f682723.cjs.prod.js');
|
|
6
6
|
var ethers = require('ethers');
|
|
7
7
|
var utils = require('ethers/lib/utils');
|
|
8
|
-
var chains = require('../../../dist/chains-
|
|
9
|
-
require('eventemitter3');
|
|
8
|
+
var chains = require('../../../dist/chains-d045862c.cjs.prod.js');
|
|
10
9
|
require('../../../dist/rpcs-b3c52116.cjs.prod.js');
|
|
11
10
|
|
|
12
11
|
function _interopNamespace(e) {
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { b as Connector, _ as _classPrivateMethodInitSpec, f as _defineProperty, g as _classPrivateFieldInitSpec, n as normalizeChainId, U as UserRejectedRequestError, h as _classPrivateFieldGet, i as _classPrivateFieldSet, c as ChainNotConfiguredError, j as _classPrivateMethodGet, A as AddChainError, e as SwitchChainError } from '../../../dist/base-
|
|
1
|
+
import { b as Connector, _ as _classPrivateMethodInitSpec, f as _defineProperty, g as _classPrivateFieldInitSpec, n as normalizeChainId, U as UserRejectedRequestError, h as _classPrivateFieldGet, i as _classPrivateFieldSet, c as ChainNotConfiguredError, j as _classPrivateMethodGet, A as AddChainError, e as SwitchChainError } from '../../../dist/base-c546f171.esm.js';
|
|
2
2
|
import { providers } from 'ethers';
|
|
3
3
|
import { getAddress, hexValue } from 'ethers/lib/utils';
|
|
4
|
-
import { a as allChains } from '../../../dist/chains-
|
|
5
|
-
import 'eventemitter3';
|
|
4
|
+
import { a as allChains } from '../../../dist/chains-fa406563.esm.js';
|
|
6
5
|
import '../../../dist/rpcs-7cfbd91c.esm.js';
|
|
7
6
|
|
|
8
7
|
var _client = /*#__PURE__*/new WeakMap();
|
|
@@ -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,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 };
|
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var base = require('../../../dist/base-
|
|
6
|
-
var client = require('../../../dist/client-
|
|
7
|
-
require('
|
|
8
|
-
require('../../../dist/chains-b4a51a84.cjs.dev.js');
|
|
5
|
+
var base = require('../../../dist/base-ae774f26.cjs.dev.js');
|
|
6
|
+
var client = require('../../../dist/client-42cf11b7.cjs.dev.js');
|
|
7
|
+
require('../../../dist/chains-7e369752.cjs.dev.js');
|
|
9
8
|
require('../../../dist/rpcs-9c4eb960.cjs.dev.js');
|
|
10
9
|
require('ethers');
|
|
11
10
|
require('zustand/vanilla');
|