@dynamic-labs/wagmi-connector 0.0.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.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # wagmi-connector
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Running unit tests
6
+
7
+ Run `nx test wagmi-connector` to execute the unit tests via [Jest](https://jestjs.io).
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { DynamicWagmiConnector } from './lib/DynamicWagmiConnector';
package/index.esm.js ADDED
@@ -0,0 +1,250 @@
1
+ import React, { useRef, useEffect } from 'react';
2
+ import { Connector as Connector$1, useConnect, useDisconnect } from 'wagmi';
3
+ import { getNetwork, useDynamicContext } from '@dynamic-labs/sdk-react';
4
+ import { getAddress, hexValue } from 'ethers/lib/utils.js';
5
+
6
+ /*! *****************************************************************************
7
+ Copyright (c) Microsoft Corporation.
8
+
9
+ Permission to use, copy, modify, and/or distribute this software for any
10
+ purpose with or without fee is hereby granted.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
+ PERFORMANCE OF THIS SOFTWARE.
19
+ ***************************************************************************** */
20
+
21
+ function __awaiter(thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ }
30
+
31
+ class Connector extends Connector$1 {
32
+ constructor({
33
+ handleLogOut,
34
+ walletConnector
35
+ }) {
36
+ super({
37
+ options: undefined
38
+ }); // wagmi properties
39
+
40
+ this.id = 'dynamic';
41
+ this.name = 'Dynamic'; // also a wagmi prop, but is only used for auto-connect
42
+ // which we have implemented ourselves, so this value
43
+ // doesn't matter
44
+
45
+ this.ready = false;
46
+
47
+ this.onAccountsChanged = accounts => __awaiter(this, void 0, void 0, function* () {
48
+ this.emit('change', {
49
+ account: getAddress(accounts[0])
50
+ });
51
+ });
52
+
53
+ this.onChainChanged = chain => __awaiter(this, void 0, void 0, function* () {
54
+ this.emit('change', {
55
+ chain: {
56
+ id: Number(chain),
57
+ unsupported: false
58
+ }
59
+ });
60
+ });
61
+
62
+ this.onDisconnect = () => __awaiter(this, void 0, void 0, function* () {});
63
+
64
+ this.handleLogOut = handleLogOut;
65
+ this.walletConnector = walletConnector;
66
+ this.setupEventListeners();
67
+ }
68
+
69
+ setupEventListeners() {
70
+ this.walletConnector.setupEventListeners({
71
+ onAccountChange: this.onAccountsChanged,
72
+ onChainChange: this.onChainChanged,
73
+ onDisconnect: this.onDisconnect
74
+ });
75
+ }
76
+
77
+ connect(config) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ if (!this.walletConnector) {
80
+ throw new Error('WalletConnector is not defined');
81
+ }
82
+
83
+ const account = yield this.getAccount();
84
+ this.setupEventListeners();
85
+ this.emit('message', {
86
+ type: 'connecting'
87
+ });
88
+ const web3Provider = yield this.getProvider();
89
+ const {
90
+ provider
91
+ } = web3Provider;
92
+ return {
93
+ account,
94
+ chain: {
95
+ id: yield this.getChainId(),
96
+ unsupported: false
97
+ },
98
+ provider
99
+ };
100
+ });
101
+ }
102
+
103
+ disconnect() {
104
+ var _a, _b, _c;
105
+
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ const web3Provider = yield this.getProvider();
108
+ const {
109
+ provider
110
+ } = web3Provider;
111
+ provider.removeListener('accountsChanged', this.onAccountsChanged);
112
+ provider.removeListener('chainChanged', this.onChainChanged);
113
+ provider.removeListener('disconnect', this.onDisconnect);
114
+ yield (_b = (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.endSession) === null || _b === void 0 ? void 0 : _b.call(_a);
115
+ (_c = this.handleLogOut) === null || _c === void 0 ? void 0 : _c.call(this);
116
+ });
117
+ }
118
+
119
+ getAccount() {
120
+ var _a;
121
+
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ const address = yield (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.fetchPublicAddress();
124
+
125
+ if (!address) {
126
+ throw new Error('Not connected');
127
+ }
128
+
129
+ return getAddress(address);
130
+ });
131
+ }
132
+
133
+ getChainId() {
134
+ return __awaiter(this, void 0, void 0, function* () {
135
+ if (!this.walletConnector) {
136
+ throw new Error('WalletConnector is not defined');
137
+ }
138
+
139
+ const network = yield getNetwork(this.walletConnector);
140
+
141
+ if (!network) {
142
+ throw new Error('Network is not defined');
143
+ }
144
+
145
+ return network;
146
+ });
147
+ }
148
+
149
+ getProvider(config) {
150
+ var _a;
151
+
152
+ return __awaiter(this, void 0, void 0, function* () {
153
+ return (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.getWeb3Provider();
154
+ });
155
+ }
156
+
157
+ getSigner(config) {
158
+ var _a;
159
+
160
+ return __awaiter(this, void 0, void 0, function* () {
161
+ return (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.getSigner();
162
+ });
163
+ }
164
+
165
+ isAuthorized() {
166
+ var _a, _b;
167
+
168
+ return __awaiter(this, void 0, void 0, function* () {
169
+ const accounts = (_b = yield (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.getConnectedAccounts()) !== null && _b !== void 0 ? _b : [];
170
+ return accounts.length > 0;
171
+ });
172
+ }
173
+
174
+ switchChain(chainId) {
175
+ var _a, _b;
176
+
177
+ return __awaiter(this, void 0, void 0, function* () {
178
+ yield (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.switchNetwork({
179
+ networkChainId: chainId
180
+ });
181
+ const id = hexValue(chainId);
182
+ return (_b = this.chains.find(x => x.id === chainId)) !== null && _b !== void 0 ? _b : {
183
+ id: chainId,
184
+ name: `Chain ${id}`,
185
+ network: `${id}`,
186
+ rpcUrls: {
187
+ default: ''
188
+ }
189
+ };
190
+ });
191
+ }
192
+
193
+ watchAsset(asset) {
194
+ throw new Error('Method not implemented.');
195
+ }
196
+
197
+ }
198
+
199
+ const DynamicWagmiConnector = ({
200
+ children
201
+ }) => {
202
+ const {
203
+ walletConnector,
204
+ handleLogOut
205
+ } = useDynamicContext();
206
+ const {
207
+ connect,
208
+ status
209
+ } = useConnect();
210
+ const {
211
+ disconnect
212
+ } = useDisconnect(); // storing as a singleton so we can re-setup event listeners on the same instance
213
+
214
+ const dynamicWagmiConnector = useRef();
215
+
216
+ if (walletConnector) {
217
+ // after connection is successful, re-setup event listeners after a short delay
218
+ // so that they are not cleared by the teardown in useWalletEventListeners
219
+ if (status === 'success') {
220
+ setTimeout(() => {
221
+ var _a;
222
+
223
+ (_a = dynamicWagmiConnector.current) === null || _a === void 0 ? void 0 : _a.setupEventListeners();
224
+ }, 100);
225
+ }
226
+ }
227
+
228
+ useEffect(() => {
229
+ if (!walletConnector) {
230
+ dynamicWagmiConnector.current = undefined;
231
+ disconnect();
232
+ } else if (!dynamicWagmiConnector.current) {
233
+ dynamicWagmiConnector.current = new Connector({
234
+ handleLogOut,
235
+ walletConnector
236
+ });
237
+ connect({
238
+ connector: dynamicWagmiConnector.current
239
+ });
240
+ }
241
+ }, [connect, disconnect, handleLogOut, status, walletConnector]); // use React.createElement to prevent bunding react/jsx-runtime,
242
+ // which is not compatible when bundling apps using React 17
243
+ // eslint-disable-next-line react/no-children-prop
244
+
245
+ return /*#__PURE__*/React.createElement(React.Fragment, {
246
+ children
247
+ });
248
+ };
249
+
250
+ export { DynamicWagmiConnector };
@@ -0,0 +1,38 @@
1
+ import { ethers } from 'ethers';
2
+ import { Address, Connector as BaseWagmiConnector, ConnectorData, Chain } from 'wagmi';
3
+ import { WalletConnector } from '@dynamic-labs/sdk-react';
4
+ export declare class Connector extends BaseWagmiConnector<ethers.providers.Web3Provider, undefined, ethers.providers.JsonRpcSigner> {
5
+ handleLogOut: () => void;
6
+ walletConnector: WalletConnector;
7
+ id: string;
8
+ name: string;
9
+ ready: boolean;
10
+ constructor({ handleLogOut, walletConnector, }: {
11
+ handleLogOut: () => void;
12
+ walletConnector: WalletConnector;
13
+ });
14
+ setupEventListeners(): void;
15
+ connect(config?: {
16
+ chainId?: number;
17
+ }): Promise<Required<ConnectorData>>;
18
+ disconnect(): Promise<void>;
19
+ getAccount(): Promise<Address>;
20
+ getChainId(): Promise<number>;
21
+ getProvider(config?: {
22
+ chainId?: number;
23
+ } | undefined): Promise<ethers.providers.Web3Provider>;
24
+ getSigner(config?: {
25
+ chainId?: number;
26
+ } | undefined): Promise<ethers.providers.JsonRpcSigner>;
27
+ isAuthorized(): Promise<boolean>;
28
+ switchChain?(chainId: number): Promise<Chain>;
29
+ watchAsset?(asset: {
30
+ address: string;
31
+ decimals?: number;
32
+ image?: string;
33
+ symbol: string;
34
+ }): Promise<boolean>;
35
+ protected onAccountsChanged: (accounts: string[]) => Promise<void>;
36
+ protected onChainChanged: (chain: number | string) => Promise<void>;
37
+ protected onDisconnect: () => Promise<void>;
38
+ }
@@ -0,0 +1,6 @@
1
+ import React, { ReactNode } from 'react';
2
+ export declare const DynamicWagmiConnector: ({ children, }: {
3
+ children: ReactNode;
4
+ }) => React.FunctionComponentElement<{
5
+ children: React.ReactNode;
6
+ }>;
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@dynamic-labs/wagmi-connector",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "main": "./index.esm.js",
6
+ "typings": "./index.d.ts",
7
+ "dependencies": {},
8
+ "peerDependencies": {
9
+ "ethers": "^5.6.9",
10
+ "wagmi": "^0.8.10",
11
+ "@dynamic-labs/sdk-react": "0.13.56",
12
+ "react": "^17.0.2 || ^18.0.0"
13
+ }
14
+ }