@dynamic-labs/wagmi-connector 3.0.0-alpha.3 → 3.0.0-alpha.30

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.
@@ -3,18 +3,18 @@ import { __awaiter } from '../../_virtual/_tslib.js';
3
3
  import { createConnector } from '@wagmi/core';
4
4
  import { getAddress, createWalletClient, custom } from 'viem';
5
5
 
6
- const getCreateConnectorFn = (parameters) => createConnector((config) => ({
6
+ const getCreateConnectorFn = ({ connectorId, wallet, preventWagmiSyncFromCallingLogout, handleLogOut, }) => createConnector((config) => ({
7
7
  connect() {
8
8
  return __awaiter(this, void 0, void 0, function* () {
9
- if (!parameters.walletConnector) {
9
+ if (!wallet.connector) {
10
10
  throw new Error('WalletConnector is not defined');
11
11
  }
12
- parameters.walletConnector.on('accountChange', ({ accounts }) => {
12
+ wallet.connector.on('accountChange', ({ accounts }) => {
13
13
  const handler = this.onAccountsChanged.bind(this);
14
14
  handler(accounts);
15
15
  });
16
- parameters.walletConnector.on('chainChange', ({ chain }) => this.onChainChanged(chain));
17
- parameters.walletConnector.on('disconnect', this.onDisconnect.bind(this));
16
+ wallet.connector.on('chainChange', ({ chain }) => this.onChainChanged(chain));
17
+ wallet.connector.on('disconnect', this.onDisconnect.bind(this));
18
18
  const accounts = yield this.getAccounts();
19
19
  return {
20
20
  accounts,
@@ -24,38 +24,30 @@ const getCreateConnectorFn = (parameters) => createConnector((config) => ({
24
24
  },
25
25
  disconnect() {
26
26
  return __awaiter(this, void 0, void 0, function* () {
27
- if (!parameters.preventWagmiSyncFromCallingLogout.current) {
28
- yield parameters.walletConnector.endSession();
29
- yield parameters.handleLogOut();
27
+ if (!preventWagmiSyncFromCallingLogout.current) {
28
+ yield wallet.connector.endSession();
29
+ yield handleLogOut();
30
30
  }
31
- parameters.walletConnector.off('accountChange');
32
- parameters.walletConnector.off('chainChange');
33
- parameters.walletConnector.off('disconnect');
31
+ wallet.connector.off('accountChange');
32
+ wallet.connector.off('chainChange');
33
+ wallet.connector.off('disconnect');
34
34
  /**
35
35
  * In our own Dynamic Wagmi sync component, we will flip the flag to true before calling disconnect,
36
36
  * and here it automatically flips back to false afterwards. This will prevent the Connector from
37
37
  * calling handleLogOut when we are the ones calling disconnect, but will preserve the existing behavior
38
38
  * for customers who use useDisconnect directly.
39
39
  */
40
- parameters.preventWagmiSyncFromCallingLogout.current = false;
40
+ preventWagmiSyncFromCallingLogout.current = false;
41
41
  });
42
42
  },
43
43
  getAccounts() {
44
44
  return __awaiter(this, void 0, void 0, function* () {
45
- var _a;
46
- const address = yield ((_a = parameters.walletConnector) === null || _a === void 0 ? void 0 : _a.getAddress());
47
- if (!address) {
48
- throw new Error('Not connected');
49
- }
50
- return [getAddress(address)];
45
+ return [getAddress(wallet.address)];
51
46
  });
52
47
  },
53
48
  getChainId() {
54
49
  return __awaiter(this, void 0, void 0, function* () {
55
- if (!parameters.walletConnector) {
56
- throw new Error('WalletConnector is not defined');
57
- }
58
- const network = yield parameters.walletConnector.getNetwork();
50
+ const network = yield wallet.getNetwork();
59
51
  if (!network) {
60
52
  throw new Error('Network is not defined');
61
53
  }
@@ -65,7 +57,7 @@ const getCreateConnectorFn = (parameters) => createConnector((config) => ({
65
57
  getClient(args) {
66
58
  return __awaiter(this, void 0, void 0, function* () {
67
59
  var _a, _b;
68
- const signer = yield parameters.walletConnector.getSigner();
60
+ const signer = yield wallet.getSigner();
69
61
  if (signer.account && signer.chain) {
70
62
  return signer;
71
63
  }
@@ -79,15 +71,14 @@ const getCreateConnectorFn = (parameters) => createConnector((config) => ({
79
71
  },
80
72
  getProvider() {
81
73
  return __awaiter(this, void 0, void 0, function* () {
82
- var _a;
83
- return (_a = parameters.walletConnector) === null || _a === void 0 ? void 0 : _a.getWalletClient();
74
+ return wallet.getWalletClient();
84
75
  });
85
76
  },
86
- id: parameters.connectorId,
77
+ id: connectorId,
87
78
  isAuthorized() {
88
79
  return __awaiter(this, void 0, void 0, function* () {
89
80
  var _a, _b;
90
- const accounts = (_b = (yield ((_a = parameters.walletConnector) === null || _a === void 0 ? void 0 : _a.getConnectedAccounts()))) !== null && _b !== void 0 ? _b : [];
81
+ const accounts = (_b = (yield ((_a = wallet.connector) === null || _a === void 0 ? void 0 : _a.getConnectedAccounts()))) !== null && _b !== void 0 ? _b : [];
91
82
  return accounts.length > 0;
92
83
  });
93
84
  },
@@ -105,14 +96,11 @@ const getCreateConnectorFn = (parameters) => createConnector((config) => ({
105
96
  onDisconnect() { },
106
97
  switchChain(_a) {
107
98
  return __awaiter(this, arguments, void 0, function* ({ chainId }) {
108
- var _b;
109
99
  const chain = config.chains.find((x) => x.id === chainId);
110
100
  if (!chain) {
111
101
  throw new Error(`Chain ${chainId} is not supported`);
112
102
  }
113
- yield ((_b = parameters.walletConnector) === null || _b === void 0 ? void 0 : _b.switchNetwork({
114
- networkChainId: chainId,
115
- }));
103
+ yield wallet.switchNetwork(chainId);
116
104
  return chain;
117
105
  });
118
106
  },
@@ -15,7 +15,7 @@ const DynamicWagmiConnectorInner = ({ suppressChainMismatchError = false, childr
15
15
  const { handleLogOut, primaryWallet } = useDynamicContext();
16
16
  const wallets = useUserWallets();
17
17
  useChainMismatchLogger({ throwOnMismatch: !suppressChainMismatchError });
18
- const evmWallet = useFindEvmWallet(primaryWallet, wallets);
18
+ const evmWallet = useFindEvmWallet(primaryWallet);
19
19
  const getConnectorId = useConnectorId();
20
20
  /**
21
21
  * This will prevent the wagmi Connector from calling handleLogOut when we are the ones calling disconnect
@@ -31,7 +31,7 @@ const DynamicWagmiConnectorInner = ({ suppressChainMismatchError = false, childr
31
31
  connectorId,
32
32
  handleLogOut,
33
33
  preventWagmiSyncFromCallingLogout,
34
- walletConnector: evmWallet.connector,
34
+ wallet: evmWallet,
35
35
  }));
36
36
  }, [config._internal.connectors, getConnectorId, handleLogOut, evmWallet]);
37
37
  /**
@@ -1,2 +1,2 @@
1
1
  import { Wallet } from '@dynamic-labs/sdk-react-core';
2
- export declare const useFindEvmWallet: (primaryWallet: Wallet | null, wallets: Wallet[]) => Wallet | undefined;
2
+ export declare const useFindEvmWallet: (primaryWallet: Wallet | null) => Wallet | undefined;
@@ -1,22 +1,26 @@
1
1
  'use client'
2
+ import { __awaiter } from '../../../../_virtual/_tslib.js';
3
+ import { useState, useEffect } from 'react';
4
+
2
5
  const isEvmWallet = (wallet) => wallet.connector.connectedChain === 'EVM';
3
- const isWalletConnected = (wallet) => wallet.connected;
4
- const useFindEvmWallet = (primaryWallet, wallets) => {
5
- /**
6
- * If the primary wallet is an EVM wallet and is connected, return it.
7
- */
8
- if (primaryWallet &&
9
- isEvmWallet(primaryWallet) &&
10
- isWalletConnected(primaryWallet)) {
11
- return primaryWallet;
12
- }
13
- /**
14
- * If the primary wallet is not connected, find the first connected EVM wallet.
15
- */
16
- const connectedEvmWallets = wallets
17
- .filter(isEvmWallet)
18
- .filter(isWalletConnected);
19
- return connectedEvmWallets[0];
6
+ const useFindEvmWallet = (primaryWallet) => {
7
+ const [wallet, setWallet] = useState(undefined);
8
+ const checkForConnectedEvmWallet = (wallet) => __awaiter(void 0, void 0, void 0, function* () {
9
+ if (!isEvmWallet(wallet)) {
10
+ return;
11
+ }
12
+ const isConnected = yield wallet.isConnected();
13
+ if (isConnected) {
14
+ setWallet(wallet);
15
+ }
16
+ });
17
+ useEffect(() => {
18
+ if (!primaryWallet) {
19
+ return;
20
+ }
21
+ checkForConnectedEvmWallet(primaryWallet);
22
+ }, [primaryWallet]);
23
+ return wallet;
20
24
  };
21
25
 
22
26
  export { useFindEvmWallet };