@wagmi/connectors 1.0.0-next.4 → 1.0.0-next.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.
@@ -0,0 +1,202 @@
1
+ import {
2
+ normalizeChainId
3
+ } from "../chunk-OQILYQDO.js";
4
+ import {
5
+ Connector,
6
+ __privateAdd,
7
+ __privateGet,
8
+ __privateMethod,
9
+ __privateSet,
10
+ __publicField
11
+ } from "../chunk-QYMCVNHT.js";
12
+
13
+ // src/mock/connector.ts
14
+ import { getAddress as getAddress2 } from "viem";
15
+
16
+ // src/mock/provider.ts
17
+ import { default as EventEmitter } from "eventemitter3";
18
+ import { UserRejectedRequestError, getAddress } from "viem";
19
+ var _options, _walletClient;
20
+ var MockProvider = class {
21
+ constructor(options) {
22
+ __publicField(this, "events", new EventEmitter());
23
+ __publicField(this, "chainId");
24
+ __privateAdd(this, _options, void 0);
25
+ __privateAdd(this, _walletClient, void 0);
26
+ this.chainId = options.chainId;
27
+ __privateSet(this, _options, options);
28
+ }
29
+ async enable() {
30
+ if (__privateGet(this, _options).flags?.failConnect)
31
+ throw new UserRejectedRequestError(new Error("Failed to connect."));
32
+ if (!__privateGet(this, _walletClient))
33
+ __privateSet(this, _walletClient, __privateGet(this, _options).walletClient);
34
+ const address = __privateGet(this, _walletClient).account.address;
35
+ this.events.emit("accountsChanged", [address]);
36
+ return [address];
37
+ }
38
+ async disconnect() {
39
+ this.events.emit("disconnect");
40
+ __privateSet(this, _walletClient, void 0);
41
+ }
42
+ async getAccounts() {
43
+ const address = __privateGet(this, _walletClient)?.account.address;
44
+ if (!address)
45
+ return [];
46
+ return [getAddress(address)];
47
+ }
48
+ getWalletClient() {
49
+ const walletClient = __privateGet(this, _walletClient);
50
+ if (!walletClient)
51
+ throw new Error("walletClient not found");
52
+ return walletClient;
53
+ }
54
+ async switchChain(chainId) {
55
+ if (__privateGet(this, _options).flags?.failSwitchChain)
56
+ throw new UserRejectedRequestError(new Error("Failed to switch chain."));
57
+ __privateGet(this, _options).chainId = chainId;
58
+ this.chainId = chainId;
59
+ this.events.emit("chainChanged", chainId);
60
+ }
61
+ async switchWalletClient(walletClient) {
62
+ const address = walletClient.account.address;
63
+ __privateSet(this, _walletClient, walletClient);
64
+ this.events.emit("accountsChanged", [address]);
65
+ }
66
+ async watchAsset(_asset) {
67
+ return true;
68
+ }
69
+ async request({ method, params }) {
70
+ return __privateGet(this, _walletClient)?.transport.request({ method, params });
71
+ }
72
+ on(event, listener) {
73
+ this.events.on(event, listener);
74
+ return this;
75
+ }
76
+ removeListener(event, listener) {
77
+ this.events.removeListener(event, listener);
78
+ return this;
79
+ }
80
+ toJSON() {
81
+ return "<MockProvider>";
82
+ }
83
+ };
84
+ _options = new WeakMap();
85
+ _walletClient = new WeakMap();
86
+
87
+ // src/mock/connector.ts
88
+ var _provider, _switchChain, switchChain_fn;
89
+ var MockConnector = class extends Connector {
90
+ constructor({
91
+ chains,
92
+ options
93
+ }) {
94
+ super({
95
+ chains,
96
+ options: {
97
+ ...options,
98
+ chainId: options.chainId ?? chains?.[0]?.id
99
+ }
100
+ });
101
+ __privateAdd(this, _switchChain);
102
+ __publicField(this, "id", "mock");
103
+ __publicField(this, "name", "Mock");
104
+ __publicField(this, "ready", true);
105
+ __privateAdd(this, _provider, void 0);
106
+ __publicField(this, "onAccountsChanged", (accounts) => {
107
+ if (accounts.length === 0)
108
+ this.emit("disconnect");
109
+ else
110
+ this.emit("change", { account: getAddress2(accounts[0]) });
111
+ });
112
+ __publicField(this, "onChainChanged", (chainId) => {
113
+ const id = normalizeChainId(chainId);
114
+ const unsupported = this.isChainUnsupported(id);
115
+ this.emit("change", { chain: { id, unsupported } });
116
+ });
117
+ __publicField(this, "onDisconnect", () => {
118
+ this.emit("disconnect");
119
+ });
120
+ }
121
+ async connect({ chainId } = {}) {
122
+ const provider = await this.getProvider({ chainId });
123
+ provider.on("accountsChanged", this.onAccountsChanged);
124
+ provider.on("chainChanged", this.onChainChanged);
125
+ provider.on("disconnect", this.onDisconnect);
126
+ this.emit("message", { type: "connecting" });
127
+ const accounts = await provider.enable();
128
+ const account = getAddress2(accounts[0]);
129
+ const id = normalizeChainId(provider.chainId);
130
+ const unsupported = this.isChainUnsupported(id);
131
+ const data = { account, chain: { id, unsupported }, provider };
132
+ if (!this.options.flags?.noSwitchChain)
133
+ this.switchChain = __privateMethod(this, _switchChain, switchChain_fn);
134
+ return new Promise(
135
+ (res) => setTimeout(() => res(data), 100)
136
+ );
137
+ }
138
+ async disconnect() {
139
+ const provider = await this.getProvider();
140
+ await provider.disconnect();
141
+ provider.removeListener("accountsChanged", this.onAccountsChanged);
142
+ provider.removeListener("chainChanged", this.onChainChanged);
143
+ provider.removeListener("disconnect", this.onDisconnect);
144
+ }
145
+ async getAccount() {
146
+ const provider = await this.getProvider();
147
+ const accounts = await provider.getAccounts();
148
+ const account = accounts[0];
149
+ if (!account)
150
+ throw new Error("Failed to get account");
151
+ return getAddress2(account);
152
+ }
153
+ async getChainId() {
154
+ const provider = await this.getProvider();
155
+ return normalizeChainId(provider.chainId);
156
+ }
157
+ async getProvider({ chainId } = {}) {
158
+ if (!__privateGet(this, _provider) || chainId)
159
+ __privateSet(this, _provider, new MockProvider({
160
+ ...this.options,
161
+ chainId: chainId ?? this.options.chainId ?? this.chains[0].id
162
+ }));
163
+ return __privateGet(this, _provider);
164
+ }
165
+ async getWalletClient() {
166
+ const provider = await this.getProvider();
167
+ return provider.getWalletClient();
168
+ }
169
+ async isAuthorized() {
170
+ try {
171
+ const provider = await this.getProvider();
172
+ const account = await provider.getAccounts();
173
+ return this.options.flags?.isAuthorized ?? !!account;
174
+ } catch {
175
+ return false;
176
+ }
177
+ }
178
+ async watchAsset(asset) {
179
+ const provider = await this.getProvider();
180
+ return provider.watchAsset(asset);
181
+ }
182
+ toJSON() {
183
+ return "<MockConnector>";
184
+ }
185
+ };
186
+ _provider = new WeakMap();
187
+ _switchChain = new WeakSet();
188
+ switchChain_fn = async function(chainId) {
189
+ const provider = await this.getProvider();
190
+ await provider.switchChain(chainId);
191
+ return this.chains.find((x) => x.id === chainId) ?? {
192
+ id: chainId,
193
+ name: `Chain ${chainId}`,
194
+ network: `${chainId}`,
195
+ nativeCurrency: { name: "Ether", decimals: 18, symbol: "ETH" },
196
+ rpcUrls: { default: { http: [""] }, public: { http: [""] } }
197
+ };
198
+ };
199
+ export {
200
+ MockConnector,
201
+ MockProvider
202
+ };